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

group_variables_finder_spec.rb « ci « finders « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c5d83d7262ea8670e22407fe6eb598b437ab938 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::GroupVariablesFinder, feature_category: :secrets_management do
  subject(:finder) { described_class.new(project, sort_key).execute }

  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group) }
  let_it_be(:subgroup) { create(:group, parent: group) }
  let_it_be(:project_with_subgroup) { create(:project, group: subgroup) }
  let_it_be(:project_without_group) { create(:project) }
  let_it_be(:variable1) { create(:ci_group_variable, group: group, key: 'GROUP_VAR_A', created_at: 1.day.ago) }
  let_it_be(:variable2) { create(:ci_group_variable, group: subgroup, key: 'SUBGROUP_VAR_B') }

  let_it_be(:inherited_ci_variables) do
    [variable1, variable2]
  end

  let(:sort_key) { nil }

  context 'when project does not have a group' do
    let_it_be(:project) { project_without_group }

    it 'returns an empty array' do
      expect(finder.to_a).to match_array([])
    end
  end

  context 'when project belongs to a group' do
    let_it_be(:project) { project_with_subgroup }

    it 'returns variable from parent group and ancestors' do
      expect(finder.to_a).to match_array([variable1, variable2])
    end
  end

  describe 'sorting behaviour' do
    let_it_be(:project) { project_with_subgroup }

    context 'with sort by created_at descending' do
      let(:sort_key) { :created_desc }

      it 'returns variables ordered by created_at in descending order' do
        expect(finder.to_a).to eq([variable2, variable1])
      end
    end

    context 'with sort by created_at ascending' do
      let(:sort_key) { :created_asc }

      it 'returns variables ordered by created_at in ascending order' do
        expect(finder.to_a).to eq([variable1, variable2])
      end
    end

    context 'with sort by key descending' do
      let(:sort_key) { :key_desc }

      it 'returns variables ordered by key in descending order' do
        expect(finder.to_a).to eq([variable2, variable1])
      end
    end

    context 'with sort by key ascending' do
      let(:sort_key) { :key_asc }

      it 'returns variables ordered by key in ascending order' do
        expect(finder.to_a).to eq([variable1, variable2])
      end
    end
  end
end