1
0

report_service_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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' 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. end
  57. context 'when forward is false' do
  58. it 'does not send anything' do
  59. subject.call(source_account, remote_account, forward: forward)
  60. expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made
  61. end
  62. end
  63. end
  64. context 'when the reported status is a DM' do
  65. subject do
  66. -> { described_class.new.call(source_account, target_account, status_ids: [status.id]) }
  67. end
  68. let(:status) { Fabricate(:status, account: target_account, visibility: :direct) }
  69. context 'when it is addressed to the reporter' do
  70. before do
  71. status.mentions.create(account: source_account)
  72. end
  73. it 'creates a report' do
  74. expect { subject.call }.to change { target_account.targeted_reports.count }.from(0).to(1)
  75. end
  76. it 'attaches the DM to the report' do
  77. subject.call
  78. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[status.id]]
  79. end
  80. end
  81. context 'when it is not addressed to the reporter' do
  82. it 'errors out' do
  83. expect { subject.call }.to raise_error(ActiveRecord::RecordNotFound)
  84. end
  85. end
  86. context 'when the reporter is remote' do
  87. let(:source_account) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/users/1') }
  88. context 'when it is addressed to the reporter' do
  89. before do
  90. status.mentions.create(account: source_account)
  91. end
  92. it 'creates a report' do
  93. expect { subject.call }.to change { target_account.targeted_reports.count }.from(0).to(1)
  94. end
  95. it 'attaches the DM to the report' do
  96. subject.call
  97. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[status.id]]
  98. end
  99. end
  100. context 'when it is not addressed to the reporter' do
  101. it 'does not add the DM to the report' do
  102. subject.call
  103. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[]]
  104. end
  105. end
  106. end
  107. end
  108. context 'when other reports already exist for the same target' do
  109. subject do
  110. -> { described_class.new.call(source_account, target_account) }
  111. end
  112. let!(:other_report) { Fabricate(:report, target_account: target_account) }
  113. before do
  114. ActionMailer::Base.deliveries.clear
  115. source_account.user.settings['notification_emails.report'] = true
  116. source_account.user.save
  117. end
  118. it 'does not send an e-mail' do
  119. expect { subject.call }.to_not change(ActionMailer::Base.deliveries, :count).from(0)
  120. end
  121. end
  122. end