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

tracking_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4621353207123736dea2f69869a9306bfccac49c (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::Tracking, feature_category: :application_instrumentation do
  include StubENV
  using RSpec::Parameterized::TableSyntax

  before do
    stub_application_setting(snowplow_enabled: true)
    stub_application_setting(snowplow_collector_hostname: 'gitfoo.com')
    stub_application_setting(snowplow_cookie_domain: '.gitfoo.com')
    stub_application_setting(snowplow_app_id: '_abc123_')

    described_class.instance_variable_set(:@tracker, nil)
  end

  after do
    described_class.instance_variable_set(:@tracker, nil)
  end

  it { is_expected.to delegate_method(:flush).to(:tracker) }

  describe '.options' do
    shared_examples 'delegates to destination' do |klass|
      before do
        allow_next_instance_of(klass) do |instance|
          allow(instance).to receive(:options).and_call_original
        end
      end

      it "delegates to #{klass} destination" do
        expect_next_instance_of(klass) do |instance|
          expect(instance).to receive(:options)
        end

        subject.options(nil)
      end
    end

    shared_examples 'delegates to SnowplowMicro destination with proper options' do
      it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::SnowplowMicro

      it 'returns useful client options' do
        expected_fields = {
          namespace: 'gl',
          hostname: 'localhost:9090',
          cookieDomain: '.gitlab.com',
          appId: '_abc123_',
          protocol: 'http',
          port: 9090,
          forceSecureTracker: false,
          formTracking: true,
          linkClickTracking: true
        }

        expect(subject.options(nil)).to match(expected_fields)
      end
    end

    context 'when destination is Snowplow' do
      it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::Snowplow

      it 'returns useful client options' do
        expected_fields = {
          namespace: 'gl',
          hostname: 'gitfoo.com',
          cookieDomain: '.gitfoo.com',
          appId: '_abc123_',
          formTracking: true,
          linkClickTracking: true
        }

        expect(subject.options(nil)).to match(expected_fields)
      end
    end

    context 'when destination is SnowplowMicro' do
      before do
        allow(Rails.env).to receive(:development?).and_return(true)
      end

      context "enabled with yml config" do
        let(:snowplow_micro_settings) do
          {
            enabled: true,
            address: "localhost:9090"
          }
        end

        before do
          stub_config(snowplow_micro: snowplow_micro_settings)
        end

        it_behaves_like 'delegates to SnowplowMicro destination with proper options'
      end
    end

    it 'when feature flag is disabled' do
      stub_feature_flags(additional_snowplow_tracking: false)

      expect(subject.options(nil)).to include(
        formTracking: false,
        linkClickTracking: false
      )
    end
  end

  context 'event tracking' do
    let(:namespace) { create(:namespace) }

    shared_examples 'rescued error raised by destination class' do
      it 'rescues error' do
        error = StandardError.new("something went wrong")
        allow_any_instance_of(destination_class).to receive(:event).and_raise(error)

        expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
                                           .with(
                                             error,
                                             snowplow_category: category,
                                             snowplow_action: action
                                           )

        expect { tracking_method }.not_to raise_error
      end
    end

    shared_examples 'delegates to destination' do |klass, method|
      before do
        allow_any_instance_of(klass).to receive(:event)
      end

      it "delegates to #{klass} destination" do
        other_context = double(:context)

        project = build_stubbed(:project)
        user = build_stubbed(:user)

        expect(Gitlab::Tracking::StandardContext)
          .to receive(:new)
                .with(project_id: project.id, user_id: user.id, namespace_id: namespace.id, plan_name: namespace.actual_plan_name, extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
                .and_call_original

        expect_any_instance_of(klass).to receive(:event) do |_, category, action, args|
          expect(category).to eq('category')
          expect(action).to eq('action')
          expect(args[:label]).to eq('label')
          expect(args[:property]).to eq('property')
          expect(args[:value]).to eq(1.5)
          expect(args[:context].length).to eq(2)
          expect(args[:context].first.to_json[:schema]).to eq(Gitlab::Tracking::StandardContext::GITLAB_STANDARD_SCHEMA_URL)
          expect(args[:context].last).to eq(other_context)
        end

        described_class.method(method).call('category', 'action',
          label: 'label',
          property: 'property',
          value: 1.5,
          context: [other_context],
          project: project,
          user: user,
          namespace: namespace,
          extra_key_1: 'extra value 1',
          extra_key_2: 'extra value 2'
        )
      end
    end

    describe '.database_event' do
      context 'when the action is not passed in as a string' do
        it 'allows symbols' do
          expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)

          described_class.database_event('category', :some_action)
        end

        it 'allows nil' do
          expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)

          described_class.database_event('category', nil)
        end

        it 'allows integers' do
          expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)

          described_class.database_event('category', 1)
        end
      end

      it_behaves_like 'rescued error raised by destination class' do
        let(:category) { 'Issue' }
        let(:action) { 'created' }
        let(:destination_class) { Gitlab::Tracking::Destinations::DatabaseEventsSnowplow }

        subject(:tracking_method) { described_class.database_event(category, action) }
      end

      it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::DatabaseEventsSnowplow, :database_event
    end

    describe '.event' do
      context 'when the action is not passed in as a string' do
        it 'allows symbols' do
          expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)

          described_class.event('category', :some_action)
        end

        it 'allows nil' do
          expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)

          described_class.event('category', nil)
        end

        it 'allows integers' do
          expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)

          described_class.event('category', 1)
        end
      end

      context 'when destination is Snowplow' do
        before do
          allow(Rails.env).to receive(:development?).and_return(true)
        end

        it_behaves_like 'rescued error raised by destination class' do
          let(:category) { 'category' }
          let(:action) { 'action' }
          let(:destination_class) { Gitlab::Tracking::Destinations::Snowplow }

          subject(:tracking_method) { described_class.event(category, action) }
        end

        it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::Snowplow, :event
      end

      context 'when destination is SnowplowMicro' do
        before do
          allow(Rails.env).to receive(:development?).and_return(true)
        end

        it_behaves_like 'rescued error raised by destination class' do
          let(:category) { 'category' }
          let(:action) { 'action' }
          let(:destination_class) { Gitlab::Tracking::Destinations::Snowplow }

          subject(:tracking_method) { described_class.event(category, action) }
        end

        it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::SnowplowMicro, :event
      end
    end
  end

  describe '.definition' do
    let(:namespace) { create(:namespace) }

    let_it_be(:definition_action) { 'definition_action' }
    let_it_be(:definition_category) { 'definition_category' }
    let_it_be(:label_description) { 'definition label description' }
    let_it_be(:test_definition) { { 'category': definition_category, 'action': definition_action } }

    before do
      allow_next_instance_of(described_class) do |instance|
        allow(instance).to receive(:event)
      end
      allow_next_instance_of(Gitlab::Tracking::Destinations::Snowplow) do |instance|
        allow(instance).to receive(:event)
      end
      allow(YAML).to receive(:load_file).with(Rails.root.join('config/events/filename.yml')).and_return(test_definition)
    end

    it 'dispatches the data to .event' do
      project = build_stubbed(:project)
      user = build_stubbed(:user)

      expect(described_class).to receive(:event) do |category, action, args|
        expect(category).to eq(definition_category)
        expect(action).to eq(definition_action)
        expect(args[:label]).to eq('label')
        expect(args[:property]).to eq('...')
        expect(args[:project]).to eq(project)
        expect(args[:user]).to eq(user)
        expect(args[:namespace]).to eq(namespace)
        expect(args[:extra_key_1]).to eq('extra value 1')
      end

      described_class.definition('filename',
        category: nil,
        action: nil,
        label: 'label',
        property: '...',
        project: project,
        user: user,
        namespace: namespace,
        extra_key_1: 'extra value 1')
    end
  end

  describe 'snowplow_micro_enabled?' do
    where(:development?, :micro_verification_enabled?, :snowplow_micro_enabled, :result) do
      true  | true  | true  | true
      true  | true  | false | false
      false | true  | true  | true
      false | true  | false | false
      false | false | true  | false
      false | false | false | false
      true  | false | true  | true
      true  | false | false | false
    end

    with_them do
      before do
        allow(Rails.env).to receive(:development?).and_return(development?)
        allow(described_class).to receive(:micro_verification_enabled?).and_return(micro_verification_enabled?)
        stub_config(snowplow_micro: { enabled: snowplow_micro_enabled })
      end

      subject { described_class.snowplow_micro_enabled? }

      it { is_expected.to be(result) }
    end

    it 'returns false when snowplow_micro is not configured' do
      allow(Rails.env).to receive(:development?).and_return(true)
      allow(Gitlab.config).to receive(:snowplow_micro).and_raise(GitlabSettings::MissingSetting)

      expect(described_class).not_to be_snowplow_micro_enabled
    end
  end

  describe '.micro_verification_enabled?' do
    where(:verify_tracking, :result) do
      nil     | false
      'true'  | true
      'false' | false
      '0'     | false
      '1'     | true
    end

    with_them do
      before do
        stub_env('VERIFY_TRACKING', verify_tracking)
      end

      subject { described_class.micro_verification_enabled? }

      it { is_expected.to be(result) }
    end
  end

  describe 'tracker' do
    it 'returns a SnowPlowMicro instance in development' do
      allow(Rails.env).to receive(:development?).and_return(true)

      expect(described_class.tracker).to be_an_instance_of(Gitlab::Tracking::Destinations::SnowplowMicro)
    end

    it 'returns a SnowPlow instance when not in development' do
      allow(Rails.env).to receive(:development?).and_return(false)

      expect(described_class.tracker).to be_an_instance_of(Gitlab::Tracking::Destinations::Snowplow)
    end
  end
end