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

124 lines
3.5 KiB
Ruby
Raw Normal View History

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
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
create(user_or_role)
end
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
gitlab_sign_in_with(user, **kwargs)
user
end
def gitlab_sign_in_via(provider, user, uid)
mock_auth_hash(provider, uid, user.email)
visit new_user_session_path
click_link provider
end
# Requires Javascript driver.
def gitlab_sign_out
find(".header-user-dropdown-toggle").click
click_link "Sign out"
expect(page).to have_button('Sign in')
2014-09-02 18:07:02 +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
#
# user - User instance to login with
# remember - Whether or not to check "Remember me" (default: false)
2017-09-10 17:25:29 +05:30
def gitlab_sign_in_with(user, remember: false)
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
fill_in "user_password", with: "12345678"
check 'user_remember_me' if remember
2017-09-10 17:25:29 +05:30
2014-09-02 18:07:02 +05:30
click_button "Sign in"
end
2017-09-10 17:25:29 +05:30
def login_via(provider, user, uid, remember_me: false)
2016-08-24 12:49:21 +05:30
mock_auth_hash(provider, uid, user.email)
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
click_link "oauth-login-#{provider}"
2016-08-24 12:49:21 +05:30
end
def mock_auth_hash(provider, uid, email)
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
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: {
raw_info: {
info: {
name: 'mockuser',
email: email,
image: 'mock_user_thumbnail_url'
}
}
}
})
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:saml]
end
2017-09-10 17:25:29 +05:30
def mock_saml_config
OpenStruct.new(name: 'saml', label: 'saml', args: {
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
2017-09-10 17:25:29 +05:30
def stub_omniauth_saml_config(messages)
set_devise_mapping(context: Rails.application)
Rails.application.routes.disable_clear_and_finalize = true
Rails.application.routes.draw do
post '/users/auth/saml' => 'omniauth_callbacks#saml'
end
allow(Gitlab::OAuth::Provider).to receive_messages(providers: [:saml], config_for: mock_saml_config)
stub_omniauth_setting(messages)
allow_any_instance_of(Object).to receive(:user_saml_omniauth_authorize_path).and_return('/users/auth/saml')
allow_any_instance_of(Object).to receive(:omniauth_authorize_path).with(:user, "saml").and_return('/users/auth/saml')
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
end