Skip to content

Commit

Permalink
[webui] Catch and handle validation errors for UserController#save
Browse files Browse the repository at this point in the history
  • Loading branch information
bgeuken committed May 30, 2016
1 parent e895d33 commit a7b6bf9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/api/app/controllers/webui/user_controller.rb
Expand Up @@ -106,9 +106,14 @@ def save
@displayed_user.state = User::STATES[params[:state]] if params[:state]
@displayed_user.update_globalroles([params[:globalrole]]) if params[:globalrole]
end
@displayed_user.save!

flash[:success] = "User data for user '#{@displayed_user.login}' successfully updated."
begin
@displayed_user.save!
flash[:success] = "User data for user '#{@displayed_user.login}' successfully updated."
rescue ActiveRecord::RecordInvalid => e
flash[:error] = "Couldn't update user: #{e.message}."
end

redirect_back_or_to :action => 'show', user: @displayed_user
end

Expand Down
30 changes: 23 additions & 7 deletions src/api/spec/controllers/webui/user_controller_spec.rb
Expand Up @@ -104,15 +104,31 @@

describe "POST #save" do
context "when user is updating its own profile" do
before do
login user
post :save, {user: user, realname: 'another real name', email: 'new_valid@email.es' }
user.reload
context "with valid data" do
before do
login user
post :save, { user: user, realname: 'another real name', email: 'new_valid@email.es' }
user.reload
end

it { expect(flash[:success]).to eq("User data for user '#{user.login}' successfully updated.") }
it { expect(user.realname).to eq('another real name') }
it { expect(user.email).to eq('new_valid@email.es') }
it { is_expected.to redirect_to user_show_path(user) }
end

it { expect(user.realname).to eq('another real name') }
it { expect(user.email).to eq('new_valid@email.es') }
it { is_expected.to redirect_to user_show_path(user) }
context "with invalid data" do
before do
login user
post :save, { user: user, realname: "another real name", email: "" }
user.reload
end

it { expect(flash[:error]).to eq("Couldn't update user: Validation failed: Email must be given, Email must be a valid email address..") }
it { expect(user.realname).to eq(user.realname) }
it { expect(user.email).to eq(user.email) }
it { is_expected.to redirect_to user_show_path(user) }
end
end

context "when user is trying to update another user's profile" do
Expand Down

0 comments on commit a7b6bf9

Please sign in to comment.