summaryrefslogtreecommitdiff
path: root/spec/services/ci/list_config_variables_service_spec.rb
blob: 5cc0481768bc40f47f2b555621a82aa84c7446ad (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::ListConfigVariablesService do
  let_it_be(:project) { create(:project, :repository) }
  let(:service) { described_class.new(project) }
  let(:result) { YAML.dump(ci_config) }

  subject { service.execute(sha) }

  before do
    stub_gitlab_ci_yml_for_sha(sha, result)
  end

  context 'when sending a valid sha' do
    let(:sha) { 'master' }
    let(:ci_config) do
      {
        variables: {
          KEY1: { value: 'val 1', description: 'description 1' },
          KEY2: { value: 'val 2', description: '' },
          KEY3: { value: 'val 3' },
          KEY4: 'val 4'
        },
        test: {
          stage: 'test',
          script: 'echo'
        }
      }
    end

    it 'returns variable list' do
      expect(subject['KEY1']).to eq({ value: 'val 1', description: 'description 1' })
      expect(subject['KEY2']).to eq({ value: 'val 2', description: '' })
      expect(subject['KEY3']).to eq({ value: 'val 3', description: nil })
      expect(subject['KEY4']).to eq({ value: 'val 4', description: nil })
    end
  end

  context 'when sending an invalid sha' do
    let(:sha) { 'invalid-sha' }
    let(:ci_config) { nil }

    it 'returns empty json' do
      expect(subject).to eq({})
    end
  end

  context 'when sending an invalid config' do
    let(:sha) { 'master' }
    let(:ci_config) do
      {
        variables: {
          KEY1: { value: 'val 1', description: 'description 1' }
        },
        test: {
          stage: 'invalid',
          script: 'echo'
        }
      }
    end

    it 'returns empty result' do
      expect(subject).to eq({})
    end
  end

  private

  def stub_gitlab_ci_yml_for_sha(sha, result)
    allow_any_instance_of(Repository)
        .to receive(:gitlab_ci_yml_for)
        .with(sha, '.gitlab-ci.yml')
        .and_return(result)
  end
end