I'm having a few issues trying to make functional testing work with authlogic - specifically logging in and logging out.
I don't want to to test the authlogic process - just that my app is behaving as expected for users that are/are not logged in.
The issue is that in each test, the login status of the initial request (eg am I logged in or out) persists for the rest of the test - regardless if I subsequestly login or logout.
Eg, if I'm logged out and make a request, then if I login and make another request, the rails controller will still see me as logged out. And vice versa - if I am logged in and make a request, then if I log out then the rails controller will still see me as logged in.
This seems to be an issue with testing - the behaviour in the application is as expected.
I've attached a transcript of a TestCase session belong to show what I mean.
Anyone know how to make the controller recognise that the login status has changed?
Thanks
Dave Smylie
## login and setup methods ##
def login
@user = User.new(:login => 'roger', :password => 'roger', :password_confirmation => 'roger')
@session = UserSession.create(@user)
end
def setup
activate_authlogic # run before tests are executed
DatabaseCleaner.clean
end
## START TEST LOGGED OUT - CAN NEVER LOGIN ###
# start request - not logging in. UserSession.find is nil (as expected)
[2] pry(#<ListsControllerTest>)> UserSession.find
=> nil
# make request - assigns[:current_user] is nil (as expected)
[3] pry(#<ListsControllerTest>)> get :new
[4] pry(#<ListsControllerTest>)> assigns
=> {"_routes"=>nil, "current_user_session"=>nil, "current_user"=>nil}
# login - UserSession.find now populated
[5] pry(#<ListsControllerTest>)> login
=> #<UserSession: {:unauthorized_record=>"<protected>"}>
[6] pry(#<ListsControllerTest>)> UserSession.find
=> #<UserSession: {:unauthorized_record=>"<protected>"}>
# make request - assigns[:current_user] still nil - *not* as expected
[7] pry(#<ListsControllerTest>)> get :new
[8] pry(#<ListsControllerTest>)> assigns
=> {"_routes"=>nil, "current_user_session"=>nil, "current_user"=>nil}
[9] pry(#<ListsControllerTest>)>
## START TEST LOGGED IN - CAN NEVER LOG OUT ###
# start test by logging in - UserSession populated as expected
[1] pry(#<ListsControllerTest>)> login
=> #<UserSession: {:unauthorized_record=>"<protected>"}>
[3] pry(#<ListsControllerTest>)> UserSession.find
=> #<UserSession: {:unauthorized_record=>"<protected>"}>
# make request - assigns[:current_user] populated (as expected)
[4] pry(#<ListsControllerTest>)> get :new
[5] pry(#<ListsControllerTest>)> assigns
=> {"_routes"=>nil,
"current_user_session"=>#<UserSession: {:unauthorized_record=>"<protected>"}>,
"current_user"=>
#<User id: 1, created_at: "2012-05-31 18:02:16", updated_at: "2012-05-31 18:02:41", login: "roger", ...}
# destroy session - UserSession.find now nil
[6] pry(#<ListsControllerTest>)> @session.destroy
=> true
[7] pry(#<ListsControllerTest>)> UserSession.find
=> nil
# make request, assigns[:current_user] *still* populated - not as expected
[8] pry(#<ListsControllerTest>)> get :new
[9] pry(#<ListsControllerTest>)> assigns
=> {"_routes"=>nil,
"current_user_session"=>#<UserSession: {:unauthorized_record=>"<protected>"}>,
"current_user"=>
#<User id: 1, created_at: "2012-05-31 18:02:16", updated_at: "2012-05-31 18:02:41", login: "roger",...}