omniauth_callbacks_controller_spec.rb 3.4 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe OmniauthCallbacksController do
  include LoginHelpers

6
  let(:user) { create(:omniauth_user, extern_uid: extern_uid, provider: provider) }
7 8

  before do
9
    mock_auth_hash(provider.to_s, extern_uid, user.email)
10 11 12
    stub_omniauth_provider(provider, context: request)
  end

T
Tiago Botelho 已提交
13
  context 'when the user is on the last sign in attempt' do
14
    let(:extern_uid) { 'my-uid' }
15

T
Tiago Botelho 已提交
16 17 18
    before do
      user.update(failed_attempts: User.maximum_attempts.pred)
      subject.response = ActionDispatch::Response.new
19
    end
20

T
Tiago Botelho 已提交
21 22 23 24 25 26 27 28
    context 'when using a form based provider' do
      let(:provider) { :ldap }

      it 'locks the user when sign in fails' do
        allow(subject).to receive(:params).and_return(ActionController::Parameters.new(username: user.username))
        request.env['omniauth.error.strategy'] = OmniAuth::Strategies::LDAP.new(nil)

        subject.send(:failure)
29

T
Tiago Botelho 已提交
30
        expect(user.reload).to be_access_locked
31
      end
32 33
    end

T
Tiago Botelho 已提交
34 35
    context 'when using a button based provider' do
      let(:provider) { :github }
36

T
Tiago Botelho 已提交
37 38
      it 'does not lock the user when sign in fails' do
        request.env['omniauth.error.strategy'] = OmniAuth::Strategies::GitHub.new(nil)
39

T
Tiago Botelho 已提交
40 41 42
        subject.send(:failure)

        expect(user.reload).not_to be_access_locked
43
      end
44
    end
T
Tiago Botelho 已提交
45
  end
46

T
Tiago Botelho 已提交
47 48 49 50
  context 'strategies' do
    context 'github' do
      let(:extern_uid) { 'my-uid' }
      let(:provider) { :github }
51

T
Tiago Botelho 已提交
52
      it 'allows sign in' do
53
        post provider
54

T
Tiago Botelho 已提交
55
        expect(request.env['warden']).to be_authenticated
56
      end
57

T
Tiago Botelho 已提交
58 59
      shared_context 'sign_up' do
        let(:user) { double(email: 'new@example.com') }
60

T
Tiago Botelho 已提交
61 62 63
        before do
          stub_omniauth_setting(block_auto_created_users: false)
        end
64
      end
65

T
Tiago Botelho 已提交
66 67 68 69 70
      context 'sign up' do
        include_context 'sign_up'

        it 'is allowed' do
          post provider
71

T
Tiago Botelho 已提交
72 73
          expect(request.env['warden']).to be_authenticated
        end
74
      end
75

T
Tiago Botelho 已提交
76 77 78 79 80 81
      context 'when OAuth is disabled' do
        before do
          stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
          settings = Gitlab::CurrentSettings.current_application_settings
          settings.update(disabled_oauth_sign_in_sources: [provider.to_s])
        end
82

T
Tiago Botelho 已提交
83
        it 'prevents login via POST' do
84
          post provider
85

86 87
          expect(request.env['warden']).not_to be_authenticated
        end
T
Tiago Botelho 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

        it 'shows warning when attempting login' do
          post provider

          expect(response).to redirect_to new_user_session_path
          expect(flash[:alert]).to eq('Signing in using GitHub has been disabled')
        end

        it 'allows linking the disabled provider' do
          user.identities.destroy_all
          sign_in(user)

          expect { post provider }.to change { user.reload.identities.count }.by(1)
        end

        context 'sign up' do
          include_context 'sign_up'

          it 'is prevented' do
            post provider

            expect(request.env['warden']).not_to be_authenticated
          end
        end
112 113
      end
    end
114

T
Tiago Botelho 已提交
115 116 117
    context 'auth0' do
      let(:extern_uid) { '' }
      let(:provider) { :auth0 }
118

T
Tiago Botelho 已提交
119 120
      it 'does not allow sign in without extern_uid' do
        post 'auth0'
121

T
Tiago Botelho 已提交
122 123 124 125
        expect(request.env['warden']).not_to be_authenticated
        expect(response.status).to eq(302)
        expect(controller).to set_flash[:alert].to('Wrong extern UID provided. Make sure Auth0 is configured correctly.')
      end
126 127
    end
  end
128
end