2012-08-23 09:19:40 +00:00
|
|
|
module LoginHelpers
|
2015-06-02 21:30:21 +00:00
|
|
|
# Internal: Log in as a specific user or a new user of a specific role
|
2012-08-23 09:19:40 +00:00
|
|
|
#
|
2015-06-02 21:30:21 +00:00
|
|
|
# user_or_role - User object, or a role to create (e.g., :admin, :user)
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# # Create a user automatically
|
|
|
|
# login_as(:user)
|
|
|
|
#
|
|
|
|
# # Create an admin automatically
|
|
|
|
# login_as(:admin)
|
|
|
|
#
|
|
|
|
# # Provide an existing User record
|
|
|
|
# user = create(:user)
|
|
|
|
# login_as(user)
|
|
|
|
def login_as(user_or_role)
|
|
|
|
if user_or_role.kind_of?(User)
|
|
|
|
@user = user_or_role
|
|
|
|
else
|
|
|
|
@user = create(user_or_role)
|
|
|
|
end
|
2013-06-22 15:57:34 +00:00
|
|
|
|
2012-08-23 09:19:40 +00:00
|
|
|
login_with(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Internal: Login as the specified user
|
|
|
|
#
|
2016-05-31 02:17:26 +00:00
|
|
|
# user - User instance to login with
|
|
|
|
# remember - Whether or not to check "Remember me" (default: false)
|
|
|
|
def login_with(user, remember: false)
|
2012-08-23 09:19:40 +00:00
|
|
|
visit new_user_session_path
|
2013-03-25 14:10:14 +00:00
|
|
|
fill_in "user_login", with: user.email
|
2013-11-25 18:07:55 +00:00
|
|
|
fill_in "user_password", with: "12345678"
|
2016-05-31 02:17:26 +00:00
|
|
|
check 'user_remember_me' if remember
|
2016-09-28 15:13:42 +00:00
|
|
|
click_button "Sign in"
|
2013-05-30 23:16:49 +00:00
|
|
|
Thread.current[:current_user] = user
|
2012-08-23 09:19:40 +00:00
|
|
|
end
|
|
|
|
|
2016-06-30 19:54:07 +00:00
|
|
|
def login_via(provider, user, uid)
|
|
|
|
mock_auth_hash(provider, uid, user.email)
|
|
|
|
visit new_user_session_path
|
|
|
|
click_link provider
|
|
|
|
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 21:34:34 +00:00
|
|
|
},
|
|
|
|
extra: {
|
|
|
|
raw_info: {
|
|
|
|
info: {
|
|
|
|
name: 'mockuser',
|
|
|
|
email: email,
|
|
|
|
image: 'mock_user_thumbnail_url'
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 19:54:07 +00:00
|
|
|
}
|
|
|
|
})
|
2016-07-11 17:41:02 +00:00
|
|
|
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:saml]
|
2016-06-30 19:54:07 +00:00
|
|
|
end
|
|
|
|
|
2014-07-04 07:26:59 +00:00
|
|
|
# Requires Javascript driver.
|
2012-08-23 09:19:40 +00:00
|
|
|
def logout
|
2016-06-27 13:31:19 +00:00
|
|
|
find(".header-user-dropdown-toggle").click
|
|
|
|
click_link "Sign out"
|
2016-09-08 17:45:33 +00:00
|
|
|
expect(page).to have_content('Signed out successfully')
|
2012-08-23 09:19:40 +00:00
|
|
|
end
|
2015-06-20 04:18:24 +00:00
|
|
|
|
|
|
|
# Logout without JavaScript driver
|
|
|
|
def logout_direct
|
|
|
|
page.driver.submit :delete, '/users/sign_out', {}
|
|
|
|
end
|
2015-09-15 13:42:02 +00:00
|
|
|
|
|
|
|
def skip_ci_admin_auth
|
|
|
|
allow_any_instance_of(Ci::Admin::ApplicationController).to receive_messages(authenticate_admin!: true)
|
|
|
|
end
|
2012-08-23 09:19:40 +00:00
|
|
|
end
|