File: application_controller_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 (253 lines) | stat: -rw-r--r-- 7,583 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
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 ApplicationController, type: :request, feature_category: :shared do
  let_it_be_with_reload(:user) { create(:user) }

  it_behaves_like 'Base action controller' do
    before do
      sign_in(user)
    end

    subject(:request) { get root_path }
  end

  it 'does not send Link header', :use_clean_rails_redis_caching do
    sign_in(user)

    get root_path

    expect(response.headers['Link']).to be_nil
  end

  describe 'session expiration' do
    context 'when user is authenticated' do
      it 'does not set the expire_after option' do
        sign_in(user)

        get root_path

        expect(request.env['rack.session.options'][:expire_after]).to be_nil
      end
    end

    context 'when user is unauthenticated' do
      it 'sets the expire_after option' do
        get root_path

        expect(request.env['rack.session.options'][:expire_after]).to eq(
          Settings.gitlab['unauthenticated_session_expire_delay']
        )
      end
    end
  end

  describe 'unknown route' do
    # This spec targets CI environment with precompiled assets to trigger
    # Sprockets' `File.binread` and find encoding issues.
    #
    # See https://gitlab.com/gitlab-com/gl-infra/production/-/issues/17627#note_1782396646
    it 'returns 404 even when locale contains UTF-8 chars' do
      user.update!(preferred_language: 'ZH-cn')

      sign_in(user)

      get "/some/undefined/path"

      expect(response).to have_gitlab_http_status(:not_found)
      expect(response.body.encoding.name).to eq('UTF-8')
    end
  end

  describe 'User-Agent header' do
    before do
      sign_in(user)

      get root_path, headers: { 'User-Agent': user_agent }
    end

    context 'when missing' do
      let(:user_agent) { nil }

      it { expect(response).to have_gitlab_http_status(:ok) }
    end

    context 'when correct' do
      let(:user_agent) { 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)' }

      it { expect(response).to have_gitlab_http_status(:ok) }
    end

    context 'when too long' do
      let(:user_agent) { 'a' * 3000 }

      it { expect(response).to have_gitlab_http_status(:forbidden) }
    end
  end

  describe 'HTTP Router headers' do
    before do
      sign_in(user)
    end

    let(:headers) do
      {
        'X-Gitlab-Http-Router-Rule-Action' => 'classify',
        'X-Gitlab-Http-Router-Rule-Type' => 'FIRST_CELL'
      }
    end

    subject(:perform_request) do
      get root_path, headers: headers
    end

    it 'includes the HTTP ROUTER headers in ApplicationContext' do
      expect_next_instance_of(RootController) do |controller|
        expect(controller).to receive(:index).and_wrap_original do |m, *args|
          m.call(*args)

          expect(Gitlab::ApplicationContext.current).to include(
            'meta.http_router_rule_action' => 'classify',
            'meta.http_router_rule_type' => 'FIRST_CELL'
          )
        end
      end

      perform_request
    end

    context 'for counters' do
      let(:http_router_rule_counter) { Gitlab::Metrics.counter(:gitlab_http_router_rule_total, 'description') }

      context 'when the headers are present' do
        context 'for classify action' do
          it 'increments the counter' do
            expect { perform_request }.to change {
              http_router_rule_counter.get(rule_action: 'classify', rule_type: 'FIRST_CELL')
            }.by(1)
          end
        end

        context 'for proxy action' do
          let(:headers) do
            {
              'X-Gitlab-Http-Router-Rule-Action' => 'proxy'
            }
          end

          it 'increments the counter' do
            expect { perform_request }.to change {
              http_router_rule_counter.get(rule_action: 'proxy', rule_type: nil)
            }.by(1)
          end
        end

        context 'for invalid action and type' do
          let(:headers) do
            {
              'X-Gitlab-Http-Router-Rule-Action' => 'invalid',
              'X-Gitlab-Http-Router-Rule-Type' => 'invalid'
            }
          end

          it 'does not increment the counter' do
            expect { perform_request }.to change {
              http_router_rule_counter.get(rule_action: 'invalid', rule_type: 'invalid')
            }.by(0)
          end
        end

        context 'when action is not present and type is present' do
          let(:headers) do
            {
              'X-Gitlab-Http-Router-Rule-Type' => 'FIRST_CELL'
            }
          end

          it 'does not increment the counter' do
            expect { perform_request }.to change {
              http_router_rule_counter.get(rule_action: nil, rule_type: 'FIRST_CELL')
            }.by(0)
          end
        end
      end

      context 'when the headers are absent' do
        let(:headers) { {} }

        it 'does not increment the counter' do
          expect { perform_request }.to change {
            http_router_rule_counter.get(rule_action: nil, rule_type: nil)
          }.by(0)
        end
      end
    end
  end

  describe 'static context middleware', feature_category: :error_budgets do
    # to check the middleware tests, go to spec/lib/gitlab/middleware/static_context_middleware_spec.rb

    let(:expected_context) do
      { 'meta.feature_category' => 'groups_and_projects',
        'meta.caller_id' => 'Dashboard::GroupsController#index' }
    end

    context 'when action is successfully called' do
      it 'pushes static context to current context' do
        context = {}
        allow_next_instance_of(Dashboard::GroupsController) do |_controller|
          context.merge!(Gitlab::ApplicationContext.current.to_h)
        end

        get '/dashboard/groups' # randomly picked route

        expect(response).to have_gitlab_http_status(:found)
        expect(context).to include expected_context
      end
    end

    context 'when an exception is thrown before action' do
      it 'pushes static context to current context before controller callbacks' do
        context = {}
        unexpected_error = 'boom 💣💥'

        allow_next_instance_of(Dashboard::GroupsController) do |controller|
          # picking up a random before_action method to raise an "unexpected" exception
          allow(controller).to receive(:authenticate_user!).and_raise(unexpected_error)

          context.merge!(Gitlab::ApplicationContext.current.to_h)
        end

        expect { get '/dashboard/groups' }.to raise_error(unexpected_error)
        expect(context).to include expected_context
      end
    end

    context 'when controller overrides feature_category with nil' do
      it 'ignores nil feature category override' do
        context = {}

        allow_next_instance_of(Projects::NotesController) do |controller|
          # mimicking a bug overriding feature_category with nil
          allow(controller).to receive(:feature_category).and_return(nil)

          context.merge!(Gitlab::ApplicationContext.current.to_h)
        end

        project = create(:project, :public)
        project_snippet = create(:project_snippet, project: project)
        create(:note_on_project_snippet, project: project, noteable: project_snippet)
        # picking a route targeting a controller that overrides feature_category
        get project_noteable_notes_path(
          project,
          target_type: 'project_snippet',
          target_id: project_snippet.id,
          html: true
        )

        expect(context).to include({ 'meta.feature_category' => 'team_planning' })
      end
    end
  end
end