loyal_devise 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +881 -0
  4. data/CONTRIBUTING.md +12 -0
  5. data/Gemfile +31 -0
  6. data/Gemfile.lock +154 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +388 -0
  9. data/Rakefile +34 -0
  10. data/app/controllers/devise/confirmations_controller.rb +44 -0
  11. data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
  12. data/app/controllers/devise/passwords_controller.rb +57 -0
  13. data/app/controllers/devise/registrations_controller.rb +120 -0
  14. data/app/controllers/devise/sessions_controller.rb +51 -0
  15. data/app/controllers/devise/unlocks_controller.rb +45 -0
  16. data/app/controllers/devise_controller.rb +193 -0
  17. data/app/helpers/devise_helper.rb +26 -0
  18. data/app/mailers/devise/mailer.rb +16 -0
  19. data/app/views/devise/_links.erb +3 -0
  20. data/app/views/devise/confirmations/new.html.erb +12 -0
  21. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  22. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  23. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  24. data/app/views/devise/passwords/edit.html.erb +16 -0
  25. data/app/views/devise/passwords/new.html.erb +12 -0
  26. data/app/views/devise/registrations/edit.html.erb +25 -0
  27. data/app/views/devise/registrations/new.html.erb +18 -0
  28. data/app/views/devise/sessions/new.html.erb +17 -0
  29. data/app/views/devise/shared/_links.erb +25 -0
  30. data/app/views/devise/unlocks/new.html.erb +12 -0
  31. data/config/locales/en.yml +59 -0
  32. data/devise.gemspec +26 -0
  33. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  34. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  35. data/lib/devise/controllers/helpers.rb +273 -0
  36. data/lib/devise/controllers/rememberable.rb +53 -0
  37. data/lib/devise/controllers/scoped_views.rb +18 -0
  38. data/lib/devise/controllers/url_helpers.rb +68 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/failure_app.rb +188 -0
  41. data/lib/devise/hooks/activatable.rb +12 -0
  42. data/lib/devise/hooks/forgetable.rb +10 -0
  43. data/lib/devise/hooks/lockable.rb +8 -0
  44. data/lib/devise/hooks/rememberable.rb +7 -0
  45. data/lib/devise/hooks/timeoutable.rb +26 -0
  46. data/lib/devise/hooks/trackable.rb +10 -0
  47. data/lib/devise/mailers/helpers.rb +92 -0
  48. data/lib/devise/mapping.rb +173 -0
  49. data/lib/devise/models/authenticatable.rb +269 -0
  50. data/lib/devise/models/confirmable.rb +271 -0
  51. data/lib/devise/models/database_authenticatable.rb +127 -0
  52. data/lib/devise/models/lockable.rb +194 -0
  53. data/lib/devise/models/omniauthable.rb +28 -0
  54. data/lib/devise/models/recoverable.rb +141 -0
  55. data/lib/devise/models/registerable.rb +26 -0
  56. data/lib/devise/models/rememberable.rb +126 -0
  57. data/lib/devise/models/timeoutable.rb +50 -0
  58. data/lib/devise/models/token_authenticatable.rb +90 -0
  59. data/lib/devise/models/trackable.rb +36 -0
  60. data/lib/devise/models/validatable.rb +67 -0
  61. data/lib/devise/models.rb +129 -0
  62. data/lib/devise/modules.rb +30 -0
  63. data/lib/devise/omniauth/config.rb +46 -0
  64. data/lib/devise/omniauth/url_helpers.rb +19 -0
  65. data/lib/devise/omniauth.rb +29 -0
  66. data/lib/devise/orm/active_record.rb +4 -0
  67. data/lib/devise/orm/mongoid.rb +4 -0
  68. data/lib/devise/param_filter.rb +42 -0
  69. data/lib/devise/rails/routes.rb +447 -0
  70. data/lib/devise/rails/warden_compat.rb +44 -0
  71. data/lib/devise/rails.rb +55 -0
  72. data/lib/devise/strategies/authenticatable.rb +177 -0
  73. data/lib/devise/strategies/base.rb +21 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  75. data/lib/devise/strategies/rememberable.rb +56 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  77. data/lib/devise/test_helpers.rb +132 -0
  78. data/lib/devise/time_inflector.rb +15 -0
  79. data/lib/devise/version.rb +4 -0
  80. data/lib/devise.rb +445 -0
  81. data/lib/generators/active_record/devise_generator.rb +80 -0
  82. data/lib/generators/active_record/templates/migration.rb +20 -0
  83. data/lib/generators/active_record/templates/migration_existing.rb +27 -0
  84. data/lib/generators/devise/devise_generator.rb +25 -0
  85. data/lib/generators/devise/install_generator.rb +25 -0
  86. data/lib/generators/devise/orm_helpers.rb +33 -0
  87. data/lib/generators/devise/views_generator.rb +117 -0
  88. data/lib/generators/mongoid/devise_generator.rb +58 -0
  89. data/lib/generators/templates/README +35 -0
  90. data/lib/generators/templates/devise.rb +241 -0
  91. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  92. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  93. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  94. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  96. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  97. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  98. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  99. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  101. data/test/controllers/custom_strategy_test.rb +63 -0
  102. data/test/controllers/helpers_test.rb +254 -0
  103. data/test/controllers/internal_helpers_test.rb +111 -0
  104. data/test/controllers/sessions_controller_test.rb +58 -0
  105. data/test/controllers/url_helpers_test.rb +60 -0
  106. data/test/delegator_test.rb +20 -0
  107. data/test/devise_test.rb +73 -0
  108. data/test/failure_app_test.rb +222 -0
  109. data/test/generators/active_record_generator_test.rb +76 -0
  110. data/test/generators/devise_generator_test.rb +40 -0
  111. data/test/generators/install_generator_test.rb +14 -0
  112. data/test/generators/mongoid_generator_test.rb +24 -0
  113. data/test/generators/views_generator_test.rb +53 -0
  114. data/test/helpers/devise_helper_test.rb +52 -0
  115. data/test/indifferent_hash.rb +34 -0
  116. data/test/integration/authenticatable_test.rb +634 -0
  117. data/test/integration/confirmable_test.rb +299 -0
  118. data/test/integration/database_authenticatable_test.rb +83 -0
  119. data/test/integration/http_authenticatable_test.rb +98 -0
  120. data/test/integration/lockable_test.rb +243 -0
  121. data/test/integration/omniauthable_test.rb +134 -0
  122. data/test/integration/recoverable_test.rb +307 -0
  123. data/test/integration/registerable_test.rb +346 -0
  124. data/test/integration/rememberable_test.rb +159 -0
  125. data/test/integration/timeoutable_test.rb +141 -0
  126. data/test/integration/token_authenticatable_test.rb +162 -0
  127. data/test/integration/trackable_test.rb +93 -0
  128. data/test/mailers/confirmation_instructions_test.rb +103 -0
  129. data/test/mailers/reset_password_instructions_test.rb +84 -0
  130. data/test/mailers/unlock_instructions_test.rb +78 -0
  131. data/test/mapping_test.rb +128 -0
  132. data/test/models/authenticatable_test.rb +8 -0
  133. data/test/models/confirmable_test.rb +392 -0
  134. data/test/models/database_authenticatable_test.rb +190 -0
  135. data/test/models/lockable_test.rb +274 -0
  136. data/test/models/omniauthable_test.rb +8 -0
  137. data/test/models/recoverable_test.rb +206 -0
  138. data/test/models/registerable_test.rb +8 -0
  139. data/test/models/rememberable_test.rb +175 -0
  140. data/test/models/serializable_test.rb +49 -0
  141. data/test/models/timeoutable_test.rb +47 -0
  142. data/test/models/token_authenticatable_test.rb +56 -0
  143. data/test/models/trackable_test.rb +14 -0
  144. data/test/models/validatable_test.rb +117 -0
  145. data/test/models_test.rb +180 -0
  146. data/test/omniauth/config_test.rb +58 -0
  147. data/test/omniauth/url_helpers_test.rb +52 -0
  148. data/test/orm/active_record.rb +10 -0
  149. data/test/orm/mongoid.rb +15 -0
  150. data/test/rails_app/Rakefile +10 -0
  151. data/test/rails_app/app/active_record/admin.rb +7 -0
  152. data/test/rails_app/app/active_record/shim.rb +3 -0
  153. data/test/rails_app/app/active_record/user.rb +7 -0
  154. data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
  155. data/test/rails_app/app/controllers/admins_controller.rb +12 -0
  156. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  157. data/test/rails_app/app/controllers/home_controller.rb +26 -0
  158. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
  159. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
  160. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
  161. data/test/rails_app/app/controllers/users_controller.rb +24 -0
  162. data/test/rails_app/app/helpers/application_helper.rb +4 -0
  163. data/test/rails_app/app/mailers/users/mailer.rb +9 -0
  164. data/test/rails_app/app/mongoid/admin.rb +28 -0
  165. data/test/rails_app/app/mongoid/shim.rb +25 -0
  166. data/test/rails_app/app/mongoid/user.rb +43 -0
  167. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  168. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  169. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  170. data/test/rails_app/app/views/home/index.html.erb +1 -0
  171. data/test/rails_app/app/views/home/join.html.erb +1 -0
  172. data/test/rails_app/app/views/home/private.html.erb +1 -0
  173. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  174. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  175. data/test/rails_app/app/views/users/index.html.erb +1 -0
  176. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  177. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  178. data/test/rails_app/config/application.rb +42 -0
  179. data/test/rails_app/config/boot.rb +9 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +6 -0
  182. data/test/rails_app/config/environments/development.rb +19 -0
  183. data/test/rails_app/config/environments/production.rb +34 -0
  184. data/test/rails_app/config/environments/test.rb +34 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
  186. data/test/rails_app/config/initializers/devise.rb +179 -0
  187. data/test/rails_app/config/initializers/inflections.rb +3 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +3 -0
  189. data/test/rails_app/config/routes.rb +101 -0
  190. data/test/rails_app/config.ru +4 -0
  191. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
  192. data/test/rails_app/db/schema.rb +53 -0
  193. data/test/rails_app/lib/shared_admin.rb +15 -0
  194. data/test/rails_app/lib/shared_user.rb +27 -0
  195. data/test/rails_app/public/404.html +26 -0
  196. data/test/rails_app/public/422.html +26 -0
  197. data/test/rails_app/public/500.html +26 -0
  198. data/test/rails_app/public/favicon.ico +0 -0
  199. data/test/rails_app/script/rails +10 -0
  200. data/test/routes_test.rb +249 -0
  201. data/test/support/assertions.rb +41 -0
  202. data/test/support/helpers.rb +92 -0
  203. data/test/support/integration.rb +93 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +25 -0
  206. data/test/test_helper.rb +28 -0
  207. data/test/test_helpers_test.rb +152 -0
  208. metadata +407 -0
