Skip to content

Commit

Permalink
Only record event if account unlocking was successful
Browse files Browse the repository at this point in the history
The implementation of `Users::UnlockingsController#update` was carried
over from `UsersController#unlock` and I thought the order of the method
calls was a bit odd. I've now added a controller test to highlight the
problem and fixed it by reordering the method calls in the action.
  • Loading branch information
floehopper committed Nov 30, 2023
1 parent 3b82f06 commit a15ae9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/users/unlockings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Users::UnlockingsController < ApplicationController
def edit; end

def update
EventLog.record_event(@user, EventLog::MANUAL_ACCOUNT_UNLOCK, initiator: current_user, ip_address: user_ip_address)
@user.unlock_access!
EventLog.record_event(@user, EventLog::MANUAL_ACCOUNT_UNLOCK, initiator: current_user, ip_address: user_ip_address)
flash[:notice] = "Unlocked #{@user.email}"
redirect_to edit_user_path(@user)
end
Expand Down
10 changes: 10 additions & 0 deletions test/controllers/users/unlockings_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ class Users::UnlockingsControllerTest < ActionController::TestCase
put :update, params: { user_id: user }
end

should "not record manual account unlock event if User#unlock! raises an exception" do
user = build(:locked_user, id: 123)
User.stubs(:find).returns(user)
user.stubs(:unlock_access!).raises("boom!")

EventLog.expects(:record_event).never

assert_raises { put :update, params: { user_id: user } }
end

should "redirect to edit user page and display success notice" do
user = create(:locked_user, email: "[email protected]")

Expand Down

0 comments on commit a15ae9c

Please sign in to comment.