Skip to content

Instantly share code, notes, and snippets.

@daveworth
Created October 4, 2011 13:05
Show Gist options
  • Save daveworth/1261596 to your computer and use it in GitHub Desktop.
Save daveworth/1261596 to your computer and use it in GitHub Desktop.
def login
login_as
end
def login_as(role=:basic, &block)
account = Account.find_by_name("testaccount") || Factory.create(:testaccount)
role = Role.find_by_name(role.to_s.capitalize) || Factory.create("#{role.downcase}_role")
user = Factory.create(:user, :account => account, :role => role)
login_as_user(user,&block)
end
# Note: This is not idempotent. Calling #login_as twice without an
# intermediate #logout will fail with an "Validation failed: name has already
# been taken" message!
#
# To use this correctly there are two methods:
# 1: pass a block which will be yielded to, aftewards logout will be called
# automatically.
# 2: call it in a before(:each) {} block with an
# after(:each){logout} or be sure to call logout after the call
# to reset tests to a working state
def login_as_user(user = nil)
account = user.account
switch_to_subdomain(account.name)
visit(new_user_session_path)
fill_in 'user_email', :with => user.email
fill_in 'user_password', :with => '123456'
click_button 'user_submit'
page.should have_no_content("Please sign in") # make sure we really did sign in
if block_given?
yield user
logout
end
end
def logout
visit(destroy_user_session_path)
page.should have_content("Please sign in") # make sure we really did log out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment