debian-mirror-gitlab/spec/requests/jwt_controller_spec.rb

224 lines
7.7 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
describe JwtController do
let(:service) { double(execute: {}) }
let(:service_class) { double(new: service) }
let(:service_name) { 'test' }
let(:parameters) { { service: service_name } }
2020-05-24 23:13:21 +05:30
let(:log_output) { StringIO.new }
let(:logger) do
Logger.new(log_output).tap { |logger| logger.formatter = ->(_, _, _, msg) { msg } }
end
let(:log_data) { Gitlab::Json.parse(log_output.string) }
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
before do
2020-05-24 23:13:21 +05:30
Lograge.logger = logger
2017-09-10 17:25:29 +05:30
stub_const('JwtController::SERVICES', service_name => service_class)
end
2016-06-02 11:05:42 +05:30
2020-05-24 23:13:21 +05:30
shared_examples 'user logging' do
it 'logs username and ID' do
expect(log_data['username']).to eq(user.username)
expect(log_data['user_id']).to eq(user.id)
expect(log_data['meta.user']).to eq(user.username)
end
end
2016-06-02 11:05:42 +05:30
context 'existing service' do
2019-02-15 15:39:39 +05:30
subject! { get '/jwt/auth', params: parameters }
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
it { expect(response).to have_gitlab_http_status(:ok) }
2016-06-02 11:05:42 +05:30
context 'returning custom http code' do
let(:service) { double(execute: { http_status: 505 }) }
2020-03-13 15:44:24 +05:30
it { expect(response).to have_gitlab_http_status(:http_version_not_supported) }
2016-06-02 11:05:42 +05:30
end
end
2016-11-24 13:41:30 +05:30
context 'when using authenticated request' do
2020-03-28 13:19:24 +05:30
shared_examples 'rejecting a blocked user' do
context 'with blocked user' do
let(:user) { create(:user, :blocked) }
it 'rejects the request as unauthorized' do
expect(response).to have_gitlab_http_status(:unauthorized)
expect(response.body).to include('HTTP Basic: Access denied')
end
end
end
2016-06-02 11:05:42 +05:30
context 'using CI token' do
2020-05-24 23:13:21 +05:30
let(:user) { create(:user) }
let(:build) { create(:ci_build, :running, user: user) }
2016-09-29 09:46:39 +05:30
let(:project) { build.project }
let(:headers) { { authorization: credentials('gitlab-ci-token', build.token) } }
2016-06-02 11:05:42 +05:30
context 'project with enabled CI' do
2019-02-15 15:39:39 +05:30
subject! { get '/jwt/auth', params: parameters, headers: headers }
2016-06-02 11:05:42 +05:30
2020-05-24 23:13:21 +05:30
it { expect(service_class).to have_received(:new).with(project, user, ActionController::Parameters.new(parameters).permit!) }
it_behaves_like 'user logging'
2016-06-02 11:05:42 +05:30
end
context 'project with disabled CI' do
2016-09-29 09:46:39 +05:30
before do
project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
end
2019-02-15 15:39:39 +05:30
subject! { get '/jwt/auth', params: parameters, headers: headers }
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
it { expect(response).to have_gitlab_http_status(:unauthorized) }
2016-06-02 11:05:42 +05:30
end
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
context 'using deploy tokens' do
let(:deploy_token) { create(:deploy_token, read_registry: true, projects: [project]) }
let(:headers) { { authorization: credentials(deploy_token.username, deploy_token.token) } }
subject! { get '/jwt/auth', params: parameters, headers: headers }
it 'authenticates correctly' do
expect(response).to have_gitlab_http_status(:ok)
expect(service_class).to have_received(:new).with(nil, deploy_token, ActionController::Parameters.new(parameters).permit!)
end
it 'does not log a user' do
expect(log_data.keys).not_to include(%w(username user_id))
end
end
2017-09-10 17:25:29 +05:30
context 'using personal access tokens' do
let(:pat) { create(:personal_access_token, user: user, scopes: ['read_registry']) }
let(:headers) { { authorization: credentials('personal_access_token', pat.token) } }
2018-03-17 18:26:18 +05:30
before do
stub_container_registry_config(enabled: true)
end
2019-02-15 15:39:39 +05:30
subject! { get '/jwt/auth', params: parameters, headers: headers }
2017-09-10 17:25:29 +05:30
it 'authenticates correctly' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-02-15 15:39:39 +05:30
expect(service_class).to have_received(:new).with(nil, user, ActionController::Parameters.new(parameters).permit!)
2017-09-10 17:25:29 +05:30
end
2020-03-28 13:19:24 +05:30
it_behaves_like 'rejecting a blocked user'
2020-05-24 23:13:21 +05:30
it_behaves_like 'user logging'
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
end
context 'using User login' do
let(:user) { create(:user) }
2016-09-29 09:46:39 +05:30
let(:headers) { { authorization: credentials(user.username, user.password) } }
2016-06-02 11:05:42 +05:30
2019-02-15 15:39:39 +05:30
subject! { get '/jwt/auth', params: parameters, headers: headers }
2016-06-02 11:05:42 +05:30
2019-02-15 15:39:39 +05:30
it { expect(service_class).to have_received(:new).with(nil, user, ActionController::Parameters.new(parameters).permit!) }
2016-09-29 09:46:39 +05:30
2020-03-28 13:19:24 +05:30
it_behaves_like 'rejecting a blocked user'
2018-11-18 11:00:15 +05:30
context 'when passing a flat array of scopes' do
# We use this trick to make rails to generate a query_string:
# scope=scope1&scope=scope2
# It works because :scope and 'scope' are the same as string, but different objects
let(:parameters) do
{
:service => service_name,
:scope => 'scope1',
'scope' => 'scope2'
}
end
let(:service_parameters) do
2019-02-15 15:39:39 +05:30
ActionController::Parameters.new({ service: service_name, scopes: %w(scope1 scope2) }).permit!
2018-11-18 11:00:15 +05:30
end
it { expect(service_class).to have_received(:new).with(nil, user, service_parameters) }
2020-05-24 23:13:21 +05:30
it_behaves_like 'user logging'
2018-11-18 11:00:15 +05:30
end
2016-09-29 09:46:39 +05:30
context 'when user has 2FA enabled' do
let(:user) { create(:user, :two_factor) }
context 'without personal token' do
it 'rejects the authorization attempt' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2017-09-10 17:25:29 +05:30
expect(response.body).to include('You must use a personal access token with \'api\' scope for Git over HTTP')
2016-09-29 09:46:39 +05:30
end
end
context 'with personal token' do
let(:access_token) { create(:personal_access_token, user: user) }
let(:headers) { { authorization: credentials(user.username, access_token.token) } }
2016-11-24 13:41:30 +05:30
it 'accepts the authorization attempt' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-09-29 09:46:39 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
it 'does not cause session based checks to be activated' do
expect(Gitlab::Session).not_to receive(:with_session)
get '/jwt/auth', params: parameters, headers: headers
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-04 20:38:33 +05:30
end
2016-06-02 11:05:42 +05:30
end
context 'using invalid login' do
let(:headers) { { authorization: credentials('invalid', 'password') } }
2017-09-10 17:25:29 +05:30
context 'when internal auth is enabled' do
it 'rejects the authorization attempt' do
2019-02-15 15:39:39 +05:30
get '/jwt/auth', params: parameters, headers: headers
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2017-09-10 17:25:29 +05:30
expect(response.body).not_to include('You must use a personal access token with \'api\' scope for Git over HTTP')
end
end
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
context 'when internal auth is disabled' do
it 'rejects the authorization attempt with personal access token message' do
2020-01-01 13:55:28 +05:30
allow_next_instance_of(ApplicationSetting) do |instance|
allow(instance).to receive(:password_authentication_enabled_for_git?) { false }
end
2019-02-15 15:39:39 +05:30
get '/jwt/auth', params: parameters, headers: headers
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2017-09-10 17:25:29 +05:30
expect(response.body).to include('You must use a personal access token with \'api\' scope for Git over HTTP')
end
end
2016-06-02 11:05:42 +05:30
end
end
2016-11-24 13:41:30 +05:30
context 'when using unauthenticated request' do
it 'accepts the authorization attempt' do
2019-02-15 15:39:39 +05:30
get '/jwt/auth', params: parameters
2016-11-24 13:41:30 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-11-24 13:41:30 +05:30
end
it 'allows read access' do
2019-07-31 22:56:46 +05:30
expect(service).to receive(:execute).with(authentication_abilities: Gitlab::Auth.read_only_authentication_abilities)
2016-11-24 13:41:30 +05:30
2019-02-15 15:39:39 +05:30
get '/jwt/auth', params: parameters
2016-11-24 13:41:30 +05:30
end
end
2016-06-02 11:05:42 +05:30
context 'unknown service' do
2019-02-15 15:39:39 +05:30
subject! { get '/jwt/auth', params: { service: 'unknown' } }
2016-06-02 11:05:42 +05:30
2020-03-13 15:44:24 +05:30
it { expect(response).to have_gitlab_http_status(:not_found) }
2016-06-02 11:05:42 +05:30
end
def credentials(login, password)
ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
end
end