authlogic 1.4.3 → 2.0.0

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 (131) hide show
  1. data/CHANGELOG.rdoc +19 -0
  2. data/Manifest.txt +111 -0
  3. data/README.rdoc +116 -389
  4. data/Rakefile +14 -7
  5. data/lib/authlogic.rb +33 -35
  6. data/lib/authlogic/acts_as_authentic/base.rb +91 -0
  7. data/lib/authlogic/acts_as_authentic/email.rb +77 -0
  8. data/lib/authlogic/acts_as_authentic/logged_in_status.rb +54 -0
  9. data/lib/authlogic/acts_as_authentic/login.rb +65 -0
  10. data/lib/authlogic/acts_as_authentic/magic_columns.rb +24 -0
  11. data/lib/authlogic/acts_as_authentic/password.rb +215 -0
  12. data/lib/authlogic/acts_as_authentic/perishable_token.rb +100 -0
  13. data/lib/authlogic/acts_as_authentic/persistence_token.rb +66 -0
  14. data/lib/authlogic/acts_as_authentic/restful_authentication.rb +60 -0
  15. data/lib/authlogic/acts_as_authentic/session_maintenance.rb +127 -0
  16. data/lib/authlogic/acts_as_authentic/single_access_token.rb +58 -0
  17. data/lib/authlogic/acts_as_authentic/validations_scope.rb +32 -0
  18. data/lib/authlogic/{session/authenticates_many_association.rb → authenticates_many/association.rb} +10 -6
  19. data/lib/authlogic/authenticates_many/base.rb +55 -0
  20. data/lib/authlogic/controller_adapters/abstract_adapter.rb +2 -3
  21. data/lib/authlogic/controller_adapters/merb_adapter.rb +0 -4
  22. data/lib/authlogic/controller_adapters/rails_adapter.rb +0 -4
  23. data/lib/authlogic/crypto_providers/aes256.rb +0 -2
  24. data/lib/authlogic/crypto_providers/bcrypt.rb +0 -2
  25. data/lib/authlogic/crypto_providers/md5.rb +34 -0
  26. data/lib/authlogic/crypto_providers/sha1.rb +0 -2
  27. data/lib/authlogic/crypto_providers/sha512.rb +1 -3
  28. data/lib/authlogic/i18n.rb +1 -4
  29. data/lib/authlogic/random.rb +33 -0
  30. data/lib/authlogic/session/activation.rb +56 -0
  31. data/lib/authlogic/session/active_record_trickery.rb +15 -7
  32. data/lib/authlogic/session/base.rb +31 -456
  33. data/lib/authlogic/session/brute_force_protection.rb +50 -27
  34. data/lib/authlogic/session/callbacks.rb +24 -15
  35. data/lib/authlogic/session/cookies.rb +108 -22
  36. data/lib/authlogic/session/existence.rb +89 -0
  37. data/lib/authlogic/session/foundation.rb +63 -0
  38. data/lib/authlogic/session/http_auth.rb +23 -0
  39. data/lib/authlogic/session/id.rb +41 -0
  40. data/lib/authlogic/session/klass.rb +75 -0
  41. data/lib/authlogic/session/magic_columns.rb +75 -0
  42. data/lib/authlogic/session/magic_states.rb +58 -0
  43. data/lib/authlogic/session/params.rb +82 -19
  44. data/lib/authlogic/session/password.rb +156 -0
  45. data/lib/authlogic/session/{perishability.rb → perishable_token.rb} +4 -4
  46. data/lib/authlogic/session/persistence.rb +70 -0
  47. data/lib/authlogic/session/priority_record.rb +34 -0
  48. data/lib/authlogic/session/scopes.rb +57 -53
  49. data/lib/authlogic/session/session.rb +46 -31
  50. data/lib/authlogic/session/timeout.rb +65 -31
  51. data/lib/authlogic/session/unauthorized_record.rb +50 -0
  52. data/lib/authlogic/session/validation.rb +76 -0
  53. data/lib/authlogic/testing/test_unit_helpers.rb +3 -3
  54. data/lib/authlogic/version.rb +3 -3
  55. data/test/acts_as_authentic_test/base_test.rb +12 -0
  56. data/test/acts_as_authentic_test/email_test.rb +79 -0
  57. data/test/acts_as_authentic_test/logged_in_status_test.rb +36 -0
  58. data/test/acts_as_authentic_test/login_test.rb +79 -0
  59. data/test/acts_as_authentic_test/magic_columns_test.rb +27 -0
  60. data/test/acts_as_authentic_test/password_test.rb +212 -0
  61. data/test/acts_as_authentic_test/perishable_token_test.rb +56 -0
  62. data/test/acts_as_authentic_test/persistence_token_test.rb +55 -0
  63. data/test/acts_as_authentic_test/session_maintenance_test.rb +68 -0
  64. data/test/acts_as_authentic_test/single_access_test.rb +39 -0
  65. data/test/authenticates_many_test.rb +16 -0
  66. data/test/{crypto_provider_tests → crypto_provider_test}/aes256_test.rb +1 -1
  67. data/test/{crypto_provider_tests → crypto_provider_test}/bcrypt_test.rb +1 -1
  68. data/test/{crypto_provider_tests → crypto_provider_test}/sha1_test.rb +1 -1
  69. data/test/{crypto_provider_tests → crypto_provider_test}/sha512_test.rb +1 -1
  70. data/test/fixtures/employees.yml +4 -4
  71. data/test/fixtures/users.yml +6 -6
  72. data/test/libs/company.rb +6 -0
  73. data/test/libs/employee.rb +7 -0
  74. data/test/libs/employee_session.rb +2 -0
  75. data/test/libs/project.rb +3 -0
  76. data/test/libs/user_session.rb +2 -0
  77. data/test/random_test.rb +49 -0
  78. data/test/session_test/activation_test.rb +43 -0
  79. data/test/session_test/active_record_trickery_test.rb +26 -0
  80. data/test/session_test/brute_force_protection_test.rb +76 -0
  81. data/test/session_test/callbacks_test.rb +6 -0
  82. data/test/session_test/cookies_test.rb +107 -0
  83. data/test/session_test/credentials_test.rb +0 -0
  84. data/test/session_test/existence_test.rb +64 -0
  85. data/test/session_test/http_auth_test.rb +16 -0
  86. data/test/session_test/id_test.rb +17 -0
  87. data/test/session_test/klass_test.rb +35 -0
  88. data/test/session_test/magic_columns_test.rb +59 -0
  89. data/test/session_test/magic_states_test.rb +60 -0
  90. data/test/session_test/params_test.rb +53 -0
  91. data/test/session_test/password_test.rb +84 -0
  92. data/test/{session_tests → session_test}/perishability_test.rb +1 -1
  93. data/test/session_test/persistence_test.rb +21 -0
  94. data/test/{session_tests → session_test}/scopes_test.rb +2 -3
  95. data/test/session_test/session_test.rb +59 -0
  96. data/test/session_test/timeout_test.rb +43 -0
  97. data/test/session_test/unauthorized_record_test.rb +13 -0
  98. data/test/session_test/validation_test.rb +23 -0
  99. data/test/test_helper.rb +14 -29
  100. metadata +120 -112
  101. data/Manifest +0 -76
  102. data/authlogic.gemspec +0 -38
  103. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/base.rb +0 -22
  104. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/config.rb +0 -238
  105. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb +0 -155
  106. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/logged_in.rb +0 -51
  107. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/perishability.rb +0 -71
  108. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/persistence.rb +0 -94
  109. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/session_maintenance.rb +0 -87
  110. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/single_access.rb +0 -61
  111. data/lib/authlogic/orm_adapters/active_record_adapter/authenticates_many.rb +0 -58
  112. data/lib/authlogic/session/config.rb +0 -421
  113. data/lib/authlogic/session/errors.rb +0 -18
  114. data/lib/authlogic/session/record_info.rb +0 -24
  115. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/config_test.rb +0 -154
  116. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/credentials_test.rb +0 -157
  117. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/logged_in_test.rb +0 -24
  118. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/perishability_test.rb +0 -41
  119. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/persistence_test.rb +0 -54
  120. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/session_maintenance_test.rb +0 -62
  121. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/single_access_test.rb +0 -41
  122. data/test/orm_adapters_tests/active_record_adapter_tests/authenticates_many_test.rb +0 -32
  123. data/test/session_tests/active_record_trickery_test.rb +0 -14
  124. data/test/session_tests/authenticates_many_association_test.rb +0 -28
  125. data/test/session_tests/base_test.rb +0 -307
  126. data/test/session_tests/brute_force_protection_test.rb +0 -53
  127. data/test/session_tests/config_test.rb +0 -184
  128. data/test/session_tests/cookies_test.rb +0 -32
  129. data/test/session_tests/params_test.rb +0 -32
  130. data/test/session_tests/session_test.rb +0 -45
  131. data/test/session_tests/timeout_test.rb +0 -71
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class PerishableTokenTest < ActiveSupport::TestCase
5
+ def test_perishable_token_valid_for_config
6
+ assert_equal 10.minutes.to_i, User.perishable_token_valid_for
7
+ assert_equal 10.minutes.to_i, Employee.perishable_token_valid_for
8
+
9
+ User.perishable_token_valid_for = 1.hour
10
+ assert_equal 1.hour.to_i, User.perishable_token_valid_for
11
+ User.perishable_token_valid_for 10.minutes
12
+ assert_equal 10.minutes.to_i, User.perishable_token_valid_for
13
+ end
14
+
15
+ def test_disable_perishable_token_maintenance_config
16
+ assert !User.disable_perishable_token_maintenance
17
+ assert !Employee.disable_perishable_token_maintenance
18
+
19
+ User.disable_perishable_token_maintenance = true
20
+ assert User.disable_perishable_token_maintenance
21
+ User.disable_perishable_token_maintenance false
22
+ assert !User.disable_perishable_token_maintenance
23
+ end
24
+
25
+ def test_validates_uniqueness_of_perishable_token
26
+ u = User.new
27
+ u.perishable_token = users(:ben).perishable_token
28
+ assert !u.valid?
29
+ assert u.errors.on(:perishable_token)
30
+ end
31
+
32
+ def test_before_save_reset_perishable_token
33
+ ben = users(:ben)
34
+ old_perishable_token = ben.perishable_token
35
+ assert ben.save
36
+ assert_not_equal old_perishable_token, ben.perishable_token
37
+ end
38
+
39
+ def test_reset_perishable_token
40
+ ben = users(:ben)
41
+ old_perishable_token = ben.perishable_token
42
+
43
+ assert ben.reset_perishable_token
44
+ assert_not_equal old_perishable_token, ben.perishable_token
45
+
46
+ ben.reload
47
+ assert_equal old_perishable_token, ben.perishable_token
48
+
49
+ assert ben.reset_perishable_token!
50
+ assert_not_equal old_perishable_token, ben.perishable_token
51
+
52
+ ben.reload
53
+ assert_not_equal old_perishable_token, ben.perishable_token
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class PersistenceTokenTest < ActiveSupport::TestCase
5
+ def test_after_password_set_reset_persistence_token
6
+ ben = users(:ben)
7
+ old_persistence_token = ben.persistence_token
8
+ ben.password = "newpass"
9
+ assert_not_equal old_persistence_token, ben.persistence_token
10
+ end
11
+
12
+ def test_after_password_verification_reset_persistence_token
13
+ ben = users(:ben)
14
+ old_persistence_token = ben.persistence_token
15
+ assert ben.valid_password?(password_for(ben))
16
+ assert_equal old_persistence_token, ben.persistence_token
17
+
18
+ # only update it if it is nil
19
+ assert ben.update_attribute(:persistence_token, nil)
20
+ assert ben.valid_password?(password_for(ben))
21
+ assert_not_equal old_persistence_token, ben.persistence_token
22
+ end
23
+
24
+ def test_before_validate_reset_persistence_token
25
+ u = User.new
26
+ assert !u.valid?
27
+ assert_not_nil u.persistence_token
28
+ end
29
+
30
+ def test_forget_all
31
+ http_basic_auth_for(users(:ben)) { UserSession.find }
32
+ http_basic_auth_for(users(:zack)) { UserSession.find(:ziggity_zack) }
33
+ assert UserSession.find
34
+ assert UserSession.find(:ziggity_zack)
35
+ User.forget_all
36
+ assert !UserSession.find
37
+ assert !UserSession.find(:ziggity_zack)
38
+ end
39
+
40
+ def test_forget
41
+ ben = users(:ben)
42
+ zack = users(:zack)
43
+ http_basic_auth_for(ben) { UserSession.find }
44
+ http_basic_auth_for(zack) { UserSession.find(:ziggity_zack) }
45
+
46
+ assert ben.reload.logged_in?
47
+ assert zack.reload.logged_in?
48
+
49
+ ben.forget!
50
+
51
+ assert !UserSession.find
52
+ assert UserSession.find(:ziggity_zack)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class SessionMaintenanceTest < ActiveSupport::TestCase
5
+ def test_login_after_create
6
+ assert User.create(:login => "awesome", :password => "saweet", :password_confirmation => "saweet", :email => "awesome@awesome.com")
7
+ assert UserSession.find
8
+ end
9
+
10
+ def test_update_session_after_password_modify
11
+ ben = users(:ben)
12
+ UserSession.create(ben)
13
+ old_session_key = @controller.session["user_credentials"]
14
+ old_cookie_key = @controller.cookies["user_credentials"]
15
+ ben.password = "newpass"
16
+ ben.password_confirmation = "newpass"
17
+ assert ben.save
18
+ assert @controller.session["user_credentials"]
19
+ assert @controller.cookies["user_credentials"]
20
+ assert_not_equal @controller.session["user_credentials"], old_session_key
21
+ assert_not_equal @controller.cookies["user_credentials"], old_cookie_key
22
+ end
23
+
24
+ def test_no_session_update_after_modify
25
+ ben = users(:ben)
26
+ UserSession.create(ben)
27
+ old_session_key = @controller.session["user_credentials"]
28
+ old_cookie_key = @controller.cookies["user_credentials"]
29
+ ben.first_name = "Ben"
30
+ assert ben.save
31
+ assert_equal @controller.session["user_credentials"], old_session_key
32
+ assert_equal @controller.cookies["user_credentials"], old_cookie_key
33
+ end
34
+
35
+ def test_creating_other_user
36
+ ben = users(:ben)
37
+ UserSession.create(ben)
38
+ old_session_key = @controller.session["user_credentials"]
39
+ old_cookie_key = @controller.cookies["user_credentials"]
40
+ assert User.create(:login => "awesome", :password => "saweet", :password_confirmation => "saweet", :email => "awesome@saweet.com")
41
+ assert_equal @controller.session["user_credentials"], old_session_key
42
+ assert_equal @controller.cookies["user_credentials"], old_cookie_key
43
+ end
44
+
45
+ def test_updating_other_user
46
+ ben = users(:ben)
47
+ UserSession.create(ben)
48
+ old_session_key = @controller.session["user_credentials"]
49
+ old_cookie_key = @controller.cookies["user_credentials"]
50
+ zack = users(:zack)
51
+ zack.password = "newpass"
52
+ zack.password_confirmation = "newpass"
53
+ assert zack.save
54
+ assert_equal @controller.session["user_credentials"], old_session_key
55
+ assert_equal @controller.cookies["user_credentials"], old_cookie_key
56
+ end
57
+
58
+ def test_resetting_password_when_logged_out
59
+ ben = users(:ben)
60
+ assert !UserSession.find
61
+ ben.password = "newpass"
62
+ ben.password_confirmation = "newpass"
63
+ assert ben.save
64
+ assert UserSession.find
65
+ assert_equal ben, UserSession.find.record
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class SingleAccessTest < ActiveSupport::TestCase
5
+ def test_change_single_access_token_with_password_config
6
+ assert !User.change_single_access_token_with_password
7
+ assert !Employee.change_single_access_token_with_password
8
+
9
+ User.change_single_access_token_with_password = true
10
+ assert User.change_single_access_token_with_password
11
+ User.change_single_access_token_with_password false
12
+ assert !User.change_single_access_token_with_password
13
+ end
14
+
15
+ def test_validates_uniqueness_of_single_access_token
16
+ u = User.new
17
+ u.single_access_token = users(:ben).single_access_token
18
+ assert !u.valid?
19
+ assert u.errors.on(:single_access_token)
20
+ end
21
+
22
+ def test_before_validation_reset_single_access_token
23
+ u = User.new
24
+ assert !u.valid?
25
+ assert_not_nil u.single_access_token
26
+ end
27
+
28
+ def test_after_password_set_reset_single_access_token
29
+ User.change_single_access_token_with_password = true
30
+
31
+ ben = users(:ben)
32
+ old_single_access_token = ben.single_access_token
33
+ ben.password = "new_pass"
34
+ assert_not_equal old_single_access_token, ben.single_access_token
35
+
36
+ User.change_single_access_token_with_password = false
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AuthenticatesManyTest < ActiveSupport::TestCase
4
+ def test_scoping
5
+ zack = users(:zack)
6
+ ben = users(:ben)
7
+ binary_logic = companies(:binary_logic)
8
+ set_session_for(zack)
9
+
10
+ assert !binary_logic.user_sessions.find
11
+
12
+ set_session_for(ben)
13
+
14
+ assert binary_logic.user_sessions.find
15
+ end
16
+ end
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
- module CryptoProviderTests
3
+ module CryptoProviderTest
4
4
  class AES256Test < ActiveSupport::TestCase
