devise-edge 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. data/CHANGELOG.rdoc +500 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +335 -0
  4. data/app/controllers/devise/confirmations_controller.rb +33 -0
  5. data/app/controllers/devise/oauth_callbacks_controller.rb +4 -0
  6. data/app/controllers/devise/passwords_controller.rb +41 -0
  7. data/app/controllers/devise/registrations_controller.rb +75 -0
  8. data/app/controllers/devise/sessions_controller.rb +23 -0
  9. data/app/controllers/devise/unlocks_controller.rb +34 -0
  10. data/app/helpers/devise_helper.rb +17 -0
  11. data/app/mailers/devise/mailer.rb +88 -0
  12. data/app/views/devise/confirmations/new.html.erb +12 -0
  13. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  14. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  15. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  16. data/app/views/devise/passwords/edit.html.erb +16 -0
  17. data/app/views/devise/passwords/new.html.erb +12 -0
  18. data/app/views/devise/registrations/edit.html.erb +25 -0
  19. data/app/views/devise/registrations/new.html.erb +18 -0
  20. data/app/views/devise/sessions/new.html.erb +17 -0
  21. data/app/views/devise/shared/_links.erb +25 -0
  22. data/app/views/devise/unlocks/new.html.erb +12 -0
  23. data/config/locales/en.yml +42 -0
  24. data/lib/devise.rb +371 -0
  25. data/lib/devise/controllers/helpers.rb +261 -0
  26. data/lib/devise/controllers/internal_helpers.rb +113 -0
  27. data/lib/devise/controllers/scoped_views.rb +33 -0
  28. data/lib/devise/controllers/url_helpers.rb +39 -0
  29. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  30. data/lib/devise/encryptors/base.rb +20 -0
  31. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  32. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  33. data/lib/devise/encryptors/sha1.rb +25 -0
  34. data/lib/devise/encryptors/sha512.rb +25 -0
  35. data/lib/devise/failure_app.rb +126 -0
  36. data/lib/devise/hooks/activatable.rb +11 -0
  37. data/lib/devise/hooks/forgetable.rb +12 -0
  38. data/lib/devise/hooks/rememberable.rb +45 -0
  39. data/lib/devise/hooks/timeoutable.rb +22 -0
  40. data/lib/devise/hooks/trackable.rb +9 -0
  41. data/lib/devise/mapping.rb +105 -0
  42. data/lib/devise/models.rb +66 -0
  43. data/lib/devise/models/authenticatable.rb +143 -0
  44. data/lib/devise/models/confirmable.rb +160 -0
  45. data/lib/devise/models/database_authenticatable.rb +94 -0
  46. data/lib/devise/models/encryptable.rb +65 -0
  47. data/lib/devise/models/lockable.rb +168 -0
  48. data/lib/devise/models/oauthable.rb +49 -0
  49. data/lib/devise/models/recoverable.rb +83 -0
  50. data/lib/devise/models/registerable.rb +21 -0
  51. data/lib/devise/models/rememberable.rb +122 -0
  52. data/lib/devise/models/timeoutable.rb +33 -0
  53. data/lib/devise/models/token_authenticatable.rb +72 -0
  54. data/lib/devise/models/trackable.rb +30 -0
  55. data/lib/devise/models/validatable.rb +60 -0
  56. data/lib/devise/modules.rb +30 -0
  57. data/lib/devise/oauth.rb +41 -0
  58. data/lib/devise/oauth/config.rb +33 -0
  59. data/lib/devise/oauth/helpers.rb +18 -0
  60. data/lib/devise/oauth/internal_helpers.rb +182 -0
  61. data/lib/devise/oauth/test_helpers.rb +29 -0
  62. data/lib/devise/oauth/url_helpers.rb +35 -0
  63. data/lib/devise/orm/active_record.rb +36 -0
  64. data/lib/devise/orm/mongo_mapper.rb +46 -0
  65. data/lib/devise/orm/mongoid.rb +29 -0
  66. data/lib/devise/path_checker.rb +18 -0
  67. data/lib/devise/rails.rb +67 -0
  68. data/lib/devise/rails/routes.rb +260 -0
  69. data/lib/devise/rails/warden_compat.rb +42 -0
  70. data/lib/devise/schema.rb +96 -0
  71. data/lib/devise/strategies/authenticatable.rb +150 -0
  72. data/lib/devise/strategies/base.rb +15 -0
  73. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  74. data/lib/devise/strategies/rememberable.rb +51 -0
  75. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  76. data/lib/devise/test_helpers.rb +100 -0
  77. data/lib/devise/version.rb +3 -0
  78. data/lib/generators/active_record/devise_generator.rb +28 -0
  79. data/lib/generators/active_record/templates/migration.rb +30 -0
  80. data/lib/generators/devise/devise_generator.rb +17 -0
  81. data/lib/generators/devise/install_generator.rb +24 -0
  82. data/lib/generators/devise/orm_helpers.rb +24 -0
  83. data/lib/generators/devise/views_generator.rb +63 -0
  84. data/lib/generators/mongoid/devise_generator.rb +17 -0
  85. data/lib/generators/templates/README +25 -0
  86. data/lib/generators/templates/devise.rb +168 -0
  87. data/test/controllers/helpers_test.rb +220 -0
  88. data/test/controllers/internal_helpers_test.rb +56 -0
  89. data/test/controllers/url_helpers_test.rb +59 -0
  90. data/test/devise_test.rb +65 -0
  91. data/test/encryptors_test.rb +30 -0
  92. data/test/failure_app_test.rb +148 -0
  93. data/test/integration/authenticatable_test.rb +424 -0
  94. data/test/integration/confirmable_test.rb +104 -0
  95. data/test/integration/database_authenticatable_test.rb +38 -0
  96. data/test/integration/http_authenticatable_test.rb +64 -0
  97. data/test/integration/lockable_test.rb +109 -0
  98. data/test/integration/oauthable_test.rb +258 -0
  99. data/test/integration/recoverable_test.rb +141 -0
  100. data/test/integration/registerable_test.rb +179 -0
  101. data/test/integration/rememberable_test.rb +179 -0
  102. data/test/integration/timeoutable_test.rb +80 -0
  103. data/test/integration/token_authenticatable_test.rb +99 -0
  104. data/test/integration/trackable_test.rb +64 -0
  105. data/test/mailers/confirmation_instructions_test.rb +84 -0
  106. data/test/mailers/reset_password_instructions_test.rb +72 -0
  107. data/test/mailers/unlock_instructions_test.rb +66 -0
  108. data/test/mapping_test.rb +95 -0
  109. data/test/models/confirmable_test.rb +221 -0
  110. data/test/models/database_authenticatable_test.rb +82 -0
  111. data/test/models/encryptable_test.rb +65 -0
  112. data/test/models/lockable_test.rb +204 -0
  113. data/test/models/oauthable_test.rb +21 -0
  114. data/test/models/recoverable_test.rb +155 -0
  115. data/test/models/rememberable_test.rb +271 -0
  116. data/test/models/timeoutable_test.rb +28 -0
  117. data/test/models/token_authenticatable_test.rb +37 -0
  118. data/test/models/trackable_test.rb +5 -0
  119. data/test/models/validatable_test.rb +99 -0
  120. data/test/models_test.rb +77 -0
  121. data/test/oauth/config_test.rb +44 -0
  122. data/test/oauth/url_helpers_test.rb +47 -0
  123. data/test/orm/active_record.rb +9 -0
  124. data/test/orm/mongoid.rb +10 -0
  125. data/test/rails_app/app/active_record/admin.rb +6 -0
  126. data/test/rails_app/app/active_record/shim.rb +2 -0
  127. data/test/rails_app/app/active_record/user.rb +8 -0
  128. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  129. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  130. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  131. data/test/rails_app/app/controllers/home_controller.rb +12 -0
  132. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  133. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  134. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  135. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  136. data/test/rails_app/app/mongoid/admin.rb +9 -0
  137. data/test/rails_app/app/mongoid/shim.rb +24 -0
  138. data/test/rails_app/app/mongoid/user.rb +10 -0
  139. data/test/rails_app/config/application.rb +35 -0
  140. data/test/rails_app/config/boot.rb +13 -0
  141. data/test/rails_app/config/environment.rb +5 -0
  142. data/test/rails_app/config/environments/development.rb +19 -0
  143. data/test/rails_app/config/environments/production.rb +33 -0
  144. data/test/rails_app/config/environments/test.rb +33 -0
  145. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  146. data/test/rails_app/config/initializers/devise.rb +172 -0
  147. data/test/rails_app/config/initializers/inflections.rb +2 -0
  148. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  149. data/test/rails_app/config/routes.rb +54 -0
  150. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  151. data/test/rails_app/db/schema.rb +52 -0
  152. data/test/rails_app/lib/shared_admin.rb +9 -0
  153. data/test/rails_app/lib/shared_user.rb +48 -0
  154. data/test/routes_test.rb +189 -0
  155. data/test/support/assertions.rb +24 -0
  156. data/test/support/helpers.rb +60 -0
  157. data/test/support/integration.rb +88 -0
  158. data/test/support/webrat/integrations/rails.rb +24 -0
  159. data/test/test_helper.rb +23 -0
  160. data/test/test_helpers_test.rb +101 -0
  161. metadata +335 -0
