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

set_feature_flag_service_spec.rb « admin « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 45ee914558a6d0a62c64151779afefdd108e8ea3 (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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Admin::SetFeatureFlagService do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:group) { create(:group) }

  let(:feature_name) { known_feature_flag.name }
  let(:service) { described_class.new(feature_flag_name: feature_name, params: params) }

  # Find any `development` feature flag name
  let(:known_feature_flag) do
    Feature::Definition.definitions
      .values.find { |defn| defn.development? && !defn.default_enabled }
  end

  describe 'sequences of executions' do
    subject(:flag) do
      Feature.get(feature_name) # rubocop: disable Gitlab/AvoidFeatureGet
    end

    context 'if we enable_percentage_of_actors and then disable' do
      before do
        described_class
          .new(feature_flag_name: feature_name, params: { key: 'percentage_of_actors', value: '50.0' })
           .execute

        described_class
          .new(feature_flag_name: feature_name, params: { key: 'percentage_of_actors', value: '0.0' })
          .execute
      end

      it 'leaves the flag off' do
        expect(flag.state).to eq(:off)
      end
    end

    context 'if we enable and then enable_percentage_of_actors' do
      before do
        described_class
          .new(feature_flag_name: feature_name, params: { key: 'percentage_of_actors', value: '100.0' })
          .execute
      end

      it 'reports an error' do
        result = described_class
            .new(feature_flag_name: feature_name, params: { key: 'percentage_of_actors', value: '50.0' })
            .execute

        expect(flag.state).to eq(:on)
        expect(result).to be_error
      end

      context 'if we disable the flag first' do
        before do
          described_class
            .new(feature_flag_name: feature_name, params: { value: 'false' })
            .execute
        end

        it 'sets the percentage of actors' do
          result = described_class
              .new(feature_flag_name: feature_name, params: { key: 'percentage_of_actors', value: '50.0' })
              .execute

          expect(flag.state).to eq(:conditional)
          expect(result).not_to be_error
        end
      end
    end
  end

  describe '#execute' do
    before do
      unstub_all_feature_flags

      Feature.reset
      Flipper.unregister_groups
      Flipper.register(:perf_team) do |actor|
        actor.respond_to?(:admin) && actor.admin?
      end
    end

    subject(:result) { service.execute }

    context 'when we cannot interpret the operation' do
      let(:params) { { value: 'wibble', key: 'unknown' } }

      it { is_expected.to be_error }
      it { is_expected.to have_attributes(reason: :illegal_operation) }
      it { is_expected.to have_attributes(message: %(Cannot set '#{feature_name}' ("unknown") to "wibble")) }

      context 'when the key is absent' do
        let(:params) { { value: 'wibble' } }

        it { is_expected.to be_error }
        it { is_expected.to have_attributes(reason: :illegal_operation) }
        it { is_expected.to have_attributes(message: %(Cannot set '#{feature_name}' to "wibble")) }
      end
    end

    context 'when the value to set cannot be parsed' do
      let(:params) { { value: 'wibble', key: 'percentage_of_actors' } }

      it { is_expected.to be_error }
      it { is_expected.to have_attributes(reason: :illegal_operation) }
      it { is_expected.to have_attributes(message: 'Not a percentage') }
    end

    context 'when value is "remove_opt_out"' do
      before do
        Feature.enable(feature_name)
      end

      context 'without a target' do
        let(:params) { { value: 'remove_opt_out' } }

        it 'returns an error' do
          expect(result).to be_error
          expect(result.reason).to eq(:illegal_operation)
        end
      end

      context 'with a target' do
        let(:params) { { value: 'remove_opt_out', user: user.username } }

        context 'when there is currently no opt-out' do
          it 'returns an error' do
            expect(result).to be_error
            expect(result.reason).to eq(:illegal_operation)
            expect(Feature).to be_enabled(feature_name, user)
          end
        end

        context 'when there is currently an opt-out' do
          before do
            Feature.opt_out(feature_name, user)
          end

          it 'removes the opt out' do
            expect(result).to be_success
            expect(Feature).to be_enabled(feature_name, user)
          end
        end
      end
    end

    context 'when value is "opt_out"' do
      let(:params) { { value: 'opt_out', namespace: group.full_path, user: user.username } }

      it 'opts the user and group out' do
        expect(Feature).to receive(:opt_out).with(feature_name, user)
        expect(Feature).to receive(:opt_out).with(feature_name, group)
        expect(result).to be_success
      end

      context 'without a target' do
        let(:params) { { value: 'opt_out' } }

        it { is_expected.to be_error }

        it { is_expected.to have_attributes(reason: :illegal_operation) }
      end
    end

    context 'when enabling the feature flag' do
      let(:params) { { value: 'true' } }

      it 'enables the feature flag' do
        expect(Feature).to receive(:enable).with(feature_name)
        expect(subject).to be_success

        feature_flag = subject.payload[:feature_flag]
        expect(feature_flag.name).to eq(feature_name)
      end

      context 'when the flag is default_enabled' do
        let(:known_feature_flag) do
          Feature::Definition.definitions
            .values.find { |defn| defn.development? && defn.default_enabled }
        end

        it 'leaves the flag enabled' do
          expect(subject).to be_success
          expect(Feature).to be_enabled(feature_name)
        end
      end

      it 'logs the event' do
        expect(Feature.logger).to receive(:info).once

        subject
      end

      context 'when enabling for a user actor' do
        let(:params) { { value: 'true', user: user.username } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, user)
          expect(subject).to be_success
        end

        context 'when the flag has been opted out for user' do
          before do
            Feature.enable(feature_name)
            Feature.opt_out(feature_name, user)
          end

          it 'records an error' do
            expect(subject).to be_error
            expect(subject.reason).to eq(:illegal_operation)
            expect(Feature).not_to be_enabled(feature_name, user)
          end
        end

        context 'when the flag is default_enabled' do
          let(:known_feature_flag) do
            Feature::Definition.definitions
              .values.find { |defn| defn.development? && defn.default_enabled }
          end

          it 'leaves the feature enabled' do
            expect(subject).to be_success
            expect(Feature).to be_enabled(feature_name, user)
          end
        end

        context 'when user does not exist' do
          let(:params) { { value: 'true', user: 'unknown-user' } }

          it 'does nothing' do
            expect(Feature).not_to receive(:enable)
            expect(subject).to be_error
            expect(subject.reason).to eq(:actor_not_found)
          end
        end
      end

      context 'when enabling for a feature group' do
        let(:params) { { value: 'true', feature_group: 'perf_team' } }
        let(:feature_group) { Feature.group('perf_team') }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, feature_group)
          expect(subject).to be_success
        end
      end

      context 'when enabling for a project' do
        let(:params) { { value: 'true', project: project.full_path } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, project)
          expect(subject).to be_success
        end
      end

      context 'when enabling for a group' do
        let(:params) { { value: 'true', group: group.full_path } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, group)
          expect(subject).to be_success
        end

        context 'when group does not exist' do
          let(:params) { { value: 'true', group: 'unknown-group' } }

          it 'returns an error' do
            expect(Feature).not_to receive(:disable)
            expect(subject).to be_error
            expect(subject.reason).to eq(:actor_not_found)
          end
        end
      end

      context 'when enabling for a user namespace' do
        let(:namespace) { user.namespace }
        let(:params) { { value: 'true', namespace: namespace.full_path } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, namespace)
          expect(subject).to be_success
        end

        context 'when namespace does not exist' do
          let(:params) { { value: 'true', namespace: 'unknown-namespace' } }

          it 'returns an error' do
            expect(Feature).not_to receive(:disable)
            expect(subject).to be_error
            expect(subject.reason).to eq(:actor_not_found)
          end
        end
      end

      context 'when enabling for a group namespace' do
        let(:params) { { value: 'true', namespace: group.full_path } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, group)
          expect(subject).to be_success
        end
      end

      context 'when enabling for a repository' do
        let(:params) { { value: 'true', repository: project.repository.full_path } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, project.repository)
          expect(subject).to be_success
        end
      end

      context 'when enabling for a user actor and a feature group' do
        let(:params) { { value: 'true', user: user.username, feature_group: 'perf_team' } }
        let(:feature_group) { Feature.group('perf_team') }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable).with(feature_name, user)
          expect(Feature).to receive(:enable).with(feature_name, feature_group)
          expect(subject).to be_success
        end
      end

      context 'when enabling given a percentage of time' do
        let(:params) { { value: '50' } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable_percentage_of_time).with(feature_name, 50)
          expect(subject).to be_success
        end

        context 'when value is a float' do
          let(:params) { { value: '0.01' } }

          it 'enables the feature flag' do
            expect(Feature).to receive(:enable_percentage_of_time).with(feature_name, 0.01)
            expect(subject).to be_success
          end
        end

        context 'with a target' do
          before do
            params[:user] = user.username
          end

          it { is_expected.to be_error }

          it { is_expected.to have_attributes(reason: :illegal_operation) }
        end
      end

      context 'when enabling given a percentage of actors' do
        let(:params) { { value: '50', key: 'percentage_of_actors' } }

        it 'enables the feature flag' do
          expect(Feature).to receive(:enable_percentage_of_actors).with(feature_name, 50)
          expect(subject).to be_success
        end

        context 'when value is a float' do
          let(:params) { { value: '0.01', key: 'percentage_of_actors' } }

          it 'enables the feature flag' do
            expect(Feature).to receive(:enable_percentage_of_actors).with(feature_name, 0.01)
            expect(subject).to be_success
          end
        end

        context 'with a target' do
          before do
            params[:user] = user.username
          end

          it { is_expected.to be_error }

          it { is_expected.to have_attributes(reason: :illegal_operation) }
        end
      end
    end

    context 'when disabling the feature flag' do
      before do
        Feature.enable(feature_name)
      end

      let(:params) { { value: 'false' } }

      it 'disables the feature flag' do
        expect(Feature).to receive(:disable).with(feature_name)
        expect(subject).to be_success

        feature_flag = subject.payload[:feature_flag]
        expect(feature_flag.name).to eq(feature_name)
      end

      it 'logs the event' do
        expect(Feature.logger).to receive(:info).once

        subject
      end

      context 'when disabling for a user actor' do
        let(:params) { { value: 'false', user: user.username } }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, user)
          expect(subject).to be_success
        end

        context 'when user does not exist' do
          let(:params) { { value: 'false', user: 'unknown-user' } }

          it 'returns an error' do
            expect(Feature).not_to receive(:disable)
            expect(subject).to be_error
            expect(subject.reason).to eq(:actor_not_found)
          end
        end
      end

      context 'when disabling for a feature group' do
        let(:params) { { value: 'false', feature_group: 'perf_team' } }
        let(:feature_group) { Feature.group('perf_team') }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, feature_group)
          expect(subject).to be_success
        end
      end

      context 'when disabling for a project' do
        let(:params) { { value: 'false', project: project.full_path } }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, project)
          expect(subject).to be_success
        end
      end

      context 'when disabling for a group' do
        let(:params) { { value: 'false', group: group.full_path } }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, group)
          expect(subject).to be_success
        end

        context 'when group does not exist' do
          let(:params) { { value: 'false', group: 'unknown-group' } }

          it 'returns an error' do
            expect(Feature).not_to receive(:disable)
            expect(subject).to be_error
            expect(subject.reason).to eq(:actor_not_found)
          end
        end
      end

      context 'when disabling for a user namespace' do
        let(:namespace) { user.namespace }
        let(:params) { { value: 'false', namespace: namespace.full_path } }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, namespace)
          expect(subject).to be_success
        end

        context 'when namespace does not exist' do
          let(:params) { { value: 'false', namespace: 'unknown-namespace' } }

          it 'returns an error' do
            expect(Feature).not_to receive(:disable)
            expect(subject).to be_error
            expect(subject.reason).to eq(:actor_not_found)
          end
        end
      end

      context 'when disabling for a group namespace' do
        let(:params) { { value: 'false', namespace: group.full_path } }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, group)
          expect(subject).to be_success
        end
      end

      context 'when disabling for a user actor and a feature group' do
        let(:params) { { value: 'false', user: user.username, feature_group: 'perf_team' } }
        let(:feature_group) { Feature.group('perf_team') }

        it 'disables the feature flag' do
          expect(Feature).to receive(:disable).with(feature_name, user)
          expect(Feature).to receive(:disable).with(feature_name, feature_group)
          expect(subject).to be_success
        end
      end
    end
  end
end