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.

170 lines
4.8 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 'processes mentions' do
  48. mention_service = double(:process_mentions_service)
  49. allow(mention_service).to receive(:call)
  50. allow(ProcessMentionsService).to receive(:new).and_return(mention_service)
  51. account = Fabricate(:account)
  52. status = subject.call(account, "test status update")
  53. expect(ProcessMentionsService).to have_received(:new)
  54. expect(mention_service).to have_received(:call).with(status)
  55. end
  56. it 'processes hashtags' do
  57. hashtags_service = double(:process_hashtags_service)
  58. allow(hashtags_service).to receive(:call)
  59. allow(ProcessHashtagsService).to receive(:new).and_return(hashtags_service)
  60. account = Fabricate(:account)
  61. status = subject.call(account, "test status update")
  62. expect(ProcessHashtagsService).to have_received(:new)
  63. expect(hashtags_service).to have_received(:call).with(status)
  64. end
  65. it 'pings PuSH hubs' do
  66. allow(DistributionWorker).to receive(:perform_async)
  67. allow(Pubsubhubbub::DistributionWorker).to receive(:perform_async)
  68. account = Fabricate(:account)
  69. status = subject.call(account, "test status update")
  70. expect(DistributionWorker).to have_received(:perform_async).with(status.id)
  71. expect(Pubsubhubbub::DistributionWorker).
  72. to have_received(:perform_async).with(status.stream_entry.id)
  73. end
  74. it 'crawls links' do
  75. allow(LinkCrawlWorker).to receive(:perform_async)
  76. account = Fabricate(:account)
  77. status = subject.call(account, "test status update")
  78. expect(LinkCrawlWorker).to have_received(:perform_async).with(status.id)
  79. end
  80. it 'attaches the given media to the created status' do
  81. account = Fabricate(:account)
  82. media = Fabricate(:media_attachment)
  83. status = subject.call(
  84. account,
  85. "test status update",
  86. nil,
  87. media_ids: [media.id],
  88. )
  89. expect(media.reload.status).to eq status
  90. end
  91. it 'does not allow attaching more than 4 files' do
  92. account = Fabricate(:account)
  93. expect do
  94. subject.call(
  95. account,
  96. "test status update",
  97. nil,
  98. media_ids: [
  99. Fabricate(:media_attachment, account: account),
  100. Fabricate(:media_attachment, account: account),
  101. Fabricate(:media_attachment, account: account),
  102. Fabricate(:media_attachment, account: account),
  103. Fabricate(:media_attachment, account: account),
  104. ].map(&:id),
  105. )
  106. end.to raise_error(
  107. Mastodon::ValidationError,
  108. I18n.t('media_attachments.validations.too_many'),
  109. )
  110. end
  111. it 'does not allow attaching both videos and images' do
  112. account = Fabricate(:account)
  113. expect do
  114. subject.call(
  115. account,
  116. "test status update",
  117. nil,
  118. media_ids: [
  119. Fabricate(:media_attachment, type: :video, account: account),
  120. Fabricate(:media_attachment, type: :image, account: account),
  121. ].map(&:id),
  122. )
  123. end.to raise_error(
  124. Mastodon::ValidationError,
  125. I18n.t('media_attachments.validations.images_and_video'),
  126. )
  127. end
  128. def create_status_with_options(options = {})
  129. subject.call(Fabricate(:account), "test", nil, options)
  130. end
  131. end