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

108 lines
3.3 KiB
Ruby
Raw Normal View History

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 } }
before { stub_const('JwtController::SERVICES', service_name => service_class) }
context 'existing service' do
subject! { get '/jwt/auth', parameters }
2016-08-24 12:49:21 +05:30
it { expect(response).to have_http_status(200) }
2016-06-02 11:05:42 +05:30
context 'returning custom http code' do
let(:service) { double(execute: { http_status: 505 }) }
2016-08-24 12:49:21 +05:30
it { expect(response).to have_http_status(505) }
2016-06-02 11:05:42 +05:30
end
end
2016-11-24 13:41:30 +05:30
context 'when using authenticated request' do
2016-06-02 11:05:42 +05:30
context 'using CI token' do
2016-09-29 09:46:39 +05:30
let(:build) { create(:ci_build, :running) }
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
2016-09-29 09:46:39 +05:30
subject! { get '/jwt/auth', parameters, headers }
2016-06-02 11:05:42 +05:30
it { expect(service_class).to have_received(:new).with(project, nil, parameters) }
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
subject! { get '/jwt/auth', parameters, headers }
2016-06-02 11:05:42 +05:30
2016-10-01 15:18:49 +05:30
it { expect(response).to have_http_status(401) }
2016-06-02 11:05:42 +05:30
end
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
subject! { get '/jwt/auth', parameters, headers }
it { expect(service_class).to have_received(:new).with(nil, user, parameters) }
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
expect(response).to have_http_status(401)
expect(response.body).to include('You have 2FA enabled, please use a personal access token for Git over HTTP')
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
2016-09-29 09:46:39 +05:30
expect(response).to have_http_status(200)
end
end
end
2016-06-02 11:05:42 +05:30
end
context 'using invalid login' do
let(:headers) { { authorization: credentials('invalid', 'password') } }
subject! { get '/jwt/auth', parameters, headers }
2016-10-01 15:18:49 +05:30
it { expect(response).to have_http_status(401) }
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
get '/jwt/auth', parameters
expect(response).to have_http_status(200)
end
it 'allows read access' do
expect(service).to receive(:execute).with(authentication_abilities: Gitlab::Auth.read_authentication_abilities)
get '/jwt/auth', parameters
end
end
2016-06-02 11:05:42 +05:30
context 'unknown service' do
subject! { get '/jwt/auth', service: 'unknown' }
2016-08-24 12:49:21 +05:30
it { expect(response).to have_http_status(404) }
2016-06-02 11:05:42 +05:30
end
def credentials(login, password)
ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
end
end