2017-08-03 10:47:11 -04:00
|
|
|
require_relative 'devise_helpers'
|
|
|
|
|
2012-08-23 05:19:40 -04:00
|
|
|
module LoginHelpers
|
2017-07-09 04:51:59 -04:00
|
|
|
include DeviseHelpers
|
|
|
|
|
2017-10-16 04:37:53 -04:00
|
|
|
# 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
|
|
|
|
|
2017-11-02 07:02:51 -04:00
|
|
|
# Overriding Devise::Test::IntegrationHelpers#sign_out to clear @current_user.
|
|
|
|
def sign_out(resource_or_scope)
|
|
|
|
super
|
|
|
|
|
|
|
|
@current_user = nil
|
|
|
|
end
|
|
|
|
|
2015-06-02 17:30:21 -04:00
|
|
|
# Internal: Log in as a specific user or a new user of a specific role
|
2012-08-23 05:19:40 -04:00
|
|
|
#
|
2015-06-02 17:30:21 -04:00
|
|
|
# user_or_role - User object, or a role to create (e.g., :admin, :user)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# # Create a user automatically
|
2017-06-05 14:22:37 -04:00
|
|
|
# gitlab_sign_in(:user)
|
2015-06-02 17:30:21 -04:00
|
|
|
#
|
|
|
|
# # Create an admin automatically
|
2017-06-05 14:22:37 -04:00
|
|
|
# gitlab_sign_in(:admin)
|
2015-06-02 17:30:21 -04:00
|
|
|
#
|
|
|
|
# # Provide an existing User record
|
|
|
|
# user = create(:user)
|
2017-06-05 14:22:37 -04:00
|
|
|
# gitlab_sign_in(user)
|
2017-06-05 14:51:54 -04:00
|
|
|
def gitlab_sign_in(user_or_role, **kwargs)
|
2017-06-25 16:19:06 -04:00
|
|
|
user =
|
2017-02-22 12:25:50 -05:00
|
|
|
if user_or_role.is_a?(User)
|
2017-02-22 10:10:32 -05:00
|
|
|
user_or_role
|
|
|
|
else
|
|
|
|
create(user_or_role)
|
|
|
|
end
|
2013-06-22 11:57:34 -04:00
|
|
|
|
2017-06-25 16:19:06 -04:00
|
|
|
gitlab_sign_in_with(user, **kwargs)
|
|
|
|
|
2017-10-16 04:37:53 -04:00
|
|
|
@current_user = user
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|
|
|
|
|
2017-06-05 14:22:37 -04:00
|
|
|
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"
|
2017-11-02 07:02:51 -04:00
|
|
|
@current_user = nil
|
2017-06-05 14:22:37 -04:00
|
|
|
|
2017-06-23 14:20:07 -04:00
|
|
|
expect(page).to have_button('Sign in')
|
2017-06-05 14:22:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Private: Login as the specified user
|
2012-08-23 05:19:40 -04:00
|
|
|
#
|
2016-05-30 22:17:26 -04:00
|
|
|
# user - User instance to login with
|
|
|
|
# remember - Whether or not to check "Remember me" (default: false)
|
2017-06-05 14:22:37 -04:00
|
|
|
def gitlab_sign_in_with(user, remember: false)
|
2012-08-23 05:19:40 -04:00
|
|
|
visit new_user_session_path
|
2017-06-05 14:22:37 -04:00
|
|
|
|
2013-03-25 10:10:14 -04:00
|
|
|
fill_in "user_login", with: user.email
|
2013-11-25 13:07:55 -05:00
|
|
|
fill_in "user_password", with: "12345678"
|
2016-05-30 22:17:26 -04:00
|
|
|
check 'user_remember_me' if remember
|
2017-06-05 14:22:37 -04:00
|
|
|
|
2016-09-28 11:13:42 -04:00
|
|
|
click_button "Sign in"
|
2016-06-30 15:54:07 -04:00
|
|
|
end
|
|
|
|
|
2017-06-15 00:40:47 -04:00
|
|
|
def login_via(provider, user, uid, remember_me: false)
|
Add integration tests around OAuth login.
- There was previously a test for `saml` login in `login_spec`, but this didn't
seem to be passing. A lot of things didn't seem right here, and I suspect that
this test hasn't been running. I'll investigate this further.
- It took almost a whole working day to figure out this line:
OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(request['REQUEST_PATH'], '') }
As always, it's obvious in retrospect, but it took some digging to figure out
tests were failing and returning 404s during the callback phase.
- Test all OAuth providers - github, twitter, bitbucket, gitlab, google, and facebook
2017-06-14 00:30:07 -04:00
|
|
|
mock_auth_hash(provider, uid, user.email)
|
|
|
|
visit new_user_session_path
|
|
|
|
expect(page).to have_content('Sign in with')
|
2017-06-15 00:40:47 -04:00
|
|
|
|
2017-07-18 09:45:11 -04:00
|
|
|
check 'remember_me' if remember_me
|
2017-06-15 00:40:47 -04:00
|
|
|
|
Add integration tests around OAuth login.
- There was previously a test for `saml` login in `login_spec`, but this didn't
seem to be passing. A lot of things didn't seem right here, and I suspect that
this test hasn't been running. I'll investigate this further.
- It took almost a whole working day to figure out this line:
OmniAuth.config.full_host = ->(request) { request['REQUEST_URI'].sub(request['REQUEST_PATH'], '') }
As always, it's obvious in retrospect, but it took some digging to figure out
tests were failing and returning 404s during the callback phase.
- Test all OAuth providers - github, twitter, bitbucket, gitlab, google, and facebook
2017-06-14 00:30:07 -04:00
|
|
|
click_link "oauth-login-#{provider}"
|
|
|
|
end
|
|
|
|
|
2016-06-30 15:54:07 -04:00
|
|
|
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'
|
2016-07-05 17:34:34 -04:00
|
|
|
},
|
|
|
|
extra: {
|
|
|
|
raw_info: {
|
|
|
|
info: {
|
|
|
|
name: 'mockuser',
|
|
|
|
email: email,
|
|
|
|
image: 'mock_user_thumbnail_url'
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 15:54:07 -04:00
|
|
|
}
|
|
|
|
})
|
2016-07-11 13:41:02 -04:00
|
|
|
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:saml]
|
2016-06-30 15:54:07 -04:00
|
|
|
end
|
2017-06-12 12:13:22 -04:00
|
|
|
|
|
|
|
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'
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-01-09 11:47:31 -05:00
|
|
|
def stub_omniauth_provider(provider, context: Rails.application)
|
|
|
|
env = env_from_context(context)
|
|
|
|
|
|
|
|
set_devise_mapping(context: context)
|
|
|
|
env['omniauth.auth'] = OmniAuth.config.mock_auth[provider]
|
|
|
|
end
|
|
|
|
|
2017-06-12 12:13:22 -04:00
|
|
|
def stub_omniauth_saml_config(messages)
|
2017-07-09 04:51:59 -04:00
|
|
|
set_devise_mapping(context: Rails.application)
|
2017-06-12 12:13:22 -04:00
|
|
|
Rails.application.routes.disable_clear_and_finalize = true
|
|
|
|
Rails.application.routes.draw do
|
|
|
|
post '/users/auth/saml' => 'omniauth_callbacks#saml'
|
|
|
|
end
|
2018-02-23 07:10:39 -05:00
|
|
|
allow(Gitlab::Auth::OAuth::Provider).to receive_messages(providers: [:saml], config_for: mock_saml_config)
|
2017-06-12 12:13:22 -04:00
|
|
|
stub_omniauth_setting(messages)
|
2017-07-03 15:37:37 -04:00
|
|
|
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')
|
2017-06-12 12:13:22 -04:00
|
|
|
end
|
2017-10-17 09:20:07 -04:00
|
|
|
|
|
|
|
def stub_omniauth_config(messages)
|
|
|
|
allow(Gitlab.config.omniauth).to receive_messages(messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_basic_saml_config
|
2018-02-23 07:10:39 -05:00
|
|
|
allow(Gitlab::Auth::Saml::Config).to receive_messages({ options: { name: 'saml', args: {} } })
|
2017-10-17 09:20:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_saml_group_config(groups)
|
2018-02-23 07:10:39 -05:00
|
|
|
allow(Gitlab::Auth::Saml::Config).to receive_messages({ options: { name: 'saml', groups_attribute: 'groups', external_groups: groups, args: {} } })
|
2017-10-17 09:20:07 -04:00
|
|
|
end
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|