Skip to content

Commit

Permalink
Merge pull request boacausa#139 from kellynvd/fix-user-update
Browse files Browse the repository at this point in the history
Fix user update when current user loses admin privileges
  • Loading branch information
carolinesalib authored Jan 11, 2020
2 parents aaa5f7f + 04f476f commit 04f463f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
10 changes: 7 additions & 3 deletions app/controllers/ngo_area/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ def edit

def update
@user = User.find(params[:id])
@user.update params_user
@user.update(params_user)

redirect_to ngo_area_users_path
if current_user == @user && !@user.admin?
redirect_to home_index_path
else
redirect_to ngo_area_users_path
end
end

private
Expand All @@ -37,4 +41,4 @@ def params_user
def check_admin_privileges
head :not_found unless current_user.admin?
end
end
end
35 changes: 27 additions & 8 deletions spec/controllers/ngo_area/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,36 @@
end
end

describe 'GET #update' do
before do
get :update, params: { id: subject.id, user: attributes_for(:user) }
end
describe 'PATCH #update' do
context 'when current user updates another user' do
before do
patch :update, params: { id: subject.id, user: attributes_for(:user) }
end

it 'returns 302 code status' do
expect(response).to have_http_status(:found)
it 'returns 302 code status' do
expect(response).to have_http_status(:found)
end

it 'redirects to index' do
expect(response).to redirect_to(action: :index)
end
end

it 'redirects to index' do
expect(response).to redirect_to(action: :index)
context 'when current user loses admin privileges' do
before do
admin = create(:user, :admin_privileges)
allow(controller).to receive(:current_user) { admin }

patch :update, params: { id: admin.id, user: attributes_for(:user) }
end

it 'returns 302 code status' do
expect(response).to have_http_status(:found)
end

it 'redirects to Home' do
expect(response).to redirect_to(home_index_path)
end
end
end
end

0 comments on commit 04f463f

Please sign in to comment.