summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/tracking_spec.rb
blob: 99ca402616a35919848b42d3ee57f62788a29820 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::Tracking do
  include StubENV

  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

  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

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

    shared_examples 'delegates to destination' do |klass|
      before do
        allow_any_instance_of(Gitlab::Tracking::Destinations::Snowplow).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: project, user: user, namespace: namespace, 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.event('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

    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 'delegates to destination', Gitlab::Tracking::Destinations::Snowplow
    end

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

      it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::SnowplowMicro
    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 'dispatchs 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
    before do
      allow(Rails.env).to receive(:development?).and_return(true)
    end

    it 'returns true when snowplow_micro is enabled' do
      stub_config(snowplow_micro: { enabled: true })

      expect(described_class).to be_snowplow_micro_enabled
    end

    it 'returns false when snowplow_micro is disabled' do
      stub_config(snowplow_micro: { enabled: false })

      expect(described_class).not_to be_snowplow_micro_enabled
    end

    it 'returns false when snowplow_micro is not configured' do
      allow(Gitlab.config).to receive(:snowplow_micro).and_raise(Settingslogic::MissingSetting)

      expect(described_class).not_to be_snowplow_micro_enabled
    end
  end
end