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.

275 lines
8.6 KiB

  1. require 'rails_helper'
  2. RSpec.describe PostStatusService, type: :service do
  3. subject { PostStatusService.new }
  4. it 'creates a new status' do
  5. account = Fabricate(:account)
  6. text = "test status update"
  7. status = subject.call(account, text: text)
  8. expect(status).to be_persisted
  9. expect(status.text).to eq text
  10. end
  11. it 'creates a new response status' do
  12. in_reply_to_status = Fabricate(:status)
  13. account = Fabricate(:account)
  14. text = "test status update"
  15. status = subject.call(account, text: text, thread: in_reply_to_status)
  16. expect(status).to be_persisted
  17. expect(status.text).to eq text
  18. expect(status.thread).to eq in_reply_to_status
  19. end
  20. context 'when scheduling a status' do
  21. let!(:account) { Fabricate(:account) }
  22. let!(:future) { Time.now.utc + 2.hours }
  23. let!(:previous_status) { Fabricate(:status, account: account) }
  24. it 'schedules a status' do
  25. status = subject.call(account, text: 'Hi future!', scheduled_at: future)
  26. expect(status).to be_a ScheduledStatus
  27. expect(status.scheduled_at).to eq future
  28. expect(status.params['text']).to eq 'Hi future!'
  29. end
  30. it 'does not immediately create a status' do
  31. media = Fabricate(:media_attachment, account: account)
  32. status = subject.call(account, text: 'Hi future!', media_ids: [media.id], scheduled_at: future)
  33. expect(status).to be_a ScheduledStatus
  34. expect(status.scheduled_at).to eq future
  35. expect(status.params['text']).to eq 'Hi future!'
  36. expect(status.params['media_ids']).to eq [media.id]
  37. expect(media.reload.status).to be_nil
  38. expect(Status.where(text: 'Hi future!').exists?).to be_falsey
  39. end
  40. it 'does not change statuses count' do
  41. expect { subject.call(account, text: 'Hi future!', scheduled_at: future, thread: previous_status) }.not_to change { [account.statuses_count, previous_status.replies_count] }
  42. end
  43. end
  44. it 'creates response to the original status of boost' do
  45. boosted_status = Fabricate(:status)
  46. in_reply_to_status = Fabricate(:status, reblog: boosted_status)
  47. account = Fabricate(:account)
  48. text = "test status update"
  49. status = subject.call(account, text: text, thread: in_reply_to_status)
  50. expect(status).to be_persisted
  51. expect(status.text).to eq text
  52. expect(status.thread).to eq boosted_status
  53. end
  54. it 'creates a sensitive status' do
  55. status = create_status_with_options(sensitive: true)
  56. expect(status).to be_persisted
  57. expect(status).to be_sensitive
  58. end
  59. it 'creates a status with spoiler text' do
  60. spoiler_text = "spoiler text"
  61. status = create_status_with_options(spoiler_text: spoiler_text)
  62. expect(status).to be_persisted
  63. expect(status.spoiler_text).to eq spoiler_text
  64. end
  65. it 'creates a sensitive status when there is a CW but no text' do
  66. status = subject.call(Fabricate(:account), text: '', spoiler_text: 'foo')
  67. expect(status).to be_persisted
  68. expect(status).to be_sensitive
  69. end
  70. it 'creates a status with empty default spoiler text' do
  71. status = create_status_with_options(spoiler_text: nil)
  72. expect(status).to be_persisted
  73. expect(status.spoiler_text).to eq ''
  74. end
  75. it 'creates a status with the given visibility' do
  76. status = create_status_with_options(visibility: :private)
  77. expect(status).to be_persisted
  78. expect(status.visibility).to eq "private"
  79. end
  80. it 'creates a status with limited visibility for silenced users' do
  81. status = subject.call(Fabricate(:account, silenced: true), text: 'test', visibility: :public)
  82. expect(status).to be_persisted
  83. expect(status.visibility).to eq "unlisted"
  84. end
  85. it 'creates a status for the given application' do
  86. application = Fabricate(:application)
  87. status = create_status_with_options(application: application)
  88. expect(status).to be_persisted
  89. expect(status.application).to eq application
  90. end
  91. it 'creates a status with a language set' do
  92. account = Fabricate(:account)
  93. text = 'This is an English text.'
  94. status = subject.call(account, text: text)
  95. expect(status.language).to eq 'en'
  96. end
  97. it 'processes mentions' do
  98. mention_service = double(:process_mentions_service)
  99. allow(mention_service).to receive(:call)
  100. allow(ProcessMentionsService).to receive(:new).and_return(mention_service)
  101. account = Fabricate(:account)
  102. status = subject.call(account, text: "test status update")
  103. expect(ProcessMentionsService).to have_received(:new)
  104. expect(mention_service).to have_received(:call).with(status, save_records: false)
  105. end
  106. it 'safeguards mentions' do
  107. account = Fabricate(:account)
  108. mentioned_account = Fabricate(:account, username: 'alice')
  109. unexpected_mentioned_account = Fabricate(:account, username: 'bob')
  110. expect do
  111. subject.call(account, text: '@alice hm, @bob is really annoying lately', allowed_mentions: [mentioned_account.id])
  112. end.to raise_error(an_instance_of(PostStatusService::UnexpectedMentionsError).and having_attributes(accounts: [unexpected_mentioned_account]))
  113. end
  114. it 'processes duplicate mentions correctly' do
  115. account = Fabricate(:account)
  116. mentioned_account = Fabricate(:account, username: 'alice')
  117. expect do
  118. subject.call(account, text: '@alice @alice @alice hey @alice')
  119. end.not_to raise_error
  120. end
  121. it 'processes hashtags' do
  122. hashtags_service = double(:process_hashtags_service)
  123. allow(hashtags_service).to receive(:call)
  124. allow(ProcessHashtagsService).to receive(:new).and_return(hashtags_service)
  125. account = Fabricate(:account)
  126. status = subject.call(account, text: "test status update")
  127. expect(ProcessHashtagsService).to have_received(:new)
  128. expect(hashtags_service).to have_received(:call).with(status)
  129. end
  130. it 'gets distributed' do
  131. allow(DistributionWorker).to receive(:perform_async)
  132. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  133. account = Fabricate(:account)
  134. status = subject.call(account, text: "test status update")
  135. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  136. expect(ActivityPub::DistributionWorker).to have_received(:perform_async).with(status.id)
  137. end
  138. it 'crawls links' do
  139. allow(LinkCrawlWorker).to receive(:perform_async)
  140. account = Fabricate(:account)
  141. status = subject.call(account, text: "test status update")
  142. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  143. end
  144. it 'attaches the given media to the created status' do
  145. account = Fabricate(:account)
  146. media = Fabricate(:media_attachment, account: account)
  147. status = subject.call(
  148. account,
  149. text: "test status update",
  150. media_ids: [media.id],
  151. )
  152. expect(media.reload.status).to eq status
  153. end
  154. it 'does not attach media from another account to the created status' do
  155. account = Fabricate(:account)
  156. media = Fabricate(:media_attachment, account: Fabricate(:account))
  157. status = subject.call(
  158. account,
  159. text: "test status update",
  160. media_ids: [media.id],
  161. )
  162. expect(media.reload.status).to eq nil
  163. end
  164. it 'does not allow attaching more than 4 files' do
  165. account = Fabricate(:account)
  166. expect do
  167. subject.call(
  168. account,
  169. text: "test status update",
  170. media_ids: [
  171. Fabricate(:media_attachment, account: account),
  172. Fabricate(:media_attachment, account: account),
  173. Fabricate(:media_attachment, account: account),
  174. Fabricate(:media_attachment, account: account),
  175. Fabricate(:media_attachment, account: account),
  176. ].map(&:id),
  177. )
  178. end.to raise_error(
  179. Mastodon::ValidationError,
  180. I18n.t('media_attachments.validations.too_many'),
  181. )
  182. end
  183. it 'does not allow attaching both videos and images' do
  184. account = Fabricate(:account)
  185. video = Fabricate(:media_attachment, type: :video, account: account)
  186. image = Fabricate(:media_attachment, type: :image, account: account)
  187. video.update(type: :video)
  188. expect do
  189. subject.call(
  190. account,
  191. text: "test status update",
  192. media_ids: [
  193. video,
  194. image,
  195. ].map(&:id),
  196. )
  197. end.to raise_error(
  198. Mastodon::ValidationError,
  199. I18n.t('media_attachments.validations.images_and_video'),
  200. )
  201. end
  202. it 'returns existing status when used twice with idempotency key' do
  203. account = Fabricate(:account)
  204. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  205. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  206. expect(status2.id).to eq status1.id
  207. end
  208. def create_status_with_options(**options)
  209. subject.call(Fabricate(:account), options.merge(text: 'test'))
  210. end
  211. end