1
0

post_status_service_spec.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. end
  45. it 'creates response to the original status of boost' do
  46. boosted_status = Fabricate(:status)
  47. in_reply_to_status = Fabricate(:status, reblog: boosted_status)
  48. account = Fabricate(:account)
  49. text = 'test status update'
  50. status = subject.call(account, text: text, thread: in_reply_to_status)
  51. expect(status).to be_persisted
  52. expect(status.text).to eq text
  53. expect(status.thread).to eq boosted_status
  54. end
  55. it 'creates a sensitive status' do
  56. status = create_status_with_options(sensitive: true)
  57. expect(status).to be_persisted
  58. expect(status).to be_sensitive
  59. end
  60. it 'creates a status with spoiler text' do
  61. spoiler_text = 'spoiler text'
  62. status = create_status_with_options(spoiler_text: spoiler_text)
  63. expect(status).to be_persisted
  64. expect(status.spoiler_text).to eq spoiler_text
  65. end
  66. it 'creates a sensitive status when there is a CW but no text' do
  67. status = subject.call(Fabricate(:account), text: '', spoiler_text: 'foo')
  68. expect(status).to be_persisted
  69. expect(status).to be_sensitive
  70. end
  71. it 'creates a status with empty default spoiler text' do
  72. status = create_status_with_options(spoiler_text: nil)
  73. expect(status).to be_persisted
  74. expect(status.spoiler_text).to eq ''
  75. end
  76. it 'creates a status with the given visibility' do
  77. status = create_status_with_options(visibility: :private)
  78. expect(status).to be_persisted
  79. expect(status.visibility).to eq 'private'
  80. end
  81. it 'creates a status with limited visibility for silenced users' do
  82. status = subject.call(Fabricate(:account, silenced: true), text: 'test', visibility: :public)
  83. expect(status).to be_persisted
  84. expect(status.visibility).to eq 'unlisted'
  85. end
  86. it 'creates a status for the given application' do
  87. application = Fabricate(:application)
  88. status = create_status_with_options(application: application)
  89. expect(status).to be_persisted
  90. expect(status.application).to eq application
  91. end
  92. it 'creates a status with a language set' do
  93. account = Fabricate(:account)
  94. text = 'This is an English text.'
  95. status = subject.call(account, text: text)
  96. expect(status.language).to eq 'en'
  97. end
  98. it 'processes mentions' do
  99. mention_service = instance_double(ProcessMentionsService)
  100. allow(mention_service).to receive(:call)
  101. allow(ProcessMentionsService).to receive(:new).and_return(mention_service)
  102. account = Fabricate(:account)
  103. status = subject.call(account, text: 'test status update')
  104. expect(ProcessMentionsService).to have_received(:new)
  105. expect(mention_service).to have_received(:call).with(status, save_records: false)
  106. end
  107. it 'safeguards mentions' do
  108. account = Fabricate(:account)
  109. mentioned_account = Fabricate(:account, username: 'alice')
  110. unexpected_mentioned_account = Fabricate(:account, username: 'bob')
  111. expect do
  112. subject.call(account, text: '@alice hm, @bob is really annoying lately', allowed_mentions: [mentioned_account.id])
  113. end.to raise_error(an_instance_of(PostStatusService::UnexpectedMentionsError).and(having_attributes(accounts: [unexpected_mentioned_account])))
  114. end
  115. it 'processes duplicate mentions correctly' do
  116. account = Fabricate(:account)
  117. mentioned_account = Fabricate(:account, username: 'alice')
  118. expect do
  119. subject.call(account, text: '@alice @alice @alice hey @alice')
  120. end.to_not raise_error
  121. end
  122. it 'processes hashtags' do
  123. hashtags_service = instance_double(ProcessHashtagsService)
  124. allow(hashtags_service).to receive(:call)
  125. allow(ProcessHashtagsService).to receive(:new).and_return(hashtags_service)
  126. account = Fabricate(:account)
  127. status = subject.call(account, text: 'test status update')
  128. expect(ProcessHashtagsService).to have_received(:new)
  129. expect(hashtags_service).to have_received(:call).with(status)
  130. end
  131. it 'gets distributed' do
  132. allow(DistributionWorker).to receive(:perform_async)
  133. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  134. account = Fabricate(:account)
  135. status = subject.call(account, text: 'test status update')
  136. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  137. expect(ActivityPub::DistributionWorker).to have_received(:perform_async).with(status.id)
  138. end
  139. it 'crawls links' do
  140. allow(LinkCrawlWorker).to receive(:perform_async)
  141. account = Fabricate(:account)
  142. status = subject.call(account, text: 'test status update')
  143. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  144. end
  145. it 'attaches the given media to the created status' do
  146. account = Fabricate(:account)
  147. media = Fabricate(:media_attachment, account: account)
  148. status = subject.call(
  149. account,
  150. text: 'test status update',
  151. media_ids: [media.id]
  152. )
  153. expect(media.reload.status).to eq status
  154. end
  155. it 'does not attach media from another account to the created status' do
  156. account = Fabricate(:account)
  157. media = Fabricate(:media_attachment, account: Fabricate(:account))
  158. status = subject.call(
  159. account,
  160. text: 'test status update',
  161. media_ids: [media.id]
  162. )
  163. expect(media.reload.status).to be_nil
  164. end
  165. it 'does not allow attaching more than 4 files' do
  166. account = Fabricate(:account)
  167. expect do
  168. subject.call(
  169. account,
  170. text: 'test status update',
  171. media_ids: [
  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. Fabricate(:media_attachment, account: account),
  177. ].map(&:id)
  178. )
  179. end.to raise_error(
  180. Mastodon::ValidationError,
  181. I18n.t('media_attachments.validations.too_many')
  182. )
  183. end
  184. it 'does not allow attaching both videos and images' do
  185. account = Fabricate(:account)
  186. video = Fabricate(:media_attachment, type: :video, account: account)
  187. image = Fabricate(:media_attachment, type: :image, account: account)
  188. video.update(type: :video)
  189. expect do
  190. subject.call(
  191. account,
  192. text: 'test status update',
  193. media_ids: [
  194. video,
  195. image,
  196. ].map(&:id)
  197. )
  198. end.to raise_error(
  199. Mastodon::ValidationError,
  200. I18n.t('media_attachments.validations.images_and_video')
  201. )
  202. end
  203. it 'returns existing status when used twice with idempotency key' do
  204. account = Fabricate(:account)
  205. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  206. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  207. expect(status2.id).to eq status1.id
  208. end
  209. def create_status_with_options(**options)
  210. subject.call(Fabricate(:account), options.merge(text: 'test'))
  211. end
  212. end