authlogic 4.4.2 → 5.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +5 -5
  2. data/lib/authlogic.rb +4 -28
  3. data/lib/authlogic/acts_as_authentic/base.rb +3 -18
  4. data/lib/authlogic/acts_as_authentic/email.rb +3 -170
  5. data/lib/authlogic/acts_as_authentic/logged_in_status.rb +3 -1
  6. data/lib/authlogic/acts_as_authentic/login.rb +7 -174
  7. data/lib/authlogic/acts_as_authentic/magic_columns.rb +7 -4
  8. data/lib/authlogic/acts_as_authentic/password.rb +54 -253
  9. data/lib/authlogic/acts_as_authentic/perishable_token.rb +8 -5
  10. data/lib/authlogic/acts_as_authentic/persistence_token.rb +10 -4
  11. data/lib/authlogic/acts_as_authentic/queries/case_sensitivity.rb +53 -0
  12. data/lib/authlogic/acts_as_authentic/queries/find_with_case.rb +36 -20
  13. data/lib/authlogic/acts_as_authentic/session_maintenance.rb +8 -6
  14. data/lib/authlogic/acts_as_authentic/single_access_token.rb +10 -8
  15. data/lib/authlogic/config.rb +9 -1
  16. data/lib/authlogic/controller_adapters/abstract_adapter.rb +7 -4
  17. data/lib/authlogic/controller_adapters/rack_adapter.rb +2 -0
  18. data/lib/authlogic/controller_adapters/rails_adapter.rb +19 -19
  19. data/lib/authlogic/controller_adapters/sinatra_adapter.rb +6 -0
  20. data/lib/authlogic/cookie_credentials.rb +63 -0
  21. data/lib/authlogic/crypto_providers.rb +5 -20
  22. data/lib/authlogic/crypto_providers/bcrypt.rb +3 -3
  23. data/lib/authlogic/crypto_providers/md5.rb +3 -6
  24. data/lib/authlogic/crypto_providers/scrypt.rb +2 -0
  25. data/lib/authlogic/crypto_providers/sha1.rb +4 -6
  26. data/lib/authlogic/crypto_providers/sha256.rb +2 -0
  27. data/lib/authlogic/crypto_providers/sha512.rb +6 -5
  28. data/lib/authlogic/i18n.rb +3 -1
  29. data/lib/authlogic/i18n/translator.rb +3 -0
  30. data/lib/authlogic/random.rb +2 -0
  31. data/lib/authlogic/session/base.rb +2087 -39
  32. data/lib/authlogic/session/magic_column/assigns_last_request_at.rb +46 -0
  33. data/lib/authlogic/test_case.rb +4 -0
  34. data/lib/authlogic/test_case/mock_controller.rb +2 -0
  35. data/lib/authlogic/test_case/mock_cookie_jar.rb +7 -0
  36. data/lib/authlogic/test_case/mock_logger.rb +2 -0
  37. data/lib/authlogic/test_case/mock_request.rb +2 -0
  38. data/lib/authlogic/test_case/rails_request_adapter.rb +2 -0
  39. data/lib/authlogic/version.rb +2 -1
  40. metadata +136 -182
  41. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -28
  42. data/.github/ISSUE_TEMPLATE/feature_proposal.md +0 -32
  43. data/.github/triage.md +0 -86
  44. data/.gitignore +0 -15
  45. data/.rubocop.yml +0 -133
  46. data/.rubocop_todo.yml +0 -74
  47. data/.travis.yml +0 -24
  48. data/CHANGELOG.md +0 -326
  49. data/CONTRIBUTING.md +0 -91
  50. data/Gemfile +0 -6
  51. data/LICENSE +0 -20
  52. data/README.md +0 -439
  53. data/Rakefile +0 -21
  54. data/UPGRADING.md +0 -22
  55. data/authlogic.gemspec +0 -40
  56. data/doc/use_normal_rails_validation.md +0 -82
  57. data/gemfiles/Gemfile.rails-4.2.x +0 -6
  58. data/gemfiles/Gemfile.rails-5.1.x +0 -6
  59. data/gemfiles/Gemfile.rails-5.2.x +0 -6
  60. data/lib/authlogic/acts_as_authentic/restful_authentication.rb +0 -106
  61. data/lib/authlogic/acts_as_authentic/validations_scope.rb +0 -35
  62. data/lib/authlogic/authenticates_many/association.rb +0 -50
  63. data/lib/authlogic/authenticates_many/base.rb +0 -81
  64. data/lib/authlogic/crypto_providers/aes256.rb +0 -71
  65. data/lib/authlogic/crypto_providers/wordpress.rb +0 -72
  66. data/lib/authlogic/regex.rb +0 -79
  67. data/lib/authlogic/session/activation.rb +0 -73
  68. data/lib/authlogic/session/active_record_trickery.rb +0 -65
  69. data/lib/authlogic/session/brute_force_protection.rb +0 -127
  70. data/lib/authlogic/session/callbacks.rb +0 -153
  71. data/lib/authlogic/session/cookies.rb +0 -296
  72. data/lib/authlogic/session/existence.rb +0 -103
  73. data/lib/authlogic/session/foundation.rb +0 -105
  74. data/lib/authlogic/session/http_auth.rb +0 -107
  75. data/lib/authlogic/session/id.rb +0 -53
  76. data/lib/authlogic/session/klass.rb +0 -73
  77. data/lib/authlogic/session/magic_columns.rb +0 -119
  78. data/lib/authlogic/session/magic_states.rb +0 -82
  79. data/lib/authlogic/session/params.rb +0 -130
  80. data/lib/authlogic/session/password.rb +0 -318
  81. data/lib/authlogic/session/perishable_token.rb +0 -24
  82. data/lib/authlogic/session/persistence.rb +0 -77
  83. data/lib/authlogic/session/priority_record.rb +0 -38
  84. data/lib/authlogic/session/scopes.rb +0 -138
  85. data/lib/authlogic/session/session.rb +0 -77
  86. data/lib/authlogic/session/timeout.rb +0 -103
  87. data/lib/authlogic/session/unauthorized_record.rb +0 -56
  88. data/lib/authlogic/session/validation.rb +0 -93
  89. data/test/acts_as_authentic_test/base_test.rb +0 -27
  90. data/test/acts_as_authentic_test/email_test.rb +0 -241
  91. data/test/acts_as_authentic_test/logged_in_status_test.rb +0 -64
  92. data/test/acts_as_authentic_test/login_test.rb +0 -153
  93. data/test/acts_as_authentic_test/magic_columns_test.rb +0 -29
  94. data/test/acts_as_authentic_test/password_test.rb +0 -263
  95. data/test/acts_as_authentic_test/perishable_token_test.rb +0 -98
  96. data/test/acts_as_authentic_test/persistence_token_test.rb +0 -62
  97. data/test/acts_as_authentic_test/restful_authentication_test.rb +0 -48
  98. data/test/acts_as_authentic_test/session_maintenance_test.rb +0 -150
  99. data/test/acts_as_authentic_test/single_access_test.rb +0 -46
  100. data/test/adapter_test.rb +0 -23
  101. data/test/authenticates_many_test.rb +0 -33
  102. data/test/config_test.rb +0 -38
  103. data/test/crypto_provider_test/aes256_test.rb +0 -16
  104. data/test/crypto_provider_test/bcrypt_test.rb +0 -16
  105. data/test/crypto_provider_test/scrypt_test.rb +0 -16
  106. data/test/crypto_provider_test/sha1_test.rb +0 -25
  107. data/test/crypto_provider_test/sha256_test.rb +0 -16
  108. data/test/crypto_provider_test/sha512_test.rb +0 -16
  109. data/test/crypto_provider_test/wordpress_test.rb +0 -26
  110. data/test/fixtures/companies.yml +0 -5
  111. data/test/fixtures/employees.yml +0 -17
  112. data/test/fixtures/projects.yml +0 -3
  113. data/test/fixtures/users.yml +0 -41
  114. data/test/i18n/lol.yml +0 -4
  115. data/test/i18n_test.rb +0 -35
  116. data/test/libs/affiliate.rb +0 -9
  117. data/test/libs/company.rb +0 -8
  118. data/test/libs/employee.rb +0 -9
  119. data/test/libs/employee_session.rb +0 -4
  120. data/test/libs/ldaper.rb +0 -5
  121. data/test/libs/project.rb +0 -5
  122. data/test/libs/user.rb +0 -9
  123. data/test/libs/user_session.rb +0 -27
  124. data/test/random_test.rb +0 -15
  125. data/test/session_test/activation_test.rb +0 -45
  126. data/test/session_test/active_record_trickery_test.rb +0 -78
  127. data/test/session_test/brute_force_protection_test.rb +0 -110
  128. data/test/session_test/callbacks_test.rb +0 -42
  129. data/test/session_test/cookies_test.rb +0 -226
  130. data/test/session_test/credentials_test.rb +0 -0
  131. data/test/session_test/existence_test.rb +0 -88
  132. data/test/session_test/foundation_test.rb +0 -24
  133. data/test/session_test/http_auth_test.rb +0 -60
  134. data/test/session_test/id_test.rb +0 -19
  135. data/test/session_test/klass_test.rb +0 -42
  136. data/test/session_test/magic_columns_test.rb +0 -62
  137. data/test/session_test/magic_states_test.rb +0 -60
  138. data/test/session_test/params_test.rb +0 -61
  139. data/test/session_test/password_test.rb +0 -107
  140. data/test/session_test/perishability_test.rb +0 -17
  141. data/test/session_test/persistence_test.rb +0 -35
  142. data/test/session_test/scopes_test.rb +0 -68
  143. data/test/session_test/session_test.rb +0 -80
  144. data/test/session_test/timeout_test.rb +0 -84
  145. data/test/session_test/unauthorized_record_test.rb +0 -15
  146. data/test/session_test/validation_test.rb +0 -25
  147. data/test/test_helper.rb +0 -272
