You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
7.0 KiB

  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. it 'schedules a status' do
  21. account = Fabricate(:account)
  22. future = Time.now.utc + 2.hours
  23. status = subject.call(account, text: 'Hi future!', scheduled_at: future)
  24. expect(status).to be_a ScheduledStatus
  25. expect(status.scheduled_at).to eq future
  26. expect(status.params['text']).to eq 'Hi future!'
  27. end
  28. it 'does not immediately create a status when scheduling a status' do
  29. account = Fabricate(:account)
  30. media = Fabricate(:media_attachment)
  31. future = Time.now.utc + 2.hours
  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(media.reload.status).to be_nil
  37. expect(Status.where(text: 'Hi future!').exists?).to be_falsey
  38. end
  39. it 'creates response to the original status of boost' do
  40. boosted_status = Fabricate(:status)
  41. in_reply_to_status = Fabricate(:status, reblog: boosted_status)
  42. account = Fabricate(:account)
  43. text = "test status update"
  44. status = subject.call(account, text: text, thread: in_reply_to_status)
  45. expect(status).to be_persisted
  46. expect(status.text).to eq text
  47. expect(status.thread).to eq boosted_status
  48. end
  49. it 'creates a sensitive status' do
  50. status = create_status_with_options(sensitive: true)
  51. expect(status).to be_persisted
  52. expect(status).to be_sensitive
  53. end
  54. it 'creates a status with spoiler text' do
  55. spoiler_text = "spoiler text"
  56. status = create_status_with_options(spoiler_text: spoiler_text)
  57. expect(status).to be_persisted
  58. expect(status.spoiler_text).to eq spoiler_text
  59. end
  60. it 'creates a status with empty default spoiler text' do
  61. status = create_status_with_options(spoiler_text: nil)
  62. expect(status).to be_persisted
  63. expect(status.spoiler_text).to eq ''
  64. end
  65. it 'creates a status with the given visibility' do
  66. status = create_status_with_options(visibility: :private)
  67. expect(status).to be_persisted
  68. expect(status.visibility).to eq "private"
  69. end
  70. it 'creates a status with limited visibility for silenced users' do
  71. status = subject.call(Fabricate(:account, silenced: true), text: 'test', visibility: :public)
  72. expect(status).to be_persisted
  73. expect(status.visibility).to eq "unlisted"
  74. end
  75. it 'creates a status for the given application' do
  76. application = Fabricate(:application)
  77. status = create_status_with_options(application: application)
  78. expect(status).to be_persisted
  79. expect(status.application).to eq application
  80. end
  81. it 'creates a status with a language set' do
  82. account = Fabricate(:account)
  83. text = 'This is an English text.'
  84. status = subject.call(account, text: text)
  85. expect(status.language).to eq 'en'
  86. end
  87. it 'processes mentions' do
  88. mention_service = double(:process_mentions_service)
  89. allow(mention_service).to receive(:call)
  90. allow(ProcessMentionsService).to receive(:new).and_return(mention_service)
  91. account = Fabricate(:account)
  92. status = subject.call(account, text: "test status update")
  93. expect(ProcessMentionsService).to have_received(:new)
  94. expect(mention_service).to have_received(:call).with(status)
  95. end
  96. it 'processes hashtags' do
  97. hashtags_service = double(:process_hashtags_service)
  98. allow(hashtags_service).to receive(:call)
  99. allow(ProcessHashtagsService).to receive(:new).and_return(hashtags_service)
  100. account = Fabricate(:account)
  101. status = subject.call(account, text: "test status update")
  102. expect(ProcessHashtagsService).to have_received(:new)
  103. expect(hashtags_service).to have_received(:call).with(status)
  104. end
  105. it 'gets distributed' do
  106. allow(DistributionWorker).to receive(:perform_async)
  107. allow(Pubsubhubbub::DistributionWorker).to receive(:perform_async)
  108. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  109. account = Fabricate(:account)
  110. status = subject.call(account, text: "test status update")
  111. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  112. expect(Pubsubhubbub::DistributionWorker).to have_received(:perform_async).with(status.stream_entry.id)
  113. expect(ActivityPub::DistributionWorker).to have_received(:perform_async).with(status.id)
  114. end
  115. it 'crawls links' do
  116. allow(LinkCrawlWorker).to receive(:perform_async)
  117. account = Fabricate(:account)
  118. status = subject.call(account, text: "test status update")
  119. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  120. end
  121. it 'attaches the given media to the created status' do
  122. account = Fabricate(:account)
  123. media = Fabricate(:media_attachment)
  124. status = subject.call(
  125. account,
  126. text: "test status update",
  127. media_ids: [media.id],
  128. )
  129. expect(media.reload.status).to eq status
  130. end
  131. it 'does not allow attaching more than 4 files' do
  132. account = Fabricate(:account)
  133. expect do
  134. subject.call(
  135. account,
  136. text: "test status update",
  137. media_ids: [
  138. Fabricate(:media_attachment, account: account),
  139. Fabricate(:media_attachment, account: account),
  140. Fabricate(:media_attachment, account: account),
  141. Fabricate(:media_attachment, account: account),
  142. Fabricate(:media_attachment, account: account),
  143. ].map(&:id),
  144. )
  145. end.to raise_error(
  146. Mastodon::ValidationError,
  147. I18n.t('media_attachments.validations.too_many'),
  148. )
  149. end
  150. it 'does not allow attaching both videos and images' do
  151. account = Fabricate(:account)
  152. expect do
  153. subject.call(
  154. account,
  155. text: "test status update",
  156. media_ids: [
  157. Fabricate(:media_attachment, type: :video, account: account),
  158. Fabricate(:media_attachment, type: :image, account: account),
  159. ].map(&:id),
  160. )
  161. end.to raise_error(
  162. Mastodon::ValidationError,
  163. I18n.t('media_attachments.validations.images_and_video'),
  164. )
  165. end
  166. it 'returns existing status when used twice with idempotency key' do
  167. account = Fabricate(:account)
  168. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  169. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  170. expect(status2.id).to eq status1.id
  171. end
  172. def create_status_with_options(**options)
  173. subject.call(Fabricate(:account), options.merge(text: 'test'))
  174. end
  175. end