diff options
author | Rémy Coutable <remy@rymai.me> | 2018-07-30 18:46:22 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2018-07-30 18:46:22 +0000 |
commit | 016dadc860251ade20860c2ca533a43dff789018 (patch) | |
tree | 11d9b2065cf09f25ba52f27812d560560997c58e | |
parent | 0a40035c0762be80330a0155dc790d3fde90da68 (diff) | |
parent | 5bab78950d652ce15d87772ae2857f370fd926a4 (diff) | |
download | gitlab-ce-016dadc860251ade20860c2ca533a43dff789018.tar.gz |
Merge branch '32821-better-error-message-add-invalid-user-to-project' into 'master'
Resolve "Improve Error Message When Adding Invalid User to Project via API"
Closes #32821
See merge request gitlab-org/gitlab-ce!20885
-rw-r--r-- | changelogs/unreleased/32821-better-error-message-add-invalid-user-to-project.yml | 5 | ||||
-rw-r--r-- | lib/api/members.rb | 5 | ||||
-rw-r--r-- | spec/requests/api/members_spec.rb | 8 |
3 files changed, 17 insertions, 1 deletions
diff --git a/changelogs/unreleased/32821-better-error-message-add-invalid-user-to-project.yml b/changelogs/unreleased/32821-better-error-message-add-invalid-user-to-project.yml new file mode 100644 index 00000000000..587d7209c2f --- /dev/null +++ b/changelogs/unreleased/32821-better-error-message-add-invalid-user-to-project.yml @@ -0,0 +1,5 @@ +--- +title: Improve error message when adding invalid user to a project +merge_request: 20885 +author: Jacopo Beschi @jacopo-beschi +type: added diff --git a/lib/api/members.rb b/lib/api/members.rb index 3d2220fed96..d23dd834c69 100644 --- a/lib/api/members.rb +++ b/lib/api/members.rb @@ -75,7 +75,10 @@ module API member = source.members.find_by(user_id: params[:user_id]) conflict!('Member already exists') if member - member = source.add_user(params[:user_id], params[:access_level], current_user: current_user, expires_at: params[:expires_at]) + user = User.find_by_id(params[:user_id]) + not_found!('User') unless user + + member = source.add_user(user, params[:access_level], current_user: current_user, expires_at: params[:expires_at]) if !member not_allowed! # This currently can only be reached in EE diff --git a/spec/requests/api/members_spec.rb b/spec/requests/api/members_spec.rb index c621760b6c4..93e1c3a2294 100644 --- a/spec/requests/api/members_spec.rb +++ b/spec/requests/api/members_spec.rb @@ -231,6 +231,14 @@ describe API::Members do expect(response).to have_gitlab_http_status(409) end + it 'returns 404 when the user_id is not valid' do + post api("/#{source_type.pluralize}/#{source.id}/members", maintainer), + user_id: 0, access_level: Member::MAINTAINER + + expect(response).to have_gitlab_http_status(404) + expect(json_response['message']).to eq('404 User Not Found') + end + it 'returns 400 when user_id is not given' do post api("/#{source_type.pluralize}/#{source.id}/members", maintainer), access_level: Member::MAINTAINER |