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

application_experiment_spec.rb « experiments « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 00370a5b7e396ea602b3b2bd70e9ea12cf54c564 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ApplicationExperiment, :experiment, feature_category: :acquisition do
  subject(:application_experiment) { described_class.new('namespaced/stub', **context) }

  let(:context) { {} }
  let(:feature_definition) { { name: 'namespaced_stub', type: 'experiment', default_enabled: false } }

  before do
    stub_feature_flag_definition(:namespaced_stub, feature_definition)

    allow(Gitlab::FIPS).to receive(:enabled?).and_return(true)
    allow(application_experiment).to receive(:enabled?).and_return(true)
  end

  it "registers a default control behavior for anonymous experiments" do
    # This default control behavior is not inherited, intentionally, but it
    # does provide anonymous experiments with a base control behavior to keep
    # them optional there.

    expect(experiment(:example)).to register_behavior(:control).with(nil)
    expect { experiment(:example) {} }.not_to raise_error
  end

  describe "#publish" do
    it "tracks the assignment", :snowplow do
      expect(application_experiment).to receive(:track).with(:assignment)

      application_experiment.publish
    end

    it "adds to the published experiments" do
      # These are surfaced in the client layer by rendering them in the
      # _published_experiments.html.haml partial.
      application_experiment.publish

      expect(described_class.published_experiments['namespaced/stub']).to include(
        experiment: 'namespaced/stub',
        excluded: false,
        key: anything,
        variant: 'control'
      )
    end
  end

  describe "#track", :snowplow do
    let(:fake_context) do
      SnowplowTracker::SelfDescribingJson.new('iglu:com.gitlab/fake/jsonschema/0-0-0', { data: '_data_' })
    end

    it "doesn't track if we shouldn't track" do
      allow(application_experiment).to receive(:should_track?).and_return(false)

      application_experiment.track(:action)

      expect_no_snowplow_event
    end

    it "tracks the event with the expected arguments and merged contexts" do
      application_experiment.track(:action, property: '_property_', context: [fake_context])

      expect_snowplow_event(
        category: 'namespaced/stub',
        action: :action,
        property: '_property_',
        context: [
          {
            schema: 'iglu:com.gitlab/fake/jsonschema/0-0-0',
            data: { data: '_data_' }
          },
          {
            schema: 'iglu:com.gitlab/gitlab_experiment/jsonschema/1-0-0',
            data: {
              experiment: 'namespaced/stub',
              key: '300b002687ba1f68591adb2f45ae67f1e56be05ad55f317cc00f1c4aa38f081a',
              variant: 'control'
            }
          }
        ]
      )
    end

    context "when using known context resources" do
      let(:user) { build(:user, id: non_existing_record_id) }
      let(:project) { build(:project, id: non_existing_record_id) }
      let(:namespace) { build(:namespace, id: non_existing_record_id) }
      let(:group) { build(:group, id: non_existing_record_id) }
      let(:actor) { user }

      let(:context) { { user: user, project: project, namespace: namespace } }

      it "includes those using the gitlab standard context" do
        subject.track(:action)

        expect_snowplow_event(
          category: 'namespaced/stub',
          action: :action,
          user: user,
          project: project,
          namespace: namespace,
          context: an_instance_of(Array)
        )
      end

      it "falls back to using the group key" do
        subject.context(namespace: nil, group: group)

        subject.track(:action)

        expect_snowplow_event(
          category: 'namespaced/stub',
          action: :action,
          user: user,
          project: project,
          namespace: group,
          context: an_instance_of(Array)
        )
      end

      context "with the actor key" do
        it "provides it to the tracking call as the user" do
          subject.context(user: nil, actor: actor)

          subject.track(:action)

          expect_snowplow_event(
            category: 'namespaced/stub',
            action: :action,
            user: actor,
            project: project,
            namespace: namespace,
            context: an_instance_of(Array)
          )
        end

        it "handles when it's not a user record" do
          subject.context(user: nil, actor: nil)

          subject.track(:action)

          expect_snowplow_event(
            category: 'namespaced/stub',
            action: :action,
            project: project,
            namespace: namespace,
            context: an_instance_of(Array)
          )
        end
      end
    end
  end

  describe "#key_for" do
    it "generates FIPS compliant SHA2 hashes" do
      expect(application_experiment.key_for(foo: :bar))
        .to eq('1206febc4d022294fc639d68c2905079898ea4fee99290785b822e5010f1a9d1')
    end

    it "falls back to legacy MD5 when FIPS isn't forced" do
      # Please see https://gitlab.com/gitlab-org/gitlab/-/issues/334590 about
      # why this remains and why it hasn't been prioritized.

      allow(Gitlab::FIPS).to receive(:enabled?).and_return(false)
      expect(application_experiment.key_for(foo: :bar))
        .to eq('6f9ac12afdb9b58c2f19a136d09f9153')
    end
  end

  describe "#process_redirect_url" do
    using RSpec::Parameterized::TableSyntax

    where(:url, :processed_url) do
      'https://about.gitlab.com/'                 | 'https://about.gitlab.com/'
      'https://gitlab.com/'                       | 'https://gitlab.com/'
      'http://docs.gitlab.com'                    | 'http://docs.gitlab.com'
      'https://docs.gitlab.com/some/path?foo=bar' | 'https://docs.gitlab.com/some/path?foo=bar'
      'http://badgitlab.com'                      | nil
      'https://gitlab.com.nefarious.net'          | nil
      'https://unknown.gitlab.com'                | nil
      "https://badplace.com\nhttps://gitlab.com"  | nil
      'https://gitlabbcom'                        | nil
      'https://gitlabbcom/'                       | nil
      'http://gdk.test/foo/bar'                   | 'http://gdk.test/foo/bar'
      'http://localhost:3000/foo/bar'             | 'http://localhost:3000/foo/bar'
    end

    with_them do
      it "returns the url or nil if invalid on SaaS", :saas do
        expect(application_experiment.process_redirect_url(url)).to eq(processed_url)
      end

      it "considers all urls invalid when not on SaaS" do
        expect(application_experiment.process_redirect_url(url)).to be_nil
      end
    end

    it "generates the correct urls based on where the engine was mounted" do
      url = Rails.application.routes.url_helpers.experiment_redirect_url(application_experiment, url: 'https://docs.gitlab.com')
      expect(url).to include("/-/experiment/namespaced%2Fstub:#{application_experiment.context.key}?https://docs.gitlab.com")
    end
  end

  context "when resolving variants" do
    before do
      stub_feature_flags(namespaced_stub: true)
    end

    it "returns an assigned name" do
      application_experiment.variant(:variant1) {}
      application_experiment.variant(:variant2) {}

      expect(application_experiment.assigned.name).to eq(:variant2)
    end
  end

  context "when nesting experiments" do
    before do
      stub_experiments(top: :control, nested: :control)
    end

    it "doesn't raise an exception" do
      expect { experiment(:top) { |e| e.control { experiment(:nested) {} } } }.not_to raise_error
    end

    it "tracks an event", :snowplow do
      experiment(:top) { |e| e.control { experiment(:nested) {} } }

      expect(Gitlab::Tracking).to have_received(:event).with( # rubocop:disable RSpec/ExpectGitlabTracking
        'top',
        :nested,
        hash_including(label: 'nested')
      )
    end
  end

  context "when caching" do
    let(:cache) { Gitlab::Experiment::Configuration.cache }

    before do
      allow(Gitlab::Experiment::Configuration).to receive(:cache).and_call_original

      cache.clear(key: application_experiment.name)

      application_experiment.control {}
      application_experiment.candidate {}
    end

    it "caches the variant determined by the variant resolver" do
      expect(application_experiment.assigned.name).to eq(:candidate) # we should be in the experiment

      application_experiment.run

      expect(application_experiment.cache.read).to eq('candidate')
    end

    it "doesn't cache a variant if we don't explicitly provide one" do
      # by not caching "empty" variants, we effectively create a mostly
      # optimal combination of caching and rollout flexibility. If we cached
      # every control variant assigned, we'd inflate the cache size and
      # wouldn't be able to roll out to subjects that we'd already assigned to
      # the control.
      stub_feature_flags(namespaced_stub: false) # simulate being not rolled out

      expect(application_experiment.assigned.name).to eq(:control) # if we ask, it should be control

      application_experiment.run

      expect(application_experiment.cache.read).to be_nil
    end

    it "caches a control variant if we assign it specifically" do
      # by specifically assigning the control variant here, we're guaranteeing
      # that this context will always get the control variant unless we delete
      # the field from the cache (or clear the entire experiment cache) -- or
      # write code that would specify a different variant.
      application_experiment.run(:control)

      expect(application_experiment.cache.read).to eq('control')
    end

    context "arbitrary attributes" do
      before do
        application_experiment.cache.store.clear(key: application_experiment.name + '_attrs')
      end

      it "sets and gets attributes about an experiment" do
        application_experiment.cache.attr_set(:foo, :bar)

        expect(application_experiment.cache.attr_get(:foo)).to eq('bar')
      end

      it "increments a value for an experiment" do
        expect(application_experiment.cache.attr_get(:foo)).to be_nil

        expect(application_experiment.cache.attr_inc(:foo)).to eq(1)
        expect(application_experiment.cache.attr_inc(:foo)).to eq(2)
      end
    end
  end
end