File: change_variable_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 (249 lines) | stat: -rw-r--r-- 8,965 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::ChangeVariableService, feature_category: :secrets_management do
  let(:service) { described_class.new(container: container, current_user: user, params: params) }
  let_it_be(:user) { create(:user) }

  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project) }

  shared_examples 'create variable flow' do
    context 'with no extra attributes' do
      let(:params) { { variable_params: { key: 'new_variable', value: 'variable_value' }, action: :create } }

      it 'persists a variable' do
        result = nil
        expect { result = execute }.to change(container.variables, :count).from(0).to(1)
        expect(result.masked).to be_falsey
        expect(result.hidden).to be_falsey
      end
    end

    context 'with masked attribute requested to be true' do
      let(:params) do
        { variable_params: { key: 'new_variable', value: 'variable_value', masked: true }, action: :create }
      end

      it 'persists a variable' do
        result = nil
        expect { result = execute }.to change(container.variables, :count).from(0).to(1)
        expect(result.masked).to be_truthy
      end
    end

    context 'with masked attribute requested to be false' do
      let(:params) do
        { variable_params: { key: 'new_variable', value: 'variable_value', masked: false }, action: :create }
      end

      it 'persists a variable' do
        result = nil
        expect { result = execute }.to change(container.variables, :count).from(0).to(1)
        expect(result.masked).to be_falsey
      end
    end

    context 'with masked_and_hidden attribute requested to be true' do
      let(:params) do
        { variable_params: { key: 'new_variable', value: 'variable_value', masked_and_hidden: true },
          action: :create }
      end

      it 'persists a variable and set hidden and masked attributes' do
        result = nil

        expect { result = execute }.to change(container.variables, :count).from(0).to(1)
        expect(result.masked).to be_truthy
        expect(result.hidden).to be_truthy
      end
    end

    context 'with masked_and_hidden attribute requested to be false' do
      let(:params) do
        { variable_params: { key: 'new_variable', value: 'variable_value', masked_and_hidden: false },
          action: :create }
      end

      it 'persists a variable and set hidden and masked attributes' do
        result = nil

        expect { result = execute }.to change(container.variables, :count).from(0).to(1)
        expect(result.masked).to be_falsey
        expect(result.hidden).to be_falsey
      end
    end
  end

  shared_examples 'update variable flow' do
    context 'when a variable is not hidden' do
      let!(:variable) { container.variables.create!(key: 'old_key', value: 'old_value', hidden: false, masked: true) }

      context 'when a change to the masked attribute is requested' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', masked: false }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.masked).to be_falsey
        end
      end

      context 'when a masked attribute is not requested for change' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value' }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.masked).to be_truthy
        end
      end

      context 'when a request is made to change a masked attribute to its current value' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', masked: true }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.masked).to be_truthy
        end
      end

      context 'when a request is made to change the hidden attribute' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', hidden: true }, action: :update } }

        it 'fails to update the hidden attribute' do
          expect(execute.valid?).to be false
          expect(variable.reload.hidden).to be false
        end
      end

      context 'when a request is made to change the hidden attribute to its current value' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', hidden: false }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.hidden).to be_falsey
        end
      end

      context 'when a variable does not exist' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value' }, action: :update } }

        before do
          variable.destroy!
        end

        it 'raises a record not found error' do
          expect { execute }.to raise_error(::ActiveRecord::RecordNotFound)
        end
      end
    end

    context 'when a variable is hidden' do
      let!(:variable) { container.variables.create!(key: 'old_key', value: 'old_value', hidden: true, masked: true) }

      context 'when a change to the masked attribute is requested' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', masked: false }, action: :update } }

        it 'fails to update the masked attribute' do
          expect(execute.valid?).to be false
          expect(variable.reload.masked).to be_truthy
        end
      end

      context 'when a masked attribute is not requested for change' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value' }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.masked).to be_truthy
        end
      end

      context 'when a request is made to change a masked attribute to its current value' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', masked: true }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.masked).to be_truthy
        end
      end

      context 'when a request is made to change the hidden attribute' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', hidden: false }, action: :update } }

        it 'fails to update the hidden attribute' do
          expect(execute.valid?).to be false
          expect(variable.reload.hidden).to be_truthy
        end
      end

      context 'when a request is made to change the hidden attribute to its current value' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value', hidden: true }, action: :update } }

        it 'updates a variable' do
          expect { execute }.to change { variable.reload.value }.from('old_value').to('new_value')
          expect(variable.reload.hidden).to be_truthy
        end
      end

      context 'when a variable does not exist' do
        let(:params) { { variable_params: { key: variable.key, value: 'new_value' }, action: :update } }

        before do
          variable.destroy!
        end

        it 'raises a record not found error' do
          expect { execute }.to raise_error(::ActiveRecord::RecordNotFound)
        end
      end
    end
  end

  shared_examples 'destroy variable flow' do
    let!(:variable) { container.variables.create!(key: 'old_key', value: 'old_value') }
    let(:params) { { variable_params: { key: variable.key }, action: :destroy } }

    it 'destroys a variable' do
      expect { execute }.to change { container.variables.exists?(variable.id) }.from(true).to(false)
    end

    context 'when the variable does not exist' do
      before do
        variable.destroy!
      end

      it 'raises a record not found error' do
        expect { execute }.to raise_error(::ActiveRecord::RecordNotFound)
      end
    end
  end

  describe 'container is a project' do
    let(:container) { project }

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

      it_behaves_like 'create variable flow'

      it_behaves_like 'update variable flow'

      it_behaves_like 'destroy variable flow'
    end
  end

  describe 'container is a group' do
    let(:container) { group }

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

      it_behaves_like 'create variable flow'

      it_behaves_like 'update variable flow'

      it_behaves_like 'destroy variable flow'
    end
  end
end