2021-01-15 03:10:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Tracking::StandardContext do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:namespace) { create(:namespace) }
|
2021-10-06 15:11:48 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
2021-01-15 03:10:30 +00:00
|
|
|
|
|
|
|
let(:snowplow_context) { subject.to_context }
|
|
|
|
|
|
|
|
describe '#to_context' do
|
2021-02-11 21:09:00 +00:00
|
|
|
context 'environment' do
|
|
|
|
shared_examples 'contains environment' do |expected_environment|
|
|
|
|
it 'contains environment' do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :environment)).to eq(expected_environment)
|
2021-01-25 15:09:00 +00:00
|
|
|
end
|
2021-02-11 21:09:00 +00:00
|
|
|
end
|
2021-01-25 15:09:00 +00:00
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
context 'development or test' do
|
|
|
|
include_examples 'contains environment', 'development'
|
|
|
|
end
|
2021-01-25 15:09:00 +00:00
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
context 'staging' do
|
|
|
|
before do
|
2021-06-14 03:09:53 +00:00
|
|
|
stub_config_setting(url: Gitlab::Saas.staging_com_url)
|
2021-01-25 15:09:00 +00:00
|
|
|
end
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
include_examples 'contains environment', 'staging'
|
|
|
|
end
|
2021-01-25 15:09:00 +00:00
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
context 'production' do
|
|
|
|
before do
|
2021-06-14 03:09:53 +00:00
|
|
|
stub_config_setting(url: Gitlab::Saas.com_url)
|
2021-01-15 03:10:30 +00:00
|
|
|
end
|
2021-01-28 21:09:04 +00:00
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
include_examples 'contains environment', 'production'
|
2021-01-28 21:09:04 +00:00
|
|
|
end
|
2021-02-20 00:10:55 +00:00
|
|
|
|
|
|
|
context 'org' do
|
|
|
|
before do
|
2021-06-14 03:09:53 +00:00
|
|
|
stub_config_setting(url: Gitlab::Saas.dev_url)
|
2021-02-20 00:10:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'contains environment', 'org'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'other self-managed instance' do
|
|
|
|
before do
|
|
|
|
stub_rails_env('production')
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'contains environment', 'self-managed'
|
|
|
|
end
|
2021-01-15 03:10:30 +00:00
|
|
|
end
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
it 'contains source' do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :source)).to eq(described_class::GITLAB_RAILS_SOURCE)
|
|
|
|
end
|
|
|
|
|
2022-01-17 18:16:07 +00:00
|
|
|
it 'contains context_generated_at timestamp', :freeze_time do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :context_generated_at)).to eq(Time.current)
|
|
|
|
end
|
|
|
|
|
2021-04-26 21:10:25 +00:00
|
|
|
context 'plan' do
|
|
|
|
context 'when namespace is not available' do
|
|
|
|
it 'is nil' do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :plan)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace is available' do
|
|
|
|
subject { described_class.new(namespace: create(:namespace)) }
|
|
|
|
|
|
|
|
it 'contains plan name' do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :plan)).to eq(Plan::DEFAULT)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-15 03:10:30 +00:00
|
|
|
context 'with extra data' do
|
2021-04-01 12:08:56 +00:00
|
|
|
subject { described_class.new(extra_key_1: 'extra value 1', extra_key_2: 'extra value 2') }
|
2021-01-15 03:10:30 +00:00
|
|
|
|
2021-04-01 12:08:56 +00:00
|
|
|
it 'includes extra data in `extra` hash' do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :extra)).to eq(extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without extra data' do
|
|
|
|
it 'contains an empty `extra` hash' do
|
|
|
|
expect(snowplow_context.to_json.dig(:data, :extra)).to be_empty
|
2021-01-15 03:10:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-07 15:08:12 +00:00
|
|
|
context 'with incorrect argument type' do
|
|
|
|
context 'when standard_context_type_check FF is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(standard_context_type_check: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(project: create(:group)) }
|
|
|
|
|
|
|
|
it 'does not call `track_and_raise_for_dev_exception`' do
|
|
|
|
expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception)
|
|
|
|
snowplow_context
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when standard_context_type_check FF is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(standard_context_type_check: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(project: create(:group)) }
|
|
|
|
|
|
|
|
it 'does call `track_and_raise_for_dev_exception`' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
|
|
|
|
snowplow_context
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-06 15:11:48 +00:00
|
|
|
it 'contains user id' do
|
|
|
|
expect(snowplow_context.to_json[:data].keys).to include(:user_id)
|
2021-08-20 15:10:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains namespace and project ids' do
|
|
|
|
expect(snowplow_context.to_json[:data].keys).to include(:project_id, :namespace_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts just project id as integer' do
|
|
|
|
expect { described_class.new(project: 1).to_context }.not_to raise_error
|
|
|
|
end
|
2021-01-15 03:10:30 +00:00
|
|
|
end
|
|
|
|
end
|