authlogic 1.1.0 → 1.1.1

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.

Files changed (39) hide show
  1. data/CHANGELOG.rdoc +11 -2
  2. data/Manifest +9 -3
  3. data/README.rdoc +32 -7
  4. data/Rakefile +1 -1
  5. data/authlogic.gemspec +4 -7
  6. data/lib/authlogic.rb +2 -4
  7. data/lib/authlogic/controller_adapters/abstract_adapter.rb +4 -0
  8. data/lib/authlogic/controller_adapters/rails_adapter.rb +4 -0
  9. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic.rb +2 -76
  10. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/config.rb +132 -0
  11. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb +77 -116
  12. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/logged_in.rb +35 -24
  13. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/persistence.rb +51 -44
  14. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/session_maintenance.rb +64 -54
  15. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/single_access.rb +61 -0
  16. data/lib/authlogic/session/base.rb +20 -9
  17. data/lib/authlogic/session/config.rb +54 -63
  18. data/lib/authlogic/session/cookies.rb +2 -2
  19. data/lib/authlogic/session/params.rb +9 -6
  20. data/lib/authlogic/session/session.rb +3 -3
  21. data/lib/authlogic/version.rb +1 -1
  22. data/shoulda_macros/authlogic.rb +13 -0
  23. data/test/fixtures/employees.yml +2 -2
  24. data/test/fixtures/users.yml +2 -0
  25. data/test/libs/mock_controller.rb +5 -0
  26. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/config_test.rb +36 -0
  27. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/credentials_test.rb +129 -0
  28. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/logged_in_test.rb +24 -0
  29. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/persistence_test.rb +45 -0
  30. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/session_maintenance_test.rb +62 -0
  31. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/single_access_test.rb +41 -0
  32. data/test/session_tests/base_test.rb +15 -0
  33. data/test/session_tests/config_test.rb +31 -14
  34. data/test/session_tests/params_test.rb +17 -1
  35. data/test/test_helper.rb +10 -2
  36. metadata +18 -17
  37. data/lib/authlogic/session/openid.rb +0 -106
  38. data/lib/authlogic/testing/shoulda_macros.rb +0 -17
  39. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_test.rb +0 -217
@@ -1,5 +1,6 @@
1
1
  class MockController < Authlogic::ControllerAdapters::AbstractAdapter
2
2
  attr_accessor :http_user, :http_password
3
+ attr_writer :request_content_type
3
4
 
4
5
  def initialize
5
6
  end
@@ -20,6 +21,10 @@ class MockController < Authlogic::ControllerAdapters::AbstractAdapter
20
21
  @request ||= MockRequest.new
21
22
  end
22
23
 
24
+ def request_content_type
25
+ @request_content_type ||= "text/html"
26
+ end
27
+
23
28
  def session
24
29
  @session ||= {}
25
30
  end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class ConfigTest < ActiveSupport::TestCase
