summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2017-08-17 13:18:02 -0700
committerMichael Kozono <mkozono@gmail.com>2017-08-17 13:18:02 -0700
commit82bafd002d40b5fa889f07d38f8236928e7b1f54 (patch)
treea5b9dac6da0e1c129a404542ae81bc44f7502bb4
parentf0ac0daf6a8788f57b98eaf29b0056899027ae68 (diff)
downloadgitlab-ce-82bafd002d40b5fa889f07d38f8236928e7b1f54.tar.gz
Make username update fail if namespace part fails
-rw-r--r--app/models/user.rb2
-rw-r--r--spec/models/user_spec.rb56
2 files changed, 57 insertions, 1 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 02c3ab6654b..579ab898784 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -837,7 +837,7 @@ class User < ActiveRecord::Base
create_namespace!(path: username, name: username) unless namespace
if username_changed?
- namespace.update_attributes(path: username, name: username)
+ namespace.update_attributes!(path: username, name: username)
end
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 97bb91a6ac8..c4bf9ebf25e 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -2024,4 +2024,60 @@ describe User do
expect(user.projects_limit_left).to eq(5)
end
end
+
+ describe '#ensure_namespace_correct' do
+ context 'for a new user' do
+ let(:user) { build(:user) }
+
+ it 'creates the namespace' do
+ expect(user.namespace).to be_nil
+ user.save!
+ expect(user.namespace).not_to be_nil
+ end
+ end
+
+ context 'for an existing user' do
+ let(:username) { 'foo' }
+ let(:user) { create(:user, username: username) }
+
+ context 'when the user is updated' do
+ context 'when the username is changed' do
+ let(:new_username) { 'bar' }
+
+ it 'changes the namespace (just to compare to when username is not changed)' do
+ expect do
+ user.update_attributes!(username: new_username)
+ end.to change { user.namespace.updated_at }
+ end
+
+ it 'updates the namespace name' do
+ user.update_attributes!(username: new_username)
+ expect(user.namespace.name).to eq(new_username)
+ end
+
+ it 'updates the namespace path' do
+ user.update_attributes!(username: new_username)
+ expect(user.namespace.path).to eq(new_username)
+ end
+
+ 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
+ expect(user.update_attributes(username: new_username)).to be_falsey
+ expect(user.namespace.errors.messages[:name].first).to eq('has already been taken')
+ end
+ end
+ end
+
+ context 'when the username is not changed' do
+ it 'does not change the namespace' do
+ expect do
+ user.update_attributes!(email: 'asdf@asdf.com')
+ end.not_to change { user.namespace.updated_at }
+ end
+ end
+ end
+ end
+ end
end