File: current_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (155 lines) | stat: -rw-r--r-- 4,460 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Current, feature_category: :cell do
  let_it_be(:current_organization) { create(:organization) }

  after do
    described_class.reset
  end

  describe '.organization=' do
    context 'when organization has not been set yet' do
      where(:value) do
        [nil, ref(:current_organization)]
      end

      with_them do
        it 'assigns the value and locks the organization setter' do
          expect do
            described_class.organization = value
          end.to change { described_class.organization_assigned }.from(nil).to(true)

          expect(described_class.organization).to eq(value)
        end
      end

      it 'pushes organization to the application context' do
        described_class.organization = current_organization

        expect(Gitlab::ApplicationContext.current)
          .to include('meta.organization_id' => current_organization.id)
      end
    end

    context 'when organization has already been set' do
      it 'assigns the value and locks the organization setter' do
        set_value = '_set_value_'

        described_class.organization = set_value

        expect(described_class.organization_assigned).to be(true)
        expect(described_class.organization).to eq(set_value)

        expect do
          described_class.organization = '_new_value_'
        end.to raise_error(
          Current::OrganizationAlreadyAssignedError,
          'Current.organization has already been set in the current thread and should not be set again.'
        )

        expect(described_class.organization).to eq(set_value)
      end

      context 'when not raise outside of dev/test environments' do
        before do
          stub_rails_env('production')
        end

        it 'returns silently without changing value' do
          set_value = '_set_value_'

          described_class.organization = set_value

          expect { described_class.organization = '_new_value_' }.not_to raise_error

          expect(described_class.organization).to eq(set_value)
        end
      end
    end
  end

  describe '.organization_id' do
    subject(:organization_id) { described_class.organization_id }

    context 'when organization is set' do
      before do
        described_class.organization = current_organization
      end

      it 'returns the id of the organization' do
        expect(organization_id).not_to be_nil
        expect(organization_id).to eq(current_organization.id)
      end
    end

    context 'when organization is nil' do
      before do
        described_class.organization = nil
      end

      it 'returns nil' do
        expect(organization_id).to be_nil
      end
    end
  end

  describe '.organization' do
    subject(:assigned_organization) { described_class.organization }

    context 'when organization is not assigned' do
      it 'raises an error' do
        expect { assigned_organization }.to raise_error(
          Current::OrganizationNotAssignedError,
          'Assign an organization to Current.organization before calling it.'
        )
      end

      context 'and environment is production' do
        before do
          stub_rails_env('production')
        end

        it 'returns nil' do
          expect(assigned_organization).to be_nil
        end
      end
    end

    context 'when organization is assigned' do
      let_it_be(:event) { 'fallback_current_organization_to_default' }

      before do
        described_class.organization = current_organization
      end

      it 'returns assigned organization' do
        expect(assigned_organization).to eq(current_organization)
      end

      it_behaves_like 'internal event not tracked'

      context 'when `fallback_organization_used` request store is true', :request_store do
        let(:category) { described_class.name }
        let(:additional_properties) { { label: 'foo' } }

        before do
          allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return('foo')

          Gitlab::SafeRequestStore.write(:fallback_organization_used, true)
        end

        it_behaves_like 'internal event tracking'

        context 'when `track_organization_fallback` flag is disabled' do
          before do
            stub_feature_flags(track_organization_fallback: false)
          end

          it_behaves_like 'internal event not tracked'
        end
      end
    end
  end
end