@@ -1,56 +0,0 @@
1
- module Authlogic
2
- module Session
3
- # Allows you to create session with an object. Ex:
4
- #
5
- # UserSession.create(my_user_object)
6
- #
7
- # Be careful with this, because Authlogic is assuming that you have already
8
- # confirmed that the user is who he says he is.
9
- #
10
- # For example, this is the method used to persist the session internally.
11
- # Authlogic finds the user with the persistence token. At this point we know
12
- # the user is who he says he is, so Authlogic just creates a session with
13
- # the record. This is particularly useful for 3rd party authentication
14
- # methods, such as OpenID. Let that method verify the identity, once it's
15
- # verified, pass the object and create a session.
16
- module UnauthorizedRecord
17
- def self.included(klass)
18
- klass.class_eval do
19
- attr_accessor :unauthorized_record
20
- validate(
21
- :validate_by_unauthorized_record,
22
- if: :authenticating_with_unauthorized_record?
23
- )
24
- end
25
- end
26
-
27
- # Returning meaningful credentials
28
- def credentials
29
- if authenticating_with_unauthorized_record?
30
- details = {}
31
- details[:unauthorized_record] = "<protected>"
32
- details
33
- else
34
- super
35
- end
36
- end
37
-
38
- # Setting the unauthorized record if it exists in the credentials passed.
39
- def credentials=(value)
40
- super
41
- values = value.is_a?(Array) ? value : [value]
42
- self.unauthorized_record = values.first if values.first.class < ::ActiveRecord::Base
43
- end
44
-
45
- private
46
-
47
- def authenticating_with_unauthorized_record?
48
- !unauthorized_record.nil?
49
- end
50
-
51
- def validate_by_unauthorized_record
52
- self.attempted_record = unauthorized_record
53
- end
54
- end
55
- end
56
- end
@@ -1,93 +0,0 @@
1
- module Authlogic
2
- module Session
3
- # Responsible for session validation
4
- module Validation
5
- # The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses
6
- # the exact same ActiveRecord errors class. Use it the same way:
7
- #
8
- # class UserSession
9
- # validate :check_if_awesome
10
- #
11
- # private
12
- # def check_if_awesome
13
- # errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
14
- # errors.add(:base, "You must be awesome to log in") unless attempted_record.awesome?
15
- # end
16
- # end
17
- class Errors < (defined?(::ActiveModel) ? ::ActiveModel::Errors : ::ActiveRecord::Errors)
18
- unless defined?(::ActiveModel)
19
- def [](key)
20
- value = super
21
- value.is_a?(Array) ? value : [value].compact
22
- end
23
- end
24
- end
25
-
26
- # You should use this as a place holder for any records that you find
27
- # during validation. The main reason for this is to allow other modules to
28
- # use it if needed. Take the failed_login_count feature, it needs this in
29
- # order to increase the failed login count.
30
- def attempted_record
31
- @attempted_record
32
- end
33
-
34
- # See attempted_record
35
- def attempted_record=(value)
36
- @attempted_record = value
37
- end
38
-
39
- # The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses
40
- # the exact same ActiveRecord errors class. Use it the same way:
41
- #
42
- # === Example
43
- #
44
- # class UserSession
45
- # before_validation :check_if_awesome
46
- #
47
- # private
48
- # def check_if_awesome
49
- # errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
50
- # errors.add(:base, "You must be awesome to log in") unless attempted_record.awesome?
51
- # end
52
- # end
53
- def errors
54
- @errors ||= Errors.new(self)
55
- end
56
-
57
- # Determines if the information you provided for authentication is valid
58
- # or not. If there is a problem with the information provided errors will
59
- # be added to the errors object and this method will return false.
60
- def valid?
61
- errors.clear
62
- self.attempted_record = nil
63
-
64
- before_validation
65
- new_session? ? before_validation_on_create : before_validation_on_update
66
- validate
67
- ensure_authentication_attempted
68
-
69
- if errors.empty?
70
- new_session? ? after_validation_on_create : after_validation_on_update
71
- after_validation
72
- end
73
-
74
- save_record(attempted_record)
75
- errors.empty?
76
- end
77
-
78
- private
79
-
80
- def ensure_authentication_attempted
81
- if errors.empty? && attempted_record.nil?
82
- errors.add(
83
- :base,
84
- I18n.t(
85
- "error_messages.no_authentication_details",
86
- default: "You did not provide any details for authentication."
87
- )
88
- )
89
- end
90
- end
91
- end
92
- end
93
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- module ActsAsAuthenticTest
6
- class BaseTest < ActiveSupport::TestCase
7
- def test_acts_as_authentic
8
- assert_nothing_raised do
9
- User.acts_as_authentic do
10
- end
11
- end
12
- end
13
-
14
- def test_acts_as_authentic_with_old_config
15
- assert_raise(ArgumentError) do
16
- User.acts_as_authentic({})
17
- end
18
- end
19
-
20
- def test_acts_as_authentic_with_no_table
21
- klass = Class.new(ActiveRecord::Base)
22
- assert_nothing_raised do
23
- klass.acts_as_authentic
24
- end
25
- end
26
- end
27
- end
@@ -1,241 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- module ActsAsAuthenticTest
6
- class EmailTest < ActiveSupport::TestCase
7
- GOOD_ASCII_EMAILS = [
8
- "a@a.com",
9
- "damien+test1...etc..@mydomain.com",
10
- "dakota.dux+1@gmail.com",
11
- "dakota.d'ux@gmail.com",
12
- "a&b@c.com",
13
- "someuser@somedomain.travelersinsurance"
14
- ].freeze
15
-
16
- BAD_ASCII_EMAILS = [
17
- "",
18
- "aaaaaaaaaaaaa",
19
- "question?mark@gmail.com",
20
- "backslash@g\\mail.com",
21
- "<script>alert(123);</script>\nnobody@example.com",
22
- "someuser@somedomain.isreallytoolongandimeanreallytoolong"
23
- ].freeze
24
-
25
- # http://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout
26
- GOOD_ISO88591_EMAILS = [
27
- "töm.öm@dömain.fi", # https://github.com/binarylogic/authlogic/issues/176
28
- "Pelé@examplé.com", # http://en.wikipedia.org/wiki/Email_address#Internationalization_examples
29
- ].freeze
30
-
31
- BAD_ISO88591_EMAILS = [
32
- "",
33
- "öm(@ava.fi", # L paren
34
- "é)@domain.com", # R paren
35
- "é[@example.com", # L bracket
36
- "question?mark@gmail.com", # question mark
37
- "back\\slash@gmail.com", # backslash
38
- ].freeze
39
-
40
- GOOD_UTF8_EMAILS = [
41
- "δκιμή@παράδεγμα.δοκμή", # http://en.wikipedia.org/wiki/Email_address#Internationalization_examples
42
- "我本@屋企.香港", # http://en.wikipedia.org/wiki/Email_address#Internationalization_examples
43
- "甲斐@黒川.日買", # http://en.wikipedia.org/wiki/Email_address#Internationalization_examples
44
- "чебурша@ящик-с-пельнами.рф", # Contains dashes in domain head
45
- "企斐@黒川.みんな", # https://github.com/binarylogic/authlogic/issues/176#issuecomment-55829320
46
- ].freeze
47
-
48
- BAD_UTF8_EMAILS = [
49
- "",
50
- ".みんな", # https://github.com/binarylogic/authlogic/issues/176#issuecomment-55829320
51
- "δκιμή@παράδεγμα.δ", # short TLD
52
- "öm(@ava.fi", # L paren
53
- "é)@domain.com", # R paren
54
- "é[@example.com", # L bracket
55
- "δ]@πράιγμα.δοκμή", # R bracket
56
- "我\.香港", # slash
57
- "甲;.日本", # semicolon
58
- "ч:@ящик-с-пельнами.рф", # colon
59
- "斐,.みんな", # comma
60
- "香<.香港", # less than
61
- "我>.香港", # greater than
62
- "我?本@屋企.香港", # question mark
63
- "чебурша@ьн\\ами.рф", # backslash
64
- "user@domain.com%0A<script>alert('hello')</script>"
65
- ].freeze
66
-
67
- def test_email_field_config
68
- assert_equal :email, User.email_field
69
- assert_equal :email, Employee.email_field
70
-
71
- User.email_field = :nope
72
- assert_equal :nope, User.email_field
73
- User.email_field :email
74
- assert_equal :email, User.email_field
75
- end
76
-
77
- def test_validate_email_field_config
78
- assert User.validate_email_field
79
- assert Employee.validate_email_field
80
-
81
- User.validate_email_field = false
82
- refute User.validate_email_field
83
- User.validate_email_field true
84
- assert User.validate_email_field
85
- end
86
-
87
- def test_validates_length_of_email_field_options_config
88
- assert_equal({ maximum: 100 }, User.validates_length_of_email_field_options)
89
- assert_equal({ maximum: 100 }, Employee.validates_length_of_email_field_options)
90
-
91
- User.validates_length_of_email_field_options = { yes: "no" }
92
- assert_equal({ yes: "no" }, User.validates_length_of_email_field_options)
93
- User.validates_length_of_email_field_options(within: 6..100)
94
- assert_equal({ within: 6..100 }, User.validates_length_of_email_field_options)
95
- end
96
-
97
- def test_validates_format_of_email_field_options_config
98
- default = {
99
- with: Authlogic::Regex::EMAIL,
100
- message: proc do
101
- I18n.t(
102
- "error_messages.email_invalid",
103
- default: "should look like an email address."
104
- )
105
- end
106
- }
107
- default_message = default.delete(:message).call
108
-
109
- options = User.validates_format_of_email_field_options
110
- message = options.delete(:message)
111
- assert message.is_a?(Proc)
112
- assert_equal default_message, message.call
113
- assert_equal default, options
114
-
115
- options = Employee.validates_format_of_email_field_options
116
- message = options.delete(:message)
117
- assert message.is_a?(Proc)
118
- assert_equal default_message, message.call
119
- assert_equal default, options
120
-
121
- User.validates_format_of_email_field_options = { yes: "no" }
122
- assert_equal({ yes: "no" }, User.validates_format_of_email_field_options)
123
- User.validates_format_of_email_field_options default
124
- assert_equal default, User.validates_format_of_email_field_options
125
-
126
- with_email_nonascii = {
127
- with: Authlogic::Regex::EMAIL_NONASCII,
128
- message: proc do
129
- I18n.t(
130
- "error_messages.email_invalid_international",
131
- default: "should look like an international email address."
132
- )
133
- end
134
- }
135
- User.validates_format_of_email_field_options = with_email_nonascii
136
- assert_equal(with_email_nonascii, User.validates_format_of_email_field_options)
137
- User.validates_format_of_email_field_options with_email_nonascii
138
- assert_equal with_email_nonascii, User.validates_format_of_email_field_options
139
- end
140
-
141
- def test_deferred_error_message_translation
142
- # ensure we successfully loaded the test locale
143
- assert I18n.available_locales.include?(:lol), "Test locale failed to load"
144
-
145
- I18n.with_locale("lol") do
146
- message = I18n.t("authlogic.error_messages.email_invalid")
147
-
148
- cat = User.new
149
- cat.email = "meow"
150
- cat.valid?
151
-
152
- # filter duplicate error messages
153
- error = cat.errors[:email]
154
- error = error.first if error.is_a?(Array)
155
-
156
- assert_equal message, error
157
- end
158
- end
159
-
160
- def test_validates_uniqueness_of_email_field_options_config
161
- default = {
162
- case_sensitive: false,
163
- scope: Employee.validations_scope,
164
- if: "#{Employee.email_field}_changed?".to_sym
165
- }
166
- assert_equal default, Employee.validates_uniqueness_of_email_field_options
167
-
168
- Employee.validates_uniqueness_of_email_field_options = { yes: "no" }
169
- assert_equal({ yes: "no" }, Employee.validates_uniqueness_of_email_field_options)
170
- Employee.validates_uniqueness_of_email_field_options default
171
- assert_equal default, Employee.validates_uniqueness_of_email_field_options
172
- end
173
-
174
- def test_validates_length_of_email_field
175
- u = User.new
176
- u.email = "a@a.a"
177
- refute u.valid?
178
- refute u.errors[:email].empty?
179
-
180
- u.email = "a@a.com"
181
- refute u.valid?
182
- assert u.errors[:email].empty?
183
- end
184
-
185
- def test_validates_format_of_email_field
186
- u = User.new
187
- u.email = "aaaaaaaaaaaaa"
188
- u.valid?
189
- refute u.errors[:email].empty?
190
-
191
- u.email = "a@a.com"
192
- u.valid?
193
- assert u.errors[:email].empty?
194
-
195
- u.email = "damien+test1...etc..@mydomain.com"
196
- u.valid?
197
- assert u.errors[:email].empty?
198
-
199
- u.email = "dakota.dux+1@gmail.com"
200
- u.valid?
201
- assert u.errors[:email].empty?
202
-
203
- u.email = "dakota.d'ux@gmail.com"
204
- u.valid?
205
- assert u.errors[:email].empty?
206
-
207
- u.email = "<script>alert(123);</script>\nnobody@example.com"
208
- refute u.valid?
209
- refute u.errors[:email].empty?
210
-
211
- u.email = "a&b@c.com"
212
- u.valid?
213
- assert u.errors[:email].empty?
214
- end
215
-
216
- def test_validates_format_of_nonascii_email_field
217
- (GOOD_ASCII_EMAILS + GOOD_ISO88591_EMAILS + GOOD_UTF8_EMAILS).each do |e|
218
- assert e =~ Authlogic::Regex::EMAIL_NONASCII, "Good email should validate: #{e}"
219
- end
220
-
221
- (BAD_ASCII_EMAILS + BAD_ISO88591_EMAILS + BAD_UTF8_EMAILS).each do |e|
222
- assert e !~ Authlogic::Regex::EMAIL_NONASCII, "Bad email should not validate: #{e}"
223
- end
224
- end
225
-
226
- def test_validates_uniqueness_of_email_field
227
- u = User.new
228
- u.email = "bjohnson@binarylogic.com"
229
- refute u.valid?
230
- refute u.errors[:email].empty?
231
-
232
- u.email = "BJOHNSON@binarylogic.com"
233
- refute u.valid?
234
- refute u.errors[:email].empty?
235
-
236
- u.email = "a@a.com"
237
- refute u.valid?
238
- assert u.errors[:email].empty?
239
- end
240
- end
241
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- module ActsAsAuthenticTest
6
- class LoggedInStatusTest < ActiveSupport::TestCase
7
- ERROR_MSG = "Multiple calls to %s should result in different relations"
8
-
9
- def test_logged_in_timeout_config
10
- assert_equal 10.minutes.to_i, User.logged_in_timeout
11
- assert_equal 10.minutes.to_i, Employee.logged_in_timeout
12
-
13
- User.logged_in_timeout = 1.hour
14
- assert_equal 1.hour.to_i, User.logged_in_timeout
15
- User.logged_in_timeout 10.minutes
16
- assert_equal 10.minutes.to_i, User.logged_in_timeout
17
- end
18
-
19
- def test_named_scope_logged_in
20
- # Testing that the scope returned differs, because the time it was called should be
21
- # slightly different. This is an attempt to make sure the scope is lambda wrapped
22
- # so that it is re-evaluated every time its called. My biggest concern is that the
23
- # test happens so fast that the test fails... I just don't know a better way to test it!
24
-
25
- # for rails 5 I've changed the where_values to to_sql to compare
26
-
27
- query1 = User.logged_in.to_sql
28
- sleep 0.1
29
- query2 = User.logged_in.to_sql
30
- assert query1 != query2, ERROR_MSG % "#logged_in"
31
-
32
- assert_equal 0, User.logged_in.count
33
- user = User.first
34
- user.last_request_at = Time.now
35
- user.current_login_at = Time.now
36
- user.save!
37
- assert_equal 1, User.logged_in.count
38
- end
39
-
40
- def test_named_scope_logged_out
41
- # Testing that the scope returned differs, because the time it was called should be
42
- # slightly different. This is an attempt to make sure the scope is lambda wrapped
43
- # so that it is re-evaluated every time its called. My biggest concern is that the
44
- # test happens so fast that the test fails... I just don't know a better way to test it!
45
-
46
- # for rails 5 I've changed the where_values to to_sql to compare
47
-
48
- assert User.logged_in.to_sql != User.logged_out.to_sql, ERROR_MSG % "#logged_out"
49
-
50
- assert_equal 3, User.logged_out.count
51
- User.first.update_attribute(:last_request_at, Time.now)
52
- assert_equal 2, User.logged_out.count
53
- end
54
-
55
- def test_logged_in_logged_out
56
- u = User.first
57
- refute u.logged_in?
58
- assert u.logged_out?
59
- u.last_request_at = Time.now
60
- assert u.logged_in?
61
- refute u.logged_out?
62
- end
63
- end
64
- end