Question

I am trying to write a simple integration test on a Rails 3.1 application that uses Authlogic and ActiveRecord SessionStore, but I'm hitting the wall.

First, I tried a classic approach: after ensuring that require "authlogic/test_case" line is already in our test_helper.rb I then wrote a setup method that calls activate_authlogic and then uses UserSession(@user) to create a session.

This did create a session (as proved by UserSession.find), but when doing a get request on protected resource, the response would be a redirect to the login form and session was killed (ie. UserSession.find would return nil)

I tried POST-ing the email/password as well, but that seems to work only if I change the session store back to cookie_store (something I found out from this comment).

Switching session store to CookieStore just for test would be an option, but there are some tests that already depend on ActiveRecord store.

Is there any way to switch the session store just for one test? Is there any other solution to this problem that I'm missing?

require 'test_helper'

class ProtectedTest < ActionController::IntegrationTest
  def setup
    activate_authlogic
    @user = Factory(:user, :roles => 'user')
    UserSession.create(@user)
  end

  def test_protected
    https!
    get '/protected'
    assert_response :success
  end
end
Was it helpful?

Solution

Dear people from the future:

I figured a workaround to this by starting tests that needed authorized users in a different environment by setting ENV["RAILS_ENV"] before including 'test_helper'. Then I changed session_store in that environment to cookie_store.

ENV["RAILS_ENV"] = "test_cs"
require 'test_helper'
end

class ProtectedTest < ActionController::IntegrationTest
  # ...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top