Commit 01fa19ed authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'grapify-deploy-keys' into 'master'

Grapify all endpoints of the deploy keys API

See merge request !8721
parents fe0dbf61 18417c9e
...@@ -38,26 +38,25 @@ module API ...@@ -38,26 +38,25 @@ module API
present key, with: Entities::SSHKey present key, with: Entities::SSHKey
end end
# TODO: for 9.0 we should check if params are there with the params block
# grape provides, at this point we'd change behaviour so we can't
# Behaviour now if you don't provide all required params: it renders a
# validation error or two.
desc 'Add new deploy key to currently authenticated user' do desc 'Add new deploy key to currently authenticated user' do
success Entities::SSHKey success Entities::SSHKey
end end
params do
requires :key, type: String, desc: 'The new deploy key'
requires :title, type: String, desc: 'The name of the deploy key'
end
post ":id/#{path}" do post ":id/#{path}" do
attrs = attributes_for_keys [:title, :key] params[:key].strip!
attrs[:key].strip! if attrs[:key]
# Check for an existing key joined to this project # Check for an existing key joined to this project
key = user_project.deploy_keys.find_by(key: attrs[:key]) key = user_project.deploy_keys.find_by(key: params[:key])
if key if key
present key, with: Entities::SSHKey present key, with: Entities::SSHKey
break break
end end
# Check for available deploy keys in other projects # Check for available deploy keys in other projects
key = current_user.accessible_deploy_keys.find_by(key: attrs[:key]) key = current_user.accessible_deploy_keys.find_by(key: params[:key])
if key if key
user_project.deploy_keys << key user_project.deploy_keys << key
present key, with: Entities::SSHKey present key, with: Entities::SSHKey
...@@ -65,7 +64,7 @@ module API ...@@ -65,7 +64,7 @@ module API
end end
# Create a new deploy key # Create a new deploy key
key = DeployKey.new attrs key = DeployKey.new(declared_params(include_missing: false))
if key.valid? && user_project.deploy_keys << key if key.valid? && user_project.deploy_keys << key
present key, with: Entities::SSHKey present key, with: Entities::SSHKey
else else
......
...@@ -73,19 +73,14 @@ describe API::DeployKeys, api: true do ...@@ -73,19 +73,14 @@ describe API::DeployKeys, api: true do
post api("/projects/#{project.id}/deploy_keys", admin), { title: 'invalid key' } post api("/projects/#{project.id}/deploy_keys", admin), { title: 'invalid key' }
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']['key']).to eq([ expect(json_response['error']).to eq('key is missing')
'can\'t be blank',
'is invalid'
])
end end
it 'should not create a key without title' do it 'should not create a key without title' do
post api("/projects/#{project.id}/deploy_keys", admin), key: 'some key' post api("/projects/#{project.id}/deploy_keys", admin), key: 'some key'
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']['title']).to eq([ expect(json_response['error']).to eq('title is missing')
'can\'t be blank'
])
end end
it 'should create new ssh key' do it 'should create new ssh key' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment