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,26 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module ActiveRecordTrickeryTest
5
+ class ClassMethodsTest < ActiveSupport::TestCase
6
+ def test_human_attribute_name
7
+ assert_equal "Some attribute", UserSession.human_attribute_name("some_attribute")
8
+ end
9
+
10
+ def test_human_name
11
+ assert_equal "User", UserSession.human_name
12
+ end
13
+
14
+ def test_self_and_descendents_from_active_record
15
+ assert_equal [UserSession], UserSession.self_and_descendents_from_active_record
16
+ end
17
+ end
18
+
19
+ class InstanceMethodsTest < ActiveSupport::TestCase
20
+ def test_new_record
21
+ session = UserSession.new
22
+ assert session.new_record?
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module BruteForceProtectionTest
5
+ class ConfigTest < ActiveSupport::TestCase
6
+ def test_consecutive_failed_logins_limit
7
+ UserSession.consecutive_failed_logins_limit = 10
8
+ assert_equal 10, UserSession.consecutive_failed_logins_limit
9
+
10
+ UserSession.consecutive_failed_logins_limit 50
11
+ assert_equal 50, UserSession.consecutive_failed_logins_limit
12
+ end
13
+
14
+ def test_failed_login_ban_for
15
+ UserSession.failed_login_ban_for = 10
16
+ assert_equal 10, UserSession.failed_login_ban_for
17
+
18
+ UserSession.failed_login_ban_for 2.hours
19
+ assert_equal 2.hours.to_i, UserSession.failed_login_ban_for
20
+ end
21
+ end
22
+
23
+ class InstaceMethodsTest < ActiveSupport::TestCase
24
+ def test_under_limit
25
+ ben = users(:ben)
26
+ ben.failed_login_count = UserSession.consecutive_failed_logins_limit - 1
27
+ assert ben.save
28
+ assert UserSession.create(:login => ben.login, :password => "benrocks")
29
+ end
30
+
31
+ def test_exceeded_limit
32
+ ben = users(:ben)
33
+ ben.failed_login_count = UserSession.consecutive_failed_logins_limit
34
+ assert ben.save
35
+ assert !UserSession.create(:login => ben.login, :password => "benrocks")
36
+ assert !UserSession.create(ben)
37
+ ben.updated_at = (UserSession.failed_login_ban_for + 2.hours.to_i).seconds.ago
38
+ assert UserSession.create(ben)
39
+ end
40
+
41
+ def test_exceeding_failed_logins_limit
42
+ UserSession.consecutive_failed_logins_limit = 2
43
+ ben = users(:ben)
44
+
45
+ 2.times do |i|
46
+ session = UserSession.new(:login => ben.login, :password => "badpassword1")
47
+ assert !session.save
48
+ assert session.errors.on(:password)
49
+ assert_equal i + 1, ben.reload.failed_login_count
50
+ end
51
+
52
+ session = UserSession.new(:login => ben.login, :password => "badpassword2")
53
+ assert !session.save
54
+ assert !session.errors.on(:password)
55
+ assert_equal 2, ben.reload.failed_login_count
56
+
57
+ UserSession.consecutive_failed_logins_limit = 50
58
+ end
59
+
60
+ def test_resetting_failed_logins_count
61
+ ben = users(:ben)
62
+
63
+ 2.times do |i|
64
+ session = UserSession.new(:login => ben.login, :password => "badpassword")
65
+ assert !session.save
66
+ assert session.errors.on(:password)
67
+ assert_equal i + 1, ben.reload.failed_login_count
68
+ end
69
+
70
+ session = UserSession.new(:login => ben.login, :password => "benrocks")
71
+ assert session.save
72
+ assert_equal 0, ben.reload.failed_login_count
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ class CallbacksTest < ActiveSupport::TestCase
5
+ end
6
+ end
@@ -0,0 +1,107 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module CookiesTest
5
+ class ConfiTest < ActiveSupport::TestCase
6
+ def test_cookie_key
7
+ UserSession.cookie_key = "my_cookie_key"
8
+ assert_equal "my_cookie_key", UserSession.cookie_key
9
+
10
+ UserSession.cookie_key "user_credentials"
11
+ assert_equal "user_credentials", UserSession.cookie_key
12
+ end
13
+
14
+ def test_remember_me
15
+ UserSession.remember_me = true
16
+ assert_equal true, UserSession.remember_me
17
+ session = UserSession.new
18
+ assert_equal true, session.remember_me
19
+
20
+ UserSession.remember_me false
21
+ assert_equal false, UserSession.remember_me
22
+ session = UserSession.new
23
+ assert_equal false, session.remember_me
24
+ end
25
+
26
+ def test_remember_me_for
27
+ UserSession.remember_me_for = 3.years
28
+ assert_equal 3.years, UserSession.remember_me_for
29
+ session = UserSession.new
30
+ session.remember_me = true
31
+ assert_equal 3.years, session.remember_me_for
32
+
33
+ UserSession.remember_me_for 3.months
34
+ assert_equal 3.months, UserSession.remember_me_for
35
+ session = UserSession.new
36
+ session.remember_me = true
37
+ assert_equal 3.months, session.remember_me_for
38
+ end
39
+ end
40
+
41
+ class InstanceMethodsTest < ActiveSupport::TestCase
42
+ def test_credentials
43
+ session = UserSession.new
44
+ session.credentials = {:remember_me => true}
45
+ assert_equal true, session.remember_me
46
+ end
47
+
48
+ def test_remember_me
49
+ session = UserSession.new
50
+ assert_equal false, session.remember_me
51
+ assert !session.remember_me?
52
+
53
+ session.remember_me = false
54
+ assert_equal false, session.remember_me
55
+ assert !session.remember_me?
56
+
57
+ session.remember_me = true
58
+ assert_equal true, session.remember_me
59
+ assert session.remember_me?
60
+
61
+ session.remember_me = nil
62
+ assert_nil session.remember_me
63
+ assert !session.remember_me?
64
+
65
+ session.remember_me = "1"
66
+ assert_equal "1", session.remember_me
67
+ assert session.remember_me?
68
+
69
+ session.remember_me = "true"
70
+ assert_equal "true", session.remember_me
71
+ assert session.remember_me?
72
+ end
73
+
74
+ def test_remember_me_until
75
+ session = UserSession.new
76
+ assert_nil session.remember_me_until
77
+
78
+ session.remember_me = true
79
+ assert 3.months.from_now <= session.remember_me_until
80
+ end
81
+
82
+ def test_persist_persist_by_cookie
83
+ ben = users(:ben)
84
+ assert !UserSession.find
85
+ set_cookie_for(ben)
86
+ assert session = UserSession.find
87
+ assert_equal ben, session.record
88
+ end
89
+
90
+ def test_after_save_save_cookie
91
+ ben = users(:ben)
92
+ session = UserSession.new(ben)
93
+ assert session.save
94
+ assert_equal ben.persistence_token, @controller.cookies["user_credentials"]
95
+ end
96
+
97
+ def test_after_destroy_destroy_cookie
98
+ ben = users(:ben)
99
+ set_cookie_for(ben)
100
+ session = UserSession.find
101
+ assert @controller.cookies["user_credentials"]
102
+ assert session.destroy
103
+ assert !@controller.cookies["user_credentials"]
104
+ end
105
+ end
106
+ end
107
+ end
File without changes
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module ExistenceTest
5
+ class ClassMethodsTest < ActiveSupport::TestCase
6
+ def test_create
7
+ ben = users(:ben)
8
+ assert !UserSession.create(:login => "somelogin", :password => "badpw2")
9
+ assert UserSession.create(:login => ben.login, :password => "benrocks")
10
+ assert_raise(Authlogic::Session::Existence::SessionInvalidError) { UserSession.create!(:login => ben.login, :password => "badpw") }
11
+ assert UserSession.create!(:login => ben.login, :password => "benrocks")
12
+ end
13
+ end
14
+
15
+ class IsntaceMethodsTest < ActiveSupport::TestCase
16
+ def test_new_session
17
+ session = UserSession.new
18
+ assert session.new_session?
19
+
20
+ set_session_for(users(:ben))
21
+ session = UserSession.find
22
+ assert !session.new_session?
23
+ end
24
+
25
+ def test_save_with_nothing
26
+ session = UserSession.new
27
+ assert !session.save
28
+ assert session.new_session?
29
+ end
30
+
31
+ def test_save_with_block
32
+ ben = users(:ben)
33
+ session = UserSession.new
34
+ block_result = session.save do |result|
35
+ assert !result
36
+ end
37
+ assert !block_result
38
+ assert session.new_session?
39
+ end
40
+
41
+ def test_save_with_bang
42
+ session = UserSession.new
43
+ assert_raise(Authlogic::Session::Existence::SessionInvalidError) { session.save! }
44
+
45
+ session.unauthorized_record = users(:ben)
46
+ assert_nothing_raised { session.save! }
47
+ end
48
+
49
+ def test_destroy
50
+ ben = users(:ben)
51
+ session = UserSession.new
52
+ assert !session.valid?
53
+ assert !session.errors.empty?
54
+ assert session.destroy
55
+ assert session.errors.empty?
56
+ session.unauthorized_record = ben
57
+ assert session.save
58
+ assert session.record
59
+ assert session.destroy
60
+ assert !session.record
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ class HttpAuthTest < ActiveSupport::TestCase
5
+ def test_persist_persist_by_http_auth
6
+ ben = users(:ben)
7
+ http_basic_auth_for { assert !UserSession.find }
8
+ http_basic_auth_for(ben) do
9
+ assert session = UserSession.find
10
+ assert_equal ben, session.record
11
+ assert_equal ben.login, session.login
12
+ assert_equal "benrocks", session.send(:protected_password)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ class IdTest < ActiveSupport::TestCase
5
+ def test_credentials
6
+ session = UserSession.new
7
+ session.credentials = [:my_id]
8
+ assert_equal :my_id, session.id
9
+ end
10
+
11
+ def test_id
12
+ session = UserSession.new
13
+ session.id = :my_id
14
+ assert_equal :my_id, session.id
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module KlassTest
5
+ class ConfigTest < ActiveSupport::TestCase
6
+ def test_authenticate_with
7
+ UserSession.authenticate_with = Employee
8
+ assert_equal "Employee", UserSession.klass_name
9
+ assert_equal Employee, UserSession.klass
10
+
11
+ UserSession.authenticate_with User
12
+ assert_equal "User", UserSession.klass_name
13
+ assert_equal User, UserSession.klass
14
+ end
15
+
16
+ def test_klass
17
+ assert_equal User, UserSession.klass
18
+ end
19
+
20
+ def test_klass_name
21
+ assert_equal "User", UserSession.klass_name
22
+ end
23
+ end
24
+
25
+ class InstanceMethodsTest < ActiveSupport::TestCase
26
+ def test_record_method
27
+ ben = users(:ben)
28
+ set_session_for(ben)
29
+ session = UserSession.find
30
+ assert_equal ben, session.record
31
+ assert_equal ben, session.user
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module MagicColumnsTest
5
+ class ConfigTest < ActiveSupport::TestCase
6
+ def test_last_request_at_threshold_config
7
+ UserSession.last_request_at_threshold = 2.minutes
8
+ assert_equal 2.minutes, UserSession.last_request_at_threshold
9
+
10
+ UserSession.last_request_at_threshold 0
11
+ assert_equal 0, UserSession.last_request_at_threshold
12
+ end
13
+ end
14
+
15
+ class InstanceMethodsTest < ActiveSupport::TestCase
16
+ def test_after_persisting_set_last_request_at
17
+ ben = users(:ben)
18
+ set_cookie_for(ben)
19
+ old_last_request_at = ben.last_request_at
20
+ assert UserSession.find
21
+ ben.reload
22
+ assert ben.last_request_at != old_last_request_at
23
+ end
24
+
25
+ def test_valide_increase_failed_login_count
26
+ ben = users(:ben)
27
+ old_failed_login_count = ben.failed_login_count
28
+ assert !UserSession.create(:login => ben.login, :password => "wrong")
29
+ ben.reload
30
+ assert_equal old_failed_login_count + 1, ben.failed_login_count
31
+ end
32
+
33
+ def test_before_save_update_info
34
+ ben = users(:ben)
35
+
36
+ # increase failed login count
37
+ assert !UserSession.create(:login => ben.login, :password => "wrong")
38
+ ben.reload
39
+
40
+ # grab old values
41
+ old_login_count = ben.login_count
42
+ old_failed_login_count = ben.failed_login_count
43
+ old_last_login_at = ben.last_login_at
44
+ old_current_login_at = ben.current_login_at
45
+ old_last_login_ip = ben.last_login_ip
46
+ old_current_login_ip = ben.current_login_ip
47
+
48
+ assert UserSession.create(ben)
49
+
50
+ assert_equal old_login_count + 1, ben.login_count
51
+ assert_equal old_failed_login_count - 1, ben.failed_login_count
52
+ assert_equal old_current_login_at, ben.last_login_at
53
+ assert ben.current_login_at != old_current_login_at
54
+ assert_equal old_current_login_ip, ben.last_login_ip
55
+ assert_equal "1.1.1.1", ben.current_login_ip
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTest
4
+ module SessionTest
5
+ class ConfigTest < ActiveSupport::TestCase
6
+ def test_disable_magic_states_config
7
+ UserSession.disable_magic_states = true
8
+ assert_equal true, UserSession.disable_magic_states
9
+
10
+ UserSession.disable_magic_states false
11
+ assert_equal false, UserSession.disable_magic_states
12
+ end
13
+ end
14
+
15
+ class InstanceMethodsTest < ActiveSupport::TestCase
16
+ def test_disabling_magic_states
17
+ UserSession.disable_magic_states = true
18
+
19
+ ben = users(:ben)
20
+ ben.update_attribute(:active, false)
21
+ assert UserSession.create(ben)
22
+
23
+ UserSession.disable_magic_states = false
24
+ end
25
+
26
+ def test_validate_validate_magic_states_active
27
+ session = UserSession.new
28
+ ben = users(:ben)
29
+ session.unauthorized_record = ben
30
+ assert session.valid?
31
+
32
+ ben.update_attribute(:active, false)
33
+ assert !session.valid?
34
+ assert session.errors.on_base.size > 0
35
+ end
36
+
37
+ def test_validate_validate_magic_states_approved
38
+ session = UserSession.new
39
+ ben = users(:ben)
40
+ session.unauthorized_record = ben
41
+ assert session.valid?
42
+
43
+ ben.update_attribute(:approved, false)
44
+ assert !session.valid?
45
+ assert session.errors.on_base.size > 0
46
+ end
47
+
48
+ def test_validate_validate_magic_states_confirmed
49
+ session = UserSession.new
50
+ ben = users(:ben)
51
+ session.unauthorized_record = ben
52
+ assert session.valid?
53
+
54
+ ben.update_attribute(:confirmed, false)
55
+ assert !session.valid?
56
+ assert session.errors.on_base.size > 0
57
+ end
58
+ end
59
+ end
60
+ end