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.

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