BigW Consortium Gitlab

jwt_controller_spec.rb 3.26 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

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

14
    it { expect(response).to have_http_status(200) }
15 16 17 18

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

19
      it { expect(response).to have_http_status(505) }
20
    end
21 22
  end

23
  context 'when using authenticated request' do
24
    context 'using CI token' do
25 26 27
      let(:build) { create(:ci_build, :running) }
      let(:project) { build.project }
      let(:headers) { { authorization: credentials('gitlab-ci-token', build.token) } }
28

29
      context 'project with enabled CI' do
30
        subject! { get '/jwt/auth', parameters, headers }
31

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

      context 'project with disabled CI' do
36 37 38 39 40
        before do
          project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED)
        end

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

42
        it { expect(response).to have_http_status(401) }
43 44 45 46 47
      end
    end

    context 'using User login' do
      let(:user) { create(:user) }
48
      let(:headers) { { authorization: credentials(user.username, user.password) } }
49

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

52
      it { expect(service_class).to have_received(:new).with(nil, user, parameters) }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

      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) } }

68
          it 'accepts the authorization attempt' do
69 70 71 72
            expect(response).to have_http_status(200)
          end
        end
      end
73 74 75
    end

    context 'using invalid login' do
76
      let(:headers) { { authorization: credentials('invalid', 'password') } }
77 78 79

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

80
      it { expect(response).to have_http_status(401) }
81 82 83
    end
  end

84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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

98 99 100
  context 'unknown service' do
    subject! { get '/jwt/auth', service: 'unknown' }

101
    it { expect(response).to have_http_status(404) }
102 103
  end

104
  def credentials(login, password)
105 106 107
    ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
  end
end