闭社主体 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.

189 lines
5.4 KiB

  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 'pings PuSH hubs' do
  74. allow(DistributionWorker).to receive(:perform_async)
  75. allow(Pubsubhubbub::DistributionWorker).to receive(:perform_async)
  76. account = Fabricate(:account)
  77. status = subject.call(account, "test status update")
  78. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  79. expect(Pubsubhubbub::DistributionWorker).
  80. to have_received(:perform_async).with(status.stream_entry.id)
  81. end
  82. it 'crawls links' do
  83. allow(LinkCrawlWorker).to receive(:perform_async)
  84. account = Fabricate(:account)
  85. status = subject.call(account, "test status update")
  86. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  87. end
  88. it 'attaches the given media to the created status' do
  89. account = Fabricate(:account)
  90. media = Fabricate(:media_attachment)
  91. status = subject.call(
  92. account,
  93. "test status update",
  94. nil,
  95. media_ids: [media.id],
  96. )
  97. expect(media.reload.status).to eq status
  98. end
  99. it 'does not allow attaching more than 4 files' do
  100. account = Fabricate(:account)
  101. expect do
  102. subject.call(
  103. account,
  104. "test status update",
  105. nil,
  106. media_ids: [
  107. Fabricate(:media_attachment, account: account),
  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. ].map(&:id),
  113. )
  114. end.to raise_error(
  115. Mastodon::ValidationError,
  116. I18n.t('media_attachments.validations.too_many'),
  117. )
  118. end
  119. it 'does not allow attaching both videos and images' do
  120. account = Fabricate(:account)
  121. expect do
  122. subject.call(
  123. account,
  124. "test status update",
  125. nil,
  126. media_ids: [
  127. Fabricate(:media_attachment, type: :video, account: account),
  128. Fabricate(:media_attachment, type: :image, account: account),
  129. ].map(&:id),
  130. )
  131. end.to raise_error(
  132. Mastodon::ValidationError,
  133. I18n.t('media_attachments.validations.images_and_video'),
  134. )
  135. end
  136. it 'returns existing status when used twice with idempotency key' do
  137. account = Fabricate(:account)
  138. status1 = subject.call(account, 'test', nil, idempotency: 'meepmeep')
  139. status2 = subject.call(account, 'test', nil, idempotency: 'meepmeep')
  140. expect(status2.id).to eq status1.id
  141. end
  142. def create_status_with_options(options = {})
  143. subject.call(Fabricate(:account), 'test', nil, options)
  144. end
  145. end