5
5
  def test_encrypt
6
6
  assert Authlogic::CryptoProviders::AES256.encrypt("mypass")
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
- module CryptoProviderTests
3
+ module CryptoProviderTest
4
4
  class BCrpytTest < ActiveSupport::TestCase
5
5
  def test_encrypt
6
6
  assert Authlogic::CryptoProviders::BCrypt.encrypt("mypass")
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
- module CryptoProviderTests
3
+ module CryptoProviderTest
4
4
  class Sha1Test < ActiveSupport::TestCase
5
5
  def test_encrypt
6
6
  assert Authlogic::CryptoProviders::Sha1.encrypt("mypass")
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
- module CryptoProviderTests
3
+ module CryptoProviderTest
4
4
  class Sha512Test < ActiveSupport::TestCase
5
5
  def test_encrypt
6
6
  assert Authlogic::CryptoProviders::Sha512.encrypt("mypass")
@@ -1,8 +1,8 @@
1
1
  drew:
2
2
  company: binary_logic
3
3
  email: dgainor@binarylogic.com
4
- password_salt: <%= salt = Employee.unique_token %>
5
- crypted_password: '<%= Employee.acts_as_authentic_config[:crypto_provider].encrypt("drewrocks" + salt) %>'
4
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
5
+ crypted_password: '<%= Employee.crypto_provider.encrypt("drewrocks" + salt) %>'
6
6
  persistence_token: 5273d85ed156e9dbd6a7c1438d319ef8c8d41dd24368db6c222de11346c7b11e53ee08d45ecf619b1c1dc91233d22b372482b751b066d0a6f6f9bac42eacaabf
