post_status_service_spec.rb 9.3 KB

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