2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe 'OAuth Login', :js, :allow_forgery_protection do
|
2017-09-10 17:25:29 +05:30
|
|
|
include DeviseHelpers
|
|
|
|
|
|
|
|
def enter_code(code)
|
|
|
|
fill_in 'user_otp_attempt', with: code
|
|
|
|
click_button 'Verify code'
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_omniauth_config(provider)
|
|
|
|
OmniAuth.config.add_mock(provider, OmniAuth::AuthHash.new(provider: provider.to_s, uid: "12345"))
|
2018-03-17 18:26:18 +05:30
|
|
|
stub_omniauth_provider(provider)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
providers = [:github, :twitter, :bitbucket, :gitlab, :google_oauth2,
|
2019-07-31 22:56:46 +05:30
|
|
|
:facebook, :cas3, :auth0, :authentiq, :salesforce]
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
around do |example|
|
2019-09-30 21:07:59 +05:30
|
|
|
with_omniauth_full_host { example.run }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 23:59:55 +05:30
|
|
|
def login_with_provider(provider, enter_two_factor: false, additional_info: {})
|
|
|
|
login_via(provider.to_s, user, uid, remember_me: remember_me, additional_info: additional_info)
|
2018-10-15 14:42:47 +05:30
|
|
|
enter_code(user.current_otp) if enter_two_factor
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
providers.each do |provider|
|
|
|
|
context "when the user logs in using the #{provider} provider" do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:uid) { 'my-uid' }
|
|
|
|
let(:remember_me) { false }
|
|
|
|
let(:user) { create(:omniauth_user, extern_uid: uid, provider: provider.to_s) }
|
|
|
|
let(:two_factor_user) { create(:omniauth_user, :two_factor, extern_uid: uid, provider: provider.to_s) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2019-09-30 23:59:55 +05:30
|
|
|
provider == :salesforce ? let(:additional_info) { { extra: { email_verified: true } } } : let(:additional_info) { {} }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
stub_omniauth_config(provider)
|
2019-10-12 21:52:04 +05:30
|
|
|
expect(ActiveSession).to receive(:cleanup).with(user).at_least(:once).and_call_original
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when two-factor authentication is disabled' do
|
|
|
|
it 'logs the user in' do
|
2019-09-30 23:59:55 +05:30
|
|
|
login_with_provider(provider, additional_info: additional_info)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(current_path).to eq root_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when two-factor authentication is enabled' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:user) { two_factor_user }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'logs the user in' do
|
2019-09-30 23:59:55 +05:30
|
|
|
login_with_provider(provider, additional_info: additional_info, enter_two_factor: true)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(current_path).to eq root_path
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it 'when bypass-two-factor is enabled' do
|
|
|
|
allow(Gitlab.config.omniauth).to receive_messages(allow_bypass_two_factor: true)
|
|
|
|
login_via(provider.to_s, user, uid, remember_me: false, additional_info: additional_info)
|
|
|
|
expect(current_path).to eq root_path
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when bypass-two-factor is disabled' do
|
|
|
|
allow(Gitlab.config.omniauth).to receive_messages(allow_bypass_two_factor: false)
|
|
|
|
login_with_provider(provider, enter_two_factor: true, additional_info: additional_info)
|
|
|
|
expect(current_path).to eq root_path
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when "remember me" is checked' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:remember_me) { true }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when two-factor authentication is disabled' do
|
|
|
|
it 'remembers the user after a browser restart' do
|
2019-09-30 23:59:55 +05:30
|
|
|
login_with_provider(provider, additional_info: additional_info)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
clear_browser_session
|
|
|
|
|
|
|
|
visit(root_path)
|
|
|
|
expect(current_path).to eq root_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when two-factor authentication is enabled' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:user) { two_factor_user }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'remembers the user after a browser restart' do
|
2019-09-30 23:59:55 +05:30
|
|
|
login_with_provider(provider, enter_two_factor: true, additional_info: additional_info)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
clear_browser_session
|
|
|
|
|
|
|
|
visit(root_path)
|
|
|
|
expect(current_path).to eq root_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when "remember me" is not checked' do
|
|
|
|
context 'when two-factor authentication is disabled' do
|
|
|
|
it 'does not remember the user after a browser restart' do
|
2019-09-30 23:59:55 +05:30
|
|
|
login_with_provider(provider, additional_info: additional_info)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
clear_browser_session
|
|
|
|
|
|
|
|
visit(root_path)
|
|
|
|
expect(current_path).to eq new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when two-factor authentication is enabled' do
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:user) { two_factor_user }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not remember the user after a browser restart' do
|
2019-09-30 23:59:55 +05:30
|
|
|
login_with_provider(provider, enter_two_factor: true, additional_info: additional_info)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
clear_browser_session
|
|
|
|
|
|
|
|
visit(root_path)
|
|
|
|
expect(current_path).to eq new_user_session_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|