Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/case_assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions spec/requests/case_assignments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading