Skip to content

Instantly share code, notes, and snippets.

@mattwynne
Created December 8, 2010 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattwynne/733772 to your computer and use it in GitHub Desktop.
Save mattwynne/733772 to your computer and use it in GitHub Desktop.
Cucumber support module for helping with authentication of users.
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)
@mattwynne
Copy link
Author

Example:

When I sign in as "a@b.com/password"

....

When /^I sign in as "[^"+]"$/ do |credentials|
  sign_in_with credentials
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment