10

For the life of me I don't understand why Authlogic isn't logging me in in this integration test. I haven't had any problems w/ Authlogic logging me in in functional tests using this code. According to the authlogic rdocs (http://tinyurl.com/mb2fp2), simulating a logged-in state is the same in functional & integration tests, so i'm pretty confused. any help is MUCH appreciated!

class TipsController < ApplicationController
  before_filter :require_user,  :only => [:destroy, :undelete]
  def destroy
    @tip = Tip.find(params[:id])

    if can_delete?(@tip)

      @tip.destroy

      set_flash("good", "Tip deleted. <a href=\"#{undelete_tip_url(@tip.id)}\">Undo?</a>")
      respond_to do |format|
        format.html { redirect_to city_path(@tip.city)} 
      end
    else
      set_flash("bad", "Seems like you can't delete this tip, sorry.")
      respond_to do |format|
        format.html { render :action => "show", :id => @tip} 
      end
    end
  end
end


class DeleteTipAndRender < ActionController::IntegrationTest
  context "log user in" do
    setup do
      @user = create_user
      @tip = create_tip
    end

    context "delete tip" do
      setup do
        activate_authlogic
        UserSession.create(@user)
        @us = UserSession.find
        post "/tips/destroy", :id => @tip.id
      end

      should_redirect_to("city_path(@tip.city)"){city_path(@tip.city)} 
    end
  end
end
kareem
  • 690
  • 1
  • 9
  • 20

9 Answers9

4

I'm also using Authlogic with Shoulda (but with factory_girl on top).

My functionnal tests look like :

require 'test_helper'

class LoansControllerTest < ActionController::TestCase
[...]

  context "as a signed-in user, with an active loan" do
    setup do
      @user = Factory(:user)
      @user_session = UserSession.create(@user)
      @loan = Factory(:loan, :ownership => Factory(:ownership, :user => @user))
    end

    context "on GET to :index" do
      setup do
        get :index
      end

      should_respond_with_success
    end
  end
end

Actually, you CAN pass a valid user to UserSession, it's in the rdoc also. You should also avoid calling activate_authlogic in each controller test :

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'

class ActiveSupport::TestCase
  [...]
  # Add more helper methods to be used by all tests here...
  include Authlogic::TestCase

  def setup
    activate_authlogic
  end
end
Gravis
  • 30,149
  • 5
  • 23
  • 20
3

Based on the code in the user_sessions_controller create method, which takes a hash of the login credentials, I was able to make it work like this in my integration test:

UserSession.create(:email => 'someone@example.com', :password => 'password')

but not with:

UserSession.create(@user)
Karol Selak
  • 4,248
  • 6
  • 35
  • 65
user163236
  • 46
  • 1
  • 1
    thanks. from this line in the rdoc: UserSession.create(users(:whomever)) i assumed i could pass a @user obj. appreciate the help! – kareem Aug 28 '09 at 11:57
  • 1
    hmm according to this: http://rdoc.info/rdoc/binarylogic/authlogic/blob/73c4cccb38189f0e52e1e362992dfb9db7d1206f/Authlogic/Session/UnauthorizedRecord.html I should be able to do UserSession.create(@user) and have it work... wtf. – kareem Aug 31 '09 at 21:47
2

I've found that for integration tests I need to login via a post:

setup do
  post 'user_session', :user_session => {:email => 'someone@example.com', :password => 'password'}
end

This sets up the session correctly, while the method mentioned above only works for me in functional tests.

James
  • 101
  • 2
  • 3
2

Yeah, I found this week that with rspec: in functional specs you simulate log in just fine w/ UserSession.create(@user). But if you try that in an integration spec it doesn't work. In order to log in from integration specs I found I had to drive the forms (with webrat) which clearly will be a problem for things like Facebook and OpenID log in.

Bill Burcham
  • 739
  • 5
  • 12
2

I couldn't make it work with the accepted answer, but was close. I had to add the exclamation sign to the end of the function:

UserSession.create!(@user)

It's working with Ruby v3.2.18 and Authlogic v3.4.2. Thanks for pointing me in the right direction though.

ZombieBsAs
  • 171
  • 3
  • 14
  • I am using the same versions as you do, and this is what I get in irb: (rdb:1) UserSession.create(User.find(25)) #""}> (rdb:1) UserSession.create(User.find(25)).save true clearly something else must be interfering with the user/session model – prusswan Jun 25 '14 at 03:09
  • Please test again using the exclamation sign (!) in the function. – ZombieBsAs Jun 26 '14 at 17:36
  • It works fine on my controllers, I didn´t tested it on the rails console. – ZombieBsAs Jun 26 '14 at 19:48
1

For the people who find this post in Google and the Greater Justice - you have to set persitence_token attribute in User model. E.g. you can call @user.reset_persistence_token! and everything just start working. :)

NikitaBaksalyar
  • 2,414
  • 1
  • 24
  • 27
0

I had the same problem and I could fix it by manually logging in (as others have already answered)

Additionally I had to fix my cookie domain:

Rails uses www.example.com for its tests and since I set the cookie domain to a different value in my application.rb (via config.cookie_domain) the session cookie created after the login was not accessible from subsequent requests.

After setting the correct cookie domain, everything worked again.

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
0

I'm using Cucumber and James' solution along with the following fix worked for me:

https://webrat.lighthouseapp.com/projects/10503/tickets/383-reset_session-and-webrat-dont-play-nicely-with-one-another

Andy Waite
  • 10,785
  • 4
  • 33
  • 47
-3

Have a look at the rdoc.

Waseem
  • 8,232
  • 9
  • 43
  • 54
  • thx... i linked to the rdoc in my original post and think i'm following what's in there. which is why i'm posting - cuz i'm not getting the result i expect :) – kareem Aug 05 '09 at 21:34
  • Hmmm. My mistake I did not see that. You can vote it down. It won't solve your issue though. – Waseem Aug 06 '09 at 03:21
  • Ok it's quite funny but I am facing the same issue. :) did you get it solved? – Waseem Aug 15 '09 at 20:07
  • ha... unfortunately not waseem. did you figure it out? – kareem Aug 23 '09 at 20:09
  • Actually my requirements changed and I could not get a chance to look into this. – Waseem Aug 24 '09 at 06:49