status_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. 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]))
  220. .to include(status_with_tag_cats, status_with_all_tags)
  221. .and not_include(status_without_tags)
  222. expect(described_class.tagged_with([tag_dogs.id]))
  223. .to include(status_with_tag_dogs, status_with_all_tags)
  224. .and not_include(status_without_tags)
  225. expect(described_class.tagged_with([tag_zebras.id]))
  226. .to include(status_tagged_with_zebras, status_with_all_tags)
  227. .and not_include(status_without_tags)
  228. end
  229. end
  230. context 'when given multiple tags' do
  231. it 'returns the expected statuses' do
  232. expect(described_class.tagged_with([tag_cats.id, tag_dogs.id]))
  233. .to include(status_with_tag_cats, status_with_tag_dogs, status_with_all_tags)
  234. .and not_include(status_without_tags)
  235. expect(described_class.tagged_with([tag_cats.id, tag_zebras.id]))
  236. .to include(status_with_tag_cats, status_tagged_with_zebras, status_with_all_tags)
  237. .and not_include(status_without_tags)
  238. expect(described_class.tagged_with([tag_dogs.id, tag_zebras.id]))
  239. .to include(status_with_tag_dogs, status_tagged_with_zebras, status_with_all_tags)
  240. .and not_include(status_without_tags)
  241. end
  242. end
  243. end
  244. describe '.tagged_with_all' do
  245. let(:tag_cats) { Fabricate(:tag, name: 'cats') }
  246. let(:tag_dogs) { Fabricate(:tag, name: 'dogs') }
  247. let(:tag_zebras) { Fabricate(:tag, name: 'zebras') }
  248. let!(:status_with_tag_cats) { Fabricate(:status, tags: [tag_cats]) }
  249. let!(:status_with_tag_dogs) { Fabricate(:status, tags: [tag_dogs]) }
  250. let!(:status_tagged_with_zebras) { Fabricate(:status, tags: [tag_zebras]) }
  251. let!(:status_without_tags) { Fabricate(:status, tags: []) }
  252. let!(:status_with_all_tags) { Fabricate(:status, tags: [tag_cats, tag_dogs]) }
  253. context 'when given one tag' do
  254. it 'returns the expected statuses' do
  255. expect(described_class.tagged_with_all([tag_cats.id]))
  256. .to include(status_with_tag_cats, status_with_all_tags)
  257. .and not_include(status_without_tags)
  258. expect(described_class.tagged_with_all([tag_dogs.id]))
  259. .to include(status_with_tag_dogs, status_with_all_tags)
  260. .and not_include(status_without_tags)
  261. expect(described_class.tagged_with_all([tag_zebras.id]))
  262. .to include(status_tagged_with_zebras)
  263. .and not_include(status_without_tags)
  264. end
  265. end
  266. context 'when given multiple tags' do
  267. it 'returns the expected statuses' do
  268. expect(described_class.tagged_with_all([tag_cats.id, tag_dogs.id]))
  269. .to include(status_with_all_tags)
  270. expect(described_class.tagged_with_all([tag_cats.id, tag_zebras.id]))
  271. .to eq []
  272. expect(described_class.tagged_with_all([tag_dogs.id, tag_zebras.id]))
  273. .to eq []
  274. end
  275. end
  276. end
  277. describe '.tagged_with_none' do
  278. let(:tag_cats) { Fabricate(:tag, name: 'cats') }
  279. let(:tag_dogs) { Fabricate(:tag, name: 'dogs') }
  280. let(:tag_zebras) { Fabricate(:tag, name: 'zebras') }
  281. let!(:status_with_tag_cats) { Fabricate(:status, tags: [tag_cats]) }
  282. let!(:status_with_tag_dogs) { Fabricate(:status, tags: [tag_dogs]) }
  283. let!(:status_tagged_with_zebras) { Fabricate(:status, tags: [tag_zebras]) }
  284. let!(:status_without_tags) { Fabricate(:status, tags: []) }
  285. let!(:status_with_all_tags) { Fabricate(:status, tags: [tag_cats, tag_dogs, tag_zebras]) }
  286. context 'when given one tag' do
  287. it 'returns the expected statuses' do
  288. expect(described_class.tagged_with_none([tag_cats.id]))
  289. .to include(status_with_tag_dogs, status_tagged_with_zebras, status_without_tags)
  290. .and not_include(status_with_all_tags)
  291. expect(described_class.tagged_with_none([tag_dogs.id]))
  292. .to include(status_with_tag_cats, status_tagged_with_zebras, status_without_tags)
  293. .and not_include(status_with_all_tags)
  294. expect(described_class.tagged_with_none([tag_zebras.id]))
  295. .to include(status_with_tag_cats, status_with_tag_dogs, status_without_tags)
  296. .and not_include(status_with_all_tags)
  297. end
  298. end
  299. context 'when given multiple tags' do
  300. it 'returns the expected statuses' do
  301. expect(described_class.tagged_with_none([tag_cats.id, tag_dogs.id]))
  302. .to include(status_tagged_with_zebras, status_without_tags)
  303. .and not_include(status_with_all_tags)
  304. expect(described_class.tagged_with_none([tag_cats.id, tag_zebras.id]))
  305. .to include(status_with_tag_dogs, status_without_tags)
  306. .and not_include(status_with_all_tags)
  307. expect(described_class.tagged_with_none([tag_dogs.id, tag_zebras.id]))
  308. .to include(status_with_tag_cats, status_without_tags)
  309. .and not_include(status_with_all_tags)
  310. end
  311. end
  312. end
  313. describe 'before_validation' do
  314. it 'sets account being replied to correctly over intermediary nodes' do
  315. first_status = Fabricate(:status, account: bob)
  316. intermediary = Fabricate(:status, thread: first_status, account: alice)
  317. final = Fabricate(:status, thread: intermediary, account: alice)
  318. expect(final.in_reply_to_account_id).to eq bob.id
  319. end
  320. it 'creates new conversation for stand-alone status' do
  321. expect(described_class.create(account: alice, text: 'First').conversation_id).to_not be_nil
  322. end
  323. it 'keeps conversation of parent node' do
  324. parent = Fabricate(:status, text: 'First')
  325. expect(described_class.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  326. end
  327. it 'sets `local` to true for status by local account' do
  328. expect(described_class.create(account: alice, text: 'foo').local).to be true
  329. end
  330. it 'sets `local` to false for status by remote account' do
  331. alice.update(domain: 'example.com')
  332. expect(described_class.create(account: alice, text: 'foo').local).to be false
  333. end
  334. end
  335. describe 'validation' do
  336. it 'disallow empty uri for remote status' do
  337. alice.update(domain: 'example.com')
  338. status = Fabricate.build(:status, uri: '', account: alice)
  339. expect(status).to model_have_error_on_field(:uri)
  340. end
  341. end
  342. describe 'after_create' do
  343. it 'saves ActivityPub uri as uri for local status' do
  344. status = described_class.create(account: alice, text: 'foo')
  345. status.reload
  346. expect(status.uri).to start_with('https://')
  347. end
  348. end
  349. end