post_status_service_spec.rb 7.8 KB

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