Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completely rewrote Authlogic::Testing, it's now called Authlogic::Tes…
…tCase. Testing Authlogic is much easier now. Please see Authlogic::TestCase for more info.
- Loading branch information
1 parent
56e9dd1
commit 26e57e6
Showing
20 changed files
with
202 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require "authlogic/test_case/mock_cookie_jar" | ||
require "authlogic/test_case/mock_request" | ||
require "authlogic/test_case/mock_controller" | ||
|
||
module Authlogic | ||
# 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. 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 connect it. Let's walk through how to do this in tests: | ||
# | ||
# === Functional tests | ||
# | ||
# You shouldn't have to do anything. Authlogic automatically sets a before_filter in your ApplicationController that | ||
# conntects Authlogic to the controller. So as soon as you make a request in your tests, it will connect Authlogic | ||
# for you. | ||
# | ||
# === 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 | ||
# | ||
# Now here is the tricky part of testing. Since there really is no controller here you need to "fake" Authlogic into | ||
# thinking there is. Don't worry, because the Authlogic::TestCase takes care of this for you. Just do the following | ||
# in your test's setup and you are good to go: | ||
# | ||
# setup :activate_authlogic | ||
# | ||
# activate_authlogic is a method provided to you by this TestCase module. | ||
# | ||
# You can even test off of this controller to make sure everything is good. For example: | ||
# | ||
# ben = users(:ben) | ||
# assert_nil controller.session["user_credentials"] | ||
# assert UserSession.create(ben) | ||
# assert_equal controller.session["user_credentials"], ben.persistence_token | ||
# | ||
# You also get the "controller" method to use in your tests as well. Now you have everything you need to properly test in unit tests. | ||
# | ||
# === How to use | ||
# | ||
# Just require the file in your test_helper.rb file. | ||
# | ||
# require "authlogic/test_case" | ||
module TestCase | ||
# Activates authlogic with an Authlogic::TestCase::MockController object. | ||
def activate_authlogic | ||
Authlogic::Session::Base.controller = controller | ||
end | ||
|
||
# The Authlogic::TestCase::MockController object passed to Authlogic to activate it. | ||
def controller | ||
@controller ||= Authlogic::TestCase::MockController.new | ||
end | ||
end | ||
|
||
::Test::Unit::TestCase.send(:include, TestCase) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Authlogic | ||
module TestCase | ||
class MockController < ControllerAdapters::AbstractAdapter | ||
attr_accessor :http_user, :http_password | ||
attr_writer :request_content_type | ||
|
||
def initialize | ||
end | ||
|
||
def authenticate_with_http_basic(&block) | ||
yield http_user, http_password | ||
end | ||
|
||
def cookies | ||
@cookies ||= MockCookieJar.new | ||
end | ||
|
||
def cookie_domain | ||
nil | ||
end | ||
|
||
def params | ||
@params ||= {} | ||
end | ||
|
||
def request | ||
@request ||= MockRequest.new | ||
end | ||
|
||
def request_content_type | ||
@request_content_type ||= "text/html" | ||
end | ||
|
||
def session | ||
@session ||= {} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Authlogic | ||
module TestCase | ||
class MockCookieJar < Hash | ||
def [](key) | ||
hash = super | ||
hash && hash[:value] | ||
end | ||
|
||
def delete(key, options = {}) | ||
super(key) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Authlogic | ||
module TestCase | ||
class MockRequest | ||
def remote_ip | ||
"1.1.1.1" | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
26e57e6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool – I had been a bit puzzled how to test Authlogic previously and bodged something together, but this should make life a lot easier. Thanks. :-)