Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only allow admins to edit their own organization's measurements #1119

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 7 additions & 1 deletion app/controllers/measurements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def index

def show; end

def edit; end
def edit
redirect_to measurements_path, alert: 'Not authorized to edit this measurement' unless current_org_measurement?
end

def update
if @measurement.update(measurement_params)
Expand Down Expand Up @@ -55,4 +57,8 @@ def measurement_params
:measurement_type_id
).merge(organization_id: current_organization.id)
end

def current_org_measurement?
@measurement.organization == current_organization
end
end
26 changes: 21 additions & 5 deletions spec/requests/measurements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,29 @@
end

describe '#edit', :aggregate_failures do
it 'should have response code 200 for admin user' do
measurement_id = FactoryBot.create(:measurement).id
user = create(:user, role: "admin")
it 'should have response code 200 for admin user editing for own organization' do
my_org = FactoryBot.create(:organization, id: 1, name: "My org")
user = create(:user, role: "admin", organization: my_org)
measurement = FactoryBot.create(:measurement, organization: my_org)

sign_in user
get edit_measurement_path(measurement_id)

expect(response).to have_http_status(:success)
get edit_measurement_path(measurement)

expect(response).to have_http_status(200)
end

it 'should have response code 302 for admin user editing for other organization' do
my_org = FactoryBot.create(:organization, id: 1, name: "My org")
other_org = FactoryBot.create(:organization, id: 2, name: "Other org")
user = create(:user, role: "admin", organization: my_org)
measurement = FactoryBot.create(:measurement, organization: other_org)

sign_in user

get edit_measurement_path(measurement)

expect(response).to have_http_status(302)
end

it 'should have response code 302 for non-admin user' do
Expand Down
Loading