1
0

report_service_spec.rb 3.5 KB

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