authlogic 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of authlogic might be problematic. Click here for more details.

data/CHANGELOG.rdoc CHANGED
@@ -1,4 +1,12 @@
1
- == 2.0.2
1
+ == 2.0.3 released 2009-3-26
2
+
3
+ * Fixed error where default session class does not exist.
4
+ * Fixed human_name for the model to use its own human name and not delegate to the associated model. Translation should be under authlogic.models.user_session (or whatever the name of your session is).
5
+ * Fixed human_attribute_name to use Authlogic keys for translation instead of ActiveRecord: authlogic.attributes.user_session.login
6
+ * For transitioning from restful_authentication, set the REST_AUTH_SITE_KEY to '' if it doesn't exist, instead of nil.
7
+ * Completely rewrote Authlogic::Testing, it's now called Authlogic::TestCase. Testing Authlogic is much easier now. Please see Authlogic::TestCase for more info.
8
+
9
+ == 2.0.2 released 2009-3-24
2
10
 
3
11
  * Reset failed_login_count if consecutive_failed_logins_limit has been exceed and the failed_login_ban_for has passed.
4
12
  * Update test helpers to use the new configuration scheme.
data/Manifest.txt CHANGED
@@ -54,7 +54,10 @@ lib/authlogic/session/session.rb
54
54
  lib/authlogic/session/timeout.rb
55
55
  lib/authlogic/session/unauthorized_record.rb
56
56
  lib/authlogic/session/validation.rb
57
- lib/authlogic/testing/test_unit_helpers.rb
57
+ lib/authlogic/test_case.rb
58
+ lib/authlogic/test_case/mock_controller.rb
59
+ lib/authlogic/test_case/mock_cookie_jar.rb
60
+ lib/authlogic/test_case/mock_request.rb
58
61
  lib/authlogic/version.rb
59
62
  shoulda_macros/authlogic.rb
60
63
  test/acts_as_authentic_test/base_test.rb
@@ -65,6 +68,7 @@ test/acts_as_authentic_test/magic_columns_test.rb
65
68
  test/acts_as_authentic_test/password_test.rb
66
69
  test/acts_as_authentic_test/perishable_token_test.rb
67
70
  test/acts_as_authentic_test/persistence_token_test.rb
71
+ test/acts_as_authentic_test/restful_authentication_test.rb
68
72
  test/acts_as_authentic_test/session_maintenance_test.rb
69
73
  test/acts_as_authentic_test/single_access_test.rb
70
74
  test/authenticates_many_test.rb
@@ -79,9 +83,6 @@ test/fixtures/users.yml
79
83
  test/libs/company.rb
80
84
  test/libs/employee.rb
81
85
  test/libs/employee_session.rb
82
- test/libs/mock_controller.rb
83
- test/libs/mock_cookie_jar.rb
84
- test/libs/mock_request.rb
85
86
  test/libs/ordered_hash.rb
86
87
  test/libs/project.rb
87
88
  test/libs/user.rb
data/README.rdoc CHANGED
@@ -62,18 +62,18 @@ These modules are for the acts_as_authentic method you call in your model. It co
62
62
 
63
63
  These modules are for the "session side" of authentication. They create a new domain for session logic, allowing you to create, destroy, and ultimately manage your sessions.
64
64
 
65
- * Authlogic::Session::BruteForceProtection - Disables accounts after a certain number of consecutive failed login attempted.
66
- * Authlogic::Session::Callbacks - Your tools to extend Authlogic, lets you hook in and add/modify behavior, on top of overriding methods.
65
+ * Authlogic::Session::BruteForceProtection - Disables accounts after a certain number of consecutive failed logins attempted.
66
+ * Authlogic::Session::Callbacks - Your tools to extend, change, or add onto Authlogic. Lets you hook in and do just about anything you want.
67
67
  * Authlogic::Session::Cookies - Authentication via cookies.
68
68
  * Authlogic::Session::Existence - Creating, saving, and destroying objects.
69
69
  * Authlogic::Session::HttpAuth - Authentication via basic HTTP authentication.
70
70
  * Authlogic::Session::Id - Allows sessions to be separated by an id, letting you have multiple sessions for a single user.
71
71
  * Authlogic::Session::MagicColumns - Maintains "magic" database columns, similar to created_at and updated_at for ActiveRecord.
72
- * Authlogic::Session::MagicStates - Automatically validates based on the records states: active, approved, and confirmed.
72
+ * Authlogic::Session::MagicStates - Automatically validates based on the records states: active?, approved?, and confirmed?. If those methods exist for the record.
73
73
  * Authlogic::Session::Params - Authentication via params, aka single access token.
74
74
  * Authlogic::Session::Password - Authentication via a traditional username and password.
75
75
  * Authlogic::Session::Persistence - Persisting sessions / finding sessions.
