diff --git a/app/controllers/case_assignments_controller.rb b/app/controllers/case_assignments_controller.rb index b2c64a4d0b..914326dd56 100644 --- a/app/controllers/case_assignments_controller.rb +++ b/app/controllers/case_assignments_controller.rb @@ -4,6 +4,7 @@ class CaseAssignmentsController < ApplicationController def create authorize CaseAssignment + ensure_case_in_organization if existing_case_assignment.present? if existing_case_assignment.update(active: true) @@ -83,12 +84,19 @@ def reimbursement def case_assignment_parent if params[:volunteer_id] - User.find(params[:volunteer_id]) + current_organization.users.find(params[:volunteer_id]) else current_organization.casa_cases.friendly.find(params[:casa_case_id]) end end + # The model only checks that the case and volunteer share an org with each other, not with the + # requester, so neither side of the assignment may come from outside the requester's org. + def ensure_case_in_organization + case_id = case_assignment_params[:casa_case_id] + current_organization.casa_cases.find(case_id) if case_id.present? + end + def after_action_path(resource) if resource.is_a? User edit_volunteer_path(resource) diff --git a/spec/requests/case_assignments_spec.rb b/spec/requests/case_assignments_spec.rb index 57d3456036..034b987c7d 100644 --- a/spec/requests/case_assignments_spec.rb +++ b/spec/requests/case_assignments_spec.rb @@ -111,6 +111,44 @@ end end + describe "when the requester is in a different org from the volunteer and case" do + let(:other_org) { create(:casa_org) } + let(:other_volunteer) { create(:volunteer, casa_org: other_org) } + let(:other_case) { create(:casa_case, casa_org: other_org) } + + it "does not assign another org's volunteer to another org's case (volunteer parent)", :aggregate_failures do + expect { + post case_assignments_url(volunteer_id: other_volunteer.id), params: {case_assignment: {casa_case_id: other_case.id}} + }.not_to change(CaseAssignment, :count) + expect(response).to have_http_status(:not_found) + end + + it "does not assign another org's volunteer to another org's case (case parent)", :aggregate_failures do + expect { + post case_assignments_url(casa_case_id: other_case.id), params: {case_assignment: {volunteer_id: other_volunteer.id}} + }.not_to change(CaseAssignment, :count) + expect(response).to have_http_status(:not_found) + end + + it "does not reactivate another org's inactive assignment", :aggregate_failures do + assignment = create(:case_assignment, volunteer: other_volunteer, casa_case: other_case, active: false) + + post case_assignments_url(volunteer_id: other_volunteer.id), params: {case_assignment: {casa_case_id: other_case.id}} + + expect(assignment.reload.active).to be false + post case_assignments_url(casa_case_id: other_case.id), params: {case_assignment: {volunteer_id: other_volunteer.id}} + expect(assignment.reload.active).to be false + end + + it "does not let a supervisor do it either" do + sign_in create(:supervisor, casa_org: casa_org) + + expect { + post case_assignments_url(volunteer_id: other_volunteer.id), params: {case_assignment: {casa_case_id: other_case.id}} + }.not_to change(CaseAssignment, :count) + end + end + describe "with another org params" do subject(:request) do post url, params: params