Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dependency_proxy_auth_controller_spec.rb « groups « controllers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 857e0570621bca965e537e059b734c9c45d86ff9 (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
# 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
      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 '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(:not_found) }
      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(:not_found) }
      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
    end
  end
end