#121 ✓moved_to_github
David North

UserSession not maintained for Integration tests

Reported by David North | June 12th, 2009 @ 07:49 AM

I'm trying to write integration tests for screens that require a logged in user. I'm using Webrat too.
I've added the following to test_helper

require 'authlogic/test_case'

And to my integration test:

  def setup 
    activate_authlogic
    UserSession.create! Factory.build(:user)
  end

The problem is that the session isn't maintained and my tests end up back at the login screen. I've confirmed that UserSession is created by checking the following in my tests:
UserSession.find (shows a valid UserSession instance)
UserSession.find.record (shows a valid User instance)
I've traced this back to current_user_session which is returning nil in application controller causing require_user to fail. In application_controller UserSession.find gives nil so somehow the session object is created fine in my tests but is lost in the controller.

My test just contains the following for now:

    visit '/clients'
    assert_contain 'Clients'

I'm using:
Authlogic 2.0.13
Rails 2.3.2
Webrat 0.4.4

Many thanks
David

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson June 12th, 2009 @ 12:03 PM

    • State changed from “new” to “open”

    Did you try going through the login process via webrat? Also, you should post this on the mailing list, because I don't think this is a bug. Plus you'd probably get faster help. Thanks.

  • David North

    David North June 12th, 2009 @ 01:25 PM

    I'll do as you suggest.
    I did try logging in through webrat, no luck there.

  • Ben Johnson

    Ben Johnson June 12th, 2009 @ 01:35 PM

    That seems weird, can you login yourself when starting up the server? If so, webrat should be able to do the same thing.

  • David North

    David North June 13th, 2009 @ 04:32 PM

    Yes, in the browser its fine, and also using your example in the docs, my controller spec can use the session fine too.
    I've had the same trouble using ordinary integration test style instead of webrat.

    I've solved this by having a user fixture rather than using factory girl, then having it go through posting the form rather than trying to force the session's existence which is a cumbersome way to do it. (factory girl was creating a valid user record in the database but the controller saw Users.all as empty, that's a mystery to me)

    As I've detailed on my post on the group, records saved in the integration test aren't available within the controller when test is run, this also goes for UserSession.create! and UserSession.find, the latter returns the session in my test but nil in the controller.

    I agree this doesn't look like an AuthLogic but, its something peculiar about integration testing but it seems someone else is having the same trouble so perhaps the documentation needs updating in this area, the method used for controller tests/specs seems not to be valid for integration tests unless I'm doing something wrong.

    Many thanks
    David

  • Ben Johnson

    Ben Johnson August 7th, 2009 @ 06:20 PM

    • State changed from “open” to “moved_to_github”

    [state:"moved_to_github" bulk edit command]

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database.

People watching this ticket

Pages