post_status_service_spec.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe PostStatusService 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.to_s], 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.to_s)
  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. it 'returns existing status when used twice with idempotency key' do
  46. account = Fabricate(:account)
  47. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep', scheduled_at: future)
  48. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep', scheduled_at: future)
  49. expect(status2.id).to eq status1.id
  50. end
  51. context 'when scheduled_at is less than min offset' do
  52. let(:invalid_scheduled_time) { 4.minutes.from_now }
  53. it 'raises invalid record error' do
  54. expect do
  55. subject.call(account, text: 'Hi future!', scheduled_at: invalid_scheduled_time)
  56. end.to raise_error(
  57. ActiveRecord::RecordInvalid,
  58. 'Validation failed: Scheduled at The scheduled date must be in the future'
  59. )
  60. end
  61. end
  62. end
  63. it 'creates response to the original status of boost' do
  64. boosted_status = Fabricate(:status)
  65. in_reply_to_status = Fabricate(:status, reblog: boosted_status)
  66. account = Fabricate(:account)
  67. text = 'test status update'
  68. status = subject.call(account, text: text, thread: in_reply_to_status)
  69. expect(status).to be_persisted
  70. expect(status.text).to eq text
  71. expect(status.thread).to eq boosted_status
  72. end
  73. it 'creates a sensitive status' do
  74. status = create_status_with_options(sensitive: true)
  75. expect(status).to be_persisted
  76. expect(status).to be_sensitive
  77. end
  78. it 'creates a status with spoiler text' do
  79. spoiler_text = 'spoiler text'
  80. status = create_status_with_options(spoiler_text: spoiler_text)
  81. expect(status).to be_persisted
  82. expect(status.spoiler_text).to eq spoiler_text
  83. end
  84. it 'creates a sensitive status when there is a CW but no text' do
  85. status = subject.call(Fabricate(:account), text: '', spoiler_text: 'foo')
  86. expect(status).to be_persisted
  87. expect(status).to be_sensitive
  88. end
  89. it 'creates a status with empty default spoiler text' do
  90. status = create_status_with_options(spoiler_text: nil)
  91. expect(status).to be_persisted
  92. expect(status.spoiler_text).to eq ''
  93. end
  94. it 'creates a status with the given visibility' do
  95. status = create_status_with_options(visibility: :private)
  96. expect(status).to be_persisted
  97. expect(status.visibility).to eq 'private'
  98. end
  99. it 'raises on an invalid visibility' do
  100. expect do
  101. create_status_with_options(visibility: :xxx)
  102. end.to raise_error(
  103. ActiveRecord::RecordInvalid,
  104. 'Validation failed: Visibility is not included in the list'
  105. )
  106. end
  107. it 'creates a status with limited visibility for silenced users' do
  108. status = subject.call(Fabricate(:account, silenced: true), text: 'test', visibility: :public)
  109. expect(status).to be_persisted
  110. expect(status.visibility).to eq 'unlisted'
  111. end
  112. it 'creates a status for the given application' do
  113. application = Fabricate(:application)
  114. status = create_status_with_options(application: application)
  115. expect(status).to be_persisted
  116. expect(status.application).to eq application
  117. end
  118. it 'creates a status with a language set' do
  119. account = Fabricate(:account)
  120. text = 'This is an English text.'
  121. status = subject.call(account, text: text)
  122. expect(status.language).to eq 'en'
  123. end
  124. it 'processes mentions' do
  125. mention_service = instance_double(ProcessMentionsService)
  126. allow(mention_service).to receive(:call)
  127. allow(ProcessMentionsService).to receive(:new).and_return(mention_service)
  128. account = Fabricate(:account)
  129. status = subject.call(account, text: 'test status update')
  130. expect(ProcessMentionsService).to have_received(:new)
  131. expect(mention_service).to have_received(:call).with(status, save_records: false)
  132. end
  133. it 'safeguards mentions' do
  134. account = Fabricate(:account)
  135. mentioned_account = Fabricate(:account, username: 'alice')
  136. unexpected_mentioned_account = Fabricate(:account, username: 'bob')
  137. expect do
  138. subject.call(account, text: '@alice hm, @bob is really annoying lately', allowed_mentions: [mentioned_account.id])
  139. end.to raise_error(an_instance_of(described_class::UnexpectedMentionsError).and(having_attributes(accounts: [unexpected_mentioned_account])))
  140. end
  141. it 'processes duplicate mentions correctly' do
  142. account = Fabricate(:account)
  143. Fabricate(:account, username: 'alice')
  144. expect do
  145. subject.call(account, text: '@alice @alice @alice hey @alice')
  146. end.to_not raise_error
  147. end
  148. it 'processes hashtags' do
  149. hashtags_service = instance_double(ProcessHashtagsService)
  150. allow(hashtags_service).to receive(:call)
  151. allow(ProcessHashtagsService).to receive(:new).and_return(hashtags_service)
  152. account = Fabricate(:account)
  153. status = subject.call(account, text: 'test status update')
  154. expect(ProcessHashtagsService).to have_received(:new)
  155. expect(hashtags_service).to have_received(:call).with(status)
  156. end
  157. it 'gets distributed' do
  158. allow(DistributionWorker).to receive(:perform_async)
  159. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  160. account = Fabricate(:account)
  161. status = subject.call(account, text: 'test status update')
  162. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  163. expect(ActivityPub::DistributionWorker).to have_received(:perform_async).with(status.id)
  164. end
  165. it 'crawls links' do
  166. allow(LinkCrawlWorker).to receive(:perform_async)
  167. account = Fabricate(:account)
  168. status = subject.call(account, text: 'test status update')
  169. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  170. end
  171. it 'attaches the given media to the created status' do
  172. account = Fabricate(:account)
  173. media = Fabricate(:media_attachment, account: account)
  174. status = subject.call(
  175. account,
  176. text: 'test status update',
  177. media_ids: [media.id.to_s]
  178. )
  179. expect(media.reload.status).to eq status
  180. end
  181. it 'does not attach media from another account to the created status' do
  182. account = Fabricate(:account)
  183. media = Fabricate(:media_attachment, account: Fabricate(:account))
  184. expect do
  185. subject.call(
  186. account,
  187. text: 'test status update',
  188. media_ids: [media.id.to_s]
  189. )
  190. end.to raise_error(
  191. Mastodon::ValidationError,
  192. I18n.t('media_attachments.validations.not_found', ids: media.id)
  193. )
  194. end
  195. it 'does not allow attaching more files than configured limit' do
  196. stub_const('Status::MEDIA_ATTACHMENTS_LIMIT', 1)
  197. account = Fabricate(:account)
  198. expect do
  199. subject.call(
  200. account,
  201. text: 'test status update',
  202. media_ids: Array.new(2) { Fabricate(:media_attachment, account: account) }.map { |m| m.id.to_s }
  203. )
  204. end.to raise_error(
  205. Mastodon::ValidationError,
  206. I18n.t('media_attachments.validations.too_many')
  207. )
  208. end
  209. it 'does not allow attaching both videos and images' do
  210. account = Fabricate(:account)
  211. video = Fabricate(:media_attachment, type: :video, account: account)
  212. image = Fabricate(:media_attachment, type: :image, account: account)
  213. video.update(type: :video)
  214. expect do
  215. subject.call(
  216. account,
  217. text: 'test status update',
  218. media_ids: [
  219. video,
  220. image,
  221. ].map { |m| m.id.to_s }
  222. )
  223. end.to raise_error(
  224. Mastodon::ValidationError,
  225. I18n.t('media_attachments.validations.images_and_video')
  226. )
  227. end
  228. it 'returns existing status when used twice with idempotency key' do
  229. account = Fabricate(:account)
  230. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  231. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  232. expect(status2.id).to eq status1.id
  233. end
  234. def create_status_with_options(**options)
  235. subject.call(Fabricate(:account), options.merge(text: 'test'))
  236. end
  237. end