debian-mirror-gitlab/spec/support/helpers/login_helpers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

274 lines
8.6 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require_relative 'devise_helpers'
2014-09-02 18:07:02 +05:30
module LoginHelpers
2017-09-10 17:25:29 +05:30
include DeviseHelpers
2018-03-17 18:26:18 +05:30
# Overriding Devise::Test::IntegrationHelpers#sign_in to store @current_user
# since we may need it in LiveDebugger#live_debug.
def sign_in(resource, scope: nil)
super
@current_user = resource
end
# Overriding Devise::Test::IntegrationHelpers#sign_out to clear @current_user.
def sign_out(resource_or_scope)
super
@current_user = nil
end
2015-09-11 14:41:01 +05:30
# Internal: Log in as a specific user or a new user of a specific role
2014-09-02 18:07:02 +05:30
#
2015-09-11 14:41:01 +05:30
# user_or_role - User object, or a role to create (e.g., :admin, :user)
#
# Examples:
#
# # Create a user automatically
2017-09-10 17:25:29 +05:30
# gitlab_sign_in(:user)
2015-09-11 14:41:01 +05:30
#
# # Create an admin automatically
2017-09-10 17:25:29 +05:30
# gitlab_sign_in(:admin)
2015-09-11 14:41:01 +05:30
#
# # Provide an existing User record
# user = create(:user)
2017-09-10 17:25:29 +05:30
# gitlab_sign_in(user)
def gitlab_sign_in(user_or_role, **kwargs)
user =
2017-08-17 22:00:37 +05:30
if user_or_role.is_a?(User)
user_or_role
else
2020-10-24 23:57:45 +05:30
create(user_or_role) # rubocop:disable Rails/SaveBang
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
gitlab_sign_in_with(user, **kwargs)
2018-03-17 18:26:18 +05:30
@current_user = user
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
def gitlab_enable_admin_mode_sign_in(user)
visit new_admin_session_path
2020-04-08 14:13:33 +05:30
fill_in 'user_password', with: user.password
2019-12-26 22:10:19 +05:30
click_button 'Enter Admin Mode'
2022-09-01 20:07:04 +05:30
wait_for_requests
2019-12-21 20:55:43 +05:30
end
2018-11-08 19:23:39 +05:30
def gitlab_sign_in_via(provider, user, uid, saml_response = nil)
2019-07-07 11:18:12 +05:30
mock_auth_hash_with_saml_xml(provider, uid, user.email, saml_response)
2017-09-10 17:25:29 +05:30
visit new_user_session_path
2020-06-23 00:09:42 +05:30
click_button provider
2017-09-10 17:25:29 +05:30
end
2020-04-08 14:13:33 +05:30
def gitlab_enable_admin_mode_sign_in_via(provider, user, uid, saml_response = nil)
mock_auth_hash_with_saml_xml(provider, uid, user.email, saml_response)
visit new_admin_session_path
2020-06-23 00:09:42 +05:30
click_button provider
2020-04-08 14:13:33 +05:30
end
2017-09-10 17:25:29 +05:30
# Requires Javascript driver.
def gitlab_sign_out
find(".header-user-dropdown-toggle").click
click_link "Sign out"
2018-03-17 18:26:18 +05:30
@current_user = nil
2017-09-10 17:25:29 +05:30
expect(page).to have_button('Sign in')
2014-09-02 18:07:02 +05:30
end
2020-04-08 14:13:33 +05:30
# Requires Javascript driver.
def gitlab_disable_admin_mode
2021-09-04 01:27:46 +05:30
open_top_nav
within_top_nav do
click_on 'Leave Admin Mode'
end
2020-04-08 14:13:33 +05:30
end
2017-09-10 17:25:29 +05:30
private
# Private: Login as the specified user
2014-09-02 18:07:02 +05:30
#
2021-10-27 15:23:28 +05:30
# user - User instance to login with
# remember - Whether or not to check "Remember me" (default: false)
2021-10-27 15:23:28 +05:30
# two_factor_auth - If two-factor authentication is enabled (default: false)
2022-08-27 11:52:29 +05:30
# password - password to attempt to login with (default: user.password)
2022-04-01 21:47:47 +05:30
def gitlab_sign_in_with(user, remember: false, two_factor_auth: false, password: nil)
2014-09-02 18:07:02 +05:30
visit new_user_session_path
2017-09-10 17:25:29 +05:30
2014-09-02 18:07:02 +05:30
fill_in "user_login", with: user.email
2022-08-27 11:52:29 +05:30
fill_in "user_password", with: (password || user.password)
check 'user_remember_me' if remember
2017-09-10 17:25:29 +05:30
2023-03-17 16:20:25 +05:30
wait_for_all_requests
2022-06-21 17:19:12 +05:30
find('[data-testid="sign-in-button"]:enabled').click
2021-10-27 15:23:28 +05:30
if two_factor_auth
fill_in "user_otp_attempt", with: user.reload.current_otp
click_button "Verify code"
end
2014-09-02 18:07:02 +05:30
end
2019-09-30 23:59:55 +05:30
def login_via(provider, user, uid, remember_me: false, additional_info: {})
mock_auth_hash(provider, uid, user.email, additional_info: additional_info)
2016-08-24 12:49:21 +05:30
visit new_user_session_path
2017-09-10 17:25:29 +05:30
expect(page).to have_content('Sign in with')
check 'remember_me' if remember_me
2020-06-23 00:09:42 +05:30
click_button "oauth-login-#{provider}"
2016-08-24 12:49:21 +05:30
end
2022-11-25 23:54:43 +05:30
def sign_in_using_ldap!(user, ldap_tab, ldap_name)
visit new_user_session_path
click_link ldap_tab
fill_in 'username', with: user.username
fill_in 'password', with: user.password
within("##{ldap_name}") do
click_button 'Sign in'
end
end
2022-07-23 23:45:48 +05:30
def register_via(provider, uid, email, additional_info: {})
mock_auth_hash(provider, uid, email, additional_info: additional_info)
visit new_user_registration_path
2022-10-11 01:57:18 +05:30
expect(page).to have_content('Create an account using').or(have_content('Register with'))
2022-07-23 23:45:48 +05:30
click_link_or_button "oauth-login-#{provider}"
end
2019-09-30 21:07:59 +05:30
def fake_successful_u2f_authentication
allow(U2fRegistration).to receive(:authenticate).and_return(true)
FakeU2fDevice.new(page, nil).fake_u2f_authentication
end
2020-11-24 15:15:51 +05:30
def fake_successful_webauthn_authentication
allow_any_instance_of(Webauthn::AuthenticateService).to receive(:execute).and_return(true)
FakeWebauthnDevice.new(page, nil).fake_webauthn_authentication
end
2019-07-07 11:18:12 +05:30
def mock_auth_hash_with_saml_xml(provider, uid, email, saml_response)
response_object = { document: saml_xml(saml_response) }
mock_auth_hash(provider, uid, email, response_object: response_object)
end
2019-09-30 23:59:55 +05:30
def configure_mock_auth(provider, uid, email, response_object: nil, additional_info: {})
2016-08-24 12:49:21 +05:30
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
2019-09-30 23:59:55 +05:30
2016-08-24 12:49:21 +05:30
OmniAuth.config.mock_auth[provider.to_sym] = OmniAuth::AuthHash.new({
provider: provider,
uid: uid,
info: {
name: 'mockuser',
email: email,
image: 'mock_user_thumbnail_url'
},
credentials: {
token: 'mock_token',
secret: 'mock_secret'
},
extra: {
2021-02-22 17:27:13 +05:30
raw_info: OneLogin::RubySaml::Attributes.new(
{
info: {
name: 'mockuser',
email: email,
image: 'mock_user_thumbnail_url'
}
2016-08-24 12:49:21 +05:30
}
2021-02-22 17:27:13 +05:30
),
2019-07-07 11:18:12 +05:30
response_object: response_object
2016-08-24 12:49:21 +05:30
}
2019-09-30 23:59:55 +05:30
}).merge(additional_info) { |_, old_hash, new_hash| old_hash.merge(new_hash) }
2019-09-30 21:07:59 +05:30
end
2019-09-30 23:59:55 +05:30
def mock_auth_hash(provider, uid, email, additional_info: {}, response_object: nil)
configure_mock_auth(provider, uid, email, additional_info: additional_info, response_object: response_object)
2019-09-30 21:07:59 +05:30
2019-07-31 22:56:46 +05:30
original_env_config_omniauth_auth = Rails.application.env_config['omniauth.auth']
2018-10-15 14:42:47 +05:30
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[provider.to_sym]
2019-07-31 22:56:46 +05:30
original_env_config_omniauth_auth
2016-08-24 12:49:21 +05:30
end
2018-11-08 19:23:39 +05:30
def saml_xml(raw_saml_response)
return '' if raw_saml_response.blank?
XMLSecurity::SignedDocument.new(raw_saml_response, [])
end
2017-09-10 17:25:29 +05:30
def mock_saml_config
2022-04-04 11:22:00 +05:30
ActiveSupport::InheritableOptions.new(name: 'saml', label: 'saml', args: {
2017-09-10 17:25:29 +05:30
assertion_consumer_service_url: 'https://localhost:3443/users/auth/saml/callback',
idp_cert_fingerprint: '26:43:2C:47:AF:F0:6B:D0:07:9C:AD:A3:74:FE:5D:94:5F:4E:9E:52',
idp_sso_target_url: 'https://idp.example.com/sso/saml',
issuer: 'https://localhost:3443/',
name_identifier_format: 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient'
})
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
2018-11-08 19:23:39 +05:30
def mock_saml_config_with_upstream_two_factor_authn_contexts
config = mock_saml_config
config.args[:upstream_two_factor_authn_contexts] = %w(urn:oasis:names:tc:SAML:2.0:ac:classes:CertificateProtectedTransport
urn:oasis:names:tc:SAML:2.0:ac:classes:SecondFactorOTPSMS
urn:oasis:names:tc:SAML:2.0:ac:classes:SecondFactorIGTOKEN)
config
end
2018-03-17 18:26:18 +05:30
def stub_omniauth_provider(provider, context: Rails.application)
env = env_from_context(context)
set_devise_mapping(context: context)
2018-10-15 14:42:47 +05:30
env['omniauth.auth'] = OmniAuth.config.mock_auth[provider.to_sym]
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
def stub_omniauth_failure(strategy, message_key, exception = nil)
env = @request.env
env['omniauth.error'] = exception
env['omniauth.error.type'] = message_key.to_sym
env['omniauth.error.strategy'] = strategy
end
2021-02-22 17:27:13 +05:30
def stub_omniauth_saml_config(context: Rails.application, **messages)
2018-11-08 19:23:39 +05:30
set_devise_mapping(context: context)
routes = Rails.application.routes
routes.disable_clear_and_finalize = true
routes.formatter.clear
routes.draw do
2017-09-10 17:25:29 +05:30
post '/users/auth/saml' => 'omniauth_callbacks#saml'
end
2018-11-08 19:23:39 +05:30
saml_config = messages.key?(:providers) ? messages[:providers].first : mock_saml_config
allow(Gitlab::Auth::OAuth::Provider).to receive_messages(providers: [:saml], config_for: saml_config)
2017-09-10 17:25:29 +05:30
stub_omniauth_setting(messages)
2018-05-09 12:01:36 +05:30
stub_saml_authorize_path_helpers
end
def stub_saml_authorize_path_helpers
2018-11-08 19:23:39 +05:30
allow_any_instance_of(ActionDispatch::Routing::RoutesProxy)
.to receive(:user_saml_omniauth_authorize_path)
.and_return('/users/auth/saml')
allow(Devise::OmniAuth::UrlHelpers)
.to receive(:omniauth_authorize_path)
.with(:user, "saml")
.and_return('/users/auth/saml')
2015-09-11 14:41:01 +05:30
end
2018-03-17 18:26:18 +05:30
def stub_omniauth_config(messages)
allow(Gitlab.config.omniauth).to receive_messages(messages)
end
def stub_basic_saml_config
2018-03-27 19:54:05 +05:30
allow(Gitlab::Auth::Saml::Config).to receive_messages({ options: { name: 'saml', args: {} } })
2018-03-17 18:26:18 +05:30
end
def stub_saml_group_config(groups)
2018-03-27 19:54:05 +05:30
allow(Gitlab::Auth::Saml::Config).to receive_messages({ options: { name: 'saml', groups_attribute: 'groups', external_groups: groups, args: {} } })
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
LoginHelpers.prepend_mod_with('LoginHelpers')