jwt_controller_spec.rb 3.8 KB
Newer Older
K
Kamil Trzcinski 已提交
1 2 3
require 'spec_helper'

describe JwtController do
K
Kamil Trzcinski 已提交
4 5 6 7
  let(:service) { double(execute: {}) }
  let(:service_class) { double(new: service) }
  let(:service_name) { 'test' }
  let(:parameters) { { service: service_name } }
K
Kamil Trzcinski 已提交
8

K
Kamil Trzcinski 已提交
9
  before { stub_const('JwtController::SERVICES', service_name => service_class) }
K
Kamil Trzcinski 已提交
10 11 12 13

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

Z
Z.J. van de Weg 已提交
14
    it { expect(response).to have_http_status(200) }
K
Kamil Trzcinski 已提交
15 16 17 18

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

Z
Z.J. van de Weg 已提交
19
      it { expect(response).to have_http_status(505) }
K
Kamil Trzcinski 已提交
20
    end
K
Kamil Trzcinski 已提交
21 22
  end

23
  context 'when using authenticated request' do
K
Kamil Trzcinski 已提交
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) } }
K
Kamil Trzcinski 已提交
28

K
Kamil Trzcinski 已提交
29
      context 'project with enabled CI' do
F
Felipe Artur 已提交
30
        subject! { get '/jwt/auth', parameters, headers }
31

K
Kamil Trzcinski 已提交
32
        it { expect(service_class).to have_received(:new).with(project, nil, parameters) }
K
Kamil Trzcinski 已提交
33 34 35
      end

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

        subject! { get '/jwt/auth', parameters, headers }
K
Kamil Trzcinski 已提交
41

42
        it { expect(response).to have_http_status(401) }
K
Kamil Trzcinski 已提交
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
K
Kamil Trzcinski 已提交
57 58 59 60
    end

    context 'using User login' do
      let(:user) { create(:user) }
61
      let(:headers) { { authorization: credentials(user.username, user.password) } }
K
Kamil Trzcinski 已提交
62

K
Kamil Trzcinski 已提交
63
      subject! { get '/jwt/auth', parameters, headers }
K
Kamil Trzcinski 已提交
64

K
Kamil Trzcinski 已提交
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
K
Kamil Trzcinski 已提交
86 87 88
    end

    context 'using invalid login' do
K
Kamil Trzcinski 已提交
89
      let(:headers) { { authorization: credentials('invalid', 'password') } }
K
Kamil Trzcinski 已提交
90 91 92

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

93
      it { expect(response).to have_http_status(401) }
K
Kamil Trzcinski 已提交
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
Z
Z.J. van de Weg 已提交
105
      expect(service).to receive(:execute).with(authentication_abilities: Gitlab::Auth.read_authentication_abilities)
106 107 108 109 110

      get '/jwt/auth', parameters
    end
  end

K
Kamil Trzcinski 已提交
111 112 113
  context 'unknown service' do
    subject! { get '/jwt/auth', service: 'unknown' }

Z
Z.J. van de Weg 已提交
114
    it { expect(response).to have_http_status(404) }
K
Kamil Trzcinski 已提交
115 116
  end

K
Kamil Trzcinski 已提交
117
  def credentials(login, password)
K
Kamil Trzcinski 已提交
118 119 120
    ActionController::HttpAuthentication::Basic.encode_credentials(login, password)
  end
end