post_status_service_spec.rb 5.5 KB

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