Skip to content

Commit cd55bcd

Browse files
compwronclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 1ec75f8 commit cd55bcd

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎app/controllers/case_court_reports_controller.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def show
3636

3737
def generate
3838
authorize CaseCourtReport
39-
casa_case = CasaCase.find_by(case_number: case_params[:case_number], casa_org: current_user.casa_org)
39+
casa_case = policy_scope(CasaCase).find_by(case_number: case_params[:case_number])
4040

4141
respond_to do |format|
4242
format.json do
@@ -76,7 +76,8 @@ def case_params
7676
end
7777

7878
def set_casa_case
79-
@casa_case = CasaCase.find_by(case_number: params[:id], casa_org: current_user.casa_org)
79+
# Volunteers only see cases they are actively assigned to; "not yours" looks the same as "not found".
80+
@casa_case = policy_scope(CasaCase).find_by(case_number: params[:id])
8081
end
8182

8283
# The case picker renders CasaCaseDecorator#court_report_select_option for every case -- for an

‎spec/requests/case_court_reports_spec.rb‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,46 @@
8686
end
8787
end
8888

89+
context "when the case belongs to someone else's caseload" do
90+
let(:other_case) { create(:casa_case, casa_org: volunteer.casa_org, case_number: "CINA-OTHER-1") }
91+
92+
before do
93+
other_case.court_reports.attach(io: StringIO.new("secret"), filename: "stored-report.docx")
94+
end
95+
96+
it "does not send an unassigned volunteer the stored report, and looks the same as a missing case", :aggregate_failures do
97+
get case_court_report_path(other_case.case_number, format: "docx")
98+
99+
expect(response).to redirect_to(case_court_reports_path)
100+
expect(response.body).not_to include("secret")
101+
expect(flash[:alert]).to eq "Report #{other_case.case_number} is not found."
102+
end
103+
104+
it "does not send a volunteer whose assignment was deactivated" do
105+
create(:case_assignment, casa_case: other_case, volunteer: volunteer, active: false)
106+
107+
get case_court_report_path(other_case.case_number, format: "docx")
108+
109+
expect(response).to redirect_to(case_court_reports_path)
110+
end
111+
112+
it "still sends a supervisor in the same org the stored report" do
113+
sign_in create(:supervisor, casa_org: volunteer.casa_org)
114+
115+
get case_court_report_path(other_case.case_number, format: "docx")
116+
117+
expect(response).to have_http_status(:ok)
118+
end
119+
120+
it "does not send a supervisor from another org the stored report" do
121+
sign_in create(:supervisor, casa_org: create(:casa_org))
122+
123+
get case_court_report_path(other_case.case_number, format: "docx")
124+
125+
expect(response).to redirect_to(case_court_reports_path)
126+
end
127+
end
128+
89129
context "when an INVALID / non-existing case is sent" do
90130
let(:invalid_casa_case) { build_stubbed(:casa_case) }
91131

@@ -143,6 +183,31 @@
143183
expect(request).to have_http_status(:ok)
144184
end
145185

186+
context "when the case is not on the volunteer's caseload" do
187+
let(:other_case) { create(:casa_case, casa_org: volunteer.casa_org, case_number: "CINA-OTHER-2") }
188+
let(:missing_params) { {case_court_report: {case_number: "NO-SUCH-CASE", start_date: "January 1, 2020", end_date: "January 1, 2021"}} }
189+
let(:params) { {case_court_report: {case_number: other_case.case_number, start_date: "January 1, 2020", end_date: "January 1, 2021"}} }
190+
191+
it "does not generate or store a report, and answers exactly like an unknown case number", :aggregate_failures do
192+
expect { request }.not_to change { other_case.court_reports.count }
193+
not_yours_status = response.status
194+
not_yours_body = response.parsed_body.slice("link", "status")
195+
196+
post generate_case_court_reports_path, params: missing_params, headers: {ACCEPT: "application/json"}
197+
198+
expect(response.status).to eq not_yours_status
199+
expect(response.parsed_body.slice("link", "status")).to eq not_yours_body
200+
expect(not_yours_status).to eq 404
201+
end
202+
203+
it "does not let a previously unassigned volunteer replace the case's latest report" do
204+
create(:case_assignment, casa_case: other_case, volunteer: volunteer, active: false)
205+
206+
expect { request }.not_to change { other_case.court_reports.count }
207+
expect(response).to have_http_status(:not_found)
208+
end
209+
end
210+
146211
context "when no custom template is set" do
147212
it "sends response as a JSON string", :aggregate_failures do
148213
expect(request.content_type).to eq("application/json; charset=utf-8")

0 commit comments

Comments
 (0)