7
7
  first_name: Drew
8
8
  last_name: Gainor
@@ -10,8 +10,8 @@ drew:
10
10
  jennifer:
11
11
  company: logic_over_data
12
12
  email: jjohnson@logicoverdata.com
13
- password_salt: <%= salt = Employee.unique_token %>
14
- crypted_password: '<%= Employee.acts_as_authentic_config[:crypto_provider].encrypt("jenniferocks" + salt) %>'
13
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
14
+ crypted_password: '<%= Employee.crypto_provider.encrypt("jenniferocks" + salt) %>'
15
15
  persistence_token: 2be52a8f741ad00056e6f94eb6844d5316527206da7a3a5e3d0e14d19499ef9fe4c47c89b87febb59a2b41a69edfb4733b6b79302040f3de83f297c6991c75a2
16
16
  first_name: Jennifer
17
17
  last_name: Johnson
@@ -2,11 +2,11 @@ ben:
2
2
  company: binary_logic
3
3
  projects: web_services
4
4
  login: bjohnson
5
- password_salt: <%= salt = User.unique_token %>
5
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
6
6
  crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("benrocks" + salt) %>
7
7
  persistence_token: 6cde0674657a8a313ce952df979de2830309aa4c11ca65805dd00bfdc65dbcc2f5e36718660a1d2e68c1a08c276d996763985d2f06fd3d076eb7bc4d97b1e317