@@ -0,0 +1,392 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class ConfirmableTest < ActiveSupport::TestCase
5
+
6
+ def setup
7
+ setup_mailer
8
+ end
9
+
10
+ test 'should generate confirmation token after creating a record' do
11
+ assert_nil new_user.confirmation_token
12
+ assert_not_nil create_user.confirmation_token
13
+ end
14
+
15
+ test 'should never generate the same confirmation token for different users' do
16
+ confirmation_tokens = []
17
+ 3.times do
18
+ token = create_user.confirmation_token
19
+ assert !confirmation_tokens.include?(token)
20
+ confirmation_tokens << token
21
+ end
22
+ end
23
+
24
+ test 'should confirm a user by updating confirmed at' do
25
+ user = create_user
26
+ assert_nil user.confirmed_at
27
+ assert user.confirm!
28
+ assert_not_nil user.confirmed_at
29
+ end
30
+
31
+ test 'should clear confirmation token while confirming a user' do
32
+ user = create_user
33
+ assert_present user.confirmation_token
34
+ user.confirm!
35
+ assert_nil user.confirmation_token
36
+ end
37
+
38
+ test 'should verify whether a user is confirmed or not' do
39
+ assert_not new_user.confirmed?
40
+ user = create_user
41
+ assert_not user.confirmed?
42
+ user.confirm!
43
+ assert user.confirmed?
44
+ end
45
+
46
+ test 'should not confirm a user already confirmed' do
47
+ user = create_user
48
+ assert user.confirm!
49
+ assert_blank user.errors[:email]
50
+
51
+ assert_not user.confirm!
52
+ assert_equal "was already confirmed, please try signing in", user.errors[:email].join
53
+ end
54
+
55
+ test 'should find and confirm a user automatically' do
56
+ user = create_user
57
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
58
+ assert_equal confirmed_user, user
59
+ assert user.reload.confirmed?
60
+ end
61
+
62
+ test 'should return a new record with errors when a invalid token is given' do
63
+ confirmed_user = User.confirm_by_token('invalid_confirmation_token')
64
+ assert_not confirmed_user.persisted?
65
+ assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
66
+ end
67
+
68
+ test 'should return a new record with errors when a blank token is given' do
69
+ confirmed_user = User.confirm_by_token('')
70
+ assert_not confirmed_user.persisted?
71
+ assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
72
+ end
73
+
74
+ test 'should generate errors for a user email if user is already confirmed' do
75
+ user = create_user
76
+ user.confirmed_at = Time.now
77
+ user.save
78
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
79
+ assert confirmed_user.confirmed?
80
+ assert_equal "was already confirmed, please try signing in", confirmed_user.errors[:email].join
81
+ end
82
+
83
+ test 'should send confirmation instructions by email' do
84
+ assert_email_sent "mynewuser@example.com" do
85
+ create_user :email => "mynewuser@example.com"
86
+ end
87
+ end
88
+
89
+ test 'should not send confirmation when trying to save an invalid user' do
90
+ assert_email_not_sent do
91
+ user = new_user
92
+ user.stubs(:valid?).returns(false)
93
+ user.save
94
+ end
95
+ end
96
+
97
+ test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
98
+ user = new_user
99
+ user.skip_confirmation!
100
+
101
+ assert_email_not_sent do
102
+ user.save!
103
+ assert_nil user.confirmation_token
104
+ assert_not_nil user.confirmed_at
105
+ end
106
+ end
107
+
108
+ test 'should find a user to send confirmation instructions' do
109
+ user = create_user
110
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
111
+ assert_equal confirmation_user, user
112
+ end
113
+
114
+ test 'should return a new user if no email was found' do
115
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@example.com")
116
+ assert_not confirmation_user.persisted?
117
+ end
118
+
119
+ test 'should add error to new user email if no email was found' do
120
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@example.com")
121
+ assert confirmation_user.errors[:email]
122
+ assert_equal "not found", confirmation_user.errors[:email].join
123
+ end
124
+
125
+ test 'should send email instructions for the user confirm its email' do
126
+ user = create_user
127
+ assert_email_sent user.email do
128
+ User.send_confirmation_instructions(:email => user.email)
129
+ end
130
+ end
131
+
132
+ test 'should always have confirmation token when email is sent' do
133
+ user = new_user
134
+ user.instance_eval { def confirmation_required?; false end }
135
+ user.save
136
+ user.send_confirmation_instructions
137
+ assert_not_nil user.reload.confirmation_token
138
+ end
139
+
140
+ test 'should not resend email instructions if the user change his email' do
141
+ user = create_user
142
+ user.email = 'new_test@example.com'
143
+ assert_email_not_sent do
144
+ user.save!
145
+ end
146
+ end
147
+
148
+ test 'should not reset confirmation status or token when updating email' do
149
+ user = create_user
150
+ user.confirm!
151
+ user.email = 'new_test@example.com'
152
+ user.save!
153
+
154
+ user.reload
155
+ assert user.confirmed?
156
+ assert_nil user.confirmation_token
157
+ end
158
+
159
+ test 'should not be able to send instructions if the user is already confirmed' do
160
+ user = create_user
161
+ user.confirm!
162
+ assert_not user.resend_confirmation_token
163
+ assert user.confirmed?
164
+ assert_equal 'was already confirmed, please try signing in', user.errors[:email].join
165
+ end
166
+
167
+ test 'confirm time should fallback to devise confirm in default configuration' do
168
+ swap Devise, :allow_unconfirmed_access_for => 1.day do
169
+ user = new_user
170
+ user.confirmation_sent_at = 2.days.ago
171
+ assert_not user.active_for_authentication?
172
+
173
+ Devise.allow_unconfirmed_access_for = 3.days
174
+ assert user.active_for_authentication?
175
+ end
176
+ end
177
+
178
+ test 'should be active when confirmation sent at is not overpast' do
179
+ swap Devise, :allow_unconfirmed_access_for => 5.days do
180
+ Devise.allow_unconfirmed_access_for = 5.days
181
+ user = create_user
182
+
183
+ user.confirmation_sent_at = 4.days.ago
184
+ assert user.active_for_authentication?
185
+
186
+ user.confirmation_sent_at = 5.days.ago
187
+ assert_not user.active_for_authentication?
188
+ end
189
+ end
190
+
191
+ test 'should be active when already confirmed' do
192
+ user = create_user
193
+ assert_not user.confirmed?
194
+ assert_not user.active_for_authentication?
195
+
196
+ user.confirm!
197
+ assert user.confirmed?
198
+ assert user.active_for_authentication?
199
+ end
200
+
201
+ test 'should not be active when confirm in is zero' do
202
+ Devise.allow_unconfirmed_access_for = 0.days
203
+ user = create_user
204
+ user.confirmation_sent_at = Date.today
205
+ assert_not user.active_for_authentication?
206
+ end
207
+
208
+ test 'should not be active without confirmation' do
209
+ user = create_user
210
+ user.confirmation_sent_at = nil
211
+ user.save
212
+ assert_not user.reload.active_for_authentication?
213
+ end
214
+
215
+ test 'should be active without confirmation when confirmation is not required' do
216
+ user = create_user
217
+ user.instance_eval { def confirmation_required?; false end }
218
+ user.confirmation_sent_at = nil
219
+ user.save
220
+ assert user.reload.active_for_authentication?
221
+ end
222
+
223
+ test 'should find a user to send email instructions for the user confirm its email by authentication_keys' do
224
+ swap Devise, :authentication_keys => [:username, :email] do
225
+ user = create_user
226
+ confirm_user = User.send_confirmation_instructions(:email => user.email, :username => user.username)
227
+ assert_equal confirm_user, user
228
+ end
229
+ end
230
+
231
+ test 'should require all confirmation_keys' do
232
+ swap Devise, :confirmation_keys => [:username, :email] do
233
+ user = create_user
234
+ confirm_user = User.send_confirmation_instructions(:email => user.email)
235
+ assert_not confirm_user.persisted?
236
+ assert_equal "can't be blank", confirm_user.errors[:username].join
237
+ end
238
+ end
239
+
240
+ def confirm_user_by_token_with_confirmation_sent_at(confirmation_sent_at)
241
+ user = create_user
242
+ user.update_attribute(:confirmation_sent_at, confirmation_sent_at)
243
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
244
+ assert_equal confirmed_user, user
245
+ user.reload.confirmed?
246
+ end
247
+
248
+ test 'should accept confirmation email token even after 5 years when no expiration is set' do
249
+ assert confirm_user_by_token_with_confirmation_sent_at(5.years.ago)
250
+ end
251
+
252
+ test 'should accept confirmation email token after 2 days when expiration is set to 3 days' do
253
+ swap Devise, :confirm_within => 3.days do
254
+ assert confirm_user_by_token_with_confirmation_sent_at(2.days.ago)
255
+ end
256
+ end
257
+
258
+ test 'should not accept confirmation email token after 4 days when expiration is set to 3 days' do
259
+ swap Devise, :confirm_within => 3.days do
260
+ assert_not confirm_user_by_token_with_confirmation_sent_at(4.days.ago)
261
+ end
262
+ end
263
+
264
+ test 'should generate a new token if the previous one has expired' do
265
+ swap Devise, :confirm_within => 3.days do
266
+ user = create_user
267
+ user.update_attribute(:confirmation_sent_at, 4.days.ago)
268
+ old = user.confirmation_token
269
+ user.resend_confirmation_token
270
+ assert_not_equal user.confirmation_token, old
271
+ end
272
+ end
273
+ end
274
+
275
+ class ReconfirmableTest < ActiveSupport::TestCase
276
+ test 'should not worry about validations on confirm even with reconfirmable' do
277
+ admin = create_admin
278
+ admin.reset_password_token = "a"
279
+ assert admin.confirm!
280
+ end
281
+
282
+ test 'should generate confirmation token after changing email' do
283
+ admin = create_admin
284
+ assert admin.confirm!
285
+ assert_nil admin.confirmation_token
286
+ assert admin.update_attributes(:email => 'new_test@example.com')
287
+ assert_not_nil admin.confirmation_token
288
+ end
289
+
290
+ test 'should not generate confirmation token if skipping reconfirmation after changing email' do
291
+ admin = create_admin
292
+ assert admin.confirm!
293
+ admin.skip_reconfirmation!
294
+ assert admin.update_attributes(:email => 'new_test@example.com')
295
+ assert_nil admin.confirmation_token
296
+ end
297
+
298
+
299
+ test 'should regenerate confirmation token after changing email' do
300
+ admin = create_admin
301
+ assert admin.confirm!
302
+ assert admin.update_attributes(:email => 'old_test@example.com')
303
+ token = admin.confirmation_token
304
+ assert admin.update_attributes(:email => 'new_test@example.com')
305
+ assert_not_equal token, admin.confirmation_token
306
+ end
307
+
308
+ test 'should send confirmation instructions by email after changing email' do
309
+ admin = create_admin
310
+ assert admin.confirm!
311
+ assert_email_sent "new_test@example.com" do
312
+ assert admin.update_attributes(:email => 'new_test@example.com')
313
+ end
314
+ end
315
+
316
+ test 'should not send confirmation by email after changing password' do
317
+ admin = create_admin
318
+ assert admin.confirm!
319
+ assert_email_not_sent do
320
+ assert admin.update_attributes(:password => 'newpass', :password_confirmation => 'newpass')
321
+ end
322
+ end
323
+
324
+ test 'should stay confirmed when email is changed' do
325
+ admin = create_admin
326
+ assert admin.confirm!
327
+ assert admin.update_attributes(:email => 'new_test@example.com')
328
+ assert admin.confirmed?
329
+ end
330
+
331
+ test 'should update email only when it is confirmed' do
332
+ admin = create_admin
333
+ assert admin.confirm!
334
+ assert admin.update_attributes(:email => 'new_test@example.com')
335
+ assert_not_equal 'new_test@example.com', admin.email
336
+ assert admin.confirm!
337
+ assert_equal 'new_test@example.com', admin.email
338
+ end
339
+
340
+ test 'should not allow admin to get past confirmation email by resubmitting their new address' do
341
+ admin = create_admin
342
+ assert admin.confirm!
343
+ assert admin.update_attributes(:email => 'new_test@example.com')
344
+ assert_not_equal 'new_test@example.com', admin.email
345
+ assert admin.update_attributes(:email => 'new_test@example.com')
346
+ assert_not_equal 'new_test@example.com', admin.email
347
+ end
348
+
349
+ test 'should find a admin by send confirmation instructions with unconfirmed_email' do
350
+ admin = create_admin
351
+ assert admin.confirm!
352
+ assert admin.update_attributes(:email => 'new_test@example.com')
353
+ confirmation_admin = Admin.send_confirmation_instructions(:email => admin.unconfirmed_email)
354
+ assert_equal confirmation_admin, admin
355
+ end
356
+
357
+ test 'should return a new admin if no email or unconfirmed_email was found' do
358
+ confirmation_admin = Admin.send_confirmation_instructions(:email => "invalid@email.com")
359
+ assert_not confirmation_admin.persisted?
360
+ end
361
+
362
+ test 'should add error to new admin email if no email or unconfirmed_email was found' do
363
+ confirmation_admin = Admin.send_confirmation_instructions(:email => "invalid@email.com")
364
+ assert confirmation_admin.errors[:email]
365
+ assert_equal "not found", confirmation_admin.errors[:email].join
366
+ end
367
+
368
+ test 'should find admin with email in unconfirmed_emails' do
369
+ admin = create_admin
370
+ admin.unconfirmed_email = "new_test@email.com"
371
+ assert admin.save
372
+ admin = Admin.find_by_unconfirmed_email_with_errors(:email => "new_test@email.com")
373
+ assert admin.persisted?
374
+ end
375
+
376
+ test 'required_fields should contain the fields that Devise uses' do
377
+ assert_same_content Devise::Models::Confirmable.required_fields(User), [
378
+ :confirmation_sent_at,
379
+ :confirmation_token,
380
+ :confirmed_at
381
+ ]
382
+ end
383
+
384
+ test 'required_fields should also contain unconfirmable when reconfirmable_email is true' do
385
+ assert_same_content Devise::Models::Confirmable.required_fields(Admin), [
386
+ :confirmation_sent_at,
387
+ :confirmation_token,
388
+ :confirmed_at,
389
+ :unconfirmed_email
390
+ ]
391
+ end
392
+ end
@@ -0,0 +1,190 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+ require 'digest/sha1'
4
+
5
+ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
6
+ test 'should downcase case insensitive keys when saving' do
7
+ # case_insensitive_keys is set to :email by default.
8
+ email = 'Foo@Bar.com'
9
+ user = new_user(:email => email)
10
+
11
+ assert_equal email, user.email
12
+ user.save!
13
+ assert_equal email.downcase, user.email
14
+ end
15
+
16
+ test 'should remove whitespace from strip whitespace keys when saving' do
17
+ # strip_whitespace_keys is set to :email by default.
18
+ email = ' foo@bar.com '
19
+ user = new_user(:email => email)
20
+
21
+ assert_equal email, user.email
22
+ user.save!
23
+ assert_equal email.strip, user.email
24
+ end
25
+
26
+ test "param filter should not convert booleans and integer to strings" do
27
+ conditions = { 'login' => 'foo@bar.com', "bool1" => true, "bool2" => false, "fixnum" => 123, "will_be_converted" => (1..10) }
28
+ conditions = Devise::ParamFilter.new([], []).filter(conditions)
29
+ assert_equal( { 'login' => 'foo@bar.com', "bool1" => true, "bool2" => false, "fixnum" => 123, "will_be_converted" => "1..10" }, conditions)
30
+ end
31
+
32
+ test "param filter should not convert regular expressions to strings" do
33
+ conditions = { "regexp" => /expression/ }
34
+ conditions = Devise::ParamFilter.new([], []).filter(conditions)
35
+ assert_equal( { "regexp" => /expression/ }, conditions)
36
+ end
37
+
38
+ test 'should respond to password and password confirmation' do
39
+ user = new_user
40
+ assert user.respond_to?(:password)
41
+ assert user.respond_to?(:password_confirmation)
42
+ end
43
+
44
+ test 'should generate encrypted password while setting password' do
45
+ user = new_user
46
+ assert_present user.encrypted_password
47
+ end
48
+
49
+ test 'allow authenticatable_salt to work even with nil encrypted password' do
50
+ user = User.new
51
+ user.encrypted_password = nil
52
+ assert_nil user.authenticatable_salt
53
+ end
54
+
55
+ test 'should not generate encrypted password if password is blank' do
56
+ assert_blank new_user(:password => nil).encrypted_password
57
+ assert_blank new_user(:password => '').encrypted_password
58
+ end
59
+
60
+ test 'should encrypt password again if password has changed' do
61
+ user = create_user
62
+ encrypted_password = user.encrypted_password
63
+ user.password = user.password_confirmation = 'new_password'
64
+ user.save!
65
+ assert_not_equal encrypted_password, user.encrypted_password
66
+ end
67
+
68
+ test 'should test for a valid password' do
69
+ user = create_user
70
+ assert user.valid_password?('12345678')
71
+ assert_not user.valid_password?('654321')
72
+ end
73
+
74
+ test 'should not raise error with an empty password' do
75
+ user = create_user
76
+ user.encrypted_password = ''
77
+ assert_nothing_raised { user.valid_password?('12345678') }
78
+ end
79
+
80
+ test 'should be an invalid password if the user has an empty password' do
81
+ user = create_user
82
+ user.encrypted_password = ''
83
+ assert_not user.valid_password?('654321')
84
+ end
85
+
86
+ test 'should respond to current password' do
87
+ assert new_user.respond_to?(:current_password)
88
+ end
89
+
90
+ test 'should update password with valid current password' do
91
+ user = create_user
92
+ assert user.update_with_password(:current_password => '12345678',
93
+ :password => 'pass4321', :password_confirmation => 'pass4321')
94
+ assert user.reload.valid_password?('pass4321')
95
+ end
96
+
97
+ test 'should update password with valid current password and :as option' do
98
+ user = create_user
99
+ assert user.update_with_password(:current_password => '12345678',
100
+ :password => 'pass4321', :password_confirmation => 'pass4321', :as => :admin)
101
+ assert user.reload.valid_password?('pass4321')
102
+ end
103
+
104
+ test 'should add an error to current password when it is invalid' do
105
+ user = create_user
106
+ assert_not user.update_with_password(:current_password => 'other',
107
+ :password => 'pass4321', :password_confirmation => 'pass4321')
108
+ assert user.reload.valid_password?('12345678')
109
+ assert_match "is invalid", user.errors[:current_password].join
110
+ end
111
+
112
+ test 'should add an error to current password when it is blank' do
113
+ user = create_user
114
+ assert_not user.update_with_password(:password => 'pass4321',
115
+ :password_confirmation => 'pass4321')
116
+ assert user.reload.valid_password?('12345678')
117
+ assert_match "can't be blank", user.errors[:current_password].join
118
+ end
119
+
120
+ test 'should run validations even when current password is invalid or blank' do
121
+ user = UserWithValidation.create!(valid_attributes)
122
+ user.save
123
+ assert user.persisted?
124
+ assert_not user.update_with_password(:username => "")
125
+ assert_match "usertest", user.reload.username
126
+ assert_match "can't be blank", user.errors[:username].join
127
+ end
128
+
129
+ test 'should ignore password and its confirmation if they are blank' do
130
+ user = create_user
131
+ assert user.update_with_password(:current_password => '12345678', :email => "new@example.com")
132
+ assert_equal "new@example.com", user.email
133
+ end
134
+
135
+ test 'should not update password with invalid confirmation' do
136
+ user = create_user
137
+ assert_not user.update_with_password(:current_password => '12345678',
138
+ :password => 'pass4321', :password_confirmation => 'other')
139
+ assert user.reload.valid_password?('12345678')
140
+ end
141
+
142
+ test 'should clean up password fields on failure' do
143
+ user = create_user
144
+ assert_not user.update_with_password(:current_password => '12345678',
145
+ :password => 'pass4321', :password_confirmation => 'other')
146
+ assert user.password.blank?
147
+ assert user.password_confirmation.blank?
148
+ end
149
+
150
+ test 'should update the user without password' do
151
+ user = create_user
152
+ user.update_without_password(:email => 'new@example.com')
153
+ assert_equal 'new@example.com', user.email
154
+ end
155
+
156
+ test 'should update the user without password with :as option' do
157
+ user = create_user
158
+ user.update_without_password(:email => 'new@example.com', :as => :admin)
159
+ assert_equal 'new@example.com', user.email
160
+ end
161
+
162
+ test 'should not update password without password' do
163
+ user = create_user
164
+ user.update_without_password(:password => 'pass4321', :password_confirmation => 'pass4321')
165
+ assert !user.reload.valid_password?('pass4321')
166
+ assert user.valid_password?('12345678')
167
+ end
168
+
169
+ test 'downcase_keys with validation' do
170
+ user = User.create(:email => "HEllO@example.com", :password => "123456")
171
+ user = User.create(:email => "HEllO@example.com", :password => "123456")
172
+ assert !user.valid?
173
+ end
174
+
175
+ test 'required_fiels should be encryptable_password and the email field by default' do
176
+ assert_same_content Devise::Models::DatabaseAuthenticatable.required_fields(User), [
177
+ :email,
178
+ :encrypted_password
179
+ ]
180
+ end
181
+
182
+ test 'required_fields should be encryptable_password and the login when the login is on authentication_keys' do
183
+ swap Devise, :authentication_keys => [:login] do
184
+ assert_same_content Devise::Models::DatabaseAuthenticatable.required_fields(User), [
185
+ :encrypted_password,
186
+ :login
187
+ ]
188
+ end
189
+ end
190
+ end