2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Tracking do
|
2019-12-04 20:38:33 +05:30
|
|
|
before do
|
|
|
|
stub_application_setting(snowplow_enabled: true)
|
|
|
|
stub_application_setting(snowplow_collector_hostname: 'gitfoo.com')
|
|
|
|
stub_application_setting(snowplow_cookie_domain: '.gitfoo.com')
|
2019-12-26 22:10:19 +05:30
|
|
|
stub_application_setting(snowplow_app_id: '_abc123_')
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
described_class.instance_variable_set("@snowplow", nil)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
describe '.options' do
|
2019-12-04 20:38:33 +05:30
|
|
|
it 'returns useful client options' do
|
2019-12-26 22:10:19 +05:30
|
|
|
expected_fields = {
|
2019-12-04 20:38:33 +05:30
|
|
|
namespace: 'gl',
|
|
|
|
hostname: 'gitfoo.com',
|
|
|
|
cookieDomain: '.gitfoo.com',
|
|
|
|
appId: '_abc123_',
|
|
|
|
formTracking: true,
|
2020-11-24 15:15:51 +05:30
|
|
|
linkClickTracking: true
|
2019-12-26 22:10:19 +05:30
|
|
|
}
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(subject.options(nil)).to match(expected_fields)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'when feature flag is disabled' do
|
|
|
|
stub_feature_flags(additional_snowplow_tracking: false)
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
expect(subject.options(nil)).to include(
|
2019-12-04 20:38:33 +05:30
|
|
|
formTracking: false,
|
|
|
|
linkClickTracking: false
|
2020-06-23 00:09:42 +05:30
|
|
|
)
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
describe '.event' do
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:namespace) { create(:namespace) }
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
shared_examples 'delegates to destination' do |klass|
|
2021-04-29 21:17:54 +05:30
|
|
|
before do
|
|
|
|
allow_any_instance_of(Gitlab::Tracking::Destinations::Snowplow).to receive(:event)
|
|
|
|
allow_any_instance_of(Gitlab::Tracking::Destinations::ProductAnalytics).to receive(:event)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it "delegates to #{klass} destination" do
|
|
|
|
other_context = double(:context)
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
project = build_stubbed(:project)
|
2021-11-18 22:05:49 +05:30
|
|
|
user = build_stubbed(:user)
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(Gitlab::Tracking::StandardContext)
|
|
|
|
.to receive(:new)
|
2021-04-29 21:17:54 +05:30
|
|
|
.with(project: project, user: user, namespace: namespace, extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
|
2021-03-11 19:13:27 +05:30
|
|
|
.and_call_original
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
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)
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(args[:context].first.to_json[:schema]).to eq(Gitlab::Tracking::StandardContext::GITLAB_STANDARD_SCHEMA_URL)
|
|
|
|
expect(args[:context].last).to eq(other_context)
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5,
|
2021-04-29 21:17:54 +05:30
|
|
|
context: [other_context], project: project, user: user, namespace: namespace,
|
|
|
|
extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::Snowplow
|
|
|
|
it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::ProductAnalytics
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'tracks errors' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception).with(
|
|
|
|
an_instance_of(ContractError),
|
|
|
|
snowplow_category: nil, snowplow_action: 'some_action'
|
|
|
|
)
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
described_class.event(nil, 'some_action')
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|