debian-mirror-gitlab/spec/requests/api/deploy_keys_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

381 lines
13 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe API::DeployKeys do
2021-04-29 21:17:54 +05:30
let_it_be(:user) { create(:user) }
let_it_be(:maintainer) { create(:user) }
let_it_be(:admin) { create(:admin) }
let_it_be(:project) { create(:project, creator_id: user.id) }
let_it_be(:project2) { create(:project, creator_id: user.id) }
2021-12-11 22:18:48 +05:30
let_it_be(:project3) { create(:project, creator_id: user.id) }
let_it_be(:deploy_key) { create(:deploy_key, public: true) }
let_it_be(:deploy_key_private) { create(:deploy_key, public: false) }
2016-09-13 17:45:13 +05:30
let!(:deploy_keys_project) do
create(:deploy_keys_project, project: project, deploy_key: deploy_key)
end
describe 'GET /deploy_keys' do
context 'when unauthenticated' do
2017-09-10 17:25:29 +05:30
it 'returns authentication error' do
2016-09-13 17:45:13 +05:30
get api('/deploy_keys')
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2016-09-13 17:45:13 +05:30
end
end
context 'when authenticated as non-admin user' do
2017-09-10 17:25:29 +05:30
it 'returns a 403 error' do
2016-09-13 17:45:13 +05:30
get api('/deploy_keys', user)
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2016-09-13 17:45:13 +05:30
end
end
context 'when authenticated as admin' do
2021-12-11 22:18:48 +05:30
let_it_be(:pat) { create(:personal_access_token, user: admin) }
def make_api_request(params = {})
get api('/deploy_keys', personal_access_token: pat), params: params
end
2017-09-10 17:25:29 +05:30
it 'returns all deploy keys' do
2021-12-11 22:18:48 +05:30
make_api_request
2016-09-13 17:45:13 +05:30
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2021-12-11 22:18:48 +05:30
expect(response).to match_response_schema('public_api/v4/deploy_keys')
2016-09-13 17:45:13 +05:30
expect(json_response).to be_an Array
2021-12-11 22:18:48 +05:30
expect(json_response[0]['id']).to eq(deploy_key.id)
expect(json_response[1]['id']).to eq(deploy_key_private.id)
end
it 'avoids N+1 database queries', :use_sql_query_cache, :request_store do
create(:deploy_keys_project, :write_access, project: project2, deploy_key: deploy_key)
control = ActiveRecord::QueryRecorder.new(skip_cached: false) { make_api_request }
deploy_key2 = create(:deploy_key, public: true)
create(:deploy_keys_project, :write_access, project: project3, deploy_key: deploy_key2)
expect { make_api_request }.not_to exceed_all_query_limit(control)
end
context 'when `public` parameter is `true`' do
it 'only returns public deploy keys' do
make_api_request({ public: true })
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(deploy_key.id)
end
end
context 'projects_with_write_access' do
let!(:deploy_keys_project2) { create(:deploy_keys_project, :write_access, project: project2, deploy_key: deploy_key) }
let!(:deploy_keys_project3) { create(:deploy_keys_project, :write_access, project: project3, deploy_key: deploy_key) }
it 'returns projects with write access' do
make_api_request
response_projects_with_write_access = json_response.first['projects_with_write_access']
expect(response_projects_with_write_access[0]['id']).to eq(project2.id)
expect(response_projects_with_write_access[1]['id']).to eq(project3.id)
end
2016-09-13 17:45:13 +05:30
end
end
end
describe 'GET /projects/:id/deploy_keys' do
2021-04-29 21:17:54 +05:30
let(:deploy_key) { create(:deploy_key, public: true, user: admin) }
def perform_request
get api("/projects/#{project.id}/deploy_keys", admin)
2017-09-10 17:25:29 +05:30
end
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
it 'returns array of ssh keys' do
2021-04-29 21:17:54 +05:30
perform_request
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2016-09-13 17:45:13 +05:30
expect(json_response).to be_an Array
expect(json_response.first['title']).to eq(deploy_key.title)
2021-12-11 22:18:48 +05:30
expect(json_response.first).not_to have_key(:projects_with_write_access)
2016-09-13 17:45:13 +05:30
end
2021-04-29 21:17:54 +05:30
it 'returns multiple deploy keys without N + 1' do
perform_request
control_count = ActiveRecord::QueryRecorder.new { perform_request }.count
create(:deploy_key, public: true, projects: [project], user: maintainer)
expect { perform_request }.not_to exceed_query_limit(control_count)
end
2016-09-13 17:45:13 +05:30
end
describe 'GET /projects/:id/deploy_keys/:key_id' do
2017-09-10 17:25:29 +05:30
it 'returns a single key' do
2016-09-13 17:45:13 +05:30
get api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-09-13 17:45:13 +05:30
expect(json_response['title']).to eq(deploy_key.title)
2021-12-11 22:18:48 +05:30
expect(json_response).not_to have_key(:projects_with_write_access)
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
it 'returns 404 Not Found with invalid ID' do
2016-09-13 17:45:13 +05:30
get api("/projects/#{project.id}/deploy_keys/404", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
end
end
describe 'POST /projects/:id/deploy_keys' do
2017-09-10 17:25:29 +05:30
it 'does not create an invalid ssh key' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/deploy_keys", admin), params: { title: 'invalid key' }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-17 22:00:37 +05:30
expect(json_response['error']).to eq('key is missing')
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
it 'does not create a key without title' do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/deploy_keys", admin), params: { key: 'some key' }
2016-09-13 17:45:13 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-17 22:00:37 +05:30
expect(json_response['error']).to eq('title is missing')
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
it 'creates new ssh key' do
2016-09-13 17:45:13 +05:30
key_attrs = attributes_for :another_key
expect do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/deploy_keys", admin), params: key_attrs
2017-09-10 17:25:29 +05:30
end.to change { project.deploy_keys.count }.by(1)
2018-05-09 12:01:36 +05:30
new_key = project.deploy_keys.last
expect(new_key.key).to eq(key_attrs[:key])
expect(new_key.user).to eq(admin)
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns an existing ssh key when attempting to add a duplicate' do
expect do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/deploy_keys", admin), params: { key: deploy_key.key, title: deploy_key.title }
2017-08-17 22:00:37 +05:30
end.not_to change { project.deploy_keys.count }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2017-08-17 22:00:37 +05:30
end
it 'joins an existing ssh key to a new project' do
expect do
2019-02-15 15:39:39 +05:30
post api("/projects/#{project2.id}/deploy_keys", admin), params: { key: deploy_key.key, title: deploy_key.title }
2017-08-17 22:00:37 +05:30
end.to change { project2.deploy_keys.count }.by(1)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2017-08-17 22:00:37 +05:30
end
it 'accepts can_push parameter' do
2018-03-17 18:26:18 +05:30
key_attrs = attributes_for(:another_key).merge(can_push: true)
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
post api("/projects/#{project.id}/deploy_keys", admin), params: key_attrs
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2017-08-17 22:00:37 +05:30
expect(json_response['can_push']).to eq(true)
end
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
describe 'PUT /projects/:id/deploy_keys/:key_id' do
2019-12-21 20:55:43 +05:30
let(:extra_params) { {} }
subject do
put api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", api_user), params: extra_params
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
context 'with non-admin' do
let(:api_user) { user }
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
it 'does not update a public deploy key' do
expect { subject }.not_to change(deploy_key, :title)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-12-21 20:55:43 +05:30
end
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
context 'with admin' do
let(:api_user) { admin }
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
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'
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
it 'updates can_push of deploy_keys_project' do
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
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
2020-10-24 23:57:45 +05:30
2019-12-21 20:55:43 +05:30
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'
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
it 'updates can_push of deploy_keys_project' do
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
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 }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2019-12-21 20:55:43 +05:30
end
end
end
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
context 'with admin as project maintainer' do
let(:api_user) { admin }
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
before do
project.add_maintainer(admin)
end
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
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'
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
it 'updates can_push of deploy_keys_project' do
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
end
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
context 'with maintainer' do
let(:api_user) { maintainer }
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
before do
project.add_maintainer(maintainer)
end
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
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 }
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
it 'updates can_push of deploy_keys_project' do
expect { subject }.to change { deploy_keys_project.reload.can_push }.from(false).to(true)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-21 20:55:43 +05:30
end
end
2017-09-10 17:25:29 +05:30
end
end
2016-09-13 17:45:13 +05:30
describe 'DELETE /projects/:id/deploy_keys/:key_id' do
2017-09-10 17:25:29 +05:30
before do
deploy_key
end
2016-09-13 17:45:13 +05:30
2018-06-03 19:52:53 +05:30
it 'removes existing key from project' do
2016-09-13 17:45:13 +05:30
expect do
delete api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2018-03-17 18:26:18 +05:30
end.to change { project.deploy_keys.count }.by(-1)
2016-09-13 17:45:13 +05:30
end
2018-06-03 19:52:53 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2018-06-03 19:52:53 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2018-06-03 19:52:53 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2018-06-03 19:52:53 +05:30
end.not_to change { DeployKey.count }
end
end
end
2017-09-10 17:25:29 +05:30
it 'returns 404 Not Found with invalid ID' do
2016-09-13 17:45:13 +05:30
delete api("/projects/#{project.id}/deploy_keys/404", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
it_behaves_like '412 response' do
let(:request) { api("/projects/#{project.id}/deploy_keys/#{deploy_key.id}", admin) }
2016-09-13 17:45:13 +05:30
end
end
describe 'POST /projects/:id/deploy_keys/:key_id/enable' do
2017-09-10 17:25:29 +05:30
let(:project2) { create(:project) }
2016-09-13 17:45:13 +05:30
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)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2016-09-13 17:45:13 +05:30
expect(json_response['id']).to eq(deploy_key.id)
end
end
context 'when authenticated as non-admin user' do
2017-09-10 17:25:29 +05:30
it 'returns a 404 error' do
2016-09-13 17:45:13 +05:30
post api("/projects/#{project2.id}/deploy_keys/#{deploy_key.id}/enable", user)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2016-09-13 17:45:13 +05:30
end
end
end
end