At present, the following works when I call it from my Rspec tests:
def login_as(role)
@role = Role.create! :name => role
@virginia = User.create!(
:username => "Virginia",
:password => "password",
:password_confirmation => "password",
:email => "[email protected]")
@assignment = Assignment.create! user_id: @virginia.id, role_id: @role.id
visit login_path
fill_in "user_session_username", :with => @virginia.username
fill_in "user_session_password", :with => @virginia.password
click_on "submit_user_session"
end
Given that I've tested the interface already, I thought it might speed things up to not hit the interface hundreds of times just because many of my examples only make sense when a user has a role and is logged in. So, I tried this:
def login_as(role)
@role = Role.create! :name => role
@virginia = User.create!(
:username => "Virginia",
:password => "password",
:password_confirmation => "password",
:email => "[email protected]")
@assignment = Assignment.create! user_id: @virginia.id, role_id: @role.id
@user_session = UserSession.create! :username => @virginia.username, :password => @virginia.password
end
However, that version is not logging me in. Any thoughts?
To be clear, I'm not asking how to test declarative_authorization. I'm asking how to quickly login when I'm testing something else. For example, I use login_as(role) to test product#delete (an action only accessible to admins) like this:
login_as "admin"
visit product_path(@search)
click_link "delete_product"
page.current_path.should == products_path
page.should_not have_content "Retail Site Search"