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
7 changes: 4 additions & 3 deletions app/controllers/contact_topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def edit

# POST /contact_topics or /contact_topics.json
def create
@contact_topic = ContactTopic.new(contact_topic_params)
@contact_topic = ContactTopic.new(contact_topic_params.merge(casa_org_id: current_user.casa_org_id))
authorize @contact_topic

if @contact_topic.save
Expand Down Expand Up @@ -56,8 +56,9 @@ def set_contact_topic
@contact_topic = ContactTopic.find(params[:id])
end

# Only allow a list of trusted parameters through.
# Only allow a list of trusted parameters through. The org is never taken from params: new topics
# belong to the creator's org and existing topics cannot be moved to another one.
def contact_topic_params
params.require(:contact_topic).permit(:casa_org_id, :question, :details, :active, :exclude_from_court_report)
params.require(:contact_topic).permit(:question, :details, :active, :exclude_from_court_report)
end
end
7 changes: 6 additions & 1 deletion app/controllers/contact_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def set_contact_type
@contact_type = ContactType.find(params[:id])
end

# The group decides which org a contact type belongs to, so it must come from the current org.
def contact_type_params
params.require(:contact_type).permit(:name, :contact_type_group_id, :active)
permitted = params.require(:contact_type).permit(:name, :contact_type_group_id, :active)
if permitted[:contact_type_group_id].present?
permitted[:contact_type_group_id] = current_organization.contact_type_groups.find(permitted[:contact_type_group_id]).id
end
permitted
end
end
1 change: 0 additions & 1 deletion app/views/contact_topics/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<h1 class="text-2xl font-bold tracking-tight text-slate-900"><%= title %></h1>
<div class="<%= card %>">
<%= form_with model: contact_topic, local: true, class: "space-y-5" do |form| %>
<%= form.hidden_field :casa_org_id %>
<%= render "shared/form_errors", resource: contact_topic %>
<div>
<%= form.label :question, class: label_class do %>Question <%= req %><% end %>
Expand Down
32 changes: 31 additions & 1 deletion spec/requests/contact_topics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
end
end

context "when casa_org_id of another org is submitted" do
let(:other_org) { create(:casa_org) }

it "creates the topic in the creator's own org" do
post contact_topics_url, params: {contact_topic: {casa_org_id: other_org.id, question: "q", details: "d"}}

expect(ContactTopic.last.casa_org).to eq(casa_org)
end
end

context "with invalid parameters" do
let(:attributes) do
{
Expand Down Expand Up @@ -129,7 +139,7 @@
end

context "with invalid parameters" do
let(:attributes) { {casa_org_id: 0} }
let(:attributes) { {question: ""} }

it "renders a response with 422 status (i.e. to display the 'edit' template)" do
patch contact_topic_url(contact_topic), params: {contact_topic: attributes}
Expand All @@ -138,6 +148,26 @@
end
end

describe "PATCH /update with another org's id" do
let(:other_org) { create(:casa_org) }
let!(:contact_topic) { create(:contact_topic, casa_org:) }

it "does not move the topic into another org" do
patch contact_topic_url(contact_topic), params: {contact_topic: {casa_org_id: other_org.id, question: "renamed"}}

expect(contact_topic.reload.casa_org).to eq(casa_org)
expect(contact_topic.question).to eq("renamed")
end

it "does not let an admin edit another org's topic" do
foreign = create(:contact_topic, casa_org: other_org)

patch contact_topic_url(foreign), params: {contact_topic: {question: "hijacked"}}

expect(foreign.reload.question).not_to eq("hijacked")
end
end

describe "DELETE /soft_delete" do
let!(:contact_topic) { create(:contact_topic, casa_org: casa_org) }

Expand Down
32 changes: 32 additions & 0 deletions spec/requests/contact_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@
RSpec.describe "/contact_types", type: :request do
let(:group) { create(:contact_type_group) }

describe "writes that reach into another org" do
let(:org) { create(:casa_org) }
let(:other_group) { create(:contact_type_group, casa_org: create(:casa_org)) }
let(:own_group) { create(:contact_type_group, casa_org: org) }

before { sign_in create(:casa_admin, casa_org: org) }

it "cannot create a contact type inside another org's group", :aggregate_failures do
expect {
post contact_types_path, params: {contact_type: {name: "Sneaky", contact_type_group_id: other_group.id}}
}.not_to change(ContactType, :count)
expect(response).to have_http_status(:not_found)
end

it "cannot move a contact type into another org's group", :aggregate_failures do
contact_type = create(:contact_type, contact_type_group: own_group)

put contact_type_path(contact_type), params: {contact_type: {contact_type_group_id: other_group.id}}

expect(contact_type.reload.contact_type_group).to eq(own_group)
expect(response).to have_http_status(:not_found)
end

it "cannot edit another org's contact type" do
foreign = create(:contact_type, contact_type_group: other_group)

put contact_type_path(foreign), params: {contact_type: {name: "hijacked"}}

expect(foreign.reload.name).not_to eq("hijacked")
end
end

describe "GET /contact_types/new" do
context "logged in as admin user" do
it "can successfully access a contact type create page" do
Expand Down
Loading