8
- single_access_token: <%= User.friendly_unique_token %>
9
- perishable_token: <%= User.friendly_unique_token %>
8
+ single_access_token: <%= Authlogic::Random.friendly_token %>
9
+ perishable_token: <%= Authlogic::Random.friendly_token %>
10
10
  email: bjohnson@binarylogic.com
11
11
  first_name: Ben
12
12
  last_name: Johnson
@@ -14,11 +14,11 @@ ben:
14
14
  zack:
15
15
  company: logic_over_data
16
16
  projects: web_services
17
- login: zham
18
- password_salt: <%= salt = User.unique_token %>
17
+ login: zackham
18
+ password_salt: <%= salt = Authlogic::Random.hex_token %>
19
19
  crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("zackrocks" + salt) %>
20
20
  persistence_token: fd3c2d5ce09ab98e7547d21f1b3dcf9158a9a19b5d3022c0402f32ae197019fce3fdbc6614d7ee57d719bae53bb089e30edc9e5d6153e5bc3afca0ac1d320342
21
- single_access_token: <%= User.friendly_unique_token %>
21
+ single_access_token: <%= Authlogic::Random.friendly_token %>
22
22
  email: zham@ziggityzack.com
23
23
  first_name: Zack
24
24
  last_name: Ham
@@ -0,0 +1,6 @@
1
+ class Company < ActiveRecord::Base
2
+ authenticates_many :employee_sessions
3
+ authenticates_many :user_sessions
4
+ has_many :employees, :dependent => :destroy
5
+ has_many :users, :dependent => :destroy
6
+ end
@@ -0,0 +1,7 @@
1
+ class Employee < ActiveRecord::Base
2
+ acts_as_authentic do |c|
3
+ c.crypto_provider Authlogic::CryptoProviders::AES256
4
+ end
5
+
6
+ belongs_to :company
7
+ end
@@ -0,0 +1,2 @@
1
+ class EmployeeSession < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Project < ActiveRecord::Base
2
+ has_and_belongs_to_many :users
3
+ end
@@ -0,0 +1,2 @@
1
+ class UserSession < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ class RandomTest < ActiveSupport::TestCase
4
+ def test_random_tokens_have_consisten_length
5
+ with_any_random do
6
+ assert_equal 128, Authlogic::Random.hex_token.length
7
+ assert_equal 20, Authlogic::Random.friendly_token.length
8
+ end
9
+ end
10
+
11
+ def test_random_tokens_are_indeed_random
12
+ # this might fail if you are *really* unlucky :)
13
+ with_any_random do
14
+ assert_not_equal Authlogic::Random.hex_token, Authlogic::Random.hex_token
15
+ assert_not_equal Authlogic::Random.friendly_token, Authlogic::Random.friendly_token
16
+ end
17
+ end
18
+
19
+ private
20
+ def with_any_random(&block)
21
+ [true, false].each {|val| with_secure_random_enabled(val, &block)}
22
+ end
23
+
24
+ def with_secure_random_enabled(enabled = true)
25
+ # can't really test SecureRandom if we don't have an implementation
26
+ return if enabled && !Authlogic::Random::SecureRandom
27
+
28
+ current_sec_rand = Authlogic::Random::SecureRandom
29
+ reload_authlogic_with_sec_random!(current_sec_rand, enabled)
30
+
31
+ yield
32
+ ensure
33
+ reload_authlogic_with_sec_random!(current_sec_rand)
34
+ end
35
+
36
+ def reload_authlogic_with_sec_random!(secure_random, enabled = true)
37
+ silence_warnings do
38
+ secure_random.parent.const_set(secure_random.name.sub("#{secure_random.parent}::", ''), enabled ? secure_random : nil)
39
+ load(File.dirname(__FILE__) + '/../../lib/authlogic/random.rb')
40
+ end
41
+ end
42
+
43
+ def silence_warnings
44
+ old_verbose, $VERBOSE = $VERBOSE, nil
45
+ yield
46
+ ensure
47
+ $VERBOSE = old_verbose
48
+ end
49
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module ActivationTest
5
+ class ClassMethodsTest < ActiveSupport::TestCase
6
+ def test_activated
7
+ assert UserSession.activated?
8
+ Authlogic::Session::Base.controller = nil
9
+ assert !UserSession.activated?
10
+ end
11
+
12
+ def test_controller
13
+ Authlogic::Session::Base.controller = nil
14
+ assert_nil Authlogic::Session::Base.controller
15
+ thread1 = Thread.new do
16
+ controller = MockController.new
17
+ Authlogic::Session::Base.controller = controller
18
+ assert_equal controller, Authlogic::Session::Base.controller
19
+ end
20
+ thread1.join
21
+
22
+ assert_nil Authlogic::Session::Base.controller
23
+
24
+ thread2 = Thread.new do
25
+ controller = MockController.new
26
+ Authlogic::Session::Base.controller = controller
27
+ assert_equal controller, Authlogic::Session::Base.controller
28
+ end
29
+ thread2.join
30
+
31
+ assert_nil Authlogic::Session::Base.controller
32
+ end
33
+ end
34
+
35
+ class InstanceMethodsTest < ActiveSupport::TestCase
36
+ def test_init
37
+ UserSession.controller = nil
38
+ assert_raise(Authlogic::Session::Activation::NotActivatedError) { UserSession.new }
39
+ UserSession.controller = @controller
40
+ end
41
+ end
42
+ end
43
+ end