report_service_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ReportService, type: :service do
  4. subject { described_class.new }
  5. let(:source_account) { Fabricate(:account) }
  6. let(:target_account) { Fabricate(:account) }
  7. context 'with a local account' do
  8. it 'has a uri' do
  9. report = subject.call(source_account, target_account)
  10. expect(report.uri).to_not be_nil
  11. end
  12. end
  13. context 'with a remote account' do
  14. let(:remote_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
  15. let(:forward) { false }
  16. before do
  17. stub_request(:post, 'http://example.com/inbox').to_return(status: 200)
  18. end
  19. context 'when forward is true' do
  20. let(:forward) { true }
  21. it 'sends ActivityPub payload when forward is true' do
  22. subject.call(source_account, remote_account, forward: forward)
  23. expect(a_request(:post, 'http://example.com/inbox')).to have_been_made
  24. end
  25. it 'has an uri' do
  26. report = subject.call(source_account, remote_account, forward: forward)
  27. expect(report.uri).to_not be_nil
  28. end
  29. context 'when reporting a reply on a different remote server' do
  30. let(:remote_thread_account) { Fabricate(:account, domain: 'foo.com', protocol: :activitypub, inbox_url: 'http://foo.com/inbox') }
  31. let(:reported_status) { Fabricate(:status, account: remote_account, thread: Fabricate(:status, account: remote_thread_account)) }
  32. before do
  33. stub_request(:post, 'http://foo.com/inbox').to_return(status: 200)
  34. end
  35. context 'when forward_to_domains includes both the replied-to domain and the origin domain' do
  36. it 'sends ActivityPub payload to both the author of the replied-to post and the reported user' do
  37. subject.call(source_account, remote_account, status_ids: [reported_status.id], forward: forward, forward_to_domains: [remote_account.domain, remote_thread_account.domain])
  38. expect(a_request(:post, 'http://foo.com/inbox')).to have_been_made
  39. expect(a_request(:post, 'http://example.com/inbox')).to have_been_made
  40. end
  41. end
  42. context 'when forward_to_domains includes only the replied-to domain' do
  43. it 'sends ActivityPub payload only to the author of the replied-to post' do
  44. subject.call(source_account, remote_account, status_ids: [reported_status.id], forward: forward, forward_to_domains: [remote_thread_account.domain])
  45. expect(a_request(:post, 'http://foo.com/inbox')).to have_been_made
  46. expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made
  47. end
  48. end
  49. context 'when forward_to_domains does not include the replied-to domain' do
  50. it 'does not send ActivityPub payload to the author of the replied-to post' do
  51. subject.call(source_account, remote_account, status_ids: [reported_status.id], forward: forward)
  52. expect(a_request(:post, 'http://foo.com/inbox')).to_not have_been_made
  53. end
  54. end
  55. end
  56. context 'when reporting a reply on the same remote server as the person being replied-to' do
  57. let(:remote_thread_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
  58. let(:reported_status) { Fabricate(:status, account: remote_account, thread: Fabricate(:status, account: remote_thread_account)) }
  59. context 'when forward_to_domains includes both the replied-to domain and the origin domain' do
  60. it 'sends ActivityPub payload only once' do
  61. subject.call(source_account, remote_account, status_ids: [reported_status.id], forward: forward, forward_to_domains: [remote_account.domain])
  62. expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
  63. end
  64. end
  65. context 'when forward_to_domains does not include the replied-to domain' do
  66. it 'sends ActivityPub payload only once' do
  67. subject.call(source_account, remote_account, status_ids: [reported_status.id], forward: forward)
  68. expect(a_request(:post, 'http://example.com/inbox')).to have_been_made.once
  69. end
  70. end
  71. end
  72. end
  73. context 'when forward is false' do
  74. it 'does not send anything' do
  75. subject.call(source_account, remote_account, forward: forward)
  76. expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made
  77. end
  78. end
  79. end
  80. context 'when the reported status is a DM' do
  81. subject do
  82. -> { described_class.new.call(source_account, target_account, status_ids: [status.id]) }
  83. end
  84. let(:status) { Fabricate(:status, account: target_account, visibility: :direct) }
  85. context 'when it is addressed to the reporter' do
  86. before do
  87. status.mentions.create(account: source_account)
  88. end
  89. it 'creates a report' do
  90. expect { subject.call }.to change { target_account.targeted_reports.count }.from(0).to(1)
  91. end
  92. it 'attaches the DM to the report' do
  93. subject.call
  94. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[status.id]]
  95. end
  96. end
  97. context 'when it is not addressed to the reporter' do
  98. it 'errors out' do
  99. expect { subject.call }.to raise_error(ActiveRecord::RecordNotFound)
  100. end
  101. end
  102. context 'when the reporter is remote' do
  103. let(:source_account) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/users/1') }
  104. context 'when it is addressed to the reporter' do
  105. before do
  106. status.mentions.create(account: source_account)
  107. end
  108. it 'creates a report' do
  109. expect { subject.call }.to change { target_account.targeted_reports.count }.from(0).to(1)
  110. end
  111. it 'attaches the DM to the report' do
  112. subject.call
  113. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[status.id]]
  114. end
  115. end
  116. context 'when it is not addressed to the reporter' do
  117. it 'does not add the DM to the report' do
  118. subject.call
  119. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[]]
  120. end
  121. end
  122. end
  123. end
  124. context 'when other reports already exist for the same target' do
  125. subject do
  126. -> { described_class.new.call(source_account, target_account) }
  127. end
  128. let!(:other_report) { Fabricate(:report, target_account: target_account) }
  129. before do
  130. ActionMailer::Base.deliveries.clear
  131. source_account.user.settings['notification_emails.report'] = true
  132. source_account.user.save
  133. end
  134. it 'does not send an e-mail' do
  135. expect { subject.call }.to_not change(ActionMailer::Base.deliveries, :count).from(0)
  136. end
  137. end
  138. end