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

features_spec.rb « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d0334cf6dd2999810469a80be01dff00b4284bf9 (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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Features, stub_feature_flags: false do
  let_it_be(:user)  { create(:user) }
  let_it_be(:admin) { create(:admin) }

  # Find any `development` feature flag name
  let(:known_feature_flag) do
    Feature::Definition.definitions
      .values.find(&:development?)
  end

  let(:known_feature_flag_definition_hash) do
    a_hash_including(
      'type' => 'development'
    )
  end

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

    skip_feature_flags_yaml_validation
    skip_default_enabled_yaml_check
  end

  describe 'GET /features' do
    let(:expected_features) do
      [
        {
          'name' => 'feature_1',
          'state' => 'on',
          'gates' => [{ 'key' => 'boolean', 'value' => true }],
          'definition' => nil
        },
        {
          'name' => 'feature_2',
          'state' => 'off',
          'gates' => [{ 'key' => 'boolean', 'value' => false }],
          'definition' => nil
        },
        {
          'name' => 'feature_3',
          'state' => 'conditional',
          'gates' => [
            { 'key' => 'boolean', 'value' => false },
            { 'key' => 'groups', 'value' => ['perf_team'] }
          ],
          'definition' => nil
        },
        {
          'name' => known_feature_flag.name,
          'state' => 'on',
          'gates' => [{ 'key' => 'boolean', 'value' => true }],
          'definition' => known_feature_flag_definition_hash
        }
      ]
    end

    before do
      Feature.enable('feature_1')
      Feature.disable('feature_2')
      Feature.enable('feature_3', Feature.group(:perf_team))
      Feature.enable(known_feature_flag.name)
    end

    it 'returns a 401 for anonymous users' do
      get api('/features')

      expect(response).to have_gitlab_http_status(:unauthorized)
    end

    it 'returns a 403 for users' do
      get api('/features', user)

      expect(response).to have_gitlab_http_status(:forbidden)
    end

    it 'returns the feature list for admins' do
      get api('/features', admin)

      expect(response).to have_gitlab_http_status(:ok)
      expect(json_response).to match_array(expected_features)
    end
  end

  describe 'POST /feature' do
    let(:feature_name) { known_feature_flag.name }

    # TODO: remove this shared examples block when set_feature_flag_service feature flag
    # is removed. Then remove also any duplicate specs covered by the service class.
    shared_examples 'sets the feature flag status' do
      context 'when the feature does not exist' do
        it 'returns a 401 for anonymous users' do
          post api("/features/#{feature_name}")

          expect(response).to have_gitlab_http_status(:unauthorized)
        end

        it 'returns a 403 for users' do
          post api("/features/#{feature_name}", user)

          expect(response).to have_gitlab_http_status(:forbidden)
        end

        context 'when passed value=true' do
          it 'creates an enabled feature' do
            post api("/features/#{feature_name}", admin), params: { value: 'true' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'on',
              'gates' => [{ 'key' => 'boolean', 'value' => true }],
              'definition' => known_feature_flag_definition_hash
            )
          end

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

            post api("/features/#{feature_name}", admin), params: { value: 'true' }
          end

          it 'creates an enabled feature for the given Flipper group when passed feature_group=perf_team' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', feature_group: 'perf_team' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'groups', 'value' => ['perf_team'] }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end

          it 'creates an enabled feature for the given user when passed user=username' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', user: user.username }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'actors', 'value' => ["User:#{user.id}"] }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end

          it 'creates an enabled feature for the given user and feature group when passed user=username and feature_group=perf_team' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', user: user.username, feature_group: 'perf_team' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response['name']).to eq(feature_name)
            expect(json_response['state']).to eq('conditional')
            expect(json_response['gates']).to contain_exactly(
              { 'key' => 'boolean', 'value' => false },
              { 'key' => 'groups', 'value' => ['perf_team'] },
              { 'key' => 'actors', 'value' => ["User:#{user.id}"] }
            )
          end
        end

        shared_examples 'does not enable the flag' do |actor_type|
          let(:actor_path) { raise NotImplementedError }
          let(:expected_inexistent_path) { actor_path }

          it 'returns the current state of the flag without changes' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', actor_type => actor_path }

            expect(response).to have_gitlab_http_status(:bad_request)
            expect(json_response['message']).to eq("400 Bad request - #{expected_inexistent_path} is not found!")
          end
        end

        shared_examples 'enables the flag for the actor' do |actor_type|
          it 'sets the feature gate' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', actor_type => actor.full_path }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'actors', 'value' => ["#{actor.class}:#{actor.id}"] }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end
        end

        shared_examples 'creates an enabled feature for the specified entries' do
          it do
            post api("/features/#{feature_name}", admin), params: { value: 'true', **gate_params }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response['name']).to eq(feature_name)
            expect(json_response['gates']).to contain_exactly(
              { 'key' => 'boolean', 'value' => false },
              { 'key' => 'actors', 'value' => array_including(expected_gate_params) }
            )
          end
        end

        context 'when enabling for a project by path' do
          context 'when the project exists' do
            it_behaves_like 'enables the flag for the actor', :project do
              let(:actor) { create(:project) }
            end
          end

          context 'when the project does not exist' do
            it_behaves_like 'does not enable the flag', :project do
              let(:actor_path) { 'mep/to/the/mep/mep' }
            end
          end
        end

        context 'when enabling for a group by path' do
          context 'when the group exists' do
            it_behaves_like 'enables the flag for the actor', :group do
              let(:actor) { create(:group) }
            end
          end

          context 'when the group does not exist' do
            it_behaves_like 'does not enable the flag', :group do
              let(:actor_path) { 'not/a/group' }
            end
          end
        end

        context 'when enabling for a namespace by path' do
          context 'when the user namespace exists' do
            it_behaves_like 'enables the flag for the actor', :namespace do
              let(:actor) { create(:namespace) }
            end
          end

          context 'when the group namespace exists' do
            it_behaves_like 'enables the flag for the actor', :namespace do
              let(:actor) { create(:group) }
            end
          end

          context 'when the user namespace does not exist' do
            it_behaves_like 'does not enable the flag', :namespace do
              let(:actor_path) { 'not/a/group' }
            end
          end

          context 'when a project namespace exists' do
            let(:project_namespace) { create(:project_namespace) }

            it_behaves_like 'does not enable the flag', :namespace do
              let(:actor_path) { project_namespace.full_path }
            end
          end
        end

        context 'with multiple users' do
          let_it_be(:users) { create_list(:user, 3) }

          it_behaves_like 'creates an enabled feature for the specified entries' do
            let(:gate_params) { { user: users.map(&:username).join(',') } }
            let(:expected_gate_params) { users.map(&:flipper_id) }
          end

          context 'when empty value exists between comma' do
            it_behaves_like 'creates an enabled feature for the specified entries' do
              let(:gate_params) { { user: "#{users.first.username},,,," } }
              let(:expected_gate_params) { users.first.flipper_id }
            end
          end

          context 'when one of the users does not exist' do
            it_behaves_like 'does not enable the flag', :user do
              let(:actor_path) { "#{users.first.username},inexistent-entry" }
              let(:expected_inexistent_path) { "inexistent-entry" }
            end
          end
        end

        context 'with multiple projects' do
          let_it_be(:projects) { create_list(:project, 3) }

          it_behaves_like 'creates an enabled feature for the specified entries' do
            let(:gate_params) { { project: projects.map(&:full_path).join(',') } }
            let(:expected_gate_params) { projects.map(&:flipper_id) }
          end

          context 'when empty value exists between comma' do
            it_behaves_like 'creates an enabled feature for the specified entries' do
              let(:gate_params) { { project: "#{projects.first.full_path},,,," } }
              let(:expected_gate_params) { projects.first.flipper_id }
            end
          end

          context 'when one of the projects does not exist' do
            it_behaves_like 'does not enable the flag', :project do
              let(:actor_path) { "#{projects.first.full_path},inexistent-entry" }
              let(:expected_inexistent_path) { "inexistent-entry" }
            end
          end
        end

        context 'with multiple groups' do
          let_it_be(:groups) { create_list(:group, 3) }

          it_behaves_like 'creates an enabled feature for the specified entries' do
            let(:gate_params) { { group: groups.map(&:full_path).join(',') } }
            let(:expected_gate_params) { groups.map(&:flipper_id) }
          end

          context 'when empty value exists between comma' do
            it_behaves_like 'creates an enabled feature for the specified entries' do
              let(:gate_params) { { group: "#{groups.first.full_path},,,," } }
              let(:expected_gate_params) { groups.first.flipper_id }
            end
          end

          context 'when one of the groups does not exist' do
            it_behaves_like 'does not enable the flag', :group do
              let(:actor_path) { "#{groups.first.full_path},inexistent-entry" }
              let(:expected_inexistent_path) { "inexistent-entry" }
            end
          end
        end

        context 'with multiple namespaces' do
          let_it_be(:namespaces) { create_list(:namespace, 3) }

          it_behaves_like 'creates an enabled feature for the specified entries' do
            let(:gate_params) { { namespace: namespaces.map(&:full_path).join(',') } }
            let(:expected_gate_params) { namespaces.map(&:flipper_id) }
          end

          context 'when empty value exists between comma' do
            it_behaves_like 'creates an enabled feature for the specified entries' do
              let(:gate_params) { { namespace: "#{namespaces.first.full_path},,,," } }
              let(:expected_gate_params) { namespaces.first.flipper_id }
            end
          end

          context 'when one of the namespaces does not exist' do
            it_behaves_like 'does not enable the flag', :namespace do
              let(:actor_path) { "#{namespaces.first.full_path},inexistent-entry" }
              let(:expected_inexistent_path) { "inexistent-entry" }
            end
          end
        end

        it 'creates a feature with the given percentage of time if passed an integer' do
          post api("/features/#{feature_name}", admin), params: { value: '50' }

          expect(response).to have_gitlab_http_status(:created)
          expect(json_response).to match(
            'name' => feature_name,
            'state' => 'conditional',
            'gates' => [
              { 'key' => 'boolean', 'value' => false },
              { 'key' => 'percentage_of_time', 'value' => 50 }
            ],
            'definition' => known_feature_flag_definition_hash
          )
        end

        it 'creates a feature with the given percentage of time if passed a float' do
          post api("/features/#{feature_name}", admin), params: { value: '0.01' }

          expect(response).to have_gitlab_http_status(:created)
          expect(json_response).to match(
            'name' => feature_name,
            'state' => 'conditional',
            'gates' => [
              { 'key' => 'boolean', 'value' => false },
              { 'key' => 'percentage_of_time', 'value' => 0.01 }
            ],
            'definition' => known_feature_flag_definition_hash
          )
        end

        it 'creates a feature with the given percentage of actors if passed an integer' do
          post api("/features/#{feature_name}", admin), params: { value: '50', key: 'percentage_of_actors' }

          expect(response).to have_gitlab_http_status(:created)
          expect(json_response).to match(
            'name' => feature_name,
            'state' => 'conditional',
            'gates' => [
              { 'key' => 'boolean', 'value' => false },
              { 'key' => 'percentage_of_actors', 'value' => 50 }
            ],
            'definition' => known_feature_flag_definition_hash
          )
        end

        it 'creates a feature with the given percentage of actors if passed a float' do
          post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors' }

          expect(response).to have_gitlab_http_status(:created)
          expect(json_response).to match(
            'name' => feature_name,
            'state' => 'conditional',
            'gates' => [
              { 'key' => 'boolean', 'value' => false },
              { 'key' => 'percentage_of_actors', 'value' => 0.01 }
            ],
            'definition' => known_feature_flag_definition_hash
          )
        end

        describe 'mutually exclusive parameters' do
          shared_examples 'fails to set the feature flag' do
            it 'returns an error' do
              expect(response).to have_gitlab_http_status(:bad_request)
              expect(json_response['error']).to match(/key, \w+ are mutually exclusive/)
            end
          end

          context 'when key and feature_group are provided' do
            before do
              post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', feature_group: 'some-value' }
            end

            it_behaves_like 'fails to set the feature flag'
          end

          context 'when key and user are provided' do
            before do
              post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', user: 'some-user' }
            end

            it_behaves_like 'fails to set the feature flag'
          end

          context 'when key and group are provided' do
            before do
              post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', group: 'somepath' }
            end

            it_behaves_like 'fails to set the feature flag'
          end

          context 'when key and namespace are provided' do
            before do
              post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', namespace: 'somepath' }
            end

            it_behaves_like 'fails to set the feature flag'
          end

          context 'when key and project are provided' do
            before do
              post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', project: 'somepath' }
            end

            it_behaves_like 'fails to set the feature flag'
          end
        end
      end

      context 'when the feature exists' do
        before do
          Feature.disable(feature_name) # This also persists the feature on the DB
        end

        context 'when passed value=true' do
          it 'enables the feature' do
            post api("/features/#{feature_name}", admin), params: { value: 'true' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'on',
              'gates' => [{ 'key' => 'boolean', 'value' => true }],
              'definition' => known_feature_flag_definition_hash
            )
          end

          it 'enables the feature for the given Flipper group when passed feature_group=perf_team' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', feature_group: 'perf_team' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'groups', 'value' => ['perf_team'] }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end

          it 'enables the feature for the given user when passed user=username' do
            post api("/features/#{feature_name}", admin), params: { value: 'true', user: user.username }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'actors', 'value' => ["User:#{user.id}"] }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end
        end

        context 'when feature is enabled and value=false is passed' do
          it 'disables the feature' do
            Feature.enable(feature_name)
            expect(Feature.enabled?(feature_name)).to eq(true)

            post api("/features/#{feature_name}", admin), params: { value: 'false' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'off',
              'gates' => [{ 'key' => 'boolean', 'value' => false }],
              'definition' => known_feature_flag_definition_hash
            )
          end

          it 'disables the feature for the given Flipper group when passed feature_group=perf_team' do
            Feature.enable(feature_name, Feature.group(:perf_team))
            expect(Feature.enabled?(feature_name, admin)).to be_truthy

            post api("/features/#{feature_name}", admin), params: { value: 'false', feature_group: 'perf_team' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'off',
              'gates' => [{ 'key' => 'boolean', 'value' => false }],
              'definition' => known_feature_flag_definition_hash
            )
          end

          it 'disables the feature for the given user when passed user=username' do
            Feature.enable(feature_name, user)
            expect(Feature.enabled?(feature_name, user)).to be_truthy

            post api("/features/#{feature_name}", admin), params: { value: 'false', user: user.username }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'off',
              'gates' => [{ 'key' => 'boolean', 'value' => false }],
              'definition' => known_feature_flag_definition_hash
            )
          end
        end

        context 'with a pre-existing percentage of time value' do
          before do
            Feature.enable_percentage_of_time(feature_name, 50)
          end

          it 'updates the percentage of time if passed an integer' do
            post api("/features/#{feature_name}", admin), params: { value: '30' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'percentage_of_time', 'value' => 30 }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end
        end

        context 'with a pre-existing percentage of actors value' do
          before do
            Feature.enable_percentage_of_actors(feature_name, 42)
          end

          it 'updates the percentage of actors if passed an integer' do
            post api("/features/#{feature_name}", admin), params: { value: '74', key: 'percentage_of_actors' }

            expect(response).to have_gitlab_http_status(:created)
            expect(json_response).to match(
              'name' => feature_name,
              'state' => 'conditional',
              'gates' => [
                { 'key' => 'boolean', 'value' => false },
                { 'key' => 'percentage_of_actors', 'value' => 74 }
              ],
              'definition' => known_feature_flag_definition_hash
            )
          end
        end
      end
    end

    before do
      stub_feature_flags(set_feature_flag_service: true)
    end

    it_behaves_like 'sets the feature flag status'

    context 'when feature flag set_feature_flag_service is disabled' do
      before do
        stub_feature_flags(set_feature_flag_service: false)
      end

      it_behaves_like 'sets the feature flag status'
    end
  end

  describe 'DELETE /feature/:name' do
    let(:feature_name) { 'my_feature' }

    context 'when the user has no access' do
      it 'returns a 401 for anonymous users' do
        delete api("/features/#{feature_name}")

        expect(response).to have_gitlab_http_status(:unauthorized)
      end

      it 'returns a 403 for users' do
        delete api("/features/#{feature_name}", user)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end

    context 'when the user has access' do
      it 'returns 204 when the value is not set' do
        delete api("/features/#{feature_name}", admin)

        expect(response).to have_gitlab_http_status(:no_content)
      end

      context 'when the gate value was set' do
        before do
          Feature.enable(feature_name)
        end

        it 'deletes an enabled feature' do
          expect do
            delete api("/features/#{feature_name}", admin)
            Feature.reset
          end.to change { Feature.persisted_name?(feature_name) }
            .and change { Feature.enabled?(feature_name) }

          expect(response).to have_gitlab_http_status(:no_content)
        end

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

          delete api("/features/#{feature_name}", admin)
        end
      end
    end
  end
end