summaryrefslogtreecommitdiff
path: root/spec/controllers/groups/dependency_proxy_auth_controller_spec.rb
blob: 50e19d5b482dfe819c36008cbcae17bcc2149a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Groups::DependencyProxyAuthController do
  include DependencyProxyHelpers

  describe 'GET #authenticate' do
    subject { get :authenticate }

    context 'feature flag disabled' do
      before do
        stub_feature_flags(dependency_proxy_for_private_groups: false)
      end

      it 'returns successfully', :aggregate_failures do
        subject

        expect(response).to have_gitlab_http_status(:success)
      end
    end

    context 'without JWT' do
      it 'returns unauthorized with oauth realm', :aggregate_failures do
        subject

        expect(response).to have_gitlab_http_status(:unauthorized)
        expect(response.headers['WWW-Authenticate']).to eq DependencyProxy::Registry.authenticate_header
      end
    end

    context 'with valid JWT' do
      context 'user' do
        let_it_be(:user) { create(:user) }

        let(:jwt) { build_jwt(user) }
        let(:token_header) { "Bearer #{jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:success) }
      end

      context 'deploy token' do
        let_it_be(:user) { create(:deploy_token) }

        let(:jwt) { build_jwt(user) }
        let(:token_header) { "Bearer #{jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:success) }
      end
    end

    context 'with invalid JWT' do
      context 'bad user' do
        let(:jwt) { build_jwt(double('bad_user', id: 999)) }
        let(:token_header) { "Bearer #{jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:unauthorized) }
      end

      context 'token with no user id' do
        let(:token_header) { "Bearer #{build_jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:unauthorized) }
      end

      context 'expired token' do
        let_it_be(:user) { create(:user) }

        let(:jwt) { build_jwt(user, expire_time: Time.zone.now - 1.hour) }
        let(:token_header) { "Bearer #{jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:unauthorized) }
      end

      context 'expired deploy token' do
        let_it_be(:user) { create(:deploy_token, :expired) }

        let(:jwt) { build_jwt(user) }
        let(:token_header) { "Bearer #{jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:unauthorized) }
      end

      context 'revoked deploy token' do
        let_it_be(:user) { create(:deploy_token, :revoked) }

        let(:jwt) { build_jwt(user) }
        let(:token_header) { "Bearer #{jwt.encoded}" }

        before do
          request.headers['HTTP_AUTHORIZATION'] = token_header
        end

        it { is_expected.to have_gitlab_http_status(:unauthorized) }
      end
    end
  end
end