2012-08-23 05:19:40 -04:00
|
|
|
module LoginHelpers
|
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-02-22 10:10:32 -05: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-05 14:51:54 -04:00
|
|
|
gitlab_sign_in_with(@user, **kwargs)
|
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"
|
|
|
|
# check the sign_in button
|
|
|
|
expect(page).to have_button('Sign in')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Logout without JavaScript driver
|
|
|
|
def gitlab_sign_out_direct
|
|
|
|
page.driver.submit :delete, '/users/sign_out', {}
|
|
|
|
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"
|
2012-08-23 05:19:40 -04:00
|
|
|
|
2017-06-05 14:22:37 -04:00
|
|
|
Thread.current[:current_user] = user
|
2016-06-30 15:54:07 -04:00
|
|
|
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'
|
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
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|