status_spec.rb 13 KB

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