### Home Classes Methods

### In Files

- lib/authlogic/test_case/mock_controller.rb
- lib/authlogic/test_case/mock_cookie_jar.rb
- lib/authlogic/test_case/mock_logger.rb
- lib/authlogic/test_case/mock_request.rb
- lib/authlogic/test_case/rails_request_adapter.rb
- lib/authlogic/test_case.rb

### Namespace

- CLASS Authlogic::TestCase::MockController
- CLASS Authlogic::TestCase::MockLogger
- CLASS Authlogic::TestCase::RailsRequestAdapter

### Methods

- #activate_authlogic
- #controller

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Authlogic
- Authlogic::ActsAsAuthentic
- Authlogic::ActsAsAuthentic::Base
- Authlogic::ActsAsAuthentic::Base::Config
- Authlogic::ActsAsAuthentic::Email
- Authlogic::ActsAsAuthentic::Email::Config
- Authlogic::ActsAsAuthentic::Email::Methods
- Authlogic::ActsAsAuthentic::LoggedInStatus
- Authlogic::ActsAsAuthentic::LoggedInStatus::Config
- Authlogic::ActsAsAuthentic::LoggedInStatus::Methods
- Authlogic::ActsAsAuthentic::LoggedInStatus::Methods::InstanceMethods
- Authlogic::ActsAsAuthentic::Login
- Authlogic::ActsAsAuthentic::Login::Config
- Authlogic::ActsAsAuthentic::Login::Methods
- Authlogic::ActsAsAuthentic::MagicColumns
- Authlogic::ActsAsAuthentic::MagicColumns::Methods
- Authlogic::ActsAsAuthentic::Password
- Authlogic::ActsAsAuthentic::Password::Callbacks
- Authlogic::ActsAsAuthentic::Password::Config
- Authlogic::ActsAsAuthentic::Password::Methods
- Authlogic::ActsAsAuthentic::Password::Methods::InstanceMethods
- Authlogic::ActsAsAuthentic::PerishableToken
- Authlogic::ActsAsAuthentic::PerishableToken::Config
- Authlogic::ActsAsAuthentic::PerishableToken::Methods
- Authlogic::ActsAsAuthentic::PerishableToken::Methods::ClassMethods
- Authlogic::ActsAsAuthentic::PerishableToken::Methods::InstanceMethods
- Authlogic::ActsAsAuthentic::PersistenceToken
- Authlogic::ActsAsAuthentic::PersistenceToken::Methods
- Authlogic::ActsAsAuthentic::PersistenceToken::Methods::ClassMethods
- Authlogic::ActsAsAuthentic::PersistenceToken::Methods::InstanceMethods
- Authlogic::ActsAsAuthentic::RestfulAuthentication
- Authlogic::ActsAsAuthentic::RestfulAuthentication::Config
- Authlogic::ActsAsAuthentic::RestfulAuthentication::InstanceMethods
- Authlogic::ActsAsAuthentic::SessionMaintenance
- Authlogic::ActsAsAuthentic::SessionMaintenance::Config
- Authlogic::ActsAsAuthentic::SessionMaintenance::Methods
- Authlogic::ActsAsAuthentic::SingleAccessToken
- Authlogic::ActsAsAuthentic::SingleAccessToken::Config
- Authlogic::ActsAsAuthentic::SingleAccessToken::Methods
- Authlogic::ActsAsAuthentic::SingleAccessToken::Methods::InstanceMethods
- Authlogic::ActsAsAuthentic::ValidationsScope
- Authlogic::ActsAsAuthentic::ValidationsScope::Config
- Authlogic::AuthenticatesMany
- Authlogic::AuthenticatesMany::Association
- Authlogic::AuthenticatesMany::Base
- Authlogic::ControllerAdapters
- Authlogic::ControllerAdapters::AbstractAdapter
- Authlogic::ControllerAdapters::MerbAdapter
- Authlogic::ControllerAdapters::MerbAdapter::MerbImplementation
- Authlogic::ControllerAdapters::RailsAdapter
- Authlogic::ControllerAdapters::RailsAdapter::AuthlogicLoadedTooLateError
- Authlogic::ControllerAdapters::RailsAdapter::RailsImplementation
- Authlogic::ControllerAdapters::SinatraAdapter
- Authlogic::ControllerAdapters::SinatraAdapter::Adapter
- Authlogic::ControllerAdapters::SinatraAdapter::Adapter::Implementation
- Authlogic::ControllerAdapters::SinatraAdapter::Controller
- Authlogic::ControllerAdapters::SinatraAdapter::Cookies
- Authlogic::CryptoProviders
- Authlogic::CryptoProviders::AES256
- Authlogic::CryptoProviders::BCrypt
- Authlogic::CryptoProviders::MD5
- Authlogic::CryptoProviders::Sha1
- Authlogic::CryptoProviders::Sha256
- Authlogic::CryptoProviders::Sha512
- Authlogic::CryptoProviders::Wordpress
- Authlogic::I18n
- Authlogic::I18n::Translator
- Authlogic::Random
- Authlogic::Regex
- Authlogic::Session
- Authlogic::Session::Activation
- Authlogic::Session::Activation::ClassMethods
- Authlogic::Session::Activation::InstanceMethods
- Authlogic::Session::ActiveRecordTrickery
- Authlogic::Session::ActiveRecordTrickery::ClassMethods
- Authlogic::Session::ActiveRecordTrickery::InstanceMethods
- Authlogic::Session::Base
- Authlogic::Session::BruteForceProtection
- Authlogic::Session::BruteForceProtection::Config
- Authlogic::Session::BruteForceProtection::InstanceMethods
- Authlogic::Session::Callbacks
- Authlogic::Session::Cookies
- Authlogic::Session::Cookies::Config
- Authlogic::Session::Cookies::InstanceMethods
- Authlogic::Session::Existence
- Authlogic::Session::Existence::ClassMethods
- Authlogic::Session::Existence::InstanceMethods
- Authlogic::Session::Foundation
- Authlogic::Session::Foundation::ClassMethods
- Authlogic::Session::Foundation::InstanceMethods
- Authlogic::Session::HttpAuth
- Authlogic::Session::HttpAuth::Config
- Authlogic::Session::HttpAuth::InstanceMethods
- Authlogic::Session::Id
- Authlogic::Session::Klass
- Authlogic::Session::Klass::Config
- Authlogic::Session::Klass::InstanceMethods
- Authlogic::Session::MagicColumns
- Authlogic::Session::MagicColumns::Config
- Authlogic::Session::MagicColumns::InstanceMethods
- Authlogic::Session::MagicStates
- Authlogic::Session::MagicStates::Config
- Authlogic::Session::MagicStates::InstanceMethods
- Authlogic::Session::Params
- Authlogic::Session::Params::Config
- Authlogic::Session::Params::InstanceMethods
- Authlogic::Session::Password
- Authlogic::Session::Password::Config
- Authlogic::Session::Password::InstanceMethods
- Authlogic::Session::PerishableToken
- Authlogic::Session::Persistence
- Authlogic::Session::Persistence::ClassMethods
- Authlogic::Session::Persistence::InstanceMethods
- Authlogic::Session::PriorityRecord
- Authlogic::Session::Scopes
- Authlogic::Session::Scopes::ClassMethods
- Authlogic::Session::Scopes::InstanceMethods
- Authlogic::Session::Session
- Authlogic::Session::Session::Config
- Authlogic::Session::Session::InstanceMethods
- Authlogic::Session::Timeout
- Authlogic::Session::Timeout::Config
- Authlogic::Session::Timeout::InstanceMethods
- Authlogic::Session::UnauthorizedRecord
- Authlogic::Session::Validation
- Authlogic::Session::Validation::Errors
- Authlogic::TestCase
- Authlogic::TestCase::MockController
- Authlogic::TestCase::MockLogger
- Authlogic::TestCase::RailsRequestAdapter
- Object

