diff options
author | Mark Fletcher <mark@gitlab.com> | 2017-05-01 13:22:11 +0800 |
---|---|---|
committer | Mark Fletcher <mark@gitlab.com> | 2017-05-03 17:37:08 +0800 |
commit | d02e7226c47db88549f54d0fed0e2b33a726908d (patch) | |
tree | 1145e47e2eadab18b9c082ab00b4dfad4db25853 | |
parent | 03f13af588cf2b578a27717db492c216fcafbf9a (diff) | |
download | gitlab-ce-d02e7226c47db88549f54d0fed0e2b33a726908d.tar.gz |
Detect already enabled DeployKeys in EnableDeployKeyService
Ensures deploy keys can't be re-added, which causes a validation error
3 files changed, 18 insertions, 1 deletions
diff --git a/app/services/projects/enable_deploy_key_service.rb b/app/services/projects/enable_deploy_key_service.rb index 3cf4264ce9b..121385afca3 100644 --- a/app/services/projects/enable_deploy_key_service.rb +++ b/app/services/projects/enable_deploy_key_service.rb @@ -4,7 +4,10 @@ module Projects key = accessible_keys.find_by(id: params[:key_id] || params[:id]) return unless key - project.deploy_keys << key + unless project.deploy_keys.include?(key) + project.deploy_keys << key + end + key end diff --git a/changelogs/unreleased/29673-500-internal-server-error-when-enabling-a-deploy-key-more-than-once-through-api.yml b/changelogs/unreleased/29673-500-internal-server-error-when-enabling-a-deploy-key-more-than-once-through-api.yml new file mode 100644 index 00000000000..3e62ede1521 --- /dev/null +++ b/changelogs/unreleased/29673-500-internal-server-error-when-enabling-a-deploy-key-more-than-once-through-api.yml @@ -0,0 +1,4 @@ +--- +title: Detect already enabled DeployKeys in EnableDeployKeyService +merge_request: +author: diff --git a/spec/services/projects/enable_deploy_key_service_spec.rb b/spec/services/projects/enable_deploy_key_service_spec.rb index a37510cf159..78626fbad4b 100644 --- a/spec/services/projects/enable_deploy_key_service_spec.rb +++ b/spec/services/projects/enable_deploy_key_service_spec.rb @@ -21,6 +21,16 @@ describe Projects::EnableDeployKeyService, services: true do end end + context 'add the same key twice' do + before do + project.deploy_keys << deploy_key + end + + it 'returns existing key' do + expect(service.execute).to eq(deploy_key) + end + end + def service Projects::EnableDeployKeyService.new(project, user, params) end |