#98 ✓resolved
Jeff Wigal

Can't get TestCase to work

Reported by Jeff Wigal | April 27th, 2009 @ 08:56 PM

I'm having a heck of a time getting my tests working with Authlogic.

I put together a bare-bones rails app up and running, the only plugin I have installed is authlogic.

  • user belongs_to :site
  • site has_many :users
  • account model only has a name:string attribute

I've tried the following variations:

  • Rails 2.2.2, 2.3.2
  • Session- and ActiveRecord-based session store
  • Windows & Linux. My windows box has a very minimal gem setup (just blew it away and started from scratch last week, not related to this problem)
  • within an application with about 12 different plugins and gems, and my app with no other gems/plugins

I'm requiring test_case in my test_helper.rb file


require 'authlogic/test_case'

Here's my integration test


require File.dirname(__FILE__) + '/../test_helper'

class LoginLogoutTest < ActionController::IntegrationTest
  fixtures :all
  setup :activate_authlogic
  # Replace this with your real tests.

  test "can login via post" do
    get new_user_session_path
    post_via_redirect user_session_path, :login => @user.login, :password => "awesomeness"
    logger.info session.inspect
    assert_response :success
    get accounts_path
    logger.info session.inspect
    assert_response :success

  end

  test "can login via create" do
    get new_user_session_path
    UserSession.create!(@user)
    logger.info session.inspect
    get accounts_path
    logger.info session.inspect
    assert_response :success
  end


  def setup
    site = Site.create!(:name => "booberry")
    @user = User.create!(
          :site => site,
          :first_name => "john",
          :last_name => "boy",
          :login => "jeff",
          :email => "jeff@wigaldesign.com",
          :password => "awesomeness", 
          :password_confirmation => "awesomeness")
  end
end
 

