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.

241 lines
7.2 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(ActivityPub::DistributionWorker).to receive(:perform_async)
  108. account = Fabricate(:account)
  109. status = subject.call(account, text: "test status update")
  110. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  111. expect(ActivityPub::DistributionWorker).to have_received(:perform_async).with(status.id)
  112. end
  113. it 'crawls links' do
  114. allow(LinkCrawlWorker).to receive(:perform_async)
  115. account = Fabricate(:account)
  116. status = subject.call(account, text: "test status update")
  117. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  118. end
  119. it 'attaches the given media to the created status' do
  120. account = Fabricate(:account)
  121. media = Fabricate(:media_attachment, account: account)
  122. status = subject.call(
  123. account,
  124. text: "test status update",
  125. media_ids: [media.id],
  126. )
  127. expect(media.reload.status).to eq status
  128. end
  129. it 'does not attach media from another account to the created status' do
  130. account = Fabricate(:account)
  131. media = Fabricate(:media_attachment, account: Fabricate(: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 nil
  138. end
  139. it 'does not allow attaching more than 4 files' 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, account: account),
  147. Fabricate(:media_attachment, account: account),
  148. Fabricate(:media_attachment, account: account),
  149. Fabricate(:media_attachment, account: account),
  150. Fabricate(:media_attachment, account: account),
  151. ].map(&:id),
  152. )
  153. end.to raise_error(
  154. Mastodon::ValidationError,
  155. I18n.t('media_attachments.validations.too_many'),
  156. )
  157. end
  158. it 'does not allow attaching both videos and images' do
  159. account = Fabricate(:account)
  160. expect do
  161. subject.call(
  162. account,
  163. text: "test status update",
  164. media_ids: [
  165. Fabricate(:media_attachment, type: :video, account: account),
  166. Fabricate(:media_attachment, type: :image, account: account),
  167. ].map(&:id),
  168. )
  169. end.to raise_error(
  170. Mastodon::ValidationError,
  171. I18n.t('media_attachments.validations.images_and_video'),
  172. )
  173. end
  174. it 'returns existing status when used twice with idempotency key' do
  175. account = Fabricate(:account)
  176. status1 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  177. status2 = subject.call(account, text: 'test', idempotency: 'meepmeep')
  178. expect(status2.id).to eq status1.id
  179. end
  180. def create_status_with_options(**options)
  181. subject.call(Fabricate(:account), options.merge(text: 'test'))
  182. end
  183. end