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.

201 lines
5.8 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 for the given application' do
  52. application = Fabricate(:application)
  53. status = create_status_with_options(application: application)
  54. expect(status).to be_persisted
  55. expect(status.application).to eq application
  56. end
  57. it 'creates a status with a language set' do
  58. account = Fabricate(:account)
  59. text = 'This is an English text.'
  60. status = subject.call(account, text)
  61. expect(status.language).to eq 'en'
  62. end
  63. it 'processes mentions' do
  64. mention_service = double(:process_mentions_service)
  65. allow(mention_service).to receive(:call)
  66. allow(ProcessMentionsService).to receive(:new).and_return(mention_service)
  67. account = Fabricate(:account)
  68. status = subject.call(account, "test status update")
  69. expect(ProcessMentionsService).to have_received(:new)
  70. expect(mention_service).to have_received(:call).with(status)
  71. end
  72. it 'processes hashtags' do
  73. hashtags_service = double(:process_hashtags_service)
  74. allow(hashtags_service).to receive(:call)
  75. allow(ProcessHashtagsService).to receive(:new).and_return(hashtags_service)
  76. account = Fabricate(:account)
  77. status = subject.call(account, "test status update")
  78. expect(ProcessHashtagsService).to have_received(:new)
  79. expect(hashtags_service).to have_received(:call).with(status)
  80. end
  81. it 'gets distributed' do
  82. allow(DistributionWorker).to receive(:perform_async)
  83. allow(Pubsubhubbub::DistributionWorker).to receive(:perform_async)
  84. allow(ActivityPub::DistributionWorker).to receive(:perform_async)
  85. account = Fabricate(:account)
  86. status = subject.call(account, "test status update")
  87. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  88. expect(Pubsubhubbub::DistributionWorker).to have_received(:perform_async).with(status.stream_entry.id)
  89. expect(ActivityPub::DistributionWorker).to have_received(:perform_async).with(status.id)
  90. end
  91. it 'crawls links' do
  92. allow(LinkCrawlWorker).to receive(:perform_async)
  93. account = Fabricate(:account)
  94. status = subject.call(account, "test status update")
  95. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  96. end
  97. it 'attaches the given media to the created status' do
  98. account = Fabricate(:account)
  99. media = Fabricate(:media_attachment)
  100. status = subject.call(
  101. account,
  102. "test status update",
  103. nil,
  104. media_ids: [media.id],
  105. )
  106. expect(media.reload.status).to eq status
  107. end
  108. it 'does not allow attaching more than 4 files' do
  109. account = Fabricate(:account)
  110. expect do
  111. subject.call(
  112. account,
  113. "test status update",
  114. nil,
  115. media_ids: [
  116. Fabricate(:media_attachment, account: account),
  117. Fabricate(:media_attachment, account: account),
  118. Fabricate(:media_attachment, account: account),
  119. Fabricate(:media_attachment, account: account),
  120. Fabricate(:media_attachment, account: account),
  121. ].map(&:id),
  122. )
  123. end.to raise_error(
  124. Mastodon::ValidationError,
  125. I18n.t('media_attachments.validations.too_many'),
  126. )
  127. end
  128. it 'does not allow attaching both videos and images' do
  129. account = Fabricate(:account)
  130. expect do
  131. subject.call(
  132. account,
  133. "test status update",
  134. nil,
  135. media_ids: [
  136. Fabricate(:media_attachment, type: :video, account: account),
  137. Fabricate(:media_attachment, type: :image, account: account),
  138. ].map(&:id),
  139. )
  140. end.to raise_error(
  141. Mastodon::ValidationError,
  142. I18n.t('media_attachments.validations.images_and_video'),
  143. )
  144. end
  145. it 'returns existing status when used twice with idempotency key' do
  146. account = Fabricate(:account)
  147. status1 = subject.call(account, 'test', nil, idempotency: 'meepmeep')
  148. status2 = subject.call(account, 'test', nil, idempotency: 'meepmeep')
  149. expect(status2.id).to eq status1.id
  150. end
  151. def create_status_with_options(**options)
  152. subject.call(Fabricate(:account), 'test', nil, options)
  153. end
  154. end