report_service_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. is_expected.to change { target_account.targeted_reports.count }.from(0).to(1)
  42. end
  43. end
  44. context 'when it is not addressed to the reporter' do
  45. it 'errors out' do
  46. is_expected.to raise_error
  47. end
  48. end
  49. end
  50. context 'when other reports already exist for the same target' do
  51. let!(:target_account) { Fabricate(:account) }
  52. let!(:other_report) { Fabricate(:report, target_account: target_account) }
  53. subject do
  54. -> { described_class.new.call(source_account, target_account) }
  55. end
  56. before do
  57. ActionMailer::Base.deliveries.clear
  58. source_account.user.settings.notification_emails['report'] = true
  59. end
  60. it 'does not send an e-mail' do
  61. is_expected.to_not change(ActionMailer::Base.deliveries, :count).from(0)
  62. end
  63. end
  64. end