reports_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Reports' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'write:reports' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'POST /api/v1/reports' do
  9. subject do
  10. post '/api/v1/reports', headers: headers, params: params
  11. end
  12. let!(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  13. let(:status) { Fabricate(:status) }
  14. let(:target_account) { status.account }
  15. let(:category) { 'other' }
  16. let(:forward) { nil }
  17. let(:rule_ids) { nil }
  18. let(:params) do
  19. {
  20. status_ids: [status.id],
  21. account_id: target_account.id,
  22. comment: 'reasons',
  23. category: category,
  24. rule_ids: rule_ids,
  25. forward: forward,
  26. }
  27. end
  28. it_behaves_like 'forbidden for wrong scope', 'read read:reports'
  29. it 'creates a report', :aggregate_failures, :inline_jobs do
  30. emails = capture_emails { subject }
  31. expect(response).to have_http_status(200)
  32. expect(response.content_type)
  33. .to start_with('application/json')
  34. expect(response.parsed_body).to match(
  35. a_hash_including(
  36. status_ids: [status.id.to_s],
  37. category: category,
  38. comment: 'reasons'
  39. )
  40. )
  41. expect(target_account.targeted_reports).to_not be_empty
  42. expect(target_account.targeted_reports.first.comment).to eq 'reasons'
  43. expect(target_account.targeted_reports.first.application).to eq token.application
  44. expect(emails.size)
  45. .to eq(1)
  46. expect(emails.first)
  47. .to have_attributes(
  48. to: contain_exactly(admin.email),
  49. subject: eq(I18n.t('admin_mailer.new_report.subject', instance: Rails.configuration.x.local_domain, id: target_account.targeted_reports.first.id))
  50. )
  51. end
  52. context 'when a status does not belong to the reported account' do
  53. let(:target_account) { Fabricate(:account) }
  54. it 'returns http not found' do
  55. subject
  56. expect(response).to have_http_status(404)
  57. expect(response.content_type)
  58. .to start_with('application/json')
  59. end
  60. end
  61. context 'when a category is chosen' do
  62. let(:category) { 'spam' }
  63. it 'saves category' do
  64. subject
  65. expect(target_account.targeted_reports.first.spam?).to be true
  66. end
  67. end
  68. context 'when violated rules are chosen' do
  69. let(:rule) { Fabricate(:rule) }
  70. let(:category) { 'violation' }
  71. let(:rule_ids) { [rule.id] }
  72. it 'saves category and rule_ids' do
  73. subject
  74. expect(target_account.targeted_reports.first.violation?).to be true
  75. expect(target_account.targeted_reports.first.rule_ids).to contain_exactly(rule.id)
  76. end
  77. end
  78. end
  79. end