Update JwtController specs

This commit is contained in:
Kamil Trzcinski 2016-05-15 00:33:06 -05:00
parent e8f7e5516b
commit 7b88dca77e

View file

@ -1,61 +1,58 @@
require 'spec_helper' require 'spec_helper'
describe JwtController do describe JwtController do
let(:services) { { 'test' => TestService } } let(:service) { double(execute: {}) }
let(:parameters) { { service: 'test' } } let(:service_class) { double(new: service) }
let(:ok_status) { { status: 'OK' } } let(:service_name) { 'test' }
let(:parameters) { { service: service_name } }
before { allow_any_instance_of(JwtController).to receive(:SERVICES).and_return services } before { stub_const('JwtController::SERVICES', service_name => service_class) }
context 'existing service' do context 'existing service' do
before { expect_any_instance_of(TestService).to receive(:execute).and_return(ok_status) }
subject! { get '/jwt/auth', parameters } subject! { get '/jwt/auth', parameters }
it { expect(response.status).to eq(200) } it { expect(response.status).to eq(200) }
context 'returning custom http code' do
let(:service) { double(execute: { http_status: 505 }) }
it { expect(response.status).to eq(505) }
end
end end
context 'when using authorized request' do context 'when using authorized request' do
context 'using CI token' do context 'using CI token' do
let(:project) { create(:empty_project, runners_token: 'token', builds_enabled: builds_enabled) } let(:project) { create(:empty_project, runners_token: 'token', builds_enabled: builds_enabled) }
let(:headers) { { HTTP_AUTHENTICATION: authorize('gitlab-ci-token', project.runners_token) } } let(:headers) { { authorization: credentials('gitlab_ci_token', project.runners_token) } }
subject! { get '/jwt/auth', parameters, headers }
context 'project with enabled CI' do context 'project with enabled CI' do
let(:builds_enabled) { true } let(:builds_enabled) { true }
it do it { expect(service_class).to have_received(:new).with(project, nil, parameters) }
expect(TestService).to receive(:new).with(project, nil, parameters).and_call_original
get '/jwt/auth', parameters, headers
end
end end
context 'project with disabled CI' do context 'project with disabled CI' do
let(:builds_enabled) { false } let(:builds_enabled) { false }
it do it { expect(response.status).to eq(403) }
expect(TestService).to receive(:new).with(project, nil, parameters).and_call_original
get '/jwt/auth', parameters, headers
end
end end
end end
context 'using User login' do context 'using User login' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:headers) { { HTTP_AUTHENTICATION: authorize('user', 'password') } } let(:headers) { { authorization: credentials('user', 'password') } }
before { expect_any_instance_of(Gitlab::Auth).to receive(:find).with('user', 'password').and_return(user) } before { expect_any_instance_of(Gitlab::Auth).to receive(:find).with('user', 'password').and_return(user) }
it do subject! { get '/jwt/auth', parameters, headers }
expect(TestService).to receive(:new).with(nil, user, parameters).and_call_original
get '/jwt/auth', parameters, headers it { expect(service_class).to have_received(:new).with(nil, user, parameters) }
end
end end
context 'using invalid login' do context 'using invalid login' do
let(:headers) { { HTTP_AUTHENTICATION: authorize('invalid', 'password') } } let(:headers) { { authorization: credentials('invalid', 'password') } }
subject! { get '/jwt/auth', parameters, headers } subject! { get '/jwt/auth', parameters, headers }
@ -69,19 +66,7 @@ describe JwtController do
it { expect(response.status).to eq(404) } it { expect(response.status).to eq(404) }
end end
def authorize(login, password) def credentials(login, password)
ActionController::HttpAuthentication::Basic.encode_credentials(login, password) ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
end end
class TestService
attr_accessor :project, :current_user, :params
def initialize(project, user, params = {})
@project, @current_user, @params = project, user, params.dup
end
def execute
{ status: 'OK' }
end
end
end end