omniauth_callbacks_controller_spec.rb 1.7 KB
Newer Older
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
require 'spec_helper'

describe OmniauthCallbacksController do
  include LoginHelpers

  let(:user) { create(:omniauth_user, extern_uid: 'my-uid', provider: provider) }
  let(:provider) { :github }

  before do
    mock_auth_hash(provider.to_s, 'my-uid', user.email)
    stub_omniauth_provider(provider, context: request)
  end

  it 'allows sign in' do
    post provider

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

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

    before do
      stub_omniauth_setting(block_auto_created_users: false)
    end
  end

  context 'sign up' do
    include_context 'sign_up'

    it 'is allowed' do
      post provider

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

  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

    it 'prevents login via POST' do
      post provider

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

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