闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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