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.

243 lines
7.3 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, account: account)
  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 attach media from another account to the created status' do
  132. account = Fabricate(:account)
  133. media = Fabricate(:media_attachment, account: Fabricate(:account))
  134. status = subject.call(
  135. account,
  136. text: "test status update",
  137. media_ids: [media.id],
  138. )
  139. expect(media.reload.status).to eq nil
  140. end
  141. it 'does not allow attaching more than 4 files' do
  142. account = Fabricate(:account)
  143. expect do
  144. subject.call(
  145. account,
  146. text: "test status update",
  147. media_ids: [
  148. Fabricate(:media_attachment, account: account),
  149. Fabricate(:media_attachment, account: account),
  150. Fabricate(:media_attachment, account: account),
  151. Fabricate(:media_attachment, account: account),
  152. Fabricate(:media_attachment, account: account),
  153. ].map(&:id),
  154. )
  155. end.to raise_error(
  156. Mastodon::ValidationError,
  157. I18n.t('media_attachments.validations.too_many'),
  158. )
  159. end
  160. it 'does not allow attaching both videos and images' do
  161. account = Fabricate(:account)
  162. expect do
  163. subject.call(
  164. account,
  165. text: "test status update",
  166. media_ids: [
  167. Fabricate(:media_attachment, type: :video, account: account),
  168. Fabricate(:media_attachment, type: :image, account: account),
  169. ].map(&:id),
  170. )
  171. end.to raise_error(
  172. Mastodon::ValidationError,
  173. I18n.t('media_attachments.validations.images_and_video'),
  174. )
  175. end
  176. it 'returns existing status when used twice with idempotency key' do
  177. account = Fabricate(:account)
  178. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  179. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  180. expect(status2.id).to eq status1.id
  181. end
  182. def create_status_with_options(**options)
  183. subject.call(Fabricate(:account), options.merge(text: 'test'))
  184. end
  185. end