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
|
|
|
|
# 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 11:57:34 -04:00
|
|
|
|
2012-08-23 05:19:40 -04:00
|
|
|
login_with(@user)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Internal: Login as the specified user
|
|
|
|
#
|
|
|
|
# user - User instance to login with
|
|
|
|
def login_with(user)
|
|
|
|
visit new_user_session_path
|
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"
|
2012-08-23 05:19:40 -04:00
|
|
|
click_button "Sign in"
|
2013-05-30 19:16:49 -04:00
|
|
|
Thread.current[:current_user] = user
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|
|
|
|
|
2014-07-04 03:26:59 -04:00
|
|
|
# Requires Javascript driver.
|
2012-08-23 05:19:40 -04:00
|
|
|
def logout
|
2014-10-01 18:21:29 -04:00
|
|
|
find(:css, ".fa.fa-sign-out").click
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|
2015-06-20 00:18:24 -04:00
|
|
|
|
|
|
|
# Logout without JavaScript driver
|
|
|
|
def logout_direct
|
|
|
|
page.driver.submit :delete, '/users/sign_out', {}
|
|
|
|
end
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|