@@ -0,0 +1,204 @@
1
+ require 'test_helper'
2
+
3
+ class LockableTest < ActiveSupport::TestCase
4
+ def setup
5
+ setup_mailer
6
+ end
7
+
8
+ test "should respect maximum attempts configuration" do
9
+ user = create_user
10
+ user.confirm!
11
+ swap Devise, :maximum_attempts => 2 do
12
+ 3.times { user.valid_for_authentication?{ false } }
13
+ assert user.reload.access_locked?
14
+ end
15
+ end
16
+
17
+ test "should clear failed_attempts on successfull validation" do
18
+ user = create_user
19
+ user.confirm!
20
+ user.valid_for_authentication?{ false }
21
+ assert_equal 1, user.reload.failed_attempts
22
+ user.valid_for_authentication?{ true }
23
+ assert_equal 0, user.reload.failed_attempts
24
+ end
25
+
26
+ test "should not touch failed_attempts if lock_strategy is none" do
27
+ user = create_user
28
+ user.confirm!
29
+ swap Devise, :lock_strategy => :none, :maximum_attempts => 2 do
30
+ 3.times { user.valid_for_authentication?{ false } }
31
+ assert !user.access_locked?
32
+ assert_equal 0, user.failed_attempts
33
+ end
34
+ end
35
+
36
+ test 'should be valid for authentication with a unlocked user' do
37
+ user = create_user
38
+ user.lock_access!
39
+ user.unlock_access!
40
+ assert user.valid_for_authentication?{ true }
41
+ end
42
+
43
+ test "should verify whether a user is locked or not" do
44
+ user = create_user
45
+ assert_not user.access_locked?
46
+ user.lock_access!
47
+ assert user.access_locked?
48
+ end
49
+
50
+ test "active? should be the opposite of locked?" do
51
+ user = create_user
52
+ user.confirm!
53
+ assert user.active?
54
+ user.lock_access!
55
+ assert_not user.active?
56
+ end
57
+
58
+ test "should unlock an user by cleaning locked_at, falied_attempts and unlock_token" do
59
+ user = create_user
60
+ user.lock_access!
61
+ assert_not_nil user.reload.locked_at
62
+ assert_not_nil user.reload.unlock_token
63
+
64
+ user.unlock_access!
65
+ assert_nil user.reload.locked_at
66
+ assert_nil user.reload.unlock_token
67
+ assert_equal 0, user.reload.failed_attempts
68
+ end
69
+
70
+ test 'should not unlock an unlocked user' do
71
+ user = create_user
72
+ assert_not user.unlock_access!
73
+ assert_match "was not locked", user.errors[:email].join
74
+ end
75
+
76
+ test "new user should not be locked and should have zero failed_attempts" do
77
+ assert_not new_user.access_locked?
78
+ assert_equal 0, create_user.failed_attempts
79
+ end
80
+
81
+ test "should unlock user after unlock_in period" do
82
+ swap Devise, :unlock_in => 3.hours do
83
+ user = new_user
84
+ user.locked_at = 2.hours.ago
85
+ assert user.access_locked?
86
+
87
+ Devise.unlock_in = 1.hour
88
+ assert_not user.access_locked?
89
+ end
90
+ end
91
+
92
+ test "should not unlock in 'unlock_in' if :time unlock strategy is not set" do
93
+ swap Devise, :unlock_strategy => :email do
94
+ user = new_user
95
+ user.locked_at = 2.hours.ago
96
+ assert user.access_locked?
97
+ end
98
+ end
99
+
100
+ test "should set unlock_token when locking" do
101
+ user = create_user
102
+ assert_nil user.unlock_token
103
+ user.lock_access!
104
+ assert_not_nil user.unlock_token
105
+ end
106
+
107
+ test "should never generate the same unlock token for different users" do
108
+ unlock_tokens = []
109
+ 3.times do
110
+ user = create_user
111
+ user.lock_access!
112
+ token = user.unlock_token
113
+ assert !unlock_tokens.include?(token)
114
+ unlock_tokens << token
115
+ end
116
+ end
117
+
118
+ test "should not generate unlock_token when :email is not an unlock strategy" do
119
+ swap Devise, :unlock_strategy => :time do
120
+ user = create_user
121
+ user.lock_access!
122
+ assert_nil user.unlock_token
123
+ end
124
+ end
125
+
126
+ test "should send email with unlock instructions when :email is an unlock strategy" do
127
+ swap Devise, :unlock_strategy => :email do
128
+ user = create_user
129
+ assert_email_sent do
130
+ user.lock_access!
131
+ end
132
+ end
133
+ end
134
+
135
+ test "should not send email with unlock instructions when :email is not an unlock strategy" do
136
+ swap Devise, :unlock_strategy => :time do
137
+ user = create_user
138
+ assert_email_not_sent do
139
+ user.lock_access!
140
+ end
141
+ end
142
+ end
143
+
144
+ test 'should find and unlock an user automatically' do
145
+ user = create_user
146
+ user.lock_access!
147
+ locked_user = User.unlock_access_by_token(user.unlock_token)
148
+ assert_equal locked_user, user
149
+ assert_not user.reload.access_locked?
150
+ end
151
+
152
+ test 'should return a new record with errors when a invalid token is given' do
153
+ locked_user = User.unlock_access_by_token('invalid_token')
154
+ assert_not locked_user.persisted?
155
+ assert_equal "is invalid", locked_user.errors[:unlock_token].join
156
+ end
157
+
158
+ test 'should return a new record with errors when a blank token is given' do
159
+ locked_user = User.unlock_access_by_token('')
160
+ assert_not locked_user.persisted?
161
+ assert_equal "can't be blank", locked_user.errors[:unlock_token].join
162
+ end
163
+
164
+ test 'should find a user to send unlock instructions' do
165
+ user = create_user
166
+ user.lock_access!
167
+ unlock_user = User.send_unlock_instructions(:email => user.email)
168
+ assert_equal unlock_user, user
169
+ end
170
+
171
+ test 'should return a new user if no email was found' do
172
+ unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
173
+ assert_not unlock_user.persisted?
174
+ end
175
+
176
+ test 'should add error to new user email if no email was found' do
177
+ unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
178
+ assert_equal 'not found', unlock_user.errors[:email].join
179
+ end
180
+
181
+ test 'should find a user to send unlock instructions by authentication_keys' do
182
+ swap Devise, :authentication_keys => [:username, :email] do
183
+ user = create_user
184
+ unlock_user = User.send_unlock_instructions(:email => user.email, :username => user.username)
185
+ assert_equal unlock_user, user
186
+ end
187
+ end
188
+
189
+ test 'should require all authentication_keys' do
190
+ swap Devise, :authentication_keys => [:username, :email] do
191
+ user = create_user
192
+ unlock_user = User.send_unlock_instructions(:email => user.email)
193
+ assert_not unlock_user.persisted?
194
+ assert_equal "can't be blank", unlock_user.errors[:username].join
195
+ end
196
+ end
197
+
198
+ test 'should not be able to send instructions if the user is not locked' do
199
+ user = create_user
200
+ assert_not user.resend_unlock_token
201
+ assert_not user.access_locked?
202
+ assert_equal 'was not locked', user.errors[:email].join
203
+ end
204
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ class OauthableTest < ActiveSupport::TestCase
4
+ teardown { Devise::Oauth.reset_stubs! }
5
+
6
+ test "oauth_configs returns all configurations relative to that model" do
7
+ swap User, :oauth_providers => [:github] do
8
+ assert_equal User.oauth_configs, Devise.oauth_configs.slice(:github)
9
+ end
10
+ end
11
+
12
+ test "oauth_access_token returns the token object for the given provider" do
13
+ Devise::Oauth.stub!(:facebook) do |b|
14
+ b.get('/me?access_token=plataformatec') { [200, {}, {}.to_json] }
15
+ end
16
+
17
+ access_token = User.oauth_access_token(:facebook, "plataformatec")
18
+ assert_kind_of OAuth2::AccessToken, access_token
19
+ assert_equal "{}", access_token.get("/me")
20
+ end
21
+ end
@@ -0,0 +1,155 @@
1
+ require 'test_helper'
2
+
3
+ class RecoverableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should not generate reset password token after creating a record' do
10
+ assert_nil new_user.reset_password_token
11
+ end
12
+
13
+ test 'should regenerate reset password token each time' do
14
+ user = create_user
15
+ 3.times do
16
+ token = user.reset_password_token
17
+ user.send_reset_password_instructions
18
+ assert_not_equal token, user.reset_password_token
19
+ end
20
+ end
21
+
22
+ test 'should never generate the same reset password token for different users' do
23
+ reset_password_tokens = []
24
+ 3.times do
25
+ user = create_user
26
+ user.send_reset_password_instructions
27
+ token = user.reset_password_token
28
+ assert !reset_password_tokens.include?(token)
29
+ reset_password_tokens << token
30
+ end
31
+ end
32
+
33
+ test 'should reset password and password confirmation from params' do
34
+ user = create_user
35
+ user.reset_password!('123456789', '987654321')
36
+ assert_equal '123456789', user.password
37
+ assert_equal '987654321', user.password_confirmation
38
+ end
39
+
40
+ test 'should reset password and save the record' do
41
+ assert create_user.reset_password!('123456789', '123456789')
42
+ end
43
+
44
+ test 'should clear reset password token while reseting the password' do
45
+ user = create_user
46
+ assert_nil user.reset_password_token
47
+
48
+ user.send_reset_password_instructions
49
+ assert_present user.reset_password_token
50
+ assert user.reset_password!('123456789', '123456789')
51
+ assert_nil user.reset_password_token
52
+ end
53
+
54
+ test 'should not clear reset password token if record is invalid' do
55
+ user = create_user
56
+ user.send_reset_password_instructions
57
+ assert_present user.reset_password_token
58
+ assert_not user.reset_password!('123456789', '987654321')
59
+ assert_present user.reset_password_token
60
+ end
61
+
62
+ test 'should not reset password with invalid data' do
63
+ user = create_user
64
+ user.stubs(:valid?).returns(false)
65
+ assert_not user.reset_password!('123456789', '987654321')
66
+ end
67
+
68
+ test 'should reset reset password token and send instructions by email' do
69
+ user = create_user
70
+ assert_email_sent do
71
+ token = user.reset_password_token
72
+ user.send_reset_password_instructions
73
+ assert_not_equal token, user.reset_password_token
74
+ end
75
+ end
76
+
77
+ test 'should find a user to send instructions by email' do
78
+ user = create_user
79
+ reset_password_user = User.send_reset_password_instructions(:email => user.email)
80
+ assert_equal reset_password_user, user
81
+ end
82
+
83
+ test 'should return a new record with errors if user was not found by e-mail' do
84
+ reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
85
+ assert_not reset_password_user.persisted?
86
+ assert_equal "not found", reset_password_user.errors[:email].join
87
+ end
88
+
89
+ test 'should find a user to send instructions by authentication_keys' do
90
+ swap Devise, :authentication_keys => [:username, :email] do
91
+ user = create_user
92
+ reset_password_user = User.send_reset_password_instructions(:email => user.email, :username => user.username)
93
+ assert_equal reset_password_user, user
94
+ end
95
+ end
96
+
97
+ test 'should require all authentication_keys' do
98
+ swap Devise, :authentication_keys => [:username, :email] do
99
+ user = create_user
100
+ reset_password_user = User.send_reset_password_instructions(:email => user.email)
101
+ assert_not reset_password_user.persisted?
102
+ assert_equal "can't be blank", reset_password_user.errors[:username].join
103
+ end
104
+ end
105
+
106
+ test 'should reset reset_password_token before send the reset instructions email' do
107
+ user = create_user
108
+ token = user.reset_password_token
109
+ reset_password_user = User.send_reset_password_instructions(:email => user.email)
110
+ assert_not_equal token, user.reload.reset_password_token
111
+ end
112
+
113
+ test 'should send email instructions to the user reset his password' do
114
+ user = create_user
115
+ assert_email_sent do
116
+ User.send_reset_password_instructions(:email => user.email)
117
+ end
118
+ end
119
+
120
+ test 'should find a user to reset his password based on reset_password_token' do
121
+ user = create_user
122
+ user.send :generate_reset_password_token!
123
+
124
+ reset_password_user = User.reset_password_by_token(:reset_password_token => user.reset_password_token)
125
+ assert_equal reset_password_user, user
126
+ end
127
+
128
+ test 'should a new record with errors if no reset_password_token is found' do
129
+ reset_password_user = User.reset_password_by_token(:reset_password_token => 'invalid_token')
130
+ assert_not reset_password_user.persisted?
131
+ assert_equal "is invalid", reset_password_user.errors[:reset_password_token].join
132
+ end
133
+
134
+ test 'should a new record with errors if reset_password_token is blank' do
135
+ reset_password_user = User.reset_password_by_token(:reset_password_token => '')
136
+ assert_not reset_password_user.persisted?
137
+ assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
138
+ end
139
+
140
+ test 'should reset successfully user password given the new password and confirmation' do
141
+ user = create_user
142
+ old_password = user.password
143
+ user.send :generate_reset_password_token!
144
+
145
+ reset_password_user = User.reset_password_by_token(
146
+ :reset_password_token => user.reset_password_token,
147
+ :password => 'new_password',
148
+ :password_confirmation => 'new_password'
149
+ )
150
+ user.reload
151
+
152
+ assert_not user.valid_password?(old_password)
153
+ assert user.valid_password?('new_password')
154
+ end
155
+ end
@@ -0,0 +1,271 @@
1
+ require 'test_helper'
2
+
3
+ module SharedRememberableTest
4
+ extend ActiveSupport::Testing::Declarative
5
+
6
+ test 'should respond to remember_me attribute' do
7
+ assert resource_class.new.respond_to?(:remember_me)
8
+ assert resource_class.new.respond_to?(:remember_me=)
9
+ end
10
+
11
+ test 'forget_me should clear remember_created_at' do
12
+ resource = create_resource
13
+ resource.remember_me!
14
+ assert_not resource.remember_created_at.nil?
15
+ resource.forget_me!
16
+ assert resource.remember_created_at.nil?
17
+ end
18
+
19
+ test 'remember is expired if not created at timestamp is set' do
20
+ assert create_resource.remember_expired?
21
+ end
22
+
23
+ test 'serialize should return nil if no resource is found' do
24
+ assert_nil resource_class.serialize_from_cookie(0, "123")
25
+ end
26
+
27
+ test 'remember me return nil if is a valid resource with invalid token' do
28
+ resource = create_resource
29
+ assert_nil resource_class.serialize_from_cookie(resource.id, "123")
30
+ end
31
+
32
+ test 'remember for should fallback to devise remember for default configuration' do
33
+ swap Devise, :remember_for => 1.day do
34
+ resource = create_resource
35
+ resource.remember_me!
36
+ assert_not resource.remember_expired?
37
+ end
38
+ end
39
+
40
+ test 'remember expires at should sum date of creation with remember for configuration' do
41
+ swap Devise, :remember_for => 3.days do
42
+ resource = create_resource
43
+ resource.remember_me!
44
+ assert_equal 3.days.from_now.to_date, resource.remember_expires_at.to_date
45
+
46
+ Devise.remember_for = 5.days
47
+ assert_equal 5.days.from_now.to_date, resource.remember_expires_at.to_date
48
+ end
49
+ end
50
+
51
+ test 'remember should be expired if remember_for is zero' do
52
+ swap Devise, :remember_for => 0.days do
53
+ Devise.remember_for = 0.days
54
+ resource = create_resource
55
+ resource.remember_me!
56
+ assert resource.remember_expired?
57
+ end
58
+ end
59
+
60
+ test 'remember should be expired if it was created before limit time' do
61
+ swap Devise, :remember_for => 1.day do
62
+ resource = create_resource
63
+ resource.remember_me!
64
+ resource.remember_created_at = 2.days.ago
65
+ resource.save
66
+ assert resource.remember_expired?
67
+ end
68
+ end
69
+
70
+ test 'remember should not be expired if it was created whitin the limit time' do
71
+ swap Devise, :remember_for => 30.days do
72
+ resource = create_resource
73
+ resource.remember_me!
74
+ resource.remember_created_at = (30.days.ago + 2.minutes)
75
+ resource.save
76
+ assert_not resource.remember_expired?
77
+ end
78
+ end
79
+
80
+ test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
81
+ swap Devise, :remember_for => 5.minutes do
82
+ resource = create_resource
83
+ resource.remember_me!(false)
84
+ assert resource.remember_created_at
85
+
86
+ resource.remember_created_at = old = 10.minutes.ago
87
+ resource.save
88
+
89
+ resource.remember_me!(false)
90
+ assert_not_equal old.to_i, resource.remember_created_at.to_i
91
+ end
92
+ end
93
+
94
+ test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
95
+ swap Devise, :remember_for => 1.year do
96
+ resource = create_resource
97
+ resource.remember_me!(false)
98
+ assert resource.remember_created_at
99
+
100
+ resource.remember_created_at = old = 10.minutes.ago.utc
101
+ resource.save
102
+
103
+ resource.remember_me!(false)
104
+ assert_equal old.to_i, resource.remember_created_at.to_i
105
+ end
106
+ end
107
+
108
+ test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
109
+ swap Devise, :remember_for => 1.year do
110
+ resource = create_resource
111
+ resource.remember_me!(true)
112
+ assert resource.remember_created_at
113
+
114
+ resource.remember_created_at = old = 10.minutes.ago
115
+ resource.save
116
+
117
+ resource.remember_me!(true)
118
+ assert_not_equal old, resource.remember_created_at
119
+ end
120
+ end
121
+ end
122
+
123
+ class RememberableTest < ActiveSupport::TestCase
124
+ include SharedRememberableTest
125
+
126
+ def resource_class
127
+ Admin
128
+ end
129
+
130
+ def create_resource
131
+ create_admin
132
+ end
133
+
134
+ test 'remember_me should generate a new token and save the record without validating' do
135
+ admin = create_admin
136
+ admin.expects(:valid?).never
137
+ token = admin.remember_token
138
+ admin.remember_me!
139
+ assert_not_equal token, admin.remember_token
140
+ assert_not admin.changed?
141
+ end
142
+
143
+ test 'forget_me should clear remember token and save the record without validating' do
144
+ admin = create_admin
145
+ admin.remember_me!
146
+ assert_not admin.remember_token.nil?
147
+ admin.expects(:valid?).never
148
+ admin.forget_me!
149
+ assert admin.remember_token.nil?
150
+ assert_not admin.changed?
151
+ end
152
+
153
+ test 'serialize into cookie' do
154
+ admin = create_admin
155
+ admin.remember_me!
156
+ assert_equal [admin.id, admin.remember_token], Admin.serialize_into_cookie(admin)
157
+ end
158
+
159
+ test 'serialize from cookie' do
160
+ admin = create_admin
161
+ admin.remember_me!
162
+ assert_equal admin, Admin.serialize_from_cookie(admin.id, admin.remember_token)
163
+ end
164
+
165
+ test 'if remember_across_browsers is true, remember_me! should create a new token if no token exists' do
166
+ swap Devise, :remember_across_browsers => true, :remember_for => 1.year do
167
+ admin = create_admin
168
+ assert_equal nil, admin.remember_token
169
+ admin.remember_me!
170
+ assert_not_equal nil, admin.remember_token
171
+ end
172
+ end
173
+
174
+ test 'if remember_across_browsers is true, remember_me! should create a new token if a token exists but has expired' do
175
+ swap Devise, :remember_across_browsers => true, :remember_for => 1.day do
176
+ admin = create_admin
177
+ admin.remember_me!
178
+ admin.remember_created_at = 2.days.ago
179
+ admin.save
180
+ token = admin.remember_token
181
+ admin.remember_me!
182
+ assert_not_equal token, admin.remember_token
183
+ end
184
+ end
185
+
186
+ test 'if remember_across_browsers is true, remember_me! should not create a new token if a token exists and has not expired' do
187
+ swap Devise, :remember_across_browsers => true, :remember_for => 2.days do
188
+ admin = create_admin
189
+ admin.remember_me!
190
+ admin.remember_created_at = 1.day.ago
191
+ admin.save
192
+ token = admin.remember_token
193
+ admin.remember_me!
194
+ assert_equal token, admin.remember_token
195
+ end
196
+ end
197
+
198
+ test 'if remember_across_browsers is false, remember_me! should create a new token if no token exists' do
199
+ swap Devise, :remember_across_browsers => false do
200
+ admin = create_admin
201
+ assert_equal nil, admin.remember_token
202
+ admin.remember_me!
203
+ assert_not_equal nil, admin.remember_token
204
+ end
205
+ end
206
+
207
+ test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists but has expired' do
208
+ swap Devise, :remember_across_browsers => false, :remember_for => 1.day do
209
+ admin = create_admin
210
+ admin.remember_me!
211
+ admin.remember_created_at = 2.days.ago
212
+ admin.save
213
+ token = admin.remember_token
214
+ admin.remember_me!
215
+ assert_not_equal token, admin.remember_token
216
+ end
217
+ end
218
+
219
+ test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists and has not expired' do
220
+ swap Devise, :remember_across_browsers => false, :remember_for => 2.days do
221
+ admin = create_admin
222
+ admin.remember_me!
223
+ admin.remember_created_at = 1.day.ago
224
+ admin.save
225
+ token = admin.remember_token
226
+ admin.remember_me!
227
+ assert_not_equal token, admin.remember_token
228
+ end
229
+ end
230
+ end
231
+
232
+ class WithSaltRememberableTest < ActiveSupport::TestCase
233
+ include SharedRememberableTest
234
+
235
+ setup do
236
+ assert_not User.new.respond_to?(:remember_token)
237
+ end
238
+
239
+ def resource_class
240
+ User
241
+ end
242
+
243
+ def create_resource
244
+ create_user
245
+ end
246
+
247
+ test 'remember_me should not generate a new token if using salt' do
248
+ user = create_user
249
+ user.expects(:valid?).never
250
+ user.remember_me!
251
+ end
252
+
253
+ test 'forget_me should not clear remember token if using salt' do
254
+ user = create_user
255
+ user.remember_me!
256
+ user.expects(:valid?).never
257
+ user.forget_me!
258
+ end
259
+
260
+ test 'serialize into cookie' do
261
+ user = create_user
262
+ user.remember_me!
263
+ assert_equal [user.id, user.authenticatable_salt], User.serialize_into_cookie(user)
264
+ end
265
+
266
+ test 'serialize from cookie' do
267
+ user = create_user
268
+ user.remember_me!
269
+ assert_equal user, User.serialize_from_cookie(user.id, user.authenticatable_salt)
270
+ end
271
+ end