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.

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