summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/tracking/standard_context_spec.rb
blob: b12a0310dacf192b2a1d64b697561ba464740eb6 (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
# 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) }

  let(:snowplow_context) { subject.to_context }

  describe '#to_context' do
    context 'default fields' do
      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)
          end
        end

        context 'development or test' do
          include_examples 'contains environment', 'development'
        end

        context 'staging' do
          before do
            allow(Gitlab).to receive(:staging?).and_return(true)
          end

          include_examples 'contains environment', 'staging'
        end

        context 'production' do
          before do
            allow(Gitlab).to receive(:com_and_canary?).and_return(true)
          end

          include_examples 'contains environment', 'production'
        end
      end

      it 'contains source' do
        expect(snowplow_context.to_json.dig(:data, :source)).to eq(described_class::GITLAB_RAILS_SOURCE)
      end
    end

    context 'with extra data' do
      subject { described_class.new(foo: 'bar') }

      it 'creates a Snowplow context with the given data' do
        expect(snowplow_context.to_json.dig(:data, :foo)).to eq('bar')
      end
    end

    context 'with namespace' do
      subject { described_class.new(namespace: namespace) }

      it 'creates a Snowplow context without namespace and project' do
        expect(snowplow_context.to_json.dig(:data, :namespace_id)).to be_nil
        expect(snowplow_context.to_json.dig(:data, :project_id)).to be_nil
      end
    end

    context 'with project' do
      subject { described_class.new(project: project) }

      it 'creates a Snowplow context without namespace and project' do
        expect(snowplow_context.to_json.dig(:data, :namespace_id)).to be_nil
        expect(snowplow_context.to_json.dig(:data, :project_id)).to be_nil
      end
    end

    context 'with project and namespace' do
      subject { described_class.new(namespace: namespace, project: project) }

      it 'creates a Snowplow context without namespace and project' do
        expect(snowplow_context.to_json.dig(:data, :namespace_id)).to be_nil
        expect(snowplow_context.to_json.dig(:data, :project_id)).to be_nil
      end
    end
  end
end