status_spec.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. require 'rails_helper'
  2. RSpec.describe Status, type: :model do
  3. let(:alice) { Fabricate(:account, username: 'alice') }
  4. let(:bob) { Fabricate(:account, username: 'bob') }
  5. let(:other) { Fabricate(:status, account: bob, text: 'Skulls for the skull god! The enemy\'s gates are sideways!') }
  6. subject { Fabricate(:status, account: alice) }
  7. describe '#local?' do
  8. it 'returns true when no remote URI is set' do
  9. expect(subject.local?).to be true
  10. end
  11. it 'returns false if a remote URI is set' do
  12. alice.update(domain: 'example.com')
  13. subject.save
  14. expect(subject.local?).to be false
  15. end
  16. it 'returns true if a URI is set and `local` is true' do
  17. subject.update(uri: 'example.com', local: true)
  18. expect(subject.local?).to be true
  19. end
  20. end
  21. describe '#reblog?' do
  22. it 'returns true when the status reblogs another status' do
  23. subject.reblog = other
  24. expect(subject.reblog?).to be true
  25. end
  26. it 'returns false if the status is self-contained' do
  27. expect(subject.reblog?).to be false
  28. end
  29. end
  30. describe '#reply?' do
  31. it 'returns true if the status references another' do
  32. subject.thread = other
  33. expect(subject.reply?).to be true
  34. end
  35. it 'returns false if the status is self-contained' do
  36. expect(subject.reply?).to be false
  37. end
  38. end
  39. describe '#verb' do
  40. context 'if destroyed?' do
  41. it 'returns :delete' do
  42. subject.destroy!
  43. expect(subject.verb).to be :delete
  44. end
  45. end
  46. context 'unless destroyed?' do
  47. context 'if reblog?' do
  48. it 'returns :share' do
  49. subject.reblog = other
  50. expect(subject.verb).to be :share
  51. end
  52. end
  53. context 'unless reblog?' do
  54. it 'returns :post' do
  55. subject.reblog = nil
  56. expect(subject.verb).to be :post
  57. end
  58. end
  59. end
  60. end
  61. describe '#object_type' do
  62. it 'is note when the status is self-contained' do
  63. expect(subject.object_type).to be :note
  64. end
  65. it 'is comment when the status replies to another' do
  66. subject.thread = other
  67. expect(subject.object_type).to be :comment
  68. end
  69. end
  70. describe '#hidden?' do
  71. context 'if private_visibility?' do
  72. it 'returns true' do
  73. subject.visibility = :private
  74. expect(subject.hidden?).to be true
  75. end
  76. end
  77. context 'if direct_visibility?' do
  78. it 'returns true' do
  79. subject.visibility = :direct
  80. expect(subject.hidden?).to be true
  81. end
  82. end
  83. context 'if public_visibility?' do
  84. it 'returns false' do
  85. subject.visibility = :public
  86. expect(subject.hidden?).to be false
  87. end
  88. end
  89. context 'if unlisted_visibility?' do
  90. it 'returns false' do
  91. subject.visibility = :unlisted
  92. expect(subject.hidden?).to be false
  93. end
  94. end
  95. end
  96. describe '#content' do
  97. it 'returns the text of the status if it is not a reblog' do
  98. expect(subject.content).to eql subject.text
  99. end
  100. it 'returns the text of the reblogged status' do
  101. subject.reblog = other
  102. expect(subject.content).to eql other.text
  103. end
  104. end
  105. describe '#target' do
  106. it 'returns nil if the status is self-contained' do
  107. expect(subject.target).to be_nil
  108. end
  109. it 'returns nil if the status is a reply' do
  110. subject.thread = other
  111. expect(subject.target).to be_nil
  112. end
  113. it 'returns the reblogged status' do
  114. subject.reblog = other
  115. expect(subject.target).to eq other
  116. end
  117. end
  118. describe '#reblogs_count' do
  119. it 'is the number of reblogs' do
  120. Fabricate(:status, account: bob, reblog: subject)
  121. Fabricate(:status, account: alice, reblog: subject)
  122. expect(subject.reblogs_count).to eq 2
  123. end
  124. it 'is decremented when reblog is removed' do
  125. reblog = Fabricate(:status, account: bob, reblog: subject)
  126. expect(subject.reblogs_count).to eq 1
  127. reblog.destroy
  128. expect(subject.reblogs_count).to eq 0
  129. end
  130. it 'does not fail when original is deleted before reblog' do
  131. reblog = Fabricate(:status, account: bob, reblog: subject)
  132. expect(subject.reblogs_count).to eq 1
  133. expect { subject.destroy }.to_not raise_error
  134. expect(Status.find_by(id: reblog.id)).to be_nil
  135. end
  136. end
  137. describe '#replies_count' do
  138. it 'is the number of replies' do
  139. reply = Fabricate(:status, account: bob, thread: subject)
  140. expect(subject.replies_count).to eq 1
  141. end
  142. it 'is decremented when reply is removed' do
  143. reply = Fabricate(:status, account: bob, thread: subject)
  144. expect(subject.replies_count).to eq 1
  145. reply.destroy
  146. expect(subject.replies_count).to eq 0
  147. end
  148. end
  149. describe '#favourites_count' do
  150. it 'is the number of favorites' do
  151. Fabricate(:favourite, account: bob, status: subject)
  152. Fabricate(:favourite, account: alice, status: subject)
  153. expect(subject.favourites_count).to eq 2
  154. end
  155. it 'is decremented when favourite is removed' do
  156. favourite = Fabricate(:favourite, account: bob, status: subject)
  157. expect(subject.favourites_count).to eq 1
  158. favourite.destroy
  159. expect(subject.favourites_count).to eq 0
  160. end
  161. end
  162. describe '#proper' do
  163. it 'is itself for original statuses' do
  164. expect(subject.proper).to eq subject
  165. end
  166. it 'is the source status for reblogs' do
  167. subject.reblog = other
  168. expect(subject.proper).to eq other
  169. end
  170. end
  171. describe '.mutes_map' do
  172. let(:status) { Fabricate(:status) }
  173. let(:account) { Fabricate(:account) }
  174. subject { Status.mutes_map([status.conversation.id], account) }
  175. it 'returns a hash' do
  176. expect(subject).to be_a Hash
  177. end
  178. it 'contains true value' do
  179. account.mute_conversation!(status.conversation)
  180. expect(subject[status.conversation.id]).to be true
  181. end
  182. end
  183. describe '.favourites_map' do
  184. let(:status) { Fabricate(:status) }
  185. let(:account) { Fabricate(:account) }
  186. subject { Status.favourites_map([status], account) }
  187. it 'returns a hash' do
  188. expect(subject).to be_a Hash
  189. end
  190. it 'contains true value' do
  191. Fabricate(:favourite, status: status, account: account)
  192. expect(subject[status.id]).to be true
  193. end
  194. end
  195. describe '.reblogs_map' do
  196. let(:status) { Fabricate(:status) }
  197. let(:account) { Fabricate(:account) }
  198. subject { Status.reblogs_map([status], account) }
  199. it 'returns a hash' do
  200. expect(subject).to be_a Hash
  201. end
  202. it 'contains true value' do
  203. Fabricate(:status, account: account, reblog: status)
  204. expect(subject[status.id]).to be true
  205. end
  206. end
  207. describe '.in_chosen_languages' do
  208. context 'for accounts with language filters' do
  209. let(:user) { Fabricate(:user, chosen_languages: ['en']) }
  210. it 'does not include statuses in not in chosen languages' do
  211. status = Fabricate(:status, language: 'de')
  212. expect(Status.in_chosen_languages(user.account)).not_to include status
  213. end
  214. it 'includes status with unknown language' do
  215. status = Fabricate(:status, language: nil)
  216. expect(Status.in_chosen_languages(user.account)).to include status
  217. end
  218. end
  219. end
  220. describe '.tagged_with' do
  221. let(:tag1) { Fabricate(:tag) }
  222. let(:tag2) { Fabricate(:tag) }
  223. let(:tag3) { Fabricate(:tag) }
  224. let!(:status1) { Fabricate(:status, tags: [tag1]) }
  225. let!(:status2) { Fabricate(:status, tags: [tag2]) }
  226. let!(:status3) { Fabricate(:status, tags: [tag3]) }
  227. let!(:status4) { Fabricate(:status, tags: []) }
  228. let!(:status5) { Fabricate(:status, tags: [tag1, tag2, tag3]) }
  229. context 'when given one tag' do
  230. it 'returns the expected statuses' do
  231. expect(Status.tagged_with([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status5.id])
  232. expect(Status.tagged_with([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status5.id])
  233. expect(Status.tagged_with([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id, status5.id])
  234. end
  235. end
  236. context 'when given multiple tags' do
  237. it 'returns the expected statuses' do
  238. expect(Status.tagged_with([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status2.id, status5.id])
  239. expect(Status.tagged_with([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status3.id, status5.id])
  240. expect(Status.tagged_with([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status3.id, status5.id])
  241. end
  242. end
  243. end
  244. describe '.tagged_with_all' do
  245. let(:tag1) { Fabricate(:tag) }
  246. let(:tag2) { Fabricate(:tag) }
  247. let(:tag3) { Fabricate(:tag) }
  248. let!(:status1) { Fabricate(:status, tags: [tag1]) }
  249. let!(:status2) { Fabricate(:status, tags: [tag2]) }
  250. let!(:status3) { Fabricate(:status, tags: [tag3]) }
  251. let!(:status4) { Fabricate(:status, tags: []) }
  252. let!(:status5) { Fabricate(:status, tags: [tag1, tag2]) }
  253. context 'when given one tag' do
  254. it 'returns the expected statuses' do
  255. expect(Status.tagged_with_all([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status5.id])
  256. expect(Status.tagged_with_all([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status5.id])
  257. expect(Status.tagged_with_all([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id])
  258. end
  259. end
  260. context 'when given multiple tags' do
  261. it 'returns the expected statuses' do
  262. expect(Status.tagged_with_all([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status5.id])
  263. expect(Status.tagged_with_all([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq []
  264. expect(Status.tagged_with_all([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq []
  265. end
  266. end
  267. end
  268. describe '.tagged_with_none' do
  269. let(:tag1) { Fabricate(:tag) }
  270. let(:tag2) { Fabricate(:tag) }
  271. let(:tag3) { Fabricate(:tag) }
  272. let!(:status1) { Fabricate(:status, tags: [tag1]) }
  273. let!(:status2) { Fabricate(:status, tags: [tag2]) }
  274. let!(:status3) { Fabricate(:status, tags: [tag3]) }
  275. let!(:status4) { Fabricate(:status, tags: []) }
  276. let!(:status5) { Fabricate(:status, tags: [tag1, tag2, tag3]) }
  277. context 'when given one tag' do
  278. it 'returns the expected statuses' do
  279. expect(Status.tagged_with_none([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status3.id, status4.id])
  280. expect(Status.tagged_with_none([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status3.id, status4.id])
  281. expect(Status.tagged_with_none([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status2.id, status4.id])
  282. end
  283. end
  284. context 'when given multiple tags' do
  285. it 'returns the expected statuses' do
  286. expect(Status.tagged_with_none([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id, status4.id])
  287. expect(Status.tagged_with_none([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status4.id])
  288. expect(Status.tagged_with_none([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status4.id])
  289. end
  290. end
  291. end
  292. describe 'before_validation' do
  293. it 'sets account being replied to correctly over intermediary nodes' do
  294. first_status = Fabricate(:status, account: bob)
  295. intermediary = Fabricate(:status, thread: first_status, account: alice)
  296. final = Fabricate(:status, thread: intermediary, account: alice)
  297. expect(final.in_reply_to_account_id).to eq bob.id
  298. end
  299. it 'creates new conversation for stand-alone status' do
  300. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  301. end
  302. it 'keeps conversation of parent node' do
  303. parent = Fabricate(:status, text: 'First')
  304. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  305. end
  306. it 'sets `local` to true for status by local account' do
  307. expect(Status.create(account: alice, text: 'foo').local).to be true
  308. end
  309. it 'sets `local` to false for status by remote account' do
  310. alice.update(domain: 'example.com')
  311. expect(Status.create(account: alice, text: 'foo').local).to be false
  312. end
  313. end
  314. describe 'validation' do
  315. it 'disallow empty uri for remote status' do
  316. alice.update(domain: 'example.com')
  317. status = Fabricate.build(:status, uri: '', account: alice)
  318. expect(status).to model_have_error_on_field(:uri)
  319. end
  320. end
  321. describe 'after_create' do
  322. it 'saves ActivityPub uri as uri for local status' do
  323. status = Status.create(account: alice, text: 'foo')
  324. status.reload
  325. expect(status.uri).to start_with('https://')
  326. end
  327. end
  328. end