post_status_service_spec.rb 8.3 KB

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