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

user_access_spec.rb « kas « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8795ad565d0ab5836e3fb5c348f4f58c9e148103 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Kas::UserAccess, feature_category: :kubernetes_management do
  describe '.enabled?' do
    subject { described_class.enabled? }

    before do
      allow(::Gitlab::Kas).to receive(:enabled?).and_return true
    end

    it { is_expected.to be true }

    context 'when flag kas_user_access is disabled' do
      before do
        stub_feature_flags(kas_user_access: false)
      end

      it { is_expected.to be false }
    end
  end

  describe '.enabled_for?' do
    subject { described_class.enabled_for?(agent) }

    let(:agent) { build(:cluster_agent) }

    before do
      allow(::Gitlab::Kas).to receive(:enabled?).and_return true
    end

    it { is_expected.to be true }

    context 'when flag kas_user_access is disabled' do
      before do
        stub_feature_flags(kas_user_access: false)
      end

      it { is_expected.to be false }
    end

    context 'when flag kas_user_access_project is disabled' do
      before do
        stub_feature_flags(kas_user_access_project: false)
      end

      it { is_expected.to be false }
    end
  end

  describe '.{encrypt,decrypt}_public_session_id' do
    let(:data) { 'the data' }
    let(:encrypted) { described_class.encrypt_public_session_id(data) }
    let(:decrypted) { described_class.decrypt_public_session_id(encrypted) }

    it { expect(encrypted).not_to include data }
    it { expect(decrypted).to eq data }
  end

  describe '.cookie_data' do
    subject(:cookie_data) { described_class.cookie_data(public_session_id) }

    let(:public_session_id) { 'the-public-session-id' }
    let(:external_k8s_proxy_url) { 'https://example.com:1234' }

    before do
      stub_config(
        gitlab: { host: 'example.com', https: true },
        gitlab_kas: { external_k8s_proxy_url: external_k8s_proxy_url }
      )
    end

    it 'is encrypted, secure, httponly', :aggregate_failures do
      expect(cookie_data[:value]).not_to include public_session_id
      expect(cookie_data).to include(httponly: true, secure: true, path: '/')
      expect(cookie_data).not_to have_key(:domain)
    end

    context 'when on non-root path' do
      let(:external_k8s_proxy_url) { 'https://example.com/k8s-proxy' }

      it 'sets :path' do
        expect(cookie_data).to include(httponly: true, secure: true, path: '/k8s-proxy')
      end
    end

    context 'when on subdomain' do
      let(:external_k8s_proxy_url) { 'https://k8s-proxy.example.com' }

      it 'sets :domain' do
        expect(cookie_data[:domain]).to eq "example.com"
      end
    end
  end
end