2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Ldap::OmniauthCallbacksController do
|
2018-10-15 14:42:47 +05:30
|
|
|
include_context 'Ldap::OmniauthCallbacksController'
|
|
|
|
|
|
|
|
it 'allows sign in' do
|
|
|
|
post provider
|
|
|
|
|
|
|
|
expect(request.env['warden']).to be_authenticated
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
context 'with sign in prevented' do
|
|
|
|
let(:ldap_settings) { ldap_setting_defaults.merge(prevent_ldap_sign_in: true) }
|
|
|
|
|
|
|
|
it 'does not allow sign in' do
|
|
|
|
expect { post provider }.to raise_error(ActionController::UrlGenerationError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
it 'respects remember me checkbox' do
|
|
|
|
expect do
|
2019-02-15 15:39:39 +05:30
|
|
|
post provider, params: { remember_me: '1' }
|
2018-10-15 14:42:47 +05:30
|
|
|
end.to change { user.reload.remember_created_at }.from(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with 2FA' do
|
|
|
|
let(:user) { create(:omniauth_user, :two_factor_via_otp, extern_uid: uid, provider: provider) }
|
|
|
|
|
|
|
|
it 'passes remember_me to the Devise view' do
|
2019-02-15 15:39:39 +05:30
|
|
|
post provider, params: { remember_me: '1' }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
expect(assigns[:user].remember_me).to eq '1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'access denied' do
|
|
|
|
let(:valid_login?) { false }
|
|
|
|
|
|
|
|
it 'warns the user' do
|
|
|
|
post provider
|
|
|
|
|
|
|
|
expect(flash[:alert]).to match(/Access denied for your LDAP account*/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't authenticate user" do
|
|
|
|
post provider
|
|
|
|
|
|
|
|
expect(request.env['warden']).not_to be_authenticated
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'sign up' do
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:user) { double(email: +'new@example.com') }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_omniauth_setting(block_auto_created_users: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is allowed' do
|
|
|
|
post provider
|
|
|
|
|
|
|
|
expect(request.env['warden']).to be_authenticated
|
|
|
|
end
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe 'enable admin mode' do
|
|
|
|
include_context 'custom session'
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in user
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a regular user' do
|
|
|
|
it 'cannot be enabled' do
|
|
|
|
reauthenticate_and_check_admin_mode(expected_admin_mode: false)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an admin user' do
|
|
|
|
let(:user) { create(:omniauth_user, :admin, extern_uid: uid, provider: provider) }
|
|
|
|
|
|
|
|
context 'when requested first' do
|
|
|
|
before do
|
|
|
|
subject.current_user_mode.request_admin_mode!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be enabled' do
|
|
|
|
reauthenticate_and_check_admin_mode(expected_admin_mode: true)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(admin_root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not requested first' do
|
|
|
|
it 'cannot be enabled' do
|
|
|
|
reauthenticate_and_check_admin_mode(expected_admin_mode: false)
|
|
|
|
|
|
|
|
expect(response).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reauthenticate_and_check_admin_mode(expected_admin_mode:)
|
|
|
|
# Initially admin mode disabled
|
|
|
|
expect(subject.current_user_mode.admin_mode?).to be(false)
|
|
|
|
|
|
|
|
# Trigger OmniAuth admin mode flow and expect admin mode status
|
|
|
|
post provider
|
|
|
|
|
|
|
|
expect(request.env['warden']).to be_authenticated
|
|
|
|
expect(subject.current_user_mode.admin_mode?).to be(expected_admin_mode)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|