7
+ def test_first_column_to_exist
8
+ assert_equal :login, User.first_column_to_exist(:login, :crypted_password)
9
+ assert_equal nil, User.first_column_to_exist(nil, :unknown)
10
+ assert_equal :login, User.first_column_to_exist(:unknown, :login)
11
+ end
12
+
13
+ def test_acts_as_authentic_config
14
+ default_config = {
15
+ :confirm_password_did_not_match_message => "did not match",
16
+ :single_access_token_field => :single_access_token,
17
+ :login_field_regex => /\A\w[\w\.\-_@ ]+\z/,
18
+ :session_ids => [nil],
19
+ :login_field_regex_failed_message => "use only letters, numbers, spaces, and .-_@ please.",
20
+ :remember_token_field => :remember_token,
21
+ :password_field => :password,
22
+ :logged_in_timeout => 600,
23
+ :password_salt_field => :password_salt,
24
+ :login_field_type => :login,
25
+ :crypto_provider => Authlogic::CryptoProviders::Sha512,
26
+ :password_blank_message => "can not be blank",
27
+ :crypted_password_field => :crypted_password,
28
+ :session_class => "UserSession",
29
+ :login_field => :login
30
+ }
31
+ assert_equal default_config, User.acts_as_authentic_config
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,129 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class CredentialsTest < ActiveSupport::TestCase
7
+ def test_user_validations
8
+ user = User.new
9
+ assert !user.valid?
10
+ assert user.errors.on(:login)
11
+ assert user.errors.on(:password)
12
+
13
+ user.login = "a"
14
+ assert !user.valid?
15
+ assert user.errors.on(:login)
16
+ assert user.errors.on(:password)
17
+
18
+ user.login = "%ben*"
19
+ assert !user.valid?
20
+ assert user.errors.on(:login)
21
+ assert user.errors.on(:password)
22
+
23
+ user.login = "bjohnson"
24
+ assert !user.valid?
25
+ assert user.errors.on(:login)
26
+ assert user.errors.on(:password)
27
+
28
+ user.login = "my login"
29
+ assert !user.valid?
30
+ assert !user.errors.on(:login)
31
+ assert user.errors.on(:password)
32
+
33
+ user.password = "my pass"
34
+ assert !user.valid?
35
+ assert !user.errors.on(:password)
36
+ assert user.errors.on(:confirm_password)
37
+
38
+ user.confirm_password = "my pizass"
39
+ assert !user.valid?
40
+ assert !user.errors.on(:password)
41
+ assert user.errors.on(:confirm_password)
42
+
43
+ user.confirm_password = "my pass"
44
+ assert user.valid?
45
+ end
46
+
47
+ def test_employee_validations
48
+ employee = Employee.new
49
+ employee.password = "pass"
50
+ employee.confirm_password = "pass"
51
+
52
+ assert !employee.valid?
53
+ assert employee.errors.on(:email)
54
+
55
+ employee.email = "fdsf"
56
+ assert !employee.valid?
57
+ assert employee.errors.on(:email)
58
+
59
+ employee.email = "fake@email.fake"
60
+ assert !employee.valid?
61
+ assert employee.errors.on(:email)
62
+
63
+ employee.email = "notfake@email.com"
64
+ assert employee.valid?
65
+ end
66
+
67
+ def test_friendly_unique_token
68
+ assert_equal 20, User.friendly_unique_token.length
69
+ assert_equal 20, Employee.friendly_unique_token.length # make sure encryptions use hashes also
70
+
71
+ unique_tokens = []
72
+ 1000.times { unique_tokens << User.friendly_unique_token }
73
+ unique_tokens.uniq!
74
+
75
+ assert_equal 1000, unique_tokens.size
76
+ end
77
+
78
+ def test_password
79
+ user = User.new
80
+ user.password = "sillywilly"
81
+ assert user.crypted_password
82
+ assert user.password_salt
83
+ assert user.remember_token
84
+ assert_equal true, user.tried_to_set_password
85
+ assert_nil user.password
86
+
87
+ employee = Employee.new
88
+ employee.password = "awesome"
89
+ assert employee.crypted_password
90
+ assert employee.remember_token
91
+ assert_equal true, employee.tried_to_set_password
92
+ assert_nil employee.password
93
+ end
94
+
95
+ def test_valid_password
96
+ ben = users(:ben)
97
+ assert ben.valid_password?("benrocks")
98
+ assert ben.valid_password?(ben.crypted_password)
99
+
100
+ drew = employees(:drew)
101
+ assert drew.valid_password?("drewrocks")
102
+ assert drew.valid_password?(drew.crypted_password)
103
+ end
104
+
105
+ def test_reset_password
106
+ ben = users(:ben)
107
+ UserSession.create(ben)
108
+ assert UserSession.find
109
+
110
+ old_password = ben.crypted_password
111
+ old_salt = ben.password_salt
112
+ old_remember_token = ben.remember_token
113
+ ben.reset_password
114
+ assert_not_equal old_password, ben.crypted_password
115
+ assert_not_equal old_salt, ben.password_salt
116
+ assert_not_equal old_remember_token, ben.remember_token
117
+ assert UserSession.find
118
+
119
+ ben.reset_password!
120
+ ben.reload
121
+ assert_not_equal old_password, ben.crypted_password
122
+ assert_not_equal old_salt, ben.password_salt
123
+ assert_not_equal old_remember_token, ben.remember_token
124
+ assert !UserSession.find
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class LoggedInTest < ActiveSupport::TestCase
7
+ def test_named_scopes
8
+ assert_equal 0, User.logged_in.count
9
+ assert_equal User.count, User.logged_out.count
10
+ http_basic_auth_for(users(:ben)) { UserSession.find }
11
+ assert_equal 1, User.logged_in.count
12
+ assert_equal User.count - 1, User.logged_out.count
13
+ end
14
+
15
+ def test_logged_in
16
+ ben = users(:ben)
17
+ assert !ben.logged_in?
18
+ assert ben.update_attribute(:last_request_at, Time.now)
19
+ assert ben.logged_in?
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class PersistenceTest < ActiveSupport::TestCase
7
+ def test_unique_token
8
+ assert_equal 128, User.unique_token.length
9
+ assert_equal 128, Employee.unique_token.length # make sure encryptions use hashes also
10
+
11
+ unique_tokens = []
12
+ 1000.times { unique_tokens << User.unique_token }
13
+ unique_tokens.uniq!
14
+
15
+ assert_equal 1000, unique_tokens.size
16
+ end
17
+
18
+ def test_forget_all
19
+ http_basic_auth_for(users(:ben)) { UserSession.find }
20
+ http_basic_auth_for(users(:zack)) { UserSession.find(:ziggity_zack) }
21
+ assert UserSession.find
22
+ assert UserSession.find(:ziggity_zack)
23
+ User.forget_all!
24
+ assert !UserSession.find
25
+ assert !UserSession.find(:ziggity_zack)
26
+ end
27
+
28
+ def test_forget
29
+ ben = users(:ben)
30
+ zack = users(:zack)
31
+ http_basic_auth_for(ben) { UserSession.find }
32
+ http_basic_auth_for(zack) { UserSession.find(:ziggity_zack) }
33
+
34
+ assert ben.reload.logged_in?
35
+ assert zack.reload.logged_in?
36
+
37
+ ben.forget!
38
+
39
+ assert !UserSession.find
40
+ assert UserSession.find(:ziggity_zack)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class SessionMaintenanceTest < ActiveSupport::TestCase
7
+ def test_login_after_create
8
+ assert User.create(:login => "awesome", :password => "saweet", :confirm_password => "saweet")
9
+ assert UserSession.find
10
+ end
11
+
12
+ def test_update_session_after_password_modify
13
+ ben = users(:ben)
14
+ UserSession.create(ben)
15
+ old_session_key = @controller.session["user_credentials"]
16
+ old_cookie_key = @controller.cookies["user_credentials"]
17
+ ben.password = "newpass"
18
+ ben.confirm_password = "newpass"
19
+ ben.save
20
+ assert @controller.session["user_credentials"]
21
+ assert @controller.cookies["user_credentials"]
22
+ assert_not_equal @controller.session["user_credentials"], old_session_key
23
+ assert_not_equal @controller.cookies["user_credentials"], old_cookie_key
24
+ end
25
+
26
+ def test_no_session_update_after_modify
27
+ ben = users(:ben)
28
+ UserSession.create(ben)
29
+ old_session_key = @controller.session["user_credentials"]
30
+ old_cookie_key = @controller.cookies["user_credentials"]
31
+ ben.first_name = "Ben"
32
+ ben.save
33
+ assert_equal @controller.session["user_credentials"], old_session_key
34
+ assert_equal @controller.cookies["user_credentials"], old_cookie_key
35
+ end
36
+
37
+ def test_updating_other_user
38
+ ben = users(:ben)
39
+ UserSession.create(ben)
40
+ old_session_key = @controller.session["user_credentials"]
41
+ old_cookie_key = @controller.cookies["user_credentials"]
42
+ zack = users(:zack)
43
+ zack.password = "newpass"
44
+ zack.confirm_password = "newpass"
45
+ zack.save
46
+ assert_equal @controller.session["user_credentials"], old_session_key
47
+ assert_equal @controller.cookies["user_credentials"], old_cookie_key
48
+ end
49
+
50
+ def test_resetting_password_when_logged_out
51
+ ben = users(:ben)
52
+ assert !UserSession.find
53
+ ben.password = "newpass"
54
+ ben.confirm_password = "newpass"
55
+ ben.save
56
+ assert UserSession.find
57
+ assert_equal ben, UserSession.find.record
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class SingleAccessTest < ActiveSupport::TestCase
7
+ def test_before_validation
8
+ user = User.new
9
+ assert_equal nil, user.single_access_token
10
+ assert !user.valid?
11
+ assert user.single_access_token
12
+ end
13
+
14
+ def test_change_with_password
15
+ ben = users(:ben)
16
+ old_single_access_token = ben.single_access_token
17
+
18
+ User.acts_as_authentic(:change_single_access_token_with_password => true)
19
+ ben.password = "new_pass"
20
+ assert_not_equal old_single_access_token, ben.single_access_token
21
+
22
+ ben.reload
23
+ User.acts_as_authentic(:change_single_access_token_with_password => false)
24
+ ben.password = "new_pass"
25
+ assert_equal old_single_access_token, ben.single_access_token
26
+ end
27
+
28
+ def test_reset_single_access_token
29
+ ben = users(:ben)
30
+ old_single_access_token = ben.single_access_token
31
+ ben.reset_single_access_token
32
+ assert_not_equal old_single_access_token, ben.single_access_token
33
+ ben.reload
34
+ assert_equal old_single_access_token, ben.single_access_token
35
+ ben.reset_single_access_token!
36
+ assert_not_equal old_single_access_token, ben.single_access_token
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -48,6 +48,21 @@ module SessionTests
48
48
  session = UserSession.find
49
49
  assert session
50
50
  assert !session.record.last_request_at.nil?
51
+
52
+ UserSession.last_request_at_threshold = 2.seconds
53
+ assert_equal 2.seconds, UserSession.last_request_at_threshold
54
+
55
+ assert UserSession.find
56
+ last_request_at = ben.reload.last_request_at
57
+ sleep(1)
58
+ assert UserSession.find
59
+ assert_equal last_request_at, ben.reload.last_request_at
60
+ sleep(1)
61
+ assert UserSession.find
62
+ assert_not_equal last_request_at, ben.reload.last_request_at
63
+
64
+ UserSession.last_request_at_threshold 0
65
+ assert_equal 0, UserSession.last_request_at_threshold
51
66
  end
52
67
 
53
68
  def test_klass
@@ -54,22 +54,15 @@ module SessionTests
54
54
  end
55
55
 
56
56
  def test_last_request_at_threshold
57
- ben = users(:ben)
58
- set_session_for(ben)
59
- UserSession.last_request_at_threshold = 2.seconds
60
- assert_equal 2.seconds, UserSession.last_request_at_threshold
61
-
62
- assert UserSession.find
63
- last_request_at = ben.reload.last_request_at
64
- sleep(1)
65
- assert UserSession.find
66
- assert_equal last_request_at, ben.reload.last_request_at
67
- sleep(1)
68
- assert UserSession.find
69
- assert_not_equal last_request_at, ben.reload.last_request_at
70
-
57
+ UserSession.last_request_at_threshold = 2.minutes
58
+ assert_equal 2.minutes, UserSession.last_request_at_threshold
59
+ session = UserSession.new
60
+ assert_equal 2.minutes, session.last_request_at_threshold
61
+
71
62
  UserSession.last_request_at_threshold 0
