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

config_variables_spec.rb « ci « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17133d7ea6605ae8a5e03e3921b64c2e6b74250d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Query.project(fullPath).ciConfigVariables(sha)' do
  include GraphqlHelpers
  include ReactiveCachingHelpers

  let_it_be(:content) do
    File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
  end

  let_it_be(:project) { create(:project, :custom_repo, :public, files: { '.gitlab-ci.yml' => content }) }
  let_it_be(:user) { create(:user) }

  let(:service) { Ci::ListConfigVariablesService.new(project, user) }
  let(:sha) { project.repository.commit.sha }

  let(:query) do
    %(
      query {
        project(fullPath: "#{project.full_path}") {
          ciConfigVariables(sha: "#{sha}") {
            key
            value
            valueOptions
            description
          }
        }
      }
    )
  end

  context 'when the user has the correct permissions' do
    before do
      project.add_maintainer(user)
      allow(Ci::ListConfigVariablesService)
        .to receive(:new)
        .and_return(service)
    end

    context 'when the cache is not empty' do
      before do
        synchronous_reactive_cache(service)
      end

      it 'returns the CI variables for the config' do
        expect(service)
          .to receive(:execute)
          .with(sha)
          .and_call_original

        post_graphql(query, current_user: user)

        expect(graphql_data.dig('project', 'ciConfigVariables')).to contain_exactly(
          {
            'key' => 'KEY_VALUE_VAR',
            'value' => 'value x',
            'valueOptions' => nil,
            'description' => 'value of KEY_VALUE_VAR'
          },
          {
            'key' => 'DB_NAME',
            'value' => 'postgres',
            'valueOptions' => nil,
            'description' => nil
          },
          {
            'key' => 'ENVIRONMENT_VAR',
            'value' => 'env var value',
            'valueOptions' => ['env var value', 'env var value2'],
            'description' => 'env var description'
          }
        )
      end
    end

    context 'when the cache is empty' do
      it 'returns nothing' do
        post_graphql(query, current_user: user)

        expect(graphql_data.dig('project', 'ciConfigVariables')).to be_nil
      end
    end
  end

  context 'when the user is not authorized' do
    before do
      project.add_guest(user)
      allow(Ci::ListConfigVariablesService)
        .to receive(:new)
        .and_return(service)
      synchronous_reactive_cache(service)
    end

    it 'returns nothing' do
      post_graphql(query, current_user: user)

      expect(graphql_data.dig('project', 'ciConfigVariables')).to be_nil
    end
  end
end