report_notes_controller_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::ReportNotesController do
  4. render_views
  5. let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'POST #create' do
  10. subject { post :create, params: params }
  11. let(:report) { Fabricate(:report, action_taken_at: action_taken, action_taken_by_account_id: account_id) }
  12. context 'when parameter is valid' do
  13. context 'when report is unsolved' do
  14. let(:action_taken) { nil }
  15. let(:account_id) { nil }
  16. context 'when create_and_resolve flag is on' do
  17. let(:params) { { report_note: { report_id: report.id, content: 'test content' }, create_and_resolve: nil } }
  18. it 'creates a report note and resolves report' do
  19. expect { subject }.to change(ReportNote, :count).by(1)
  20. expect(report.reload).to be_action_taken
  21. expect(response).to redirect_to admin_reports_path
  22. end
  23. end
  24. context 'when create_and_resolve flag is false' do
  25. let(:params) { { report_note: { report_id: report.id, content: 'test content' } } }
  26. it 'creates a report note and does not resolve report' do
  27. expect { subject }.to change(ReportNote, :count).by(1)
  28. expect(report.reload).to_not be_action_taken
  29. expect(response).to redirect_to admin_report_path(report)
  30. end
  31. end
  32. end
  33. context 'when report is resolved' do
  34. let(:action_taken) { Time.now.utc }
  35. let(:account_id) { user.account.id }
  36. context 'when create_and_unresolve flag is on' do
  37. let(:params) { { report_note: { report_id: report.id, content: 'test content' }, create_and_unresolve: nil } }
  38. it 'creates a report note and unresolves report' do
  39. expect { subject }.to change(ReportNote, :count).by(1)
  40. expect(report.reload).to_not be_action_taken
  41. expect(response).to redirect_to admin_report_path(report)
  42. end
  43. end
  44. context 'when create_and_unresolve flag is false' do
  45. let(:params) { { report_note: { report_id: report.id, content: 'test content' } } }
  46. it 'creates a report note and does not unresolve report' do
  47. expect { subject }.to change(ReportNote, :count).by(1)
  48. expect(report.reload).to be_action_taken
  49. expect(response).to redirect_to admin_report_path(report)
  50. end
  51. end
  52. end
  53. end
  54. context 'when content is too short' do
  55. let(:params) { { report_note: { report_id: report.id, content: '' } } }
  56. let(:action_taken) { nil }
  57. let(:account_id) { nil }
  58. it 'renders admin/reports/show' do
  59. expect { subject }.to_not change(ReportNote, :count)
  60. expect(subject).to render_template 'admin/reports/show'
  61. end
  62. end
  63. context 'when content is too long' do
  64. let(:params) { { report_note: { report_id: report.id, content: 'test' * ReportNote::CONTENT_SIZE_LIMIT } } }
  65. let(:action_taken) { nil }
  66. let(:account_id) { nil }
  67. it 'renders admin/reports/show' do
  68. expect { subject }.to_not change(ReportNote, :count)
  69. expect(subject).to render_template 'admin/reports/show'
  70. end
  71. end
  72. end
  73. describe 'DELETE #destroy' do
  74. subject { delete :destroy, params: { id: report_note.id } }
  75. let!(:report_note) { Fabricate(:report_note) }
  76. it 'deletes note' do
  77. expect { subject }.to change(ReportNote, :count).by(-1)
  78. expect(response).to redirect_to admin_report_path(report_note.report)
  79. end
  80. end
  81. end