omniauth_callbacks_controller_spec.rb 2.3 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

13 14 15
  context 'github' do
    let(:extern_uid) { 'my-uid' }
    let(:provider) { :github }
16

17 18 19 20 21
    it 'allows sign in' do
      post provider

      expect(request.env['warden']).to be_authenticated
    end
22

23 24
    shared_context 'sign_up' do
      let(:user) { double(email: 'new@example.com') }
25

26 27 28
      before do
        stub_omniauth_setting(block_auto_created_users: false)
      end
29 30
    end

31 32
    context 'sign up' do
      include_context 'sign_up'
33

34 35
      it 'is allowed' do
        post provider
36

37 38
        expect(request.env['warden']).to be_authenticated
      end
39 40
    end

41 42 43 44 45 46
    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
47

48 49
      it 'prevents login via POST' do
        post provider
50

51 52
        expect(request.env['warden']).not_to be_authenticated
      end
53

54 55
      it 'shows warning when attempting login' do
        post provider
56

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

61 62 63
      it 'allows linking the disabled provider' do
        user.identities.destroy_all
        sign_in(user)
64

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

68 69
      context 'sign up' do
        include_context 'sign_up'
70

71 72
        it 'is prevented' do
          post provider
73

74 75
          expect(request.env['warden']).not_to be_authenticated
        end
76 77 78
      end
    end
  end
79 80 81 82 83 84 85 86 87 88 89 90 91

  context 'auth0' do
    let(:extern_uid) { '' }
    let(:provider) { :auth0 }

    it 'does not allow sign in without extern_uid' do
      post 'auth0'

      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
  end
92
end