2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-29 05:52:21 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Keys do
|
2015-08-29 05:52:21 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:admin) { create(:admin) }
|
2020-03-13 05:09:23 -04:00
|
|
|
let(:key) { create(:key, user: user, expires_at: 1.day.from_now) }
|
2017-06-14 14:18:56 -04:00
|
|
|
let(:email) { create(:email, user: user) }
|
2015-08-29 05:52:21 -04:00
|
|
|
|
|
|
|
describe 'GET /keys/:uid' do
|
|
|
|
context 'when unauthenticated' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns authentication error' do
|
2015-08-29 05:52:21 -04:00
|
|
|
get api("/keys/#{key.id}")
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2015-08-29 05:52:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns 404 for non-existing key' do
|
2019-02-26 18:18:40 -05:00
|
|
|
get api('/keys/0', admin)
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2015-08-29 05:52:21 -04:00
|
|
|
expect(json_response['message']).to eq('404 Not found')
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns single ssh key with user information' do
|
2015-08-29 05:52:21 -04:00
|
|
|
user.keys << key
|
|
|
|
get api("/keys/#{key.id}", admin)
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2015-08-29 05:52:21 -04:00
|
|
|
expect(json_response['title']).to eq(key.title)
|
2020-03-13 05:09:23 -04:00
|
|
|
expect(Time.parse(json_response['expires_at'])).to be_like_time(key.expires_at)
|
2015-08-29 05:52:21 -04:00
|
|
|
expect(json_response['user']['id']).to eq(user.id)
|
|
|
|
expect(json_response['user']['username']).to eq(user.username)
|
|
|
|
end
|
2017-04-21 05:47:58 -04:00
|
|
|
|
|
|
|
it "does not include the user's `is_admin` flag" do
|
|
|
|
get api("/keys/#{key.id}", admin)
|
|
|
|
|
|
|
|
expect(json_response['user']['is_admin']).to be_nil
|
|
|
|
end
|
2015-08-29 05:52:21 -04:00
|
|
|
end
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
|
|
|
describe 'GET /keys?fingerprint=' do
|
|
|
|
it 'returns authentication error' do
|
|
|
|
get api("/keys?fingerprint=#{key.fingerprint}")
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns authentication error when authenticated as user' do
|
|
|
|
get api("/keys?fingerprint=#{key.fingerprint}", user)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when authenticated as admin' do
|
|
|
|
it 'returns 404 for non-existing SSH md5 fingerprint' do
|
|
|
|
get api("/keys?fingerprint=11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11", admin)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-12-11 13:08:10 -05:00
|
|
|
expect(json_response['message']).to eq('404 Key Not Found')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 for non-existing SSH sha256 fingerprint' do
|
|
|
|
get api("/keys?fingerprint=#{URI.encode_www_form_component("SHA256:nUhzNyftwADy8AH3wFY31tAKs7HufskYTte2aXo1lCg")}", admin)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-12-11 13:08:10 -05:00
|
|
|
expect(json_response['message']).to eq('404 Key Not Found')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns user if SSH md5 fingerprint found' do
|
|
|
|
user.keys << key
|
|
|
|
|
|
|
|
get api("/keys?fingerprint=#{key.fingerprint}", admin)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-11 13:08:10 -05:00
|
|
|
expect(json_response['title']).to eq(key.title)
|
|
|
|
expect(json_response['user']['id']).to eq(user.id)
|
|
|
|
expect(json_response['user']['username']).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns user if SSH sha256 fingerprint found' do
|
|
|
|
user.keys << key
|
|
|
|
|
|
|
|
get api("/keys?fingerprint=#{URI.encode_www_form_component("SHA256:" + key.fingerprint_sha256)}", admin)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-11 13:08:10 -05:00
|
|
|
expect(json_response['title']).to eq(key.title)
|
|
|
|
expect(json_response['user']['id']).to eq(user.id)
|
|
|
|
expect(json_response['user']['username']).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns user if SSH sha256 fingerprint found' do
|
|
|
|
user.keys << key
|
|
|
|
|
|
|
|
get api("/keys?fingerprint=#{URI.encode_www_form_component("sha256:" + key.fingerprint_sha256)}", admin)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-12-11 13:08:10 -05:00
|
|
|
expect(json_response['title']).to eq(key.title)
|
|
|
|
expect(json_response['user']['id']).to eq(user.id)
|
|
|
|
expect(json_response['user']['username']).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include the user's `is_admin` flag" do
|
|
|
|
get api("/keys?fingerprint=#{key.fingerprint}", admin)
|
|
|
|
|
|
|
|
expect(json_response['user']['is_admin']).to be_nil
|
|
|
|
end
|
2020-01-13 19:08:14 -05:00
|
|
|
|
|
|
|
context 'when searching a DeployKey' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:project_push) { create(:project, :repository) }
|
|
|
|
let(:deploy_key) { create(:deploy_key) }
|
|
|
|
|
|
|
|
let!(:deploy_keys_project) do
|
|
|
|
create(:deploy_keys_project, project: project, deploy_key: deploy_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:deploy_keys_project_push) do
|
|
|
|
create(:deploy_keys_project, project: project_push, deploy_key: deploy_key, can_push: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns user and projects if SSH sha256 fingerprint for DeployKey found' do
|
|
|
|
user.keys << deploy_key
|
|
|
|
|
|
|
|
get api("/keys?fingerprint=#{URI.encode_www_form_component("SHA256:" + deploy_key.fingerprint_sha256)}", admin)
|
|
|
|
|
2020-02-25 07:08:48 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-13 19:08:14 -05:00
|
|
|
expect(json_response['title']).to eq(deploy_key.title)
|
|
|
|
expect(json_response['user']['id']).to eq(user.id)
|
|
|
|
|
|
|
|
expect(json_response['deploy_keys_projects'].count).to eq(2)
|
|
|
|
expect(json_response['deploy_keys_projects'][0]['project_id']).to eq(deploy_keys_project.project.id)
|
|
|
|
expect(json_response['deploy_keys_projects'][0]['can_push']).to eq(deploy_keys_project.can_push)
|
|
|
|
expect(json_response['deploy_keys_projects'][1]['project_id']).to eq(deploy_keys_project_push.project.id)
|
|
|
|
expect(json_response['deploy_keys_projects'][1]['can_push']).to eq(deploy_keys_project_push.can_push)
|
|
|
|
end
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
end
|
2015-08-29 05:52:21 -04:00
|
|
|
end
|