Unable to Login in and out correctly in functional tests

39 views
Skip to first unread message

Dave Smylie

unread,
May 31, 2012, 2:12:49 PM5/31/12
to auth...@googlegroups.com
I'm having a bit of weirdness with functional testing and authlogic. I suspect I'm not doing thing 100% correctly.
I want to be able to login and out during the functional tests to verify restrictions on what logged in/logged out users can see are working as I expect.

The issue I'm having is that it seems as the though the login status of the first call I make during a test is what rails will see for the rest of the test - regardless if I login/logout after that first request. 

Eg, If I make a request before logging in, then even if I login after that request (and UserSessions seems to show me as being logged in), the rails controller shows me as still being logged out.  And vice versa, if I start a test logged in, and then logout/destroy that UserSession, the rails controller still shows me as being logged in.  

Is there something else I need to be doing to make the rails controller register these login/logouts in the functional test? 

In my test I have a setup method and a login method as follows:

  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 

And here is a transcript of me trying to login and log out.  First, making the initial request without logging in. UserSession is nil:

[2] pry(#<ListsControllerTest>)> UserSession.find
=> nil
[3] pry(#<ListsControllerTest>)> get :new

# no current user in the controller
[4] pry(#<ListsControllerTest>)> assigns
=> {"_routes"=>nil, "current_user_session"=>nil, "current_user"=>nil}

# and now login - UserSession seems to be populated
[5] pry(#<ListsControllerTest>)> login
=> #<UserSession: {:unauthorized_record=>"<protected>"}>
[6] pry(#<ListsControllerTest>)> UserSession.find
=> #<UserSession: {:unauthorized_record=>"<protected>"}>

# make a request and *still* no current user
[7] pry(#<ListsControllerTest>)> get :new
[8] pry(#<ListsControllerTest>)> assigns
=> {"_routes"=>nil, "current_user_session"=>nil, "current_user"=>nil}



And the opposite approach - logging in and then trying to log out

# login - UserSession populated
1] pry(#<ListsControllerTest>)> login
=> #<UserSession: {:unauthorized_record=>"<protected>"}>
[3] pry(#<ListsControllerTest>)> UserSession.find
=> #<UserSession: {:unauthorized_record=>"<protected>"}>

# make the request - the controller populates current_user (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", ....>}

# try and log out. UserSession seems to be emptied (as expected)
[6] pry(#<ListsControllerTest>)> @session.destroy
=> true
[7] pry(#<ListsControllerTest>)> UserSession.find
=> nil

# make a request - the controller is still populating current_user
[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",....>}


Thanks
Dave Smylie

Reply all
Reply to author
Forward
0 new messages