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.

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