2016-05-15 00:45:48 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe JwtController do
|
2016-05-15 05:33:06 +00:00
|
|
|
let(:service) { double(execute: {}) }
|
|
|
|
let(:service_class) { double(new: service) }
|
|
|
|
let(:service_name) { 'test' }
|
|
|
|
let(:parameters) { { service: service_name } }
|
2016-05-15 00:45:48 +00:00
|
|
|
|
2016-05-15 05:33:06 +00:00
|
|
|
before { stub_const('JwtController::SERVICES', service_name => service_class) }
|
2016-05-15 00:45:48 +00:00
|
|
|
|
|
|
|
context 'existing service' do
|
|
|
|
subject! { get '/jwt/auth', parameters }
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(200) }
|
2016-05-15 05:33:06 +00:00
|
|
|
|
|
|
|
context 'returning custom http code' do
|
|
|
|
let(:service) { double(execute: { http_status: 505 }) }
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(505) }
|
|
|
|
end
|
2016-05-15 00:45:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using authorized request' do
|
|
|
|
context 'using CI token' do
|
|
|
|
let(:project) { create(:empty_project, runners_token: 'token', builds_enabled: builds_enabled) }
|
2016-05-22 19:14:12 +00:00
|
|
|
let(:headers) { { authorization: credentials('gitlab-ci-token', project.runners_token) } }
|
2016-05-15 05:33:06 +00:00
|
|
|
|
|
|
|
subject! { get '/jwt/auth', parameters, headers }
|
2016-05-15 00:45:48 +00:00
|
|
|
|
|
|
|
context 'project with enabled CI' do
|
|
|
|
let(:builds_enabled) { true }
|
|
|
|
|
2016-05-15 05:33:06 +00:00
|
|
|
it { expect(service_class).to have_received(:new).with(project, nil, parameters) }
|
2016-05-15 00:45:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'project with disabled CI' do
|
|
|
|
let(:builds_enabled) { false }
|
|
|
|
|
2016-05-15 05:33:06 +00:00
|
|
|
it { expect(response.status).to eq(403) }
|
2016-05-15 00:45:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using User login' do
|
|
|
|
let(:user) { create(:user) }
|
2016-05-15 05:33:06 +00:00
|
|
|
let(:headers) { { authorization: credentials('user', 'password') } }
|
2016-05-15 00:45:48 +00:00
|
|
|
|
|
|
|
before { expect_any_instance_of(Gitlab::Auth).to receive(:find).with('user', 'password').and_return(user) }
|
|
|
|
|
2016-05-15 05:33:06 +00:00
|
|
|
subject! { get '/jwt/auth', parameters, headers }
|
2016-05-15 00:45:48 +00:00
|
|
|
|
2016-05-15 05:33:06 +00:00
|
|
|
it { expect(service_class).to have_received(:new).with(nil, user, parameters) }
|
2016-05-15 00:45:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'using invalid login' do
|
2016-05-15 05:33:06 +00:00
|
|
|
let(:headers) { { authorization: credentials('invalid', 'password') } }
|
2016-05-15 00:45:48 +00:00
|
|
|
|
|
|
|
subject! { get '/jwt/auth', parameters, headers }
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(403) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unknown service' do
|
|
|
|
subject! { get '/jwt/auth', service: 'unknown' }
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(404) }
|
|
|
|
end
|
|
|
|
|
2016-05-15 05:33:06 +00:00
|
|
|
def credentials(login, password)
|
2016-05-15 00:45:48 +00:00
|
|
|
ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
|
|
|
|
end
|
|
|
|
end
|