72
63
  assert_equal 0, UserSession.last_request_at_threshold
64
+ session = UserSession.new
65
+ assert_equal 0, session.last_request_at_threshold
73
66
  end
74
67
 
75
68
  def test_login_field
@@ -85,6 +78,18 @@ module SessionTests
85
78
  assert_equal :login, session.login_field
86
79
  assert session.respond_to?(:login)
87
80
  end
81
+
82
+ def test_params_key
83
+ UserSession.params_key = "my_params_key"
84
+ assert_equal "my_params_key", UserSession.params_key
85
+ session = UserSession.new
86
+ assert_equal "my_params_key", session.params_key
87
+
88
+ UserSession.params_key "user_credentials"
89
+ assert_equal "user_credentials", UserSession.params_key
90
+ session = UserSession.new
91
+ assert_equal "user_credentials", session.params_key
92
+ end
88
93
 
89
94
  def test_password_field
90
95
  UserSession.password_field = :saweet
@@ -149,6 +154,18 @@ module SessionTests
149
154
  session = UserSession.new
150
155
  assert_equal "user_credentials", session.session_key
151
156
  end
157
+
158
+ def test_single_access_allowed_request_types
159
+ UserSession.single_access_allowed_request_types = "my request type"
160
+ assert_equal ["my request type"], UserSession.single_access_allowed_request_types
161
+ session = UserSession.new
162
+ assert_equal ["my request type"], session.single_access_allowed_request_types
163
+
164
+ UserSession.single_access_allowed_request_types "application/rss+xml", "application/atom+xml"
165
+ assert_equal ["application/rss+xml", "application/atom+xml"], UserSession.single_access_allowed_request_types
166
+ session = UserSession.new
167
+ assert_equal ["application/rss+xml", "application/atom+xml"], session.single_access_allowed_request_types
168
+ end
152
169
 
153
170
  def test_verify_password_method
154
171
  UserSession.verify_password_method = "my_login_method"