report_service_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. require 'rails_helper'
  2. RSpec.describe ReportService, type: :service do
  3. subject { described_class.new }
  4. let(:source_account) { Fabricate(:account) }
  5. context 'for a remote account' do
  6. let(:remote_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub, inbox_url: 'http://example.com/inbox') }
  7. before do
  8. stub_request(:post, 'http://example.com/inbox').to_return(status: 200)
  9. end
  10. it 'sends ActivityPub payload when forward is true' do
  11. subject.call(source_account, remote_account, forward: true)
  12. expect(a_request(:post, 'http://example.com/inbox')).to have_been_made
  13. end
  14. it 'does not send anything when forward is false' do
  15. subject.call(source_account, remote_account, forward: false)
  16. expect(a_request(:post, 'http://example.com/inbox')).to_not have_been_made
  17. end
  18. it 'has an uri' do
  19. report = subject.call(source_account, remote_account, forward: true)
  20. expect(report.uri).to_not be_nil
  21. end
  22. end
  23. context 'when the reported status is a DM' do
  24. let(:target_account) { Fabricate(:account) }
  25. let(:status) { Fabricate(:status, account: target_account, visibility: :direct) }
  26. subject do
  27. -> { described_class.new.call(source_account, target_account, status_ids: [status.id]) }
  28. end
  29. context 'when it is addressed to the reporter' do
  30. before do
  31. status.mentions.create(account: source_account)
  32. end
  33. it 'creates a report' do
  34. expect { subject.call }.to change { target_account.targeted_reports.count }.from(0).to(1)
  35. end
  36. it 'attaches the DM to the report' do
  37. subject.call
  38. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[status.id]]
  39. end
  40. end
  41. context 'when it is not addressed to the reporter' do
  42. it 'errors out' do
  43. expect { subject.call }.to raise_error(ActiveRecord::RecordNotFound)
  44. end
  45. end
  46. context 'when the reporter is remote' do
  47. let(:source_account) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/users/1') }
  48. context 'when it is addressed to the reporter' do
  49. before do
  50. status.mentions.create(account: source_account)
  51. end
  52. it 'creates a report' do
  53. expect { subject.call }.to change { target_account.targeted_reports.count }.from(0).to(1)
  54. end
  55. it 'attaches the DM to the report' do
  56. subject.call
  57. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[status.id]]
  58. end
  59. end
  60. context 'when it is not addressed to the reporter' do
  61. it 'does not add the DM to the report' do
  62. subject.call
  63. expect(target_account.targeted_reports.pluck(:status_ids)).to eq [[]]
  64. end
  65. end
  66. end
  67. end
  68. context 'when other reports already exist for the same target' do
  69. let!(:target_account) { Fabricate(:account) }
  70. let!(:other_report) { Fabricate(:report, target_account: target_account) }
  71. subject do
  72. -> { described_class.new.call(source_account, target_account) }
  73. end
  74. before do
  75. ActionMailer::Base.deliveries.clear
  76. source_account.user.settings.notification_emails['report'] = true
  77. end
  78. it 'does not send an e-mail' do
  79. expect { subject.call }.to_not change(ActionMailer::Base.deliveries, :count).from(0)
  80. end
  81. end
  82. end