post_status_service_spec.rb 8.5 KB

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