No matching classes.

# Authlogic::TestCase

This module 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.

### The quick and dirty

    require "authlogic/test_case" # include at the top of test_helper.rb
    setup :activate_authlogic # run before tests are executed
    UserSession.create(users(:whomever)) # logs a user in

For a more detailed explanation, see below.

### Setting up

Authlogic comes with some simple testing tools. To get these, you need to first require Authlogic's TestCase. If you are doing this in a rails app, you would require this file at the top of your test_helper.rb file:

    require "authlogic/test_case"

If you are using Test::Unit::TestCase, the standard testing library that comes with ruby, then you can skip this next part. If you are not, you need to include the Authlogic::TestCase into your testing suite as follows:

    include Authlogic::TestCase

Now that everything is ready to go, let’s move onto actually testing. Here is the basic idea behind 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:

### Fixtures / Factories

Creating users via fixtures / factories is easy. Here’s an example of a fixture:

    ben:
      email: whatever@whatever.com
      password_salt: <%= salt = Authlogic::Random.hex_token %>
      crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("benrocks" + salt) %>
      persistence_token: <%= Authlogic::Random.hex_token %>
      single_access_token: <%= Authlogic::Random.friendly_token %>
      perishable_token: <%= Authlogic::Random.friendly_token %>

Notice the crypted_password value. Just supplement that with whatever crypto provider you are using, if you are not using the default.

### 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. Solving this is simple, just do this:

    setup :activate_authlogic

For those of you unfamiliar with TestUnit, the setup method bascially just executes a method before any test is ran. It is essentially “setting up” your tests.

Once you have done this, just log users in like usual:

    UserSession.create(users(:whomever))
    # access my restricted area here

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. If you want to activate Authlogic before making a request follow the same steps described in the “functional tests” section above. It works in the same manner.

### Unit tests

The only time you need to do any trickiness here is if you want to test Authlogic models. Maybe you added some custom code or methods in your Authlogic models. Maybe you are writing a plugin or a library that extends Authlogic.

That being said, in this environment there is no controller. So you need to use a “mock” controller. Something that looks like a controller, acts like a controller, but isn’t a “real” controller. You are essentially connecting Authlogic to your “mock” controller, then you can test off of the mock controller to make sure everything is functioning properly.

I use a mock controller to test Authlogic myself. It’s part of the Authlogic library that you can easily use. It’s as simple as functional and integration tests. Just do the following:

    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

See how I am checking that Authlogic is interacting with the controller properly? That’s the idea here.

### Public Instance Methods

activate_authlogic() click to toggle source

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

    # File lib/authlogic/test_case.rb, line 102
    def activate_authlogic
      if @request && ! @request.respond_to?(:params)
        class <<@request
          alias_method :params, :parameters
        end
      end

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

controller() click to toggle source

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.

    # File lib/authlogic/test_case.rb, line 114
    def controller
      @controller ||= Authlogic::TestCase::MockController.new
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.