Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration testing with Authlogic?

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
like image 370
kareem Avatar asked Aug 05 '09 18:08

kareem


4 Answers

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
like image 192
Gravis Avatar answered Oct 11 '22 06:10

Gravis


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 => '[email protected]', :password => 'password')

but not with:

UserSession.create(@user)
like image 30
user163236 Avatar answered Oct 11 '22 06:10

user163236


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

setup do
  post 'user_session', :user_session => {:email => '[email protected]', :password => 'password'}
end

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

like image 2
James Avatar answered Oct 11 '22 06:10

James


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.

like image 2
Bill Burcham Avatar answered Oct 11 '22 04:10

Bill Burcham