From cd55bcd3340295422fc969833b802f564aaa71bc Mon Sep 17 00:00:00 2001 From: compwron Date: Mon, 16 Nov 2026 00:24:31 -0800 Subject: [PATCH] Scope court report download and generation to the user's visible cases show and generate looked the case up by number within the org, so any volunteer could download the stored report of a case they were not assigned to, and a formerly assigned volunteer could regenerate it. Resolve the case through policy_scope(CasaCase) so volunteers only reach actively assigned cases, and 'not yours' answers exactly like 'not found'. Co-Authored-By: Claude Sonnet 5.5 --- .../case_court_reports_controller.rb | 5 +- spec/requests/case_court_reports_spec.rb | 65 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/app/controllers/case_court_reports_controller.rb b/app/controllers/case_court_reports_controller.rb index e708824435..c361528eae 100644 --- a/app/controllers/case_court_reports_controller.rb +++ b/app/controllers/case_court_reports_controller.rb @@ -36,7 +36,7 @@ def show def generate authorize CaseCourtReport - casa_case = CasaCase.find_by(case_number: case_params[:case_number], casa_org: current_user.casa_org) + casa_case = policy_scope(CasaCase).find_by(case_number: case_params[:case_number]) respond_to do |format| format.json do @@ -76,7 +76,8 @@ def case_params end def set_casa_case - @casa_case = CasaCase.find_by(case_number: params[:id], casa_org: current_user.casa_org) + # Volunteers only see cases they are actively assigned to; "not yours" looks the same as "not found". + @casa_case = policy_scope(CasaCase).find_by(case_number: params[:id]) end # The case picker renders CasaCaseDecorator#court_report_select_option for every case -- for an diff --git a/spec/requests/case_court_reports_spec.rb b/spec/requests/case_court_reports_spec.rb index 53644d5a5b..1cfcab1755 100644 --- a/spec/requests/case_court_reports_spec.rb +++ b/spec/requests/case_court_reports_spec.rb @@ -86,6 +86,46 @@ end end + context "when the case belongs to someone else's caseload" do + let(:other_case) { create(:casa_case, casa_org: volunteer.casa_org, case_number: "CINA-OTHER-1") } + + before do + other_case.court_reports.attach(io: StringIO.new("secret"), filename: "stored-report.docx") + end + + it "does not send an unassigned volunteer the stored report, and looks the same as a missing case", :aggregate_failures do + get case_court_report_path(other_case.case_number, format: "docx") + + expect(response).to redirect_to(case_court_reports_path) + expect(response.body).not_to include("secret") + expect(flash[:alert]).to eq "Report #{other_case.case_number} is not found." + end + + it "does not send a volunteer whose assignment was deactivated" do + create(:case_assignment, casa_case: other_case, volunteer: volunteer, active: false) + + get case_court_report_path(other_case.case_number, format: "docx") + + expect(response).to redirect_to(case_court_reports_path) + end + + it "still sends a supervisor in the same org the stored report" do + sign_in create(:supervisor, casa_org: volunteer.casa_org) + + get case_court_report_path(other_case.case_number, format: "docx") + + expect(response).to have_http_status(:ok) + end + + it "does not send a supervisor from another org the stored report" do + sign_in create(:supervisor, casa_org: create(:casa_org)) + + get case_court_report_path(other_case.case_number, format: "docx") + + expect(response).to redirect_to(case_court_reports_path) + end + end + context "when an INVALID / non-existing case is sent" do let(:invalid_casa_case) { build_stubbed(:casa_case) } @@ -143,6 +183,31 @@ expect(request).to have_http_status(:ok) end + context "when the case is not on the volunteer's caseload" do + let(:other_case) { create(:casa_case, casa_org: volunteer.casa_org, case_number: "CINA-OTHER-2") } + let(:missing_params) { {case_court_report: {case_number: "NO-SUCH-CASE", start_date: "January 1, 2020", end_date: "January 1, 2021"}} } + let(:params) { {case_court_report: {case_number: other_case.case_number, start_date: "January 1, 2020", end_date: "January 1, 2021"}} } + + it "does not generate or store a report, and answers exactly like an unknown case number", :aggregate_failures do + expect { request }.not_to change { other_case.court_reports.count } + not_yours_status = response.status + not_yours_body = response.parsed_body.slice("link", "status") + + post generate_case_court_reports_path, params: missing_params, headers: {ACCEPT: "application/json"} + + expect(response.status).to eq not_yours_status + expect(response.parsed_body.slice("link", "status")).to eq not_yours_body + expect(not_yours_status).to eq 404 + end + + it "does not let a previously unassigned volunteer replace the case's latest report" do + create(:case_assignment, casa_case: other_case, volunteer: volunteer, active: false) + + expect { request }.not_to change { other_case.court_reports.count } + expect(response).to have_http_status(:not_found) + end + end + context "when no custom template is set" do it "sends response as a JSON string", :aggregate_failures do expect(request.content_type).to eq("application/json; charset=utf-8")