闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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