76
- * Authlogic::Session::Session - Authentication via the session.
76
+ * Authlogic::Session::Session - Authentication via the session, the controller session that is.
77
77
  * Authlogic::Session::Timeout - Automatically logging out after a certain period of inactivity.
78
78
  * Authlogic::Session::UnauthorizedRecord - Handles authentication by passing an ActiveRecord object.
79
79
  * Authlogic::Session::Validation - Validation / errors.
@@ -39,7 +39,7 @@ module Authlogic
39
39
  crypto_provider_key = act_like_restful_authentication ? :crypto_provider : :transition_from_crypto_providers
40
40
  self.send("#{crypto_provider_key}=", CryptoProviders::Sha1)
41
41
  if !defined?(::REST_AUTH_SITE_KEY) || ::REST_AUTH_SITE_KEY.nil?
42
- class_eval("::REST_AUTH_SITE_KEY = nil") if !defined?(::REST_AUTH_SITE_KEY)
42
+ class_eval("::REST_AUTH_SITE_KEY = ''") if !defined?(::REST_AUTH_SITE_KEY)
43
43
  CryptoProviders::Sha1.stretches = 1
44
44
  end
45
45
  end
@@ -42,7 +42,8 @@ module Authlogic
42
42
  # * <tt>Default:</tt> "#{klass.name}Session".constantize
43
43
  # * <tt>Accepts:</tt> Class
44
44
  def session_class(value = nil)
45
- config(:session_class, value, "#{name}Session".constantize)
45
+ const = "#{name}Session".constantize rescue nil
46
+ config(:session_class, value, const)
46
47
  end
47
48
  alias_method :session_class=, :session_class
48
49
  end
@@ -12,6 +12,13 @@ module Authlogic
12
12
 
13
13
  # All configuration for the single_access token aspect of acts_as_authentic.
14
14
  module Config
15
+ # The single access token is used for authentication via URLs, such as a private feed. That being said,
16
+ # if the user changes their password, that token probably shouldn't change. If it did, the user would have
17
+ # to update all of their URLs. So be default this is option is disabled, if you need it, feel free to turn
18
+ # it on.
19
+ #
20
+ # * <tt>Default:</tt> false
21
+ # * <tt>Accepts:</tt> Boolean
15
22
  def change_single_access_token_with_password(value = nil)
16
23
  config(:change_single_access_token_with_password, value, false)
17
24
  end
@@ -39,6 +39,14 @@ module Authlogic
39
39
  # not_confirmed: Your account is not confirmed
40
40
  # not_approved: Your account is not approved
41
41
  # no_authentication_details: You did not provide any details for authentication.
42
+ # models:
43
+ # user_session: UserSession (or whatever name you are using)
44
+ # attributes:
45
+ # user_session: (or whatever name you are using)
46
+ # login: login
47
+ # email: email
48
+ # passwword: password
49
+ # remember_me: remember me
42
50
  class I18n
43
51
  class << self
44
52
  # All message translation is passed to this method. The first argument is the key for the message. The second is options, see the rails I18n library for a list of options used.
@@ -49,6 +57,7 @@ module Authlogic
49
57
  options[:default]
50
58
  end
51
59
  end
60
+ alias_method :translate, :t
52
61
  end
53
62
  end
54
63
  end
@@ -11,12 +11,14 @@ module Authlogic
11
11
  end
12
12
 
13
13
  module ClassMethods
14
- def human_attribute_name(*args)
15
- klass.human_attribute_name(*args)
14
+ def human_attribute_name(attribute_key_name, options = {})
15
+ options[:count] ||= 1
16
+ options[:default] ||= attribute_key_name.humanize
17
+ I18n.t("attributes.#{name.underscore}.#{attribute_key_name}", options)
16
18
  end
17
19
 
18
20
  def human_name(*args)
19
- klass.human_name(*args)
21
+ I18n.t("models.#{name.underscore}", {:count => 1, :default => name.humanize})
20
22
  end
21
23
 
22
24
  # For rails < 2.3, mispelled
@@ -1,8 +1,16 @@
1
1
  module Authlogic
2
2
  module Session
3
- # Just like in ActiveRecord you have before_save, before_validation, etc. You have similar callbacks with Authlogic, see the METHODS constant below. The order of execution is as follows:
3
+ # Between these callsbacks and the configuration, this is the contract between me and you to safely
4
+ # modify Authlogic's behavior. The ONLY reason these things will change is during a big version upgrade.
5
+ # For example, going from v1.X.X to 2.0.0.
4
6
  #
