Created
December 8, 2010 19:31
-
-
Save mattwynne/733772 to your computer and use it in GitHub Desktop.
Cucumber support module for helping with authentication of users.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module UserHelper | |
attr_accessor :current_user | |
class Credentials < Struct.new(:email, :password) | |
def self.parse(arg) | |
return arg if arg.is_a?(Credentials) | |
new(arg) | |
end | |
def initialize(credentials) | |
self.email, self.password = credentials.split('/') | |
end | |
end | |
def sign_up_with(credentials) | |
credentials = Credentials.parse(credentials) | |
self.current_user = Factory(:user, | |
:email => credentials.email, | |
:password => credentials.password, | |
:password_confirmation => credentials.password) | |
# hack the credentials into the user object so we can retrieve them later | |
current_user.class_eval do | |
attr_accessor :credentials | |
def password | |
credentials.password | |
end | |
end | |
current_user.credentials = credentials | |
end | |
def sign_in | |
sign_in_with current_user.credentials | |
end | |
def sign_in_with(credentials) | |
credentials = Credentials.parse(credentials) | |
visit new_user_session_path | |
fill_in 'Email', :with => credentials.email | |
fill_in 'Password', :with => credentials.password | |
click_button 'Sign in' | |
end | |
end | |
World(UserHelper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: