summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/experimentation/controller_concern_spec.rb
blob: 7a619c9f15538fd8595607d093354f8e7cd1fcbf (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
677
678
679
680
681
682
683
684
685
686
687
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Experimentation::ControllerConcern, type: :controller do
  before do
    stub_const('Gitlab::Experimentation::EXPERIMENTS', {
        backwards_compatible_test_experiment: {
          tracking_category: 'Team',
          use_backwards_compatible_subject_index: true
        },
        test_experiment: {
          tracking_category: 'Team',
          rollout_strategy: rollout_strategy
        },
        my_experiment: {
          tracking_category: 'Team'
        }
      }
    )

    allow(Gitlab).to receive(:dev_env_or_com?).and_return(is_gitlab_com)

    Feature.enable_percentage_of_time(:backwards_compatible_test_experiment_experiment_percentage, enabled_percentage)
    Feature.enable_percentage_of_time(:test_experiment_experiment_percentage, enabled_percentage)
  end

  let(:enabled_percentage) { 10 }
  let(:rollout_strategy) { nil }
  let(:is_gitlab_com) { true }

  controller(ApplicationController) do
    include Gitlab::Experimentation::ControllerConcern

    def index
      head :ok
    end
  end

  describe '#set_experimentation_subject_id_cookie' do
    let(:do_not_track) { nil }
    let(:cookie) { cookies.permanent.signed[:experimentation_subject_id] }
    let(:cookie_value) { nil }

    before do
      request.headers['DNT'] = do_not_track if do_not_track.present?
      request.cookies[:experimentation_subject_id] = cookie_value if cookie_value

      get :index
    end

    context 'cookie is present' do
      let(:cookie_value) { 'test' }

      it 'does not change the cookie' do
        expect(cookies[:experimentation_subject_id]).to eq 'test'
      end
    end

    context 'cookie is not present' do
      it 'sets a permanent signed cookie' do
        expect(cookie).to be_present
      end

      context 'DNT: 0' do
        let(:do_not_track) { '0' }

        it 'sets a permanent signed cookie' do
          expect(cookie).to be_present
        end
      end

      context 'DNT: 1' do
        let(:do_not_track) { '1' }

        it 'does nothing' do
          expect(cookie).not_to be_present
        end
      end
    end

    context 'when not on gitlab.com' do
      let(:is_gitlab_com) { false }

      context 'when cookie was set' do
        let(:cookie_value) { 'test' }

        it 'cookie gets deleted' do
          expect(cookie).not_to be_present
        end
      end

      context 'when no cookie was set before' do
        it 'does nothing' do
          expect(cookie).not_to be_present
        end
      end
    end
  end

  describe '#push_frontend_experiment' do
    it 'pushes an experiment to the frontend' do
      gon = instance_double('gon')
      stub_experiment_for_subject(my_experiment: true)
      allow(controller).to receive(:gon).and_return(gon)

      expect(gon).to receive(:push).with({ experiments: { 'myExperiment' => true } }, true)

      controller.push_frontend_experiment(:my_experiment)
    end
  end

  describe '#experiment_enabled?' do
    def check_experiment(exp_key = :test_experiment, subject = nil)
      controller.experiment_enabled?(exp_key, subject: subject)
    end

    subject { check_experiment }

    context 'cookie is not present' do
      it { is_expected.to eq(false) }
    end

    context 'cookie is present' do
      using RSpec::Parameterized::TableSyntax

      before do
        cookies.permanent.signed[:experimentation_subject_id] = 'abcd-1234'
        get :index
      end

      where(:experiment_key, :index_value) do
        :test_experiment | 'abcd-1234'
        :backwards_compatible_test_experiment | 'abcd1234'
      end

      with_them do
        it 'calls Gitlab::Experimentation.in_experiment_group?? with the name of the experiment and the calculated experimentation_subject_index based on the uuid' do
          expect(Gitlab::Experimentation).to receive(:in_experiment_group?).with(experiment_key, subject: index_value)

          check_experiment(experiment_key)
        end
      end

      context 'when subject is given' do
        let(:rollout_strategy) { :user }
        let(:user) { build(:user) }

        it 'uses the subject' do
          expect(Gitlab::Experimentation).to receive(:in_experiment_group?).with(:test_experiment, subject: user)

          check_experiment(:test_experiment, user)
        end
      end
    end

    context 'do not track' do
      before do
        allow(Gitlab::Experimentation).to receive(:in_experiment_group?) { true }
      end

      context 'when do not track is disabled' do
        before do
          controller.request.headers['DNT'] = '0'
        end

        it { is_expected.to eq(true) }
      end

      context 'when do not track is enabled' do
        before do
          controller.request.headers['DNT'] = '1'
        end

        it { is_expected.to eq(false) }
      end
    end

    context 'URL parameter to force enable experiment' do
      it 'returns true unconditionally' do
        get :index, params: { force_experiment: :test_experiment }

        is_expected.to eq(true)
      end
    end

    context 'Cookie parameter to force enable experiment' do
      it 'returns true unconditionally' do
        cookies[:force_experiment] = 'test_experiment,another_experiment'
        get :index

        expect(check_experiment(:test_experiment)).to eq(true)
        expect(check_experiment(:another_experiment)).to eq(true)
      end
    end
  end

  describe '#track_experiment_event', :snowplow do
    let(:user) { build(:user) }

    context 'when the experiment is enabled' do
      before do
        stub_experiment(test_experiment: true)
        allow(controller).to receive(:current_user).and_return(user)
      end

      context 'the user is part of the experimental group' do
        before do
          stub_experiment_for_subject(test_experiment: true)
        end

        it 'tracks the event with the right parameters' do
          controller.track_experiment_event(:test_experiment, 'start', 1)

          expect_snowplow_event(
            category: 'Team',
            action: 'start',
            property: 'experimental_group',
            value: 1,
            user: user
          )
        end
      end

      context 'the user is part of the control group' do
        before do
          stub_experiment_for_subject(test_experiment: false)
        end

        it 'tracks the event with the right parameters' do
          controller.track_experiment_event(:test_experiment, 'start', 1)

          expect_snowplow_event(
            category: 'Team',
            action: 'start',
            property: 'control_group',
            value: 1,
            user: user
          )
        end
      end

      context 'do not track is disabled' do
        before do
          request.headers['DNT'] = '0'
        end

        it 'does track the event' do
          controller.track_experiment_event(:test_experiment, 'start', 1)

          expect_snowplow_event(
            category: 'Team',
            action: 'start',
            property: 'control_group',
            value: 1,
            user: user
          )
        end
      end

      context 'do not track enabled' do
        before do
          request.headers['DNT'] = '1'
        end

        it 'does not track the event' do
          controller.track_experiment_event(:test_experiment, 'start', 1)

          expect_no_snowplow_event
        end
      end

      context 'subject is provided' do
        before do
          stub_experiment_for_subject(test_experiment: false)
        end

        it "provides the subject's hashed global_id as label" do
          experiment_subject = double(:subject, to_global_id: 'abc')
          allow(Gitlab::Experimentation).to receive(:valid_subject_for_rollout_strategy?).and_return(true)

          controller.track_experiment_event(:test_experiment, 'start', 1, subject: experiment_subject)

          expect_snowplow_event(
            category: 'Team',
            action: 'start',
            property: 'control_group',
            value: 1,
            label: Digest::MD5.hexdigest('abc'),
            user: user
          )
        end

        it "provides the subject's hashed string representation as label" do
          experiment_subject = 'somestring'

          controller.track_experiment_event(:test_experiment, 'start', 1, subject: experiment_subject)

          expect_snowplow_event(
            category: 'Team',
            action: 'start',
            property: 'control_group',
            value: 1,
            label: Digest::MD5.hexdigest('somestring'),
            user: user
          )
        end
      end

      context 'no subject is provided but cookie is set' do
        before do
          get :index
          stub_experiment_for_subject(test_experiment: false)
        end

        it 'uses the experimentation_subject_id as fallback' do
          controller.track_experiment_event(:test_experiment, 'start', 1)

          expect_snowplow_event(
            category: 'Team',
            action: 'start',
            property: 'control_group',
            value: 1,
            label: cookies.permanent.signed[:experimentation_subject_id],
            user: user
          )
        end
      end
    end

    context 'when the experiment is disabled' do
      before do
        stub_experiment(test_experiment: false)
      end

      it 'does not track the event' do
        controller.track_experiment_event(:test_experiment, 'start')

        expect_no_snowplow_event
      end
    end
  end

  describe '#frontend_experimentation_tracking_data' do
    context 'when the experiment is enabled' do
      before do
        stub_experiment(test_experiment: true)
      end

      context 'the user is part of the experimental group' do
        before do
          stub_experiment_for_subject(test_experiment: true)
        end

        it 'pushes the right parameters to gon' do
          controller.frontend_experimentation_tracking_data(:test_experiment, 'start', 'team_id')
          expect(Gon.tracking_data).to eq(
            {
              category: 'Team',
              action: 'start',
              property: 'experimental_group',
              value: 'team_id'
            }
          )
        end
      end

      context 'the user is part of the control group' do
        before do
          stub_experiment_for_subject(test_experiment: false)
        end

        it 'pushes the right parameters to gon' do
          controller.frontend_experimentation_tracking_data(:test_experiment, 'start', 'team_id')
          expect(Gon.tracking_data).to eq(
            {
              category: 'Team',
              action: 'start',
              property: 'control_group',
              value: 'team_id'
            }
          )
        end

        it 'does not send nil value to gon' do
          controller.frontend_experimentation_tracking_data(:test_experiment, 'start')
          expect(Gon.tracking_data).to eq(
            {
              category: 'Team',
              action: 'start',
              property: 'control_group'
            }
          )
        end
      end

      context 'do not track disabled' do
        before do
          request.headers['DNT'] = '0'
        end

        it 'pushes the right parameters to gon' do
          controller.frontend_experimentation_tracking_data(:test_experiment, 'start')

          expect(Gon.tracking_data).to eq(
            {
              category: 'Team',
              action: 'start',
              property: 'control_group'
            }
          )
        end
      end

      context 'do not track enabled' do
        before do
          request.headers['DNT'] = '1'
        end

        it 'does not push data to gon' do
          controller.frontend_experimentation_tracking_data(:test_experiment, 'start')

          expect(Gon.method_defined?(:tracking_data)).to eq(false)
        end
      end
    end

    context 'when the experiment is disabled' do
      before do
        stub_experiment(test_experiment: false)
      end

      it 'does not push data to gon' do
        expect(Gon.method_defined?(:tracking_data)).to eq(false)
        controller.track_experiment_event(:test_experiment, 'start')
      end
    end
  end

  describe '#record_experiment_user' do
    let(:user) { build(:user) }
    let(:context) { { a: 42 } }

    context 'when the experiment is enabled' do
      before do
        stub_experiment(test_experiment: true)
        allow(controller).to receive(:current_user).and_return(user)
      end

      context 'the user is part of the experimental group' do
        before do
          stub_experiment_for_subject(test_experiment: true)
        end

        it 'calls add_user on the Experiment model' do
          expect(::Experiment).to receive(:add_user).with(:test_experiment, :experimental, user, context)

          controller.record_experiment_user(:test_experiment, context)
        end

        context 'with a cookie based rollout strategy' do
          it 'calls tracking_group with a nil subject' do
            expect(controller).to receive(:tracking_group).with(:test_experiment, nil, subject: nil).and_return(:experimental)
            allow(::Experiment).to receive(:add_user).with(:test_experiment, :experimental, user, context)

            controller.record_experiment_user(:test_experiment, context)
          end
        end

        context 'with a user based rollout strategy' do
          let(:rollout_strategy) { :user }

          it 'calls tracking_group with a user subject' do
            expect(controller).to receive(:tracking_group).with(:test_experiment, nil, subject: user).and_return(:experimental)
            allow(::Experiment).to receive(:add_user).with(:test_experiment, :experimental, user, context)

            controller.record_experiment_user(:test_experiment, context)
          end
        end
      end

      context 'the user is part of the control group' do
        before do
          stub_experiment_for_subject(test_experiment: false)
        end

        it 'calls add_user on the Experiment model' do
          expect(::Experiment).to receive(:add_user).with(:test_experiment, :control, user, context)

          controller.record_experiment_user(:test_experiment, context)
        end
      end
    end

    context 'when the experiment is disabled' do
      before do
        stub_experiment(test_experiment: false)
        allow(controller).to receive(:current_user).and_return(user)
      end

      it 'does not call add_user on the Experiment model' do
        expect(::Experiment).not_to receive(:add_user)

        controller.record_experiment_user(:test_experiment, context)
      end
    end

    context 'when there is no current_user' do
      before do
        stub_experiment(test_experiment: true)
      end

      it 'does not call add_user on the Experiment model' do
        expect(::Experiment).not_to receive(:add_user)

        controller.record_experiment_user(:test_experiment, context)
      end
    end

    context 'do not track' do
      before do
        stub_experiment(test_experiment: true)
        allow(controller).to receive(:current_user).and_return(user)
      end

      context 'is disabled' do
        before do
          request.headers['DNT'] = '0'
          stub_experiment_for_subject(test_experiment: false)
        end

        it 'calls add_user on the Experiment model' do
          expect(::Experiment).to receive(:add_user).with(:test_experiment, :control, user, context)

          controller.record_experiment_user(:test_experiment, context)
        end
      end

      context 'is enabled' do
        before do
          request.headers['DNT'] = '1'
        end

        it 'does not call add_user on the Experiment model' do
          expect(::Experiment).not_to receive(:add_user)

          controller.record_experiment_user(:test_experiment, context)
        end
      end
    end
  end

  describe '#record_experiment_group' do
    let(:group) { 'a group object' }
    let(:experiment_key) { :some_experiment_key }
    let(:dnt_enabled) { false }
    let(:experiment_active) { true }
    let(:rollout_strategy) { :whatever }
    let(:variant) { 'variant' }

    before do
      allow(controller).to receive(:dnt_enabled?).and_return(dnt_enabled)
      allow(::Gitlab::Experimentation).to receive(:active?).and_return(experiment_active)
      allow(::Gitlab::Experimentation).to receive(:rollout_strategy).and_return(rollout_strategy)
      allow(controller).to receive(:tracking_group).and_return(variant)
      allow(::Experiment).to receive(:add_group)
    end

    subject(:record_experiment_group) { controller.record_experiment_group(experiment_key, group) }

    shared_examples 'exits early without recording' do
      it 'returns early without recording the group as an ExperimentSubject' do
        expect(::Experiment).not_to receive(:add_group)
        record_experiment_group
      end
    end

    shared_examples 'calls tracking_group' do |using_cookie_rollout|
      it "calls tracking_group with #{using_cookie_rollout ? 'a nil' : 'the group as the'} subject" do
        expect(controller).to receive(:tracking_group).with(experiment_key, nil, subject: using_cookie_rollout ? nil : group).and_return(variant)
        record_experiment_group
      end
    end

    shared_examples 'records the group' do
      it 'records the group' do
        expect(::Experiment).to receive(:add_group).with(experiment_key, group: group, variant: variant)
        record_experiment_group
      end
    end

    context 'when DNT is enabled' do
      let(:dnt_enabled) { true }

      include_examples 'exits early without recording'
    end

    context 'when the experiment is not active' do
      let(:experiment_active) { false }

      include_examples 'exits early without recording'
    end

    context 'when a nil group is given' do
      let(:group) { nil }

      include_examples 'exits early without recording'
    end

    context 'when the experiment uses a cookie-based rollout strategy' do
      let(:rollout_strategy) { :cookie }

      include_examples 'calls tracking_group', true
      include_examples 'records the group'
    end

    context 'when the experiment uses a non-cookie-based rollout strategy' do
      let(:rollout_strategy) { :group }

      include_examples 'calls tracking_group', false
      include_examples 'records the group'
    end
  end

  describe '#record_experiment_conversion_event' do
    let(:user) { build(:user) }

    before do
      allow(controller).to receive(:dnt_enabled?).and_return(false)
      allow(controller).to receive(:current_user).and_return(user)
      stub_experiment(test_experiment: true)
    end

    subject(:record_conversion_event) do
      controller.record_experiment_conversion_event(:test_experiment)
    end

    it 'records the conversion event for the experiment & user' do
      expect(::Experiment).to receive(:record_conversion_event).with(:test_experiment, user, {})
      record_conversion_event
    end

    shared_examples 'does not record the conversion event' do
      it 'does not record the conversion event' do
        expect(::Experiment).not_to receive(:record_conversion_event)
        record_conversion_event
      end
    end

    context 'when DNT is enabled' do
      before do
        allow(controller).to receive(:dnt_enabled?).and_return(true)
      end

      include_examples 'does not record the conversion event'
    end

    context 'when there is no current user' do
      before do
        allow(controller).to receive(:current_user).and_return(nil)
      end

      include_examples 'does not record the conversion event'
    end

    context 'when the experiment is not enabled' do
      before do
        stub_experiment(test_experiment: false)
      end

      include_examples 'does not record the conversion event'
    end
  end

  describe '#experiment_tracking_category_and_group' do
    let_it_be(:experiment_key) { :test_something }

    subject { controller.experiment_tracking_category_and_group(experiment_key) }

    it 'returns a string with the experiment tracking category & group joined with a ":"' do
      expect(controller).to receive(:tracking_category).with(experiment_key).and_return('Experiment::Category')
      expect(controller).to receive(:tracking_group).with(experiment_key, '_group', subject: nil).and_return('experimental_group')

      expect(subject).to eq('Experiment::Category:experimental_group')
    end
  end
end