Plugin seems to work great in production/development mode using a browser. What am I missing?

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson April 27th, 2009 @ 11:00 PM

    • State changed from “new” to “open”

    You should be posting to user_sessions_path (plural). Does that fix it?

  • Jeff Wigal

    Jeff Wigal April 28th, 2009 @ 08:27 AM

    Originally I was posting to login_path, but I changed it to make sure it wasn't a routing issue.

    I'm defining the user_session as a singular resource, which I think is what was laid out in the example.

    I've also tried this with specific named routes, e.g. get login_path, post login_path as needed, same results.

    My routes:

    
      map.resources :sites
      map.resources :accounts
      map.resource :user_session
      map.root :controller => "user_sessions", :action => "new" # optional, this just sets the root route
      map.login '/login', :controller => 'user_sessions', :action => 'new', :conditions => {:method => :get} 
      map.login '/login', :controller => 'user_sessions', :action => 'create', :conditions => {:method => :post} 
      map.logout '/logout', :controller => 'user_sessions', :action => 'destroy'  
    
    
  • Jeff Wigal

    Jeff Wigal April 28th, 2009 @ 08:32 AM

    Oops, didn't answer your question. Yes, tried posting to user_sessions_path but that is not a named route.

    Each test is failing on the very last assert_response statement. Also routing should not matter for the "can login via create" test, since it is just using the UserSession.create method, then doing a "get" request on a controller that is protected with "before_filter :require_user"

  • Steve Noyce

    Steve Noyce April 29th, 2009 @ 12:38 PM

    This sounds close to the problem I'm seeing also. Things work great in the real world, but my functional tests all fail.

    I do PersonSession.create(people(:one)) (using Person model) and nothing seems to happen. Anything that requires a login insists on redirecting to the login world.

  • Jeff Wigal

    Jeff Wigal April 29th, 2009 @ 04:33 PM

    I'm seeing the same behavior for functional tests as well.

    My work-around for those was to grab the old test_unit_helper.rb file and re-deploy it as part of my application. That does work for functional tests, but not for integration tests.

  • Ben Johnson

    Ben Johnson May 4th, 2009 @ 12:32 AM

    Let me update the tests in the authlogic exmaple app and see if I can reproduce the problem. I am not having any issues in the app I am currently working on, but I'll update the example app so we can all have something to play around with and compare it to.

  • Jeff Wigal

    Jeff Wigal May 7th, 2009 @ 12:55 PM

    Wow... sitting at RailsConf watching the Webrat presentation.

    I refactored my test to work as a Webrat test, and it worked.

    
      test "can login with webrat" do
        visit login_path
        assert_response :success
        fill_in "Login", :with => @user.login
        fill_in "Password", :with => @user.password
        click_button "Login"
        visit games_path
        assert_response :success      
      end
    
  • Ben Johnson

    Ben Johnson May 7th, 2009 @ 01:00 PM

    Nice, I'm at the Erector presentation. I really like webrat too.

  • Jeff Wigal

    Jeff Wigal May 11th, 2009 @ 08:27 AM

    Ben, since it's working for you and isn't working for me, any chance I could get a list of gems you have installed? I'll cross-check this with what I have to come up with a list of stuff I have that you do not have, and vice versa...

  • Steve Noyce

    Steve Noyce May 11th, 2009 @ 01:11 PM

    I guess I'm a little concerned that I haven't gotten a bare-bones installation to work even for functional tests. (Let me know if this is a different issue than integration tests) Should it be necessary to hack in stuff from the earlier release in order to make things go?

    Steve

  • Ben Johnson

    Ben Johnson May 11th, 2009 @ 02:02 PM

    I pushed out some changes to the authlogic_example, checkout the tests and see what you think.

  • Jeff Wigal

    Jeff Wigal May 11th, 2009 @ 02:18 PM

    The tests in your example are passing for me, once I added another migration (added the email field to the User model). I'll see if I can see what the difference is between my sample app and your sample app.

  • Ben Johnson

    Ben Johnson May 11th, 2009 @ 02:20 PM

    That would be great, thanks.

  • Jeff Wigal

    Jeff Wigal May 11th, 2009 @ 04:20 PM

    OK still looking at this but I was able to hack something together to work for functional tests.

    My application is Rails 2.2, it looks like your demo is 2.3.

    This was inspired by this fork:

    http://github.com/Shadowfiend/au...

    and this post:

    http://groups.google.com/group/a...

    
    class ActiveSupport::TestCase
      def setup
        if @request && ! @request.respond_to?(:params)
          class <<@request
            alias_method :params, :parameters
          end
        end
        activate_authlogic
      end
    end
    

    In each functional tests, I end up doing this:

    
    def setup
      super
      # do my other setup stuff here
    end
    

    In your 2.3 demo app, I could get the following integration test to pass with no extra effort:

    
    require 'test_helper'
    
    class LoginLogoutTest < ActionController::IntegrationTest
      fixtures :all
    
      test "login via post using integration test" do 
        get new_user_session_path
        assert_response :success
        post user_session_path, :user_session => { :login => "bjohnson", :password => "benrocks" }
        assert user_session = UserSession.find
        assert_equal users(:ben), user_session.user
        assert_redirected_to account_path
      end
    end
    
    

    Still can't get an integration test to properly work with my own code with Rails 2.2 (other than using Webrat).

  • Ben Johnson

    Ben Johnson June 3rd, 2009 @ 03:20 AM

    Is this still an issue? Did the above method work? If so, that's an easy fix. Correct me if I'm wrong but isn't params still available in 2.2? I thought params has been around since rails started.

  • Steve Noyce

    Steve Noyce June 3rd, 2009 @ 11:01 AM

    FWIW, I got things working for my functional tests using Factory Girl and creating new user objects for each test.

    Thanks,
    Steve

  • Ben Johnson

    Ben Johnson June 3rd, 2009 @ 12:11 PM

    • State changed from “open” to “resolved”

    Good deal.

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

Tags

Pages