FIX: do not log username change if the save process was unsuccessful

This commit is contained in:
Arpit Jalan 2019-05-13 22:19:42 +05:30
parent 5605dba85c
commit a2b1789c3e
2 changed files with 8 additions and 4 deletions

View File

@ -16,13 +16,13 @@ class UsernameChanger
end
def change(asynchronous: true, run_update_job: true)
if @actor && @old_username != @new_username
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
end
@user.username = @new_username
if @user.save
if @actor && @old_username != @new_username
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
end
UsernameChanger.update_username(user_id: @user.id,
old_username: @old_username,
new_username: @new_username,

View File

@ -56,6 +56,10 @@ describe UsernameChanger do
expect do
UsernameChanger.change(myself, "HanSolo", myself)
end.to change { UserHistory.count }.by(0) # make sure it does not log a dupe
expect do
UsernameChanger.change(myself, user.username, myself)
end.to change { UserHistory.count }.by(0) # does not log if the username already exists
end
end