2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-03 08:45:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-21 16:32:02 -04:00
|
|
|
describe API::DeployKeys do
|
2016-08-05 05:35:44 -04:00
|
|
|
let(:user) { create(:user) }
|
2019-10-16 20:07:27 -04:00
|
|
|
let(:maintainer) { create(:user) }
|
2016-08-05 05:35:44 -04:00
|
|
|
let(:admin) { create(:admin) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, creator_id: user.id) }
|
|
|
|
let(:project2) { create(:project, creator_id: user.id) }
|
2016-08-05 05:35:44 -04:00
|
|
|
let(:deploy_key) { create(:deploy_key, public: true) }
|
|
|
|
|
|
|
|
let!(:deploy_keys_project) do
|
|
|
|
create(:deploy_keys_project, project: project, deploy_key: deploy_key)
|
|
|
|
end
|
2016-08-03 08:45:32 -04:00
|
|
|
|
|
|
|
describe 'GET /deploy_keys' do
|
|
|
|
context 'when unauthenticated' do
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns authentication error' do
|
2016-08-03 08:45:32 -04:00
|
|
|
get api('/deploy_keys')
|
|
|
|
|
|
|
|
expect(response.status).to eq(401)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as non-admin user' do
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns a 403 error' do
|
2016-08-03 08:45:32 -04:00
|
|
|
get api('/deploy_keys', user)
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as admin' do
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns all deploy keys' do
|
2016-08-03 08:45:32 -04:00
|
|
|
get api('/deploy_keys', admin)
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2016-08-03 08:45:32 -04:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.first['id']).to eq(deploy_keys_project.deploy_key.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 07:09:10 -04:00
|
|
|
describe 'GET /projects/:id/deploy_keys' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
deploy_key
|
|
|
|
end
|
2016-08-04 07:09:10 -04:00
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns array of ssh keys' do
|
2016-08-04 07:09:10 -04:00
|
|
|
get api("/projects/#{project.id}/deploy_keys", admin)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2016-08-04 07:09:10 -04:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.first['title']).to eq(deploy_key.title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/deploy_keys/:key_id' do
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns a single key' do
|
2016-08-04 07:09:10 -04:00
|
|
|
get api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-08-04 07:09:10 -04:00
|
|
|
expect(json_response['title']).to eq(deploy_key.title)
|
|
|
|
end
|
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns 404 Not Found with invalid ID' do
|
2016-08-04 07:09:10 -04:00
|
|
|
get api("/projects/#{project.id}/deploy_keys/404", admin)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/deploy_keys' do
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'does not create an invalid ssh key' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/deploy_keys", admin), params: { title: 'invalid key' }
|
2016-08-04 07:09:10 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2017-01-23 11:06:57 -05:00
|
|
|
expect(json_response['error']).to eq('key is missing')
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'does not create a key without title' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/deploy_keys", admin), params: { key: 'some key' }
|
2016-08-04 07:09:10 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2017-01-23 11:06:57 -05:00
|
|
|
expect(json_response['error']).to eq('title is missing')
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'creates new ssh key' do
|
2016-08-04 07:09:10 -04:00
|
|
|
key_attrs = attributes_for :another_key
|
|
|
|
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/deploy_keys", admin), params: key_attrs
|
2017-03-31 08:54:38 -04:00
|
|
|
end.to change { project.deploy_keys.count }.by(1)
|
2018-03-28 12:53:52 -04:00
|
|
|
|
|
|
|
new_key = project.deploy_keys.last
|
|
|
|
expect(new_key.key).to eq(key_attrs[:key])
|
|
|
|
expect(new_key.user).to eq(admin)
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
2016-10-11 05:02:51 -04:00
|
|
|
|
|
|
|
it 'returns an existing ssh key when attempting to add a duplicate' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/deploy_keys", admin), params: { key: deploy_key.key, title: deploy_key.title }
|
2016-10-11 05:02:51 -04:00
|
|
|
end.not_to change { project.deploy_keys.count }
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2016-10-11 05:02:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'joins an existing ssh key to a new project' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project2.id}/deploy_keys", admin), params: { key: deploy_key.key, title: deploy_key.title }
|
2016-10-11 05:02:51 -04:00
|
|
|
end.to change { project2.deploy_keys.count }.by(1)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2016-10-11 05:02:51 -04:00
|
|
|
end
|
2017-04-05 02:22:28 -04:00
|
|
|
|
|
|
|
it 'accepts can_push parameter' do
|
2018-01-05 10:23:44 -05:00
|
|
|
key_attrs = attributes_for(:another_key).merge(can_push: true)
|
2017-04-05 02:22:28 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/projects/#{project.id}/deploy_keys", admin), params: key_attrs
|
2017-04-05 02:22:28 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-04-05 02:22:28 -04:00
|
|
|
expect(json_response['can_push']).to eq(true)
|
|
|
|
end
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
describe 'PUT /projects/:id/deploy_keys/:key_id' do
|
2019-10-16 20:07:27 -04:00
|
|
|
let(:extra_params) { {} }
|
|
|
|
|
|
|
|
subject do
|
|
|
|
put api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", api_user), params: extra_params
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'with non-admin' do
|
|
|
|
let(:api_user) { user }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
it 'does not update a public deploy key' do
|
|
|
|
expect { subject }.not_to change(deploy_key, :title)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'with admin' do
|
|
|
|
let(:api_user) { admin }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'public deploy key attached to project' do
|
|
|
|
let(:extra_params) { { title: 'new title', can_push: true } }
|
|
|
|
|
|
|
|
it 'updates the title of the deploy key' do
|
|
|
|
expect { subject }.to change { deploy_key.reload.title }.to 'new title'
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates can_push of deploy_keys_project' do
|
|
|
|
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'private deploy key' do
|
|
|
|
let(:deploy_key) { create(:another_deploy_key, public: false) }
|
|
|
|
let(:deploy_keys_project) do
|
|
|
|
create(:deploy_keys_project, project: project, deploy_key: deploy_key)
|
|
|
|
end
|
|
|
|
let(:extra_params) { { title: 'new title', can_push: true } }
|
|
|
|
|
|
|
|
it 'updates the title of the deploy key' do
|
|
|
|
expect { subject }.to change { deploy_key.reload.title }.to 'new title'
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates can_push of deploy_keys_project' do
|
|
|
|
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invalid title' do
|
|
|
|
let(:extra_params) { { title: '' } }
|
|
|
|
|
|
|
|
it 'does not update the title of the deploy key' do
|
|
|
|
expect { subject }.not_to change { deploy_key.reload.title }
|
|
|
|
expect(response).to have_gitlab_http_status(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'with admin as project maintainer' do
|
|
|
|
let(:api_user) { admin }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
before do
|
|
|
|
project.add_maintainer(admin)
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'public deploy key attached to project' do
|
|
|
|
let(:extra_params) { { title: 'new title', can_push: true } }
|
|
|
|
|
|
|
|
it 'updates the title of the deploy key' do
|
|
|
|
expect { subject }.to change { deploy_key.reload.title }.to 'new title'
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates can_push of deploy_keys_project' do
|
|
|
|
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'with maintainer' do
|
|
|
|
let(:api_user) { maintainer }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
before do
|
|
|
|
project.add_maintainer(maintainer)
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'public deploy key attached to project' do
|
|
|
|
let(:extra_params) { { title: 'new title', can_push: true } }
|
|
|
|
|
|
|
|
it 'does not update the title of the deploy key' do
|
|
|
|
expect { subject }.not_to change { deploy_key.reload.title }
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates can_push of deploy_keys_project' do
|
|
|
|
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 07:09:10 -04:00
|
|
|
describe 'DELETE /projects/:id/deploy_keys/:key_id' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
deploy_key
|
|
|
|
end
|
2016-08-04 07:09:10 -04:00
|
|
|
|
2018-05-04 06:40:37 -04:00
|
|
|
it 'removes existing key from project' do
|
2016-08-04 07:09:10 -04:00
|
|
|
expect do
|
|
|
|
delete api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
|
2017-02-20 13:18:12 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
2017-08-09 05:52:22 -04:00
|
|
|
end.to change { project.deploy_keys.count }.by(-1)
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
|
|
|
|
2018-05-04 06:40:37 -04:00
|
|
|
context 'when the deploy key is public' do
|
|
|
|
it 'does not delete the deploy key' do
|
|
|
|
expect do
|
|
|
|
delete api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(204)
|
|
|
|
end.not_to change { DeployKey.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the deploy key is not public' do
|
|
|
|
let!(:deploy_key) { create(:deploy_key, public: false) }
|
|
|
|
|
|
|
|
context 'when the deploy key is only used by this project' do
|
|
|
|
it 'deletes the deploy key' do
|
|
|
|
expect do
|
|
|
|
delete api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(204)
|
|
|
|
end.to change { DeployKey.count }.by(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the deploy key is used by other projects' do
|
|
|
|
before do
|
|
|
|
create(:deploy_keys_project, project: project2, deploy_key: deploy_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not delete the deploy key' do
|
|
|
|
expect do
|
|
|
|
delete api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(204)
|
|
|
|
end.not_to change { DeployKey.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns 404 Not Found with invalid ID' do
|
2016-08-04 07:09:10 -04:00
|
|
|
delete api("/projects/#{project.id}/deploy_keys/404", admin)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
2017-08-24 12:03:39 -04:00
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin) }
|
|
|
|
end
|
2016-08-04 07:09:10 -04:00
|
|
|
end
|
|
|
|
|
2016-08-03 08:45:32 -04:00
|
|
|
describe 'POST /projects/:id/deploy_keys/:key_id/enable' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project2) { create(:project) }
|
2016-08-03 08:45:32 -04:00
|
|
|
|
|
|
|
context 'when the user can admin the project' do
|
|
|
|
it 'enables the key' do
|
|
|
|
expect do
|
|
|
|
post api("/projects/#{project2.id}/deploy_keys/#{deploy_key.id}/enable", admin)
|
|
|
|
end.to change { project2.deploy_keys.count }.from(0).to(1)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2016-08-03 08:45:32 -04:00
|
|
|
expect(json_response['id']).to eq(deploy_key.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as non-admin user' do
|
2017-03-31 08:54:38 -04:00
|
|
|
it 'returns a 404 error' do
|
2016-08-03 08:45:32 -04:00
|
|
|
post api("/projects/#{project2.id}/deploy_keys/#{deploy_key.id}/enable", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-08-03 08:45:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|