File: sessions_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 (63 lines) | stat: -rw-r--r-- 2,198 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Sessions', feature_category: :system_access do
  include SessionHelpers

  let_it_be(:user) { create(:user) }

  it_behaves_like 'Base action controller' do
    subject(:request) { get new_user_session_path }
  end

  context 'for authentication', :allow_forgery_protection do
    it 'logout does not require a csrf token' do
      login_as(user)

      post(destroy_user_session_path, headers: { 'X-CSRF-Token' => 'invalid' })

      expect(response).to redirect_to(new_user_session_path)
    end
  end

  context 'when user has pending invitations' do
    it 'accepts the invitations and stores a user location' do
      create(:group_member, :invited, invite_email: user.email)
      member = create(:group_member, :invited, invite_email: user.email)

      post user_session_path(user: { login: user.username, password: user.password })

      expect(response).to redirect_to(group_path(member.source))
    end
  end

  context 'when using two-factor authentication via OTP' do
    let_it_be(:user) { create(:user, :two_factor, :invalid) }
    let(:user_params) { { login: user.username, password: user.password } }

    context 'with an invalid user' do
      it 'raises StandardError when ActiveRecord::RecordInvalid is raised to return 500 instead of 422' do
        otp = user.current_otp

        expect { authenticate_2fa(otp_attempt: otp) }.to raise_error(StandardError)
      end
    end

    context 'with an invalid record other than user' do
      it 'raises ActiveRecord::RecordInvalid for invalid record to return 422f' do
        otp = user.current_otp
        allow_next_instance_of(ActiveRecord::RecordInvalid) do |instance|
          allow(instance).to receive(:record).and_return(nil) # Simulate it's not a user
        end

        expect { authenticate_2fa(otp_attempt: otp) }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end

    def authenticate_2fa(otp_attempt:)
      post(user_session_path(params: { user: user_params })) # First sign-in request for password, second for OTP
      post(user_session_path(params: { user: user_params.merge(otp_attempt: otp_attempt) }))
    end
  end
end