status_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 '.tagged_with' do
  208. let(:tag1) { Fabricate(:tag) }
  209. let(:tag2) { Fabricate(:tag) }
  210. let(:tag3) { Fabricate(:tag) }
  211. let!(:status1) { Fabricate(:status, tags: [tag1]) }
  212. let!(:status2) { Fabricate(:status, tags: [tag2]) }
  213. let!(:status3) { Fabricate(:status, tags: [tag3]) }
  214. let!(:status4) { Fabricate(:status, tags: []) }
  215. let!(:status5) { Fabricate(:status, tags: [tag1, tag2, tag3]) }
  216. context 'when given one tag' do
  217. it 'returns the expected statuses' do
  218. expect(Status.tagged_with([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status5.id])
  219. expect(Status.tagged_with([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status5.id])
  220. expect(Status.tagged_with([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id, status5.id])
  221. end
  222. end
  223. context 'when given multiple tags' do
  224. it 'returns the expected statuses' do
  225. expect(Status.tagged_with([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status2.id, status5.id])
  226. expect(Status.tagged_with([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status3.id, status5.id])
  227. expect(Status.tagged_with([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status3.id, status5.id])
  228. end
  229. end
  230. end
  231. describe '.tagged_with_all' do
  232. let(:tag1) { Fabricate(:tag) }
  233. let(:tag2) { Fabricate(:tag) }
  234. let(:tag3) { Fabricate(:tag) }
  235. let!(:status1) { Fabricate(:status, tags: [tag1]) }
  236. let!(:status2) { Fabricate(:status, tags: [tag2]) }
  237. let!(:status3) { Fabricate(:status, tags: [tag3]) }
  238. let!(:status4) { Fabricate(:status, tags: []) }
  239. let!(:status5) { Fabricate(:status, tags: [tag1, tag2]) }
  240. context 'when given one tag' do
  241. it 'returns the expected statuses' do
  242. expect(Status.tagged_with_all([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status5.id])
  243. expect(Status.tagged_with_all([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status5.id])
  244. expect(Status.tagged_with_all([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id])
  245. end
  246. end
  247. context 'when given multiple tags' do
  248. it 'returns the expected statuses' do
  249. expect(Status.tagged_with_all([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status5.id])
  250. expect(Status.tagged_with_all([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq []
  251. expect(Status.tagged_with_all([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to eq []
  252. end
  253. end
  254. end
  255. describe '.tagged_with_none' do
  256. let(:tag1) { Fabricate(:tag) }
  257. let(:tag2) { Fabricate(:tag) }
  258. let(:tag3) { Fabricate(:tag) }
  259. let!(:status1) { Fabricate(:status, tags: [tag1]) }
  260. let!(:status2) { Fabricate(:status, tags: [tag2]) }
  261. let!(:status3) { Fabricate(:status, tags: [tag3]) }
  262. let!(:status4) { Fabricate(:status, tags: []) }
  263. let!(:status5) { Fabricate(:status, tags: [tag1, tag2, tag3]) }
  264. context 'when given one tag' do
  265. it 'returns the expected statuses' do
  266. expect(Status.tagged_with_none([tag1.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status3.id, status4.id])
  267. expect(Status.tagged_with_none([tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status3.id, status4.id])
  268. expect(Status.tagged_with_none([tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status2.id, status4.id])
  269. end
  270. end
  271. context 'when given multiple tags' do
  272. it 'returns the expected statuses' do
  273. expect(Status.tagged_with_none([tag1.id, tag2.id]).reorder(:id).pluck(:id).uniq).to match_array([status3.id, status4.id])
  274. expect(Status.tagged_with_none([tag1.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status2.id, status4.id])
  275. expect(Status.tagged_with_none([tag2.id, tag3.id]).reorder(:id).pluck(:id).uniq).to match_array([status1.id, status4.id])
  276. end
  277. end
  278. end
  279. describe 'before_validation' do
  280. it 'sets account being replied to correctly over intermediary nodes' do
  281. first_status = Fabricate(:status, account: bob)
  282. intermediary = Fabricate(:status, thread: first_status, account: alice)
  283. final = Fabricate(:status, thread: intermediary, account: alice)
  284. expect(final.in_reply_to_account_id).to eq bob.id
  285. end
  286. it 'creates new conversation for stand-alone status' do
  287. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  288. end
  289. it 'keeps conversation of parent node' do
  290. parent = Fabricate(:status, text: 'First')
  291. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  292. end
  293. it 'sets `local` to true for status by local account' do
  294. expect(Status.create(account: alice, text: 'foo').local).to be true
  295. end
  296. it 'sets `local` to false for status by remote account' do
  297. alice.update(domain: 'example.com')
  298. expect(Status.create(account: alice, text: 'foo').local).to be false
  299. end
  300. end
  301. describe 'validation' do
  302. it 'disallow empty uri for remote status' do
  303. alice.update(domain: 'example.com')
  304. status = Fabricate.build(:status, uri: '', account: alice)
  305. expect(status).to model_have_error_on_field(:uri)
  306. end
  307. end
  308. describe 'after_create' do
  309. it 'saves ActivityPub uri as uri for local status' do
  310. status = Status.create(account: alice, text: 'foo')
  311. status.reload
  312. expect(status.uri).to start_with('https://')
  313. end
  314. end
  315. end