Skip to content
Snippets Groups Projects
Commit 6ee50a75 authored by William George's avatar William George
Browse files

Fix return correct 409 conflit error code

When registering or updating a user with a duplicate, albeit differently cased
username a 400 error would fire, as opposed to the preferred 409
parent c15e05bb
No related branches found
No related tags found
No related merge requests found
Pipeline #30163270 failed
......@@ -164,11 +164,11 @@ module API
present user, with: Entities::UserPublic, current_user: current_user
else
conflict!('Email has already been taken') if User
.where(email: user.email)
.iwhere(email: user.email)
.count > 0
conflict!('Username has already been taken') if User
.where(username: user.username)
.iwhere(username: user.username)
.count > 0
render_validation_error!(user)
......@@ -196,11 +196,11 @@ module API
not_found!('User') unless user
conflict!('Email has already been taken') if params[:email] &&
User.where(email: params[:email])
User.iwhere(email: params[:email])
.where.not(id: user.id).count > 0
conflict!('Username has already been taken') if params[:username] &&
User.where(username: params[:username])
User.iwhere(username: params[:username])
.where.not(id: user.id).count > 0
user_params = declared_params(include_missing: false)
......
......@@ -551,6 +551,18 @@ describe API::Users do
expect(json_response['message']).to eq('Username has already been taken')
end
it 'returns 409 conflict error if same username exists (case insensitive)' do
expect do
post api('/users', admin),
name: 'foo',
email: 'foo@example.com',
password: 'password',
username: 'TEST'
end.to change { User.count }.by(0)
expect(response).to have_gitlab_http_status(409)
expect(json_response['message']).to eq('Username has already been taken')
end
it 'creates user with new identity' do
post api("/users", admin), attributes_for(:user, provider: 'github', extern_uid: '67890')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment