debian-mirror-gitlab/spec/controllers/omniauth_callbacks_controller_spec.rb

164 lines
5 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
require 'spec_helper'
2018-11-08 19:23:39 +05:30
describe OmniauthCallbacksController, type: :controller do
2018-03-17 18:26:18 +05:30
include LoginHelpers
2018-11-08 19:23:39 +05:30
describe 'omniauth' do
let(:user) { create(:omniauth_user, extern_uid: extern_uid, provider: provider) }
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
before do
2018-11-08 19:23:39 +05:30
mock_auth_hash(provider.to_s, extern_uid, user.email)
stub_omniauth_provider(provider, context: request)
2018-03-26 14:24:53 +05:30
end
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
context 'when the user is on the last sign in attempt' do
let(:extern_uid) { 'my-uid' }
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
before do
user.update(failed_attempts: User.maximum_attempts.pred)
subject.response = ActionDispatch::Response.new
2018-03-26 14:24:53 +05:30
end
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
context 'when using a form based provider' do
let(:provider) { :ldap }
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
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)
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
subject.send(:failure)
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
expect(user.reload).to be_access_locked
end
2018-03-26 14:24:53 +05:30
end
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
context 'when using a button based provider' do
let(:provider) { :github }
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
it 'does not lock the user when sign in fails' do
request.env['omniauth.error.strategy'] = OmniAuth::Strategies::GitHub.new(nil)
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
subject.send(:failure)
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
expect(user.reload).not_to be_access_locked
2018-05-09 12:01:36 +05:30
end
2018-03-26 14:24:53 +05:30
end
2018-11-08 19:23:39 +05:30
end
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
context 'strategies' do
context 'github' do
let(:extern_uid) { 'my-uid' }
let(:provider) { :github }
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
it 'allows sign in' do
2018-05-09 12:01:36 +05:30
post provider
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
expect(request.env['warden']).to be_authenticated
end
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
shared_context 'sign_up' do
let(:user) { double(email: 'new@example.com') }
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
before do
stub_omniauth_setting(block_auto_created_users: false)
end
2018-03-26 14:24:53 +05:30
end
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
context 'sign up' do
include_context 'sign_up'
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
it 'is allowed' do
post provider
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
expect(request.env['warden']).to be_authenticated
end
2018-05-09 12:01:36 +05:30
end
2018-11-08 19:23:39 +05:30
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
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
it 'prevents login via POST' do
2018-05-09 12:01:36 +05:30
post provider
expect(request.env['warden']).not_to be_authenticated
end
2018-11-08 19:23:39 +05:30
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
2018-11-20 20:47:30 +05:30
user.identities.destroy_all # rubocop: disable DestroyAll
2018-11-08 19:23:39 +05:30
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
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.')
2018-05-09 12:01:36 +05:30
end
2018-03-17 18:26:18 +05:30
end
end
2018-11-08 19:23:39 +05:30
end
describe '#saml' do
let(:user) { create(:omniauth_user, :two_factor, extern_uid: 'my-uid', provider: 'saml') }
let(:mock_saml_response) { File.read('spec/fixtures/authentication/saml_response.xml') }
let(:saml_config) { mock_saml_config_with_upstream_two_factor_authn_contexts }
before do
stub_omniauth_saml_config({ enabled: true, auto_link_saml_user: true, allow_single_sign_on: ['saml'],
providers: [saml_config] })
mock_auth_hash('saml', 'my-uid', user.email, mock_saml_response)
request.env["devise.mapping"] = Devise.mappings[:user]
request.env['omniauth.auth'] = Rails.application.env_config['omniauth.auth']
post :saml, params: { SAMLResponse: mock_saml_response }
end
2018-03-26 14:24:53 +05:30
2018-11-08 19:23:39 +05:30
context 'when worth two factors' do
let(:mock_saml_response) do
File.read('spec/fixtures/authentication/saml_response.xml')
.gsub('urn:oasis:names:tc:SAML:2.0:ac:classes:Password', 'urn:oasis:names:tc:SAML:2.0:ac:classes:SecondFactorIGTOKEN')
end
2018-03-26 14:24:53 +05:30
2018-11-08 19:23:39 +05:30
it 'expects user to be signed_in' do
expect(request.env['warden']).to be_authenticated
end
end
2018-03-26 14:24:53 +05:30
2018-11-08 19:23:39 +05:30
context 'when not worth two factors' do
it 'expects user to provide second factor' do
expect(response).to render_template('devise/sessions/two_factor')
2018-05-09 12:01:36 +05:30
expect(request.env['warden']).not_to be_authenticated
end
2018-03-26 14:24:53 +05:30
end
end
2018-03-17 18:26:18 +05:30
end