jwt_controller_spec.rb 3.77 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
      end
44 45 46 47 48 49 50 51 52 53 54 55 56

      context 'using personal access tokens' do
        let(:user) { create(:user) }
        let(:pat) { create(:personal_access_token, user: user, scopes: ['read_registry']) }
        let(:headers) { { authorization: credentials('personal_access_token', pat.token) } }

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

        it 'authenticates correctly' do
          expect(response).to have_http_status(200)
          expect(service_class).to have_received(:new).with(nil, user, parameters)
        end
      end
57 58 59 60
    end

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

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

65
      it { expect(service_class).to have_received(:new).with(nil, user, parameters) }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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

81
          it 'accepts the authorization attempt' do
82 83 84 85
            expect(response).to have_http_status(200)
          end
        end
      end
86 87 88
    end

    context 'using invalid login' do
89
      let(:headers) { { authorization: credentials('invalid', 'password') } }
90 91 92

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

93
      it { expect(response).to have_http_status(401) }
94 95 96
    end
  end

97 98 99 100 101 102 103 104
  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
105
      expect(service).to receive(:execute).with(authentication_abilities: Gitlab::Auth.read_api_abilities)
106 107 108 109 110

      get '/jwt/auth', parameters
    end
  end

111 112 113
  context 'unknown service' do
    subject! { get '/jwt/auth', service: 'unknown' }

114
    it { expect(response).to have_http_status(404) }
115 116
  end

117
  def credentials(login, password)
118 119 120
    ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
  end
end