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.

412 lines
13 KiB

8 years ago
8 years ago
  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 '.local_only' do
  157. it 'returns only statuses from local accounts' do
  158. local_account = Fabricate(:account, domain: nil)
  159. remote_account = Fabricate(:account, domain: 'test.com')
  160. local_status = Fabricate(:status, account: local_account)
  161. remote_status = Fabricate(:status, account: remote_account)
  162. results = described_class.local_only
  163. expect(results).to include(local_status)
  164. expect(results).not_to include(remote_status)
  165. end
  166. end
  167. describe '.as_home_timeline' do
  168. before do
  169. account = Fabricate(:account)
  170. followed = Fabricate(:account)
  171. not_followed = Fabricate(:account)
  172. Fabricate(:follow, account: account, target_account: followed)
  173. @self_status = Fabricate(:status, account: account)
  174. @followed_status = Fabricate(:status, account: followed)
  175. @not_followed_status = Fabricate(:status, account: not_followed)
  176. @results = Status.as_home_timeline(account)
  177. end
  178. it 'includes statuses from self' do
  179. expect(@results).to include(@self_status)
  180. end
  181. it 'includes statuses from followed' do
  182. expect(@results).to include(@followed_status)
  183. end
  184. it 'does not include statuses from non-followed' do
  185. expect(@results).not_to include(@not_followed_status)
  186. end
  187. end
  188. describe '.as_public_timeline' do
  189. it 'only includes statuses with public visibility' do
  190. public_status = Fabricate(:status, visibility: :public)
  191. private_status = Fabricate(:status, visibility: :private)
  192. results = Status.as_public_timeline
  193. expect(results).to include(public_status)
  194. expect(results).not_to include(private_status)
  195. end
  196. it 'does not include replies' do
  197. status = Fabricate(:status)
  198. reply = Fabricate(:status, in_reply_to_id: status.id)
  199. results = Status.as_public_timeline
  200. expect(results).to include(status)
  201. expect(results).not_to include(reply)
  202. end
  203. it 'does not include boosts' do
  204. status = Fabricate(:status)
  205. boost = Fabricate(:status, reblog_of_id: status.id)
  206. results = Status.as_public_timeline
  207. expect(results).to include(status)
  208. expect(results).not_to include(boost)
  209. end
  210. it 'filters out silenced accounts' do
  211. account = Fabricate(:account)
  212. silenced_account = Fabricate(:account, silenced: true)
  213. status = Fabricate(:status, account: account)
  214. silenced_status = Fabricate(:status, account: silenced_account)
  215. results = Status.as_public_timeline
  216. expect(results).to include(status)
  217. expect(results).not_to include(silenced_status)
  218. end
  219. context 'with a local_only option set' do
  220. it 'does not include remote instances statuses' do
  221. local_account = Fabricate(:account, domain: nil)
  222. remote_account = Fabricate(:account, domain: 'test.com')
  223. local_status = Fabricate(:status, account: local_account)
  224. remote_status = Fabricate(:status, account: remote_account)
  225. results = Status.as_public_timeline(nil, true)
  226. expect(results).to include(local_status)
  227. expect(results).not_to include(remote_status)
  228. end
  229. end
  230. describe 'with an account passed in' do
  231. before do
  232. @account = Fabricate(:account)
  233. end
  234. it 'excludes statuses from accounts blocked by the account' do
  235. blocked = Fabricate(:account)
  236. Fabricate(:block, account: @account, target_account: blocked)
  237. blocked_status = Fabricate(:status, account: blocked)
  238. results = Status.as_public_timeline(@account)
  239. expect(results).not_to include(blocked_status)
  240. end
  241. it 'excludes statuses from accounts who have blocked the account' do
  242. blocked = Fabricate(:account)
  243. Fabricate(:block, account: blocked, target_account: @account)
  244. blocked_status = Fabricate(:status, account: blocked)
  245. results = Status.as_public_timeline(@account)
  246. expect(results).not_to include(blocked_status)
  247. end
  248. it 'excludes statuses from accounts muted by the account' do
  249. muted = Fabricate(:account)
  250. Fabricate(:mute, account: @account, target_account: muted)
  251. muted_status = Fabricate(:status, account: muted)
  252. results = Status.as_public_timeline(@account)
  253. expect(results).not_to include(muted_status)
  254. end
  255. context 'with language preferences' do
  256. it 'excludes statuses in languages not allowed by the account user' do
  257. user = Fabricate(:user, allowed_languages: [:en, :es])
  258. @account.update(user: user)
  259. en_status = Fabricate(:status, language: 'en')
  260. es_status = Fabricate(:status, language: 'es')
  261. fr_status = Fabricate(:status, language: 'fr')
  262. results = Status.as_public_timeline(@account)
  263. expect(results).to include(en_status)
  264. expect(results).to include(es_status)
  265. expect(results).not_to include(fr_status)
  266. end
  267. it 'includes all languages when user does not have a setting' do
  268. user = Fabricate(:user, allowed_languages: [])
  269. @account.update(user: user)
  270. en_status = Fabricate(:status, language: 'en')
  271. es_status = Fabricate(:status, language: 'es')
  272. results = Status.as_public_timeline(@account)
  273. expect(results).to include(en_status)
  274. expect(results).to include(es_status)
  275. end
  276. it 'includes all languages when account does not have a user' do
  277. expect(@account.user).to be_nil
  278. en_status = Fabricate(:status, language: 'en')
  279. es_status = Fabricate(:status, language: 'es')
  280. results = Status.as_public_timeline(@account)
  281. expect(results).to include(en_status)
  282. expect(results).to include(es_status)
  283. end
  284. end
  285. context 'where that account is silenced' do
  286. it 'includes statuses from other accounts that are silenced' do
  287. @account.update(silenced: true)
  288. other_silenced_account = Fabricate(:account, silenced: true)
  289. other_status = Fabricate(:status, account: other_silenced_account)
  290. results = Status.as_public_timeline(@account)
  291. expect(results).to include(other_status)
  292. end
  293. end
  294. end
  295. end
  296. describe '.as_tag_timeline' do
  297. it 'includes statuses with a tag' do
  298. tag = Fabricate(:tag)
  299. status = Fabricate(:status, tags: [tag])
  300. other = Fabricate(:status)
  301. results = Status.as_tag_timeline(tag)
  302. expect(results).to include(status)
  303. expect(results).not_to include(other)
  304. end
  305. it 'allows replies to be included' do
  306. original = Fabricate(:status)
  307. tag = Fabricate(:tag)
  308. status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
  309. results = Status.as_tag_timeline(tag)
  310. expect(results).to include(status)
  311. end
  312. end
  313. describe 'before_create' 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(Status.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(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
  326. end
  327. end
  328. end