Skip to content

Commit af9712b

Browse files
committed
Added Test::Unit helpers and a session generator
1 parent 5238a02 commit af9712b

File tree

8 files changed

+71
-2
lines changed

8 files changed

+71
-2
lines changed

CHANGELOG.rdoc

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
== 1.3.7 released 2008-11-30
2+
3+
* Added session generator: script/generate session UserSession
4+
* Added Test::Unit helpers file, see testing in the README
5+
16
== 1.3.6 released 2008-11-30
27

38
* Modified validates_length_of for password so that there is a fallback validation if the passed "if statement" fails

Manifest

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ test/libs/mock_controller.rb
4646
test/libs/mock_cookie_jar.rb
4747
test/libs/mock_request.rb
4848
test/libs/ordered_hash.rb
49+
test/libs/user.rb
4950
test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/config_test.rb
5051
test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/credentials_test.rb
5152
test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/logged_in_test.rb

README.rdoc

+17
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,23 @@ Obviously there is a little more to it than this, but hopefully this clarifies a
439439

440440
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!
441441

442+
== Testing
443+
444+
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:
445+
446+
# test/test_helper.rb
447+
require 'authlogic/testing/test_unit_helpers'
448+
449+
You get the following methods:
450+
451+
set_session_for(record_object)
452+
set_cookie_for(record_object)
453+
set_http_auth_for(username, password)
454+
455+
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.
456+
457+
You can also checkout the authlogic_example application (see helpful links above), the tests there use this.
458+
442459
== Framework agnostic (Rails, Merb, etc.)
443460

444461
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.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class SessionGenerator < Rails::Generator::NamedBase
2+
def manifest
3+
record do |m|
4+
m.class_collisions class_name
5+
m.directory File.join('app/models', class_path)
6+
m.template 'session.rb', File.join('app/models', class_path, "#{file_name}.rb")
7+
end
8+
end
9+
end
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class <%= class_name %> < Authlogic::Session::Base
2+
end

lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def validate_#{options[:password_field]}?
121121
return false unless send(#{options[:password_field_validates_length_of_options][:if].inspect})
122122
end
123123
124-
self.#{options[:crypted_password_field]}.blank?
124+
#{options[:crypted_password_field]}.blank?
125125
end
126126
127127
private
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Authlogic
2+
module Testing
3+
# = Test Unit Helpers
4+
#
5+
# Provides useful methods for testing in Test::Unit, lets you log records in, etc.
6+
module TestUnitHelpers
7+
private
8+
def session_class(record) # :nodoc:
9+
record.class.acts_as_authentic_config[:session_class].constantize
10+
end
11+
12+
# Sets the session for a record. This way when you execute a request in your test, session values will be present.
13+
def set_session_for(record)
14+
session_class = session_class(record)
15+
@request.session[session_class.session_key] = record.persistence_token
16+
@request.session["#{session_class.session_key}_id"] = record.id
17+
end
18+
19+
# Sets the cookie for a record. This way when you execute a request in your test, cookie values will be present.
20+
def set_cookie_for(record)
21+
session_class = session_class(record)
22+
@request.cookies[session_class.cookie_key] = record.persistence_token
23+
end
24+
25+
# Sets the HTTP_AUTHORIZATION header for basic HTTP auth. This way when you execute a request in your test that is trying to authenticate
26+
# with HTTP basic auth, the neccessary headers will be present.
27+
def set_http_auth_for(username, password)
28+
session_class = session_class(record)
29+
@request.env['HTTP_AUTHORIZATION'] = @controller.encode_credentials(username, password)
30+
end
31+
end
32+
end
33+
end
34+
35+
Test::Unit::TestCase.send(:include, Authlogic::Testing::TestUnitHelpers)

lib/authlogic/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def to_a
4444

4545
MAJOR = 1
4646
MINOR = 3
47-
TINY = 5
47+
TINY = 6
4848

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

0 commit comments

Comments
 (0)