From bd578d96b47c9fadd91fa95a7913bf65f3623128 Mon Sep 17 00:00:00 2001 From: Michael Kozono Date: Fri, 18 Aug 2017 10:31:59 -0700 Subject: [PATCH] Add namespace errors from User#after_update --- app/models/user.rb | 7 ++++++- spec/models/user_spec.rb | 7 ++++++- spec/services/users/update_service_spec.rb | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 579ab898784..fbd08bc4d0a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -837,7 +837,12 @@ class User < ActiveRecord::Base create_namespace!(path: username, name: username) unless namespace if username_changed? - namespace.update_attributes!(path: username, name: username) + unless namespace.update_attributes(path: username, name: username) + namespace.errors.each do |attribute, message| + self.errors.add(:"namespace_#{attribute}", message) + end + raise ActiveRecord::RecordInvalid.new(namespace) + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c4bf9ebf25e..9a9e255f874 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2063,10 +2063,15 @@ describe User do context 'when there is a validation error (namespace name taken) while updating namespace' do let!(:conflicting_namespace) { create(:group, name: new_username, path: 'quz') } - it "causes the user save to fail" do + it 'causes the user save to fail' do expect(user.update_attributes(username: new_username)).to be_falsey expect(user.namespace.errors.messages[:name].first).to eq('has already been taken') end + + it 'adds the namespace errors to the user' do + user.update_attributes(username: new_username) + expect(user.errors.full_messages.first).to eq('Namespace name has already been taken') + end end end diff --git a/spec/services/users/update_service_spec.rb b/spec/services/users/update_service_spec.rb index 343804e3de0..985f6d94876 100644 --- a/spec/services/users/update_service_spec.rb +++ b/spec/services/users/update_service_spec.rb @@ -12,9 +12,22 @@ describe Users::UpdateService do end it 'returns an error result when record cannot be updated' do + result = {} expect do - update_user(user, { email: 'invalid' }) + result = update_user(user, { email: 'invalid' }) end.not_to change { user.reload.email } + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('Email is invalid') + end + + it 'includes namespace error messages' do + create(:group, name: 'taken', path: 'something_else') + result = {} + expect do + result = update_user(user, { username: 'taken' }) + end.not_to change { user.reload.username } + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('Namespace name has already been taken') end def update_user(user, opts)