5
- # Here is the order they execute
7
+ # Check out the sub modules of Authlogic::Session. They are very concise, clear, and to the point. More
8
+ # importantly they use the same API that you would use to extend Authlogic. That being said, they are great
9
+ # examples of how to extend Authlogic and add / modify behavior. These modules could easily be pulled out
10
+ # into their own plugin and become an "add on" without any change.
11
+ #
12
+ # Now to the point of this module. Just like in ActiveRecord you have before_save, before_validation, etc.
13
+ # You have similar callbacks with Authlogic, see the METHODS constant below. The order of execution is as follows:
6
14
  #
7
15
  # before_persisting
8
16
  # persist
@@ -27,11 +35,13 @@ module Authlogic
27
35
  # [save record if record.changed?]
28
36
  #
29
37
  # before_destroy
38
+ # [save record if record.changed?]
30
39
  # destroy
31
40
  # after_destroy
32
41
  #
33
- # Notice the "save record if changed?" lines above. This helps with performance. If you need to make changes to the associated record, there is no need to save the record, Authlogic will do it for you.
34
- # This allow multiple modules to modify the record and execute as few queries as possible.
42
+ # Notice the "save record if changed?" lines above. This helps with performance. If you need to make
43
+ # changes to the associated record, there is no need to save the record, Authlogic will do it for you.
44
+ # This allows multiple modules to modify the record and execute as few queries as possible.
35
45
  #
36
46
  # **WARNING**: unlike ActiveRecord, these callbacks must be set up on the class level:
37
47
  #
@@ -41,7 +51,8 @@ module Authlogic
41
51
  # # ..etc
42
52
  # end
43
53
  #
44
- # You can NOT define a "before_validation" method, this is bad practice and does not allow Authlogic to extend properly with multiple extensions. Please ONLY use the method above.
54
+ # You can NOT define a "before_validation" method, this is bad practice and does not allow Authlogic
55
+ # to extend properly with multiple extensions. Please ONLY use the method above.
45
56
  module Callbacks
