BigW Consortium Gitlab

jwt_controller_spec.rb 2.13 KB
Newer Older
1 2 3
require 'spec_helper'

describe JwtController do
4 5 6 7
  let(:service) { double(execute: {}) }
  let(:service_class) { double(new: service) }
  let(:service_name) { 'test' }
  let(:parameters) { { service: service_name } }
8

9
  before { stub_const('JwtController::SERVICES', service_name => service_class) }
10 11 12 13 14

  context 'existing service' do
    subject! { get '/jwt/auth', parameters }

    it { expect(response.status).to eq(200) }
15 16 17 18 19 20

    context 'returning custom http code' do
      let(:service) { double(execute: { http_status: 505 }) }

      it { expect(response.status).to eq(505) }
    end
21 22 23 24 25
  end

  context 'when using authorized request' do
    context 'using CI token' do
      let(:project) { create(:empty_project, runners_token: 'token', builds_enabled: builds_enabled) }
26 27 28
      let(:headers) { { authorization: credentials('gitlab_ci_token', project.runners_token) } }

      subject! { get '/jwt/auth', parameters, headers }
29 30 31 32

      context 'project with enabled CI' do
        let(:builds_enabled) { true }

33
        it { expect(service_class).to have_received(:new).with(project, nil, parameters) }
34 35 36 37 38
      end

      context 'project with disabled CI' do
        let(:builds_enabled) { false }

39
        it { expect(response.status).to eq(403) }
40 41 42 43 44
      end
    end

    context 'using User login' do
      let(:user) { create(:user) }
45
      let(:headers) { { authorization: credentials('user', 'password') } }
46 47 48

      before { expect_any_instance_of(Gitlab::Auth).to receive(:find).with('user', 'password').and_return(user) }

49
      subject! { get '/jwt/auth', parameters, headers }
50

51
      it { expect(service_class).to have_received(:new).with(nil, user, parameters) }
52 53 54
    end

    context 'using invalid login' do
55
      let(:headers) { { authorization: credentials('invalid', 'password') } }
56 57 58 59 60 61 62 63 64 65 66 67 68

      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

69
  def credentials(login, password)
70 71 72
    ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
  end
end