File: create_service_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (190 lines) | stat: -rw-r--r-- 6,875 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::Exclusions::CreateService, feature_category: :source_code_management do
  let(:integration_name) { 'beyond_identity' }
  let_it_be(:admin_user) { create(:admin) }
  let(:current_user) { admin_user }
  let_it_be(:project) { create(:project, :in_subgroup) }
  let_it_be(:other_project) { create(:project) }
  let_it_be(:group) { create(:group) }
  let_it_be(:other_group) { create(:group) }
  let(:projects) { [project] }
  let(:groups) { [group] }
  let(:service) do
    described_class.new(current_user: current_user, integration_name: integration_name, projects: projects,
      groups: groups)
  end

  describe '#execute', :enable_admin_mode do
    subject(:execute) { service.execute }

    it_behaves_like 'performs exclusions service validations'

    context 'when called with too many projects' do
      before do
        stub_const('Integrations::Exclusions::CreateService::MAX_PROJECTS', 0)
      end

      it 'returns an error response' do
        result = execute

        expect(result).to be_error
        expect(result.message).to eq('project limit exceeded')
      end
    end

    context 'when called with too many groups' do
      before do
        stub_const('Integrations::Exclusions::CreateService::MAX_GROUPS', 0)
      end

      it 'returns an error response' do
        result = execute

        expect(result).to be_error
        expect(result.message).to eq('group limit exceeded')
      end
    end

    it 'creates custom settings' do
      expect { execute }.to change { Integration.count }.by(2)

      group_integration, project_integration = execute.payload
      expect(PropagateIntegrationWorker.jobs).to contain_exactly(
        a_hash_including('args' => [group_integration.id])
      )
      expect(group_integration.active).to be_falsey
      expect(group_integration.inherit_from_id).to be_nil
      expect(project_integration.active).to be_falsey
      expect(project_integration.inherit_from_id).to be_nil
    end

    context 'when there are no projects or groups passed' do
      let(:projects) { [] }
      let(:groups) { [] }

      it 'returns success response' do
        expect(execute).to be_success
        expect(execute.payload).to eq([])
      end
    end

    context 'when there are existing custom settings' do
      let_it_be(:instance_level_integration) { create(:beyond_identity_integration) }
      let_it_be(:project_level_integration) do
        create(
          :beyond_identity_integration,
          active: true,
          project: other_project,
          instance: false,
          inherit_from_id: instance_level_integration.id
        )
      end

      let_it_be(:group_level_integration) do
        create(
          :beyond_identity_integration,
          active: true,
          group: other_group,
          instance: false,
          inherit_from_id: instance_level_integration.id
        )
      end

      let_it_be(:previously_excluded_group) { create(:group) }
      let_it_be(:excluded_group_integration) do
        create(
          :beyond_identity_integration,
          active: false,
          group: previously_excluded_group,
          instance: false,
          inherit_from_id: nil
        )
      end

      let(:expected_group_integration) do
        Integrations::BeyondIdentity.find_by!(group: group, inherit_from_id: nil, active: false)
      end

      let(:expected_project_integration) do
        Integrations::BeyondIdentity.find_by!(project: project, inherit_from_id: nil, active: false)
      end

      let(:projects) { [project, other_project] }
      let(:groups) { [group, other_group] }

      it 'creates exclusions and updates existing ones' do
        expect { execute }.to change { project_level_integration.reload.active }.from(true).to(false)
          .and change { group_level_integration.reload.active }.from(true).to(false)
          .and change { project_level_integration.inherit_from_id }.from(instance_level_integration.id).to(nil)
          .and change { group_level_integration.inherit_from_id }.from(instance_level_integration.id).to(nil)
        expect(PropagateIntegrationWorker.jobs).to contain_exactly(
          a_hash_including('args' => [expected_group_integration.id]),
          a_hash_including('args' => [group_level_integration.id])
        )
      end

      it 'returns the exclusions' do
        expect(execute.payload).to contain_exactly(
          group_level_integration,
          project_level_integration,
          expected_group_integration,
          expected_project_integration
        )
      end

      context 'when there are existing exclusions' do
        let(:groups) { [previously_excluded_group] }
        let(:projects) { [] }

        it 'does not propagate existing' do
          result = execute

          expect(result.payload).to be_blank
          expect(PropagateIntegrationWorker.jobs).to be_blank
        end
      end
    end

    context 'when there are ancestor exclusions' do
      let!(:ancestor_exclusion) do
        create(:beyond_identity_integration, active: false, instance: false, inherit_from_id: nil,
          group: project.root_namespace)
      end

      let(:group_covered_by_ancestor_exclusion) { create(:group, parent: project.parent) }
      let(:project_covered_by_ancestor_exclusion) { project }

      let(:projects) { [project_covered_by_ancestor_exclusion, other_project] }
      let(:groups) { [group_covered_by_ancestor_exclusion, other_group] }

      it 'only creates exclusions for groups and projects not covered by ancestors with exclusions' do
        expect { execute }.to change { Integration.count }.by(2)
        group_integration, project_integration = execute.payload
        expect(group_integration.group_id).to eq(other_group.id)
        expect(project_integration.project_id).to eq(other_project.id)

        expect(group_integration.active).to be_falsey
        expect(group_integration.inherit_from_id).to be_nil
        expect(project_integration.active).to be_falsey
        expect(project_integration.inherit_from_id).to be_nil
      end
    end

    context 'when projects and groups are descendants of another group' do
      let(:projects) { [project] }
      let(:groups) { [project.parent, project.root_namespace] }

      it 'only creates exclusions for groups and projects not covered by ancestors' do
        expect { execute }.to change { Integration.count }.by(1)
        created_integration = execute.payload.first
        expect(PropagateIntegrationWorker.jobs).to contain_exactly(a_hash_including('args' => [created_integration.id]))
        expect(created_integration.group_id).to eq(project.root_namespace.id)
        expect(created_integration.active).to be_falsey
        expect(created_integration.inherit_from_id).to be_nil
      end
    end
  end
end