Module: Authlogic::TestCase

Defined in:
lib/authlogic/test_case.rb,
lib/authlogic/test_case/mock_logger.rb,
lib/authlogic/test_case/mock_request.rb,
lib/authlogic/test_case/mock_controller.rb,
lib/authlogic/test_case/mock_cookie_jar.rb,
lib/authlogic/test_case/rails_request_adapter.rb

Overview

This is a collection of methods and classes that help you easily test Authlogic. In fact, I use these same tools to test the internals of Authlogic.

Some important things to keep in mind when testing:

Authlogic requires a “connection” to your controller to activate it. In the same manner that ActiveRecord requires a connection to your database. It can’t do anything until it gets connnected. That being said, Authlogic will raise an Authlogic::Session::Activation::NotActivatedError any time you try to instantiate an object without a “connection”. So before you do anything with Authlogic, you need to activate / connect Authlogic. Let’s walk through how to do this in tests:

Functional tests

Activating Authlogic isn’t a problem here, because making a request will activate Authlogic for you. The problem is logging users in so they can access restricted areas. Solvin this is simple, just do this:

setup :activate_authlogic

Now log users in using Authlogic:

UserSession.create(users(:whomever))

Do this before you make your request and it will act as if that user is logged in.

Integration tests

Again, just like functional tests, you don’t have to do anything. As soon as you make a request, Authlogic will be conntected.

Unit tests

The only time you need to do any trickiness here is if you want to test Authlogic yourself. Maybe you added some custom code or methods in your Session class. Maybe you are writing a plugin or a library that extends Authlogic. Whatever it is you need to make sure your code is tested and working properly.

That being said, in this environment there is no controller. So you need to “fake” Authlogic into thinking there is. Don’t worry, because the this module takes care of this for you. Just do the following in your test’s setup and you are good to go:

setup :activate_authlogic

You also get a controller method that you can test off of. For example:

ben = users(:ben)
assert_nil controller.session["user_credentials"]
assert UserSession.create(ben)
assert_equal controller.session["user_credentials"], ben.persistence_token

That’s it.

How to use

Just require the file in your test_helper.rb file.

require "authlogic/test_case"

Defined Under Namespace

Classes: MockController, MockCookieJar, MockLogger, MockRequest, RailsRequestAdapter

Instance Method Summary collapse

Instance Method Details

#activate_authlogicObject

Activates authlogic so that you can use it in your tests. You should call this method in your test’s setup. Ex:

setup :activate_authlogic


66
67
68
# File 'lib/authlogic/test_case.rb', line 66

def activate_authlogic
  Authlogic::Session::Base.controller = (@request && Authlogic::TestCase::RailsRequestAdapter.new(@request)) || controller
end

#controllerObject

The Authlogic::TestCase::MockController object passed to Authlogic to activate it. You can access this in your test. See the module description for an example.



72
73
74
# File 'lib/authlogic/test_case.rb', line 72

def controller
  @controller ||= Authlogic::TestCase::MockController.new
end