reports_controller_spec.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::ReportsController, type: :controller do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. describe 'POST #create' do
  11. let!(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  12. let(:scopes) { 'write:reports' }
  13. let(:status) { Fabricate(:status) }
  14. let(:target_account) { status.account }
  15. let(:category) { nil }
  16. let(:forward) { nil }
  17. let(:rule_ids){ nil }
  18. before do
  19. allow(AdminMailer).to receive(:new_report).and_return(double('email', deliver_later: nil))
  20. post :create, params: { status_ids: [status.id], account_id: target_account.id, comment: 'reasons', category: category, rule_ids: rule_ids, forward: forward }
  21. end
  22. it 'returns http success' do
  23. expect(response).to have_http_status(200)
  24. end
  25. it 'creates a report' do
  26. expect(target_account.targeted_reports).to_not be_empty
  27. end
  28. it 'saves comment' do
  29. expect(target_account.targeted_reports.first.comment).to eq 'reasons'
  30. end
  31. it 'sends e-mails to admins' do
  32. expect(AdminMailer).to have_received(:new_report).with(admin.account, Report)
  33. end
  34. context 'when a status does not belong to the reported account' do
  35. let(:target_account) { Fabricate(:account) }
  36. it 'returns http not found' do
  37. expect(response).to have_http_status(404)
  38. end
  39. end
  40. context 'when a category is chosen' do
  41. let(:category) { 'spam' }
  42. it 'saves category' do
  43. expect(target_account.targeted_reports.first.spam?).to be true
  44. end
  45. end
  46. context 'when violated rules are chosen' do
  47. let(:rule) { Fabricate(:rule) }
  48. let(:category) { 'violation' }
  49. let(:rule_ids) { [rule.id] }
  50. it 'saves category' do
  51. expect(target_account.targeted_reports.first.violation?).to be true
  52. end
  53. it 'saves rule_ids' do
  54. expect(target_account.targeted_reports.first.rule_ids).to match_array([rule.id])
  55. end
  56. end
  57. end
  58. end