summaryrefslogtreecommitdiff
path: root/spec/services/ci/fetch_kubernetes_token_service_spec.rb
blob: 1d05c9671a9b00b64a9e739d5452975fe8f39f4f (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
require 'spec_helper'

describe Ci::FetchKubernetesTokenService do
  describe '#execute' do
    subject { described_class.new(api_url, ca_pem, username, password).execute }

    let(:api_url) { 'http://111.111.111.111' }
    let(:ca_pem) { '' }
    let(:username) { 'admin' }
    let(:password) { 'xxx' }

    context 'when params correct' do
      let(:token) { 'xxx.token.xxx' }

      let(:secrets_json) do
        [
          {
            'metadata': {
              name: metadata_name
            },
            'data': {
              'token': Base64.encode64(token)
            }
          }
        ]
      end

      before do
        allow_any_instance_of(Kubeclient::Client)
          .to receive(:get_secrets).and_return(secrets_json)
      end

      context 'when default-token exists' do
        let(:metadata_name) { 'default-token-123' }

        it { is_expected.to eq(token) }
      end

      context 'when default-token does not exist' do
        let(:metadata_name) { 'another-token-123' }

        it { is_expected.to be_nil }
      end
    end

    context 'when api_url is nil' do
      let(:api_url) { nil }

      it { expect { subject }.to raise_error("Incomplete settings") }
    end

    context 'when username is nil' do
      let(:username) { nil }

      it { expect { subject }.to raise_error("Incomplete settings") }
    end

    context 'when password is nil' do
      let(:password) { nil }

      it { expect { subject }.to raise_error("Incomplete settings") }
    end
  end
end