Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Test::Unit helpers and a session generator

  • Loading branch information...
commit af9712b32a518aa9ce59c2b6b1d3900c6ac0014c 1 parent 5238a02
@binarylogic authored
View
5 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
View
1  Manifest
@@ -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
View
17 README.rdoc
@@ -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.
View
9 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
View
2  generators/session/templates/session.rb
@@ -0,0 +1,2 @@
+class <%= class_name %> < Authlogic::Session::Base
+end
View
2  lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb
@@ -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
View
35 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)
View
2  lib/authlogic/version.rb
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.