46
57
  METHODS = [
47
58
  "before_persisting", "persist", "after_persisting",
@@ -42,6 +42,7 @@ module Authlogic
42
42
  # the user to authenticate again if it is needed.
43
43
  def destroy
44
44
  before_destroy
45
+ save_record
45
46
  errors.clear
46
47
  @record = nil
47
48
  after_destroy
@@ -17,7 +17,7 @@ module Authlogic
17
17
  klass.class_eval do
18
18
  extend Config
19
19
  include InstanceMethods
20
- validate :validate_magic_states
20
+ validate :validate_magic_states, :unless => :disable_magic_states?
21
21
  end
22
22
  end
23
23
 
@@ -43,7 +43,7 @@ module Authlogic
43
43
  end
44
44
 
45
45
  def validate_magic_states
46
- return true if disable_magic_states? || attempted_record.nil?
46
+ return true if attempted_record.nil?
47
47
  [:active, :approved, :confirmed].each do |required_status|
48
48
  if attempted_record.respond_to?("#{required_status}?") && !attempted_record.send("#{required_status}?")
49
49
  errors.add_to_base(I18n.t("error_messages.not_#{required_status}", :default => "Your account is not #{required_status}"))
@@ -0,0 +1,64 @@
1
+ require "authlogic/test_case/mock_cookie_jar"
2
+ require "authlogic/test_case/mock_request"
3
+ require "authlogic/test_case/mock_controller"
4
+
5
+ module Authlogic
6
+ # This is a collection of methods and classes that help you easily test Authlogic. In fact, I use these same tools
7
+ # to test the internals of Authlogic.
8
+ #
9
+ # Some important things to keep in mind when testing:
10
+ #
11
+ # Authlogic requires a "connection" to your controller. In the same manner that ActiveRecord requires a connection to
12
+ # your database. It can't do anything until it gets connnected. That being said, Authlogic will raise an
13
+ # Authlogic::Session::Activation::NotActivatedError any time you try to instantiate an object without a "connection".
14
+ # So before you do anything with Authlogic, you need to connect it. Let's walk through how to do this in tests:
15
+ #
16
+ # === Functional tests
17
+ #
18
+ # You shouldn't have to do anything. Authlogic automatically sets a before_filter in your ApplicationController that
19
+ # conntects Authlogic to the controller. So as soon as you make a request in your tests, it will connect Authlogic
20
+ # for you.
21
+ #
22
+ # === Integration tests
23
+ #
24
+ # Again, just like functional tests, you don't have to do anything. As soon as you make a request, Authlogic will be
25
+ # conntected.
26
+ #
27
+ # === Unit tests
28
+ #
29
+ # Now here is the tricky part of testing. Since there really is no controller here you need to "fake" Authlogic into
30
+ # thinking there is. Don't worry, because the Authlogic::TestCase takes care of this for you. Just do the following
31
+ # in your test's setup and you are good to go:
32
+ #
33
+ # setup :activate_authlogic
34
+ #
35
+ # activate_authlogic is a method provided to you by this TestCase module.
36
+ #
37
+ # You can even test off of this controller to make sure everything is good. For example:
38
+ #
39
+ # ben = users(:ben)
40
+ # assert_nil controller.session["user_credentials"]
41
+ # assert UserSession.create(ben)
42
+ # assert_equal controller.session["user_credentials"], ben.persistence_token
43
+ #
44
+ # 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.
45
+ #
46
+ # === How to use
47
+ #
48
+ # Just require the file in your test_helper.rb file.
49
+ #
50
+ # require "authlogic/test_case"
51
+ module TestCase
52
+ # Activates authlogic with an Authlogic::TestCase::MockController object.
53
+ def activate_authlogic
54
+ Authlogic::Session::Base.controller = controller
55
+ end
56
+
57
+ # The Authlogic::TestCase::MockController object passed to Authlogic to activate it.
58
+ def controller
59
+ @controller ||= Authlogic::TestCase::MockController.new
60
+ end
61
+ end
62
+
63
+ ::Test::Unit::TestCase.send(:include, TestCase)
64
+ end
@@ -0,0 +1,39 @@
1
+ module Authlogic
2
+ module TestCase
3
+ class MockController < ControllerAdapters::AbstractAdapter
4
+ attr_accessor :http_user, :http_password
5
+ attr_writer :request_content_type
6
+
7
+ def initialize
8
+ end
9
+
10
+ def authenticate_with_http_basic(&block)
11
+ yield http_user, http_password
12
+ end
13
+
14
+ def cookies
15
+ @cookies ||= MockCookieJar.new
16
+ end
17
+
18
+ def cookie_domain
19
+ nil
20
+ end
21
+
22
+ def params
23
+ @params ||= {}
24
+ end
25
+
26
+ def request
27
+ @request ||= MockRequest.new
28
+ end
29
+
30
+ def request_content_type
31
+ @request_content_type ||= "text/html"
32
+ end
33
+
34
+ def session
35
+ @session ||= {}
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ module Authlogic
2
+ module TestCase
3
+ class MockCookieJar < Hash
4
+ def [](key)
5
+ hash = super
6
+ hash && hash[:value]
7
+ end
8
+
9
+ def delete(key, options = {})
10
+ super(key)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Authlogic
2
+ module TestCase
3
+ class MockRequest
4
+ def remote_ip
5
+ "1.1.1.1"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -44,7 +44,7 @@ module Authlogic # :nodoc:
44
44
 
45
45
  MAJOR = 2
46
46
  MINOR = 0
47
- TINY = 2
47
+ TINY = 3
48
48
 
49
49
  # The current version as a Version instance
50
50
  CURRENT = new(MAJOR, MINOR, TINY)
@@ -85,39 +85,6 @@ module ActsAsAuthenticTest
85
85
  assert_equal [], User.transition_from_crypto_providers
86
86
  end
87
87
 
88
- def test_act_like_restful_authentication_config
89
- assert !User.act_like_restful_authentication
90
- assert !Employee.act_like_restful_authentication
91
-
92
- User.act_like_restful_authentication = true
93
- assert User.act_like_restful_authentication
94
- assert_equal Authlogic::CryptoProviders::Sha1, User.crypto_provider
95
- assert defined?(::REST_AUTH_SITE_KEY)
96
- assert_equal 1, Authlogic::CryptoProviders::Sha1.stretches
97
-
98
- User.act_like_restful_authentication false
99
- assert !User.act_like_restful_authentication
100
-
101
- User.crypto_provider = Authlogic::CryptoProviders::Sha512
102
- User.transition_from_crypto_providers = []
103
- end
104
-
105
- def test_transition_from_restful_authentication_config
106
- assert !User.transition_from_restful_authentication
107
- assert !Employee.transition_from_restful_authentication
108
-
109
- User.transition_from_restful_authentication = true
110
- assert User.transition_from_restful_authentication
111
- assert defined?(::REST_AUTH_SITE_KEY)
112
- assert_equal 1, Authlogic::CryptoProviders::Sha1.stretches
113
-
114
- User.transition_from_restful_authentication false
115
- assert !User.transition_from_restful_authentication
116
-
117
- User.crypto_provider = Authlogic::CryptoProviders::Sha512
118
- User.transition_from_crypto_providers = []
119
- end
120
-
121
88
  def test_validates_length_of_password
122
89
  u = User.new
123
90
  u.password_confirmation = "test2"
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class RestfulAuthenticationTest < ActiveSupport::TestCase
5
+ def test_act_like_restful_authentication_config
6
+ assert !User.act_like_restful_authentication
7
+ assert !Employee.act_like_restful_authentication
8
+
9
+ User.act_like_restful_authentication = true
10
+ assert User.act_like_restful_authentication
11
+ assert_equal Authlogic::CryptoProviders::Sha1, User.crypto_provider
12
+ assert defined?(::REST_AUTH_SITE_KEY)
13
+ assert_equal '', ::REST_AUTH_SITE_KEY
14
+ assert_equal 1, Authlogic::CryptoProviders::Sha1.stretches
15
+
16
+ User.act_like_restful_authentication false
17
+ assert !User.act_like_restful_authentication
18
+
19
+ User.crypto_provider = Authlogic::CryptoProviders::Sha512
20
+ User.transition_from_crypto_providers = []
21
+ end
22
+
23
+ def test_transition_from_restful_authentication_config
24
+ assert !User.transition_from_restful_authentication
25
+ assert !Employee.transition_from_restful_authentication
26
+
27
+ User.transition_from_restful_authentication = true
28
+ assert User.transition_from_restful_authentication
29
+ assert defined?(::REST_AUTH_SITE_KEY)
30
+ assert_equal '', ::REST_AUTH_SITE_KEY
31
+ assert_equal 1, Authlogic::CryptoProviders::Sha1.stretches
32
+
33
+ User.transition_from_restful_authentication false
34
+ assert !User.transition_from_restful_authentication
35
+
36
+ User.crypto_provider = Authlogic::CryptoProviders::Sha512
37
+ User.transition_from_crypto_providers = []
38
+ end
39
+ end
40
+ end
@@ -18,49 +18,49 @@ module ActsAsAuthenticTest
18
18
  def test_update_session_after_password_modify
19
19
  ben = users(:ben)
20
20
  UserSession.create(ben)
21
- old_session_key = @controller.session["user_credentials"]
22
- old_cookie_key = @controller.cookies["user_credentials"]
21
+ old_session_key = controller.session["user_credentials"]
22
+ old_cookie_key = controller.cookies["user_credentials"]
23
23
  ben.password = "newpass"
24
24
  ben.password_confirmation = "newpass"
25
25
  assert ben.save
26
- assert @controller.session["user_credentials"]
27
- assert @controller.cookies["user_credentials"]
28
- assert_not_equal @controller.session["user_credentials"], old_session_key
29
- assert_not_equal @controller.cookies["user_credentials"], old_cookie_key
26
+ assert controller.session["user_credentials"]
27
+ assert controller.cookies["user_credentials"]
28
+ assert_not_equal controller.session["user_credentials"], old_session_key
29
+ assert_not_equal controller.cookies["user_credentials"], old_cookie_key
30
30
  end
31
31
 
32
32
  def test_no_session_update_after_modify
33
33
  ben = users(:ben)
34
34
  UserSession.create(ben)
35
- old_session_key = @controller.session["user_credentials"]
36
- old_cookie_key = @controller.cookies["user_credentials"]
35
+ old_session_key = controller.session["user_credentials"]
36
+ old_cookie_key = controller.cookies["user_credentials"]
37
37
  ben.first_name = "Ben"
38
38
  assert ben.save
39
- assert_equal @controller.session["user_credentials"], old_session_key
40
- assert_equal @controller.cookies["user_credentials"], old_cookie_key
39
+ assert_equal controller.session["user_credentials"], old_session_key
40
+ assert_equal controller.cookies["user_credentials"], old_cookie_key
41
41
  end
42
42
 
43
43
  def test_creating_other_user
44
44
  ben = users(:ben)
45
45
  UserSession.create(ben)
46
- old_session_key = @controller.session["user_credentials"]
47
- old_cookie_key = @controller.cookies["user_credentials"]
46
+ old_session_key = controller.session["user_credentials"]
47
+ old_cookie_key = controller.cookies["user_credentials"]
48
48
  assert User.create(:login => "awesome", :password => "saweet", :password_confirmation => "saweet", :email => "awesome@saweet.com")
49
- assert_equal @controller.session["user_credentials"], old_session_key
50
- assert_equal @controller.cookies["user_credentials"], old_cookie_key
49
+ assert_equal controller.session["user_credentials"], old_session_key
50
+ assert_equal controller.cookies["user_credentials"], old_cookie_key
51
51
  end
52
52
 
53
53
  def test_updating_other_user
54
54
  ben = users(:ben)
55
55
  UserSession.create(ben)
56
- old_session_key = @controller.session["user_credentials"]
57
- old_cookie_key = @controller.cookies["user_credentials"]
56
+ old_session_key = controller.session["user_credentials"]
57
+ old_cookie_key = controller.cookies["user_credentials"]
58
58
  zack = users(:zack)
59
59
  zack.password = "newpass"
60
60
  zack.password_confirmation = "newpass"
61
61
  assert zack.save
62
- assert_equal @controller.session["user_credentials"], old_session_key
63
- assert_equal @controller.cookies["user_credentials"], old_cookie_key
62
+ assert_equal controller.session["user_credentials"], old_session_key
63
+ assert_equal controller.cookies["user_credentials"], old_cookie_key
64
64
  end
65
65
 
66
66
  def test_resetting_password_when_logged_out
@@ -36,7 +36,7 @@ module SessionTest
36
36
  def test_init
37
37
  UserSession.controller = nil
38
38
  assert_raise(Authlogic::Session::Activation::NotActivatedError) { UserSession.new }
39
- UserSession.controller = @controller
39
+ UserSession.controller = controller
40
40
  end
41
41
  end
42
42
  end
@@ -8,7 +8,7 @@ module SessionTest
8
8
  end
9
9
 
10
10
  def test_human_name
11
- assert_equal "User", UserSession.human_name
11
+ assert_equal "Usersession", UserSession.human_name
12
12
  end
13
13
 
14
14
  def test_self_and_descendents_from_active_record
@@ -91,16 +91,16 @@ module SessionTest
91
91
  ben = users(:ben)
92
92
  session = UserSession.new(ben)
93
93
  assert session.save
94
- assert_equal ben.persistence_token, @controller.cookies["user_credentials"]
94
+ assert_equal ben.persistence_token, controller.cookies["user_credentials"]
95
95
  end
96
96
 
97
97
  def test_after_destroy_destroy_cookie
98
98
  ben = users(:ben)
99
99
  set_cookie_for(ben)
100
100
  session = UserSession.find
101
- assert @controller.cookies["user_credentials"]
101
+ assert controller.cookies["user_credentials"]
102
102
  assert session.destroy
103
- assert !@controller.cookies["user_credentials"]
103
+ assert !controller.cookies["user_credentials"]
104
104
  end
105
105
  end
106
106
  end
@@ -31,22 +31,22 @@ module SessionTest
31
31
  assert !session.persisting?
32
32
  assert !session.unauthorized_record
33
33
  assert !session.record
34
- assert_nil @controller.session["user_credentials"]
34
+ assert_nil controller.session["user_credentials"]
35
35
 
36
36
  set_request_content_type("text/plain")
37
37
  assert !session.persisting?
38
38
  assert !session.unauthorized_record
39
- assert_nil @controller.session["user_credentials"]
39
+ assert_nil controller.session["user_credentials"]
40
40
 
41
41
  set_request_content_type("application/atom+xml")
42
42
  assert session.persisting?
43
43
  assert_equal ben, session.record
44
- assert_nil @controller.session["user_credentials"] # should not persist since this is single access
44
+ assert_nil controller.session["user_credentials"] # should not persist since this is single access
45
45
 
46
46
  set_request_content_type("application/rss+xml")
47
47
  assert session.persisting?
48
48
  assert_equal ben, session.unauthorized_record
49
- assert_nil @controller.session["user_credentials"]
49
+ assert_nil controller.session["user_credentials"]
50
50
  end
51
51
  end
52
52
  end
@@ -18,41 +18,41 @@ module SessionTest
18
18
  set_session_for(ben)
19
19
  assert session = UserSession.find
20
20
  assert_equal ben, session.record
21
- assert_equal ben.persistence_token, @controller.session["user_credentials"]
21
+ assert_equal ben.persistence_token, controller.session["user_credentials"]
22
22
  end
23
23
 
24
24
  def test_persist_persist_by_session_with_token_only
25
25
  ben = users(:ben)
26
26
  set_session_for(ben)
27
- @controller.session["user_credentials_id"] = nil
27
+ controller.session["user_credentials_id"] = nil
28
28
  assert session = UserSession.find
29
29
  assert_equal ben, session.record
30
- assert_equal ben.persistence_token, @controller.session["user_credentials"]
30
+ assert_equal ben.persistence_token, controller.session["user_credentials"]
31
31
  end
32
32
 
33
33
  def test_after_save_update_session
34
34
  ben = users(:ben)
35
35
  session = UserSession.new(ben)
36
- assert @controller.session["user_credentials"].blank?
36
+ assert controller.session["user_credentials"].blank?
37
37
  assert session.save
38
- assert_equal ben.persistence_token, @controller.session["user_credentials"]
38
+ assert_equal ben.persistence_token, controller.session["user_credentials"]
39
39
  end
40
40
 
41
41
  def test_after_destroy_update_session
42
42
  ben = users(:ben)
43
43
  set_session_for(ben)
44
- assert_equal ben.persistence_token, @controller.session["user_credentials"]
44
+ assert_equal ben.persistence_token, controller.session["user_credentials"]
45
45
  assert session = UserSession.find
46
46
  assert session.destroy
47
- assert @controller.session["user_credentials"].blank?
47
+ assert controller.session["user_credentials"].blank?
48
48
  end
49
49
 
50
50
  def test_after_persisting_update_session
51
51
  ben = users(:ben)
52
52
  set_cookie_for(ben)
53
- assert @controller.session["user_credentials"].blank?
53
+ assert controller.session["user_credentials"].blank?
54
54
  assert UserSession.find
55
- assert_equal ben.persistence_token, @controller.session["user_credentials"]
55
+ assert_equal ben.persistence_token, controller.session["user_credentials"]
56
56
  end
57
57
  end
58
58
  end
@@ -25,7 +25,7 @@ module SessionTest
25
25
  assert session.stale?
26
26
  assert_equal ben, session.stale_record
27
27
  assert_nil session.record
28
- assert_nil @controller.session["user_credentials_id"]
28
+ assert_nil controller.session["user_credentials_id"]
29
29
 
30
30
  set_session_for(ben)
31
31
 
data/test/test_helper.rb CHANGED
@@ -72,9 +72,7 @@ ActiveRecord::Schema.define(:version => 1) do
72
72
  end
73
73
 
74
74
  require File.dirname(__FILE__) + '/../lib/authlogic' unless defined?(Authlogic)
75
- require File.dirname(__FILE__) + '/libs/mock_request'
76
- require File.dirname(__FILE__) + '/libs/mock_cookie_jar'
77
- require File.dirname(__FILE__) + '/libs/mock_controller'
75
+ require File.dirname(__FILE__) + '/../lib/authlogic/test_case'
78
76
  require File.dirname(__FILE__) + '/libs/project'
79
77
  require File.dirname(__FILE__) + '/libs/employee'
80
78
  require File.dirname(__FILE__) + '/libs/employee_session'
@@ -94,11 +92,6 @@ class ActiveSupport::TestCase
94
92
  setup :activate_authlogic
95
93
 
96
94
  private
97
- def activate_authlogic
98
- @controller = MockController.new
99
- Authlogic::Session::Base.controller = @controller
100
- end
101
-
102
95
  def password_for(user)
103
96
  case user
104
97
  when users(:ben)
@@ -110,43 +103,43 @@ class ActiveSupport::TestCase
110
103
 
111
104
  def http_basic_auth_for(user = nil, &block)
112
105
  unless user.blank?
113
- @controller.http_user = user.login
114
- @controller.http_password = password_for(user)
106
+ controller.http_user = user.login
107
+ controller.http_password = password_for(user)
115
108
  end
116
109
  yield
117
- @controller.http_user = @controller.http_password = nil
110
+ controller.http_user = controller.http_password = nil
118
111
  end
119
112
 
120
113
  def set_cookie_for(user, id = nil)
121
- @controller.cookies["user_credentials"] = {:value => user.persistence_token, :expires => nil}
114
+ controller.cookies["user_credentials"] = {:value => user.persistence_token, :expires => nil}
122
115
  end
123
116
 
124
117
  def unset_cookie
125
- @controller.cookies["user_credentials"] = nil
118
+ controller.cookies["user_credentials"] = nil
126
119
  end
127
120
 
128
121
  def set_params_for(user, id = nil)
129
- @controller.params["user_credentials"] = user.single_access_token
122
+ controller.params["user_credentials"] = user.single_access_token
130
123
  end
131
124
 
132
125
  def unset_params
133
- @controller.params["user_credentials"] = nil
126
+ controller.params["user_credentials"] = nil
134
127
  end
135
128
 
136
129
  def set_request_content_type(type)
137
- @controller.request_content_type = type
130
+ controller.request_content_type = type
138
131
  end
139
132
 
140
133
  def unset_request_content_type
141
- @controller.request_content_type = nil
134
+ controller.request_content_type = nil
142
135
  end
143
136
 
144
137
  def set_session_for(user, id = nil)
145
- @controller.session["user_credentials"] = user.persistence_token
146
- @controller.session["user_credentials_id"] = user.id
138
+ controller.session["user_credentials"] = user.persistence_token
139
+ controller.session["user_credentials_id"] = user.id
147
140
  end
148
141
 
149
142
  def unset_session
150
- @controller.session["user_credentials"] = @controller.session["user_credentials_id"] = nil
143
+ controller.session["user_credentials"] = controller.session["user_credentials_id"] = nil
151
144
  end
152
145
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authlogic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-24 00:00:00 -04:00
12
+ date: 2009-03-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -99,7 +99,10 @@ files:
99
99
  - lib/authlogic/session/timeout.rb
100
100
  - lib/authlogic/session/unauthorized_record.rb
101
101
  - lib/authlogic/session/validation.rb
102
- - lib/authlogic/testing/test_unit_helpers.rb
102
+ - lib/authlogic/test_case.rb
103
+ - lib/authlogic/test_case/mock_controller.rb
104
+ - lib/authlogic/test_case/mock_cookie_jar.rb
105
+ - lib/authlogic/test_case/mock_request.rb
103
106
  - lib/authlogic/version.rb
104
107
  - shoulda_macros/authlogic.rb
105
108
  - test/acts_as_authentic_test/base_test.rb
@@ -110,6 +113,7 @@ files:
110
113
  - test/acts_as_authentic_test/password_test.rb
111
114
  - test/acts_as_authentic_test/perishable_token_test.rb
112
115
  - test/acts_as_authentic_test/persistence_token_test.rb
116
+ - test/acts_as_authentic_test/restful_authentication_test.rb
113
117
  - test/acts_as_authentic_test/session_maintenance_test.rb
114
118
  - test/acts_as_authentic_test/single_access_test.rb
115
119
  - test/authenticates_many_test.rb
@@ -124,9 +128,6 @@ files:
124
128
  - test/libs/company.rb
125
129
  - test/libs/employee.rb
126
130
  - test/libs/employee_session.rb
127
- - test/libs/mock_controller.rb
128
- - test/libs/mock_cookie_jar.rb
129
- - test/libs/mock_request.rb
130
131
  - test/libs/ordered_hash.rb
131
132
  - test/libs/project.rb
132
133
  - test/libs/user.rb
@@ -190,6 +191,7 @@ test_files:
190
191
  - test/acts_as_authentic_test/password_test.rb
191
192
  - test/acts_as_authentic_test/perishable_token_test.rb
192
193
  - test/acts_as_authentic_test/persistence_token_test.rb
194
+ - test/acts_as_authentic_test/restful_authentication_test.rb
193
195
  - test/acts_as_authentic_test/session_maintenance_test.rb
194
196
  - test/acts_as_authentic_test/single_access_test.rb
195
197
  - test/crypto_provider_test/aes256_test.rb
@@ -1,39 +0,0 @@
1
- module Authlogic
2
- # Various utilities to help with testing. Keep in mind, Authlogic is thoroughly tested for you, the only thing you should be
3
- # testing is code you write, such as code in your controller.
4
- module Testing
5
- # Provides useful methods for testing in Test::Unit, lets you log records in, etc. Just include this in your test_helper filter:
6
- #
7
- # require "authlogic/testing/test_unit_helpers"
8
- #
9
- # Then you will have the methods below to use in your tests.
10
- module TestUnitHelpers
11
- private
12
- def session_class(record)
13
- record.class.session_class
14
- end
15
-
16
- # Sets the session for a record. This way when you execute a request in your test, session values will be present.
17
- def set_session_for(record)
18
- session_class = session_class(record)
19
- @request.session[session_class.session_key] = record.persistence_token
20
- @request.session["#{session_class.session_key}_#{record.class.primary_key}"] = record.id
21
- end
22
-
23
- # Sets the cookie for a record. This way when you execute a request in your test, cookie values will be present.
24
- def set_cookie_for(record)
25
- session_class = session_class(record)
26
- @request.cookies[session_class.cookie_key] = record.persistence_token
27
- end
28
-
29
- # Sets the HTTP_AUTHORIZATION header for basic HTTP auth. This way when you execute a request in your test that is trying to authenticate
30
- # with HTTP basic auth, the neccessary headers will be present.
31
- def set_http_auth_for(username, password)
32
- session_class = session_class(record)
33
- @request.env['HTTP_AUTHORIZATION'] = @controller.encode_credentials(username, password)
34
- end
35
- end
36
- end
37
- end
38
-
39
- Test::Unit::TestCase.send(:include, Authlogic::Testing::TestUnitHelpers)
@@ -1,35 +0,0 @@
1
- class MockController < Authlogic::ControllerAdapters::AbstractAdapter
2
- attr_accessor :http_user, :http_password
3
- attr_writer :request_content_type
4
-
5
- def initialize
6
- end
7
-
8
- def authenticate_with_http_basic(&block)
9
- yield http_user, http_password
10
- end
11
-
12
- def cookies
13
- @cookies ||= MockCookieJar.new
14
- end
15
-
16
- def cookie_domain
17
- nil
18
- end
19
-
20
- def params
21
- @params ||= {}
22
- end
23
-
24
- def request
25
- @request ||= MockRequest.new
26
- end
27
-
28
- def request_content_type
29
- @request_content_type ||= "text/html"
30
- end
31
-
32
- def session
33
- @session ||= {}
34
- end
35
- end
@@ -1,10 +0,0 @@
1
- class MockCookieJar < Hash
2
- def [](key)
3
- hash = super
4
- hash && hash[:value]
5
- end
6
-
7
- def delete(key, options = {})
8
- super(key)
9
- end
10
- end
@@ -1,5 +0,0 @@
1
- class MockRequest
2
- def remote_ip
3
- "1.1.1.1"
4
- end
5
- end