The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

461 lines
14 KiB

  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. subject.uri = 'a'
  13. expect(subject.local?).to be false
  14. end
  15. end
  16. describe '#reblog?' do
  17. it 'returns true when the status reblogs another status' do
  18. subject.reblog = other
  19. expect(subject.reblog?).to be true
  20. end
  21. it 'returns false if the status is self-contained' do
  22. expect(subject.reblog?).to be false
  23. end
  24. end
  25. describe '#reply?' do
  26. it 'returns true if the status references another' do
  27. subject.thread = other
  28. expect(subject.reply?).to be true
  29. end
  30. it 'returns false if the status is self-contained' do
  31. expect(subject.reply?).to be false
  32. end
  33. end
  34. describe '#verb' do
  35. it 'is always post' do
  36. expect(subject.verb).to be :post
  37. end
  38. end
  39. describe '#object_type' do
  40. it 'is note when the status is self-contained' do
  41. expect(subject.object_type).to be :note
  42. end
  43. it 'is comment when the status replies to another' do
  44. subject.thread = other
  45. expect(subject.object_type).to be :comment
  46. end
  47. end
  48. describe '#title' do
  49. it 'is a shorter version of the content' do
  50. expect(subject.title).to be_a String
  51. end
  52. end
  53. describe '#content' do
  54. it 'returns the text of the status if it is not a reblog' do
  55. expect(subject.content).to eql subject.text
  56. end
  57. it 'returns the text of the reblogged status' do
  58. subject.reblog = other
  59. expect(subject.content).to eql other.text
  60. end
  61. end
  62. describe '#target' do
  63. it 'returns nil if the status is self-contained' do
  64. expect(subject.target).to be_nil
  65. end
  66. it 'returns nil if the status is a reply' do
  67. subject.thread = other
  68. expect(subject.target).to be_nil
  69. end
  70. it 'returns the reblogged status' do
  71. subject.reblog = other
  72. expect(subject.target).to eq other
  73. end
  74. end
  75. describe '#reblogs_count' do
  76. it 'is the number of reblogs' do
  77. Fabricate(:status, account: bob, reblog: subject)
  78. Fabricate(:status, account: alice, reblog: subject)
  79. expect(subject.reblogs_count).to eq 2
  80. end
  81. end
  82. describe '#favourites_count' do
  83. it 'is the number of favorites' do
  84. Fabricate(:favourite, account: bob, status: subject)
  85. Fabricate(:favourite, account: alice, status: subject)
  86. expect(subject.favourites_count).to eq 2
  87. end
  88. end
  89. describe '#proper' do
  90. it 'is itself for original statuses' do
  91. expect(subject.proper).to eq subject
  92. end
  93. it 'is the source status for reblogs' do
  94. subject.reblog = other
  95. expect(subject.proper).to eq other
  96. end
  97. end
  98. describe '#permitted?' do
  99. it 'returns true when direct and account is viewer' do
  100. subject.visibility = :direct
  101. expect(subject.permitted?(subject.account)).to be true
  102. end
  103. it 'returns true when direct and viewer is mentioned' do
  104. subject.visibility = :direct
  105. subject.mentions = [Fabricate(:mention, account: alice)]
  106. expect(subject.permitted?(alice)).to be true
  107. end
  108. it 'returns false when direct and viewer is not mentioned' do
  109. viewer = Fabricate(:account)
  110. subject.visibility = :direct
  111. expect(subject.permitted?(viewer)).to be false
  112. end
  113. it 'returns true when private and account is viewer' do
  114. subject.visibility = :direct
  115. expect(subject.permitted?(subject.account)).to be true
  116. end
  117. it 'returns true when private and account is following viewer' do
  118. follow = Fabricate(:follow)
  119. subject.visibility = :private
  120. subject.account = follow.target_account
  121. expect(subject.permitted?(follow.account)).to be true
  122. end
  123. it 'returns true when private and viewer is mentioned' do
  124. subject.visibility = :private
  125. subject.mentions = [Fabricate(:mention, account: alice)]
  126. expect(subject.permitted?(alice)).to be true
  127. end
  128. it 'returns false when private and viewer is not mentioned or followed' do
  129. viewer = Fabricate(:account)
  130. subject.visibility = :private
  131. expect(subject.permitted?(viewer)).to be false
  132. end
  133. it 'returns true when no viewer' do
  134. expect(subject.permitted?).to be true
  135. end
  136. it 'returns false when viewer is blocked' do
  137. block = Fabricate(:block)
  138. subject.visibility = :private
  139. subject.account = block.target_account
  140. expect(subject.permitted?(block.account)).to be false
  141. end
  142. end
  143. describe '#ancestors' do
  144. it 'ignores deleted records' do
  145. first_status = Fabricate(:status, account: bob)
  146. second_status = Fabricate(:status, thread: first_status, account: alice)
  147. # Create cache and delete cached record
  148. second_status.ancestors
  149. first_status.destroy
  150. expect(second_status.ancestors).to eq([])
  151. end
  152. end
  153. describe '#filter_from_context?' do
  154. pending
  155. end
  156. describe '.mutes_map' do
  157. let(:status) { Fabricate(:status) }
  158. let(:account) { Fabricate(:account) }
  159. subject { Status.mutes_map([status.conversation.id], account) }
  160. it 'returns a hash' do
  161. expect(subject).to be_a Hash
  162. end
  163. it 'contains true value' do
  164. account.mute_conversation!(status.conversation)
  165. expect(subject[status.conversation.id]).to be true
  166. end
  167. end
  168. describe '.favourites_map' do
  169. let(:status) { Fabricate(:status) }
  170. let(:account) { Fabricate(:account) }
  171. subject { Status.favourites_map([status], account) }
  172. it 'returns a hash' do
  173. expect(subject).to be_a Hash
  174. end
  175. it 'contains true value' do
  176. Fabricate(:favourite, status: status, account: account)
  177. expect(subject[status.id]).to be true
  178. end
  179. end
  180. describe '.reblogs_map' do
  181. let(:status) { Fabricate(:status) }
  182. let(:account) { Fabricate(:account) }
  183. subject { Status.reblogs_map([status], account) }
  184. it 'returns a hash' do
  185. expect(subject).to be_a Hash
  186. end
  187. it 'contains true value' do
  188. Fabricate(:status, account: account, reblog: status)
  189. expect(subject[status.id]).to be true
  190. end
  191. end
  192. describe '.local_only' do
  193. it 'returns only statuses from local accounts' do
  194. local_account = Fabricate(:account, domain: nil)
  195. remote_account = Fabricate(:account, domain: 'test.com')
  196. local_status = Fabricate(:status, account: local_account)
  197. remote_status = Fabricate(:status, account: remote_account)
  198. results = described_class.local_only
  199. expect(results).to include(local_status)
  200. expect(results).not_to include(remote_status)
  201. end
  202. end
  203. describe '.as_home_timeline' do
  204. before do
  205. account = Fabricate(:account)
  206. followed = Fabricate(:account)
  207. not_followed = Fabricate(:account)
  208. Fabricate(:follow, account: account, target_account: followed)
  209. @self_status = Fabricate(:status, account: account)
  210. @followed_status = Fabricate(:status, account: followed)
  211. @not_followed_status = Fabricate(:status, account: not_followed)
  212. @results = Status.as_home_timeline(account)
  213. end
  214. it 'includes statuses from self' do
  215. expect(@results).to include(@self_status)
  216. end
  217. it 'includes statuses from followed' do
  218. expect(@results).to include(@followed_status)
  219. end
  220. it 'does not include statuses from non-followed' do
  221. expect(@results).not_to include(@not_followed_status)
  222. end
  223. end
  224. describe '.as_public_timeline' do
  225. it 'only includes statuses with public visibility' do
  226. public_status = Fabricate(:status, visibility: :public)
  227. private_status = Fabricate(:status, visibility: :private)
  228. results = Status.as_public_timeline
  229. expect(results).to include(public_status)
  230. expect(results).not_to include(private_status)
  231. end
  232. it 'does not include replies' do
  233. status = Fabricate(:status)
  234. reply = Fabricate(:status, in_reply_to_id: status.id)
  235. results = Status.as_public_timeline
  236. expect(results).to include(status)
  237. expect(results).not_to include(reply)
  238. end
  239. it 'does not include boosts' do
  240. status = Fabricate(:status)
  241. boost = Fabricate(:status, reblog_of_id: status.id)
  242. results = Status.as_public_timeline
  243. expect(results).to include(status)
  244. expect(results).not_to include(boost)
  245. end
  246. it 'filters out silenced accounts' do
  247. account = Fabricate(:account)
  248. silenced_account = Fabricate(:account, silenced: true)
  249. status = Fabricate(:status, account: account)
  250. silenced_status = Fabricate(:status, account: silenced_account)
  251. results = Status.as_public_timeline
  252. expect(results).to include(status)
  253. expect(results).not_to include(silenced_status)
  254. end
  255. context 'with a local_only option set' do
  256. it 'does not include remote instances statuses' do
  257. local_account = Fabricate(:account, domain: nil)
  258. remote_account = Fabricate(:account, domain: 'test.com')
  259. local_status = Fabricate(:status, account: local_account)
  260. remote_status = Fabricate(:status, account: remote_account)
  261. results = Status.as_public_timeline(nil, true)
  262. expect(results).to include(local_status)
  263. expect(results).not_to include(remote_status)
  264. end
  265. end
  266. describe 'with an account passed in' do
  267. before do
  268. @account = Fabricate(:account)
  269. end
  270. it 'excludes statuses from accounts blocked by the account' do
  271. blocked = Fabricate(:account)
  272. Fabricate(:block, account: @account, target_account: blocked)
  273. blocked_status = Fabricate(:status, account: blocked)
  274. results = Status.as_public_timeline(@account)
  275. expect(results).not_to include(blocked_status)
  276. end
  277. it 'excludes statuses from accounts who have blocked the account' do
  278. blocked = Fabricate(:account)
  279. Fabricate(:block, account: blocked, target_account: @account)
  280. blocked_status = Fabricate(:status, account: blocked)
  281. results = Status.as_public_timeline(@account)
  282. expect(results).not_to include(blocked_status)
  283. end
  284. it 'excludes statuses from accounts muted by the account' do
  285. muted = Fabricate(:account)
  286. Fabricate(:mute, account: @account, target_account: muted)
  287. muted_status = Fabricate(:status, account: muted)
  288. results = Status.as_public_timeline(@account)
  289. expect(results).not_to include(muted_status)
  290. end
  291. context 'with language preferences' do
  292. it 'excludes statuses in languages not allowed by the account user' do
  293. user = Fabricate(:user, allowed_languages: [:en, :es])
  294. @account.update(user: user)
  295. en_status = Fabricate(:status, language: 'en')
  296. es_status = Fabricate(:status, language: 'es')
  297. fr_status = Fabricate(:status, language: 'fr')
  298. results = Status.as_public_timeline(@account)
  299. expect(results).to include(en_status)
  300. expect(results).to include(es_status)
  301. expect(results).not_to include(fr_status)
  302. end
  303. it 'includes all languages when user does not have a setting' do
  304. user = Fabricate(:user, allowed_languages: [])
  305. @account.update(user: user)
  306. en_status = Fabricate(:status, language: 'en')
  307. es_status = Fabricate(:status, language: 'es')
  308. results = Status.as_public_timeline(@account)
  309. expect(results).to include(en_status)
  310. expect(results).to include(es_status)
  311. end
  312. it 'includes all languages when account does not have a user' do
  313. expect(@account.user).to be_nil
  314. en_status = Fabricate(:status, language: 'en')
  315. es_status = Fabricate(:status, language: 'es')
  316. results = Status.as_public_timeline(@account)
  317. expect(results).to include(en_status)
  318. expect(results).to include(es_status)
  319. end
  320. end
  321. context 'where that account is silenced' do
  322. it 'includes statuses from other accounts that are silenced' do
  323. @account.update(silenced: true)
  324. other_silenced_account = Fabricate(:account, silenced: true)
  325. other_status = Fabricate(:status, account: other_silenced_account)
  326. results = Status.as_public_timeline(@account)
  327. expect(results).to include(other_status)
  328. end
  329. end
  330. end
  331. end
  332. describe '.as_tag_timeline' do
  333. it 'includes statuses with a tag' do
  334. tag = Fabricate(:tag)
  335. status = Fabricate(:status, tags: [tag])
  336. other = Fabricate(:status)
  337. results = Status.as_tag_timeline(tag)
  338. expect(results).to include(status)
  339. expect(results).not_to include(other)
  340. end
  341. it 'allows replies to be included' do
  342. original = Fabricate(:status)
  343. tag = Fabricate(:tag)
  344. status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
  345. results = Status.as_tag_timeline(tag)
  346. expect(results).to include(status)
  347. end
  348. end
  349. describe 'before_create' do
  350. it 'sets account being replied to correctly over intermediary nodes' do
  351. first_status = Fabricate(:status, account: bob)
  352. intermediary = Fabricate(:status, thread: first_status, account: alice)
  353. final = Fabricate(:status, thread: intermediary, account: alice)
  354. expect(final.in_reply_to_account_id).to eq bob.id
  355. end
  356. it 'creates new conversation for stand-alone status' do
  357. expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
  358. end
  359. it 'keeps conversation of parent node' do
  360. parent = Fabricate(:status, text: 'First')
  361. expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  362. end
  363. end
  364. end