Skip to content

Commit

Permalink
Added Test::Unit helpers and a session generator
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Dec 1, 2008
1 parent 5238a02 commit af9712b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rdoc
@@ -1,3 +1,8 @@
== 1.3.7 released 2008-11-30

* Added session generator: script/generate session UserSession
* Added Test::Unit helpers file, see testing in the README

== 1.3.6 released 2008-11-30

* Modified validates_length_of for password so that there is a fallback validation if the passed "if statement" fails
Expand Down
1 change: 1 addition & 0 deletions Manifest
Expand Up @@ -46,6 +46,7 @@ test/libs/mock_controller.rb
test/libs/mock_cookie_jar.rb
test/libs/mock_request.rb
test/libs/ordered_hash.rb
test/libs/user.rb
test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/config_test.rb
test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/credentials_test.rb
test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/logged_in_test.rb
Expand Down
17 changes: 17 additions & 0 deletions README.rdoc
Expand Up @@ -439,6 +439,23 @@ Obviously there is a little more to it than this, but hopefully this clarifies a

When things come together like this I think its a sign that you are doing something right. Put that in your pipe and smoke it!

== Testing

Testing with authlogic is easy, there is a helper file that will add some convenient test helpers for you. In your test_helper.rb file do the following:

# test/test_helper.rb
require 'authlogic/testing/test_unit_helpers'

You get the following methods:

set_session_for(record_object)
set_cookie_for(record_object)
set_http_auth_for(username, password)

In your test, before you execute a request, just call one of those methods and it will set the proper values so that it will seems as if that record is logged in.

You can also checkout the authlogic_example application (see helpful links above), the tests there use this.

== Framework agnostic (Rails, Merb, etc.)

I designed Authlogic to be framework agnostic, meaning it doesn't care what framework you use it in. Right out of the box it supports rails and merb. I have not had the opportunity to use other frameworks, but the only thing stopping Authlogic from being used in other frameworks is a simple adapter. Check out controller_adapters/rails_adapter, or controller_adapters/merb_adapter.
Expand Down
9 changes: 9 additions & 0 deletions generators/session/session_generator.rb
@@ -0,0 +1,9 @@
class SessionGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
m.class_collisions class_name
m.directory File.join('app/models', class_path)
m.template 'session.rb', File.join('app/models', class_path, "#{file_name}.rb")
end
end
end
2 changes: 2 additions & 0 deletions generators/session/templates/session.rb
@@ -0,0 +1,2 @@
class <%= class_name %> < Authlogic::Session::Base
end
Expand Up @@ -121,7 +121,7 @@ def validate_#{options[:password_field]}?
return false unless send(#{options[:password_field_validates_length_of_options][:if].inspect})
end
self.#{options[:crypted_password_field]}.blank?
#{options[:crypted_password_field]}.blank?
end
private
Expand Down
35 changes: 35 additions & 0 deletions lib/authlogic/testing/test_unit_helpers.rb
@@ -0,0 +1,35 @@
module Authlogic
module Testing
# = Test Unit Helpers
#
# Provides useful methods for testing in Test::Unit, lets you log records in, etc.
module TestUnitHelpers
private
def session_class(record) # :nodoc:
record.class.acts_as_authentic_config[:session_class].constantize
end

# Sets the session for a record. This way when you execute a request in your test, session values will be present.
def set_session_for(record)
session_class = session_class(record)
@request.session[session_class.session_key] = record.persistence_token
@request.session["#{session_class.session_key}_id"] = record.id
end

# Sets the cookie for a record. This way when you execute a request in your test, cookie values will be present.
def set_cookie_for(record)
session_class = session_class(record)
@request.cookies[session_class.cookie_key] = record.persistence_token
end

# Sets the HTTP_AUTHORIZATION header for basic HTTP auth. This way when you execute a request in your test that is trying to authenticate
# with HTTP basic auth, the neccessary headers will be present.
def set_http_auth_for(username, password)
session_class = session_class(record)
@request.env['HTTP_AUTHORIZATION'] = @controller.encode_credentials(username, password)
end
end
end
end

Test::Unit::TestCase.send(:include, Authlogic::Testing::TestUnitHelpers)
2 changes: 1 addition & 1 deletion lib/authlogic/version.rb
Expand Up @@ -44,7 +44,7 @@ def to_a

MAJOR = 1
MINOR = 3
TINY = 5
TINY = 6

# The current version as a Version instance
CURRENT = new(MAJOR, MINOR, TINY)
Expand Down

0 comments on commit af9712b

Please sign in to comment.