devise 1.5.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. data/CHANGELOG.rdoc +111 -68
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +7 -4
  4. data/app/controllers/devise/confirmations_controller.rb +3 -6
  5. data/app/controllers/devise/omniauth_callbacks_controller.rb +1 -3
  6. data/app/controllers/devise/passwords_controller.rb +3 -6
  7. data/app/controllers/devise/registrations_controller.rb +40 -42
  8. data/app/controllers/devise/sessions_controller.rb +2 -3
  9. data/app/controllers/devise/unlocks_controller.rb +4 -7
  10. data/app/controllers/devise_controller.rb +169 -0
  11. data/app/views/devise/_links.erb +25 -0
  12. data/app/views/devise/confirmations/new.html.erb +1 -1
  13. data/app/views/devise/mailer/confirmation_instructions.html.erb +1 -1
  14. data/app/views/devise/passwords/edit.html.erb +1 -1
  15. data/app/views/devise/passwords/new.html.erb +1 -1
  16. data/app/views/devise/registrations/new.html.erb +1 -1
  17. data/app/views/devise/sessions/new.html.erb +1 -1
  18. data/app/views/devise/shared/_links.erb +3 -25
  19. data/app/views/devise/unlocks/new.html.erb +1 -1
  20. data/config/locales/en.yml +5 -6
  21. data/lib/devise/controllers/helpers.rb +8 -2
  22. data/lib/devise/controllers/scoped_views.rb +0 -16
  23. data/lib/devise/controllers/url_helpers.rb +16 -2
  24. data/lib/devise/delegator.rb +2 -2
  25. data/lib/devise/failure_app.rb +43 -8
  26. data/lib/devise/mapping.rb +1 -4
  27. data/lib/devise/models/authenticatable.rb +22 -1
  28. data/lib/devise/models/confirmable.rb +80 -22
  29. data/lib/devise/models/database_authenticatable.rb +5 -16
  30. data/lib/devise/models/lockable.rb +1 -1
  31. data/lib/devise/models/recoverable.rb +5 -5
  32. data/lib/devise/models/rememberable.rb +5 -20
  33. data/lib/devise/models/serializable.rb +5 -2
  34. data/lib/devise/models/timeoutable.rb +9 -7
  35. data/lib/devise/models/token_authenticatable.rb +1 -4
  36. data/lib/devise/models/validatable.rb +1 -1
  37. data/lib/devise/models.rb +1 -1
  38. data/lib/devise/modules.rb +2 -2
  39. data/lib/devise/omniauth/config.rb +1 -1
  40. data/lib/devise/orm/active_record.rb +6 -0
  41. data/lib/devise/param_filter.rb +1 -1
  42. data/lib/devise/path_checker.rb +7 -2
  43. data/lib/devise/rails/routes.rb +23 -14
  44. data/lib/devise/rails/warden_compat.rb +0 -83
  45. data/lib/devise/rails.rb +61 -0
  46. data/lib/devise/schema.rb +5 -0
  47. data/lib/devise/strategies/authenticatable.rb +14 -10
  48. data/lib/devise/strategies/token_authenticatable.rb +3 -3
  49. data/lib/devise/version.rb +1 -1
  50. data/lib/devise.rb +56 -33
  51. data/lib/generators/active_record/devise_generator.rb +40 -2
  52. data/lib/generators/active_record/templates/migration.rb +1 -19
  53. data/lib/generators/active_record/templates/migration_existing.rb +1 -9
  54. data/lib/generators/devise/views_generator.rb +6 -14
  55. data/lib/generators/mongoid/devise_generator.rb +43 -0
  56. data/lib/generators/templates/devise.rb +28 -14
  57. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +1 -1
  58. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +1 -1
  59. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +1 -1
  60. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +1 -1
  61. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +1 -1
  62. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +1 -1
  63. data/test/controllers/internal_helpers_test.rb +5 -4
  64. data/test/delegator_test.rb +19 -0
  65. data/test/devise_test.rb +2 -2
  66. data/test/failure_app_test.rb +24 -20
  67. data/test/generators/active_record_generator_test.rb +3 -13
  68. data/test/generators/views_generator_test.rb +1 -1
  69. data/test/integration/authenticatable_test.rb +4 -7
  70. data/test/integration/confirmable_test.rb +55 -3
  71. data/test/integration/http_authenticatable_test.rb +20 -5
  72. data/test/integration/lockable_test.rb +26 -14
  73. data/test/integration/registerable_test.rb +33 -2
  74. data/test/integration/rememberable_test.rb +0 -50
  75. data/test/integration/timeoutable_test.rb +18 -4
  76. data/test/integration/token_authenticatable_test.rb +5 -5
  77. data/test/integration/trackable_test.rb +6 -6
  78. data/test/mapping_test.rb +2 -3
  79. data/test/models/confirmable_test.rb +101 -8
  80. data/test/models/database_authenticatable_test.rb +19 -0
  81. data/test/models/encryptable_test.rb +1 -1
  82. data/test/models/lockable_test.rb +13 -0
  83. data/test/models/recoverable_test.rb +0 -27
  84. data/test/models/rememberable_test.rb +41 -160
  85. data/test/models/serializable_test.rb +1 -1
  86. data/test/models/timeoutable_test.rb +14 -0
  87. data/test/models_test.rb +7 -7
  88. data/test/path_checker_test.rb +21 -0
  89. data/test/rails_app/app/mongoid/admin.rb +22 -1
  90. data/test/rails_app/app/mongoid/user.rb +35 -0
  91. data/test/rails_app/config/initializers/devise.rb +6 -7
  92. data/test/rails_app/config/routes.rb +3 -5
  93. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +58 -12
  94. data/test/rails_app/lib/shared_admin.rb +6 -2
  95. data/test/rails_app/log/development.log +13 -0
  96. data/test/rails_app/log/test.log +319550 -0
  97. data/test/routes_test.rb +4 -0
  98. data/test/support/assertions.rb +4 -1
  99. data/test/support/helpers.rb +0 -17
  100. data/test/support/integration.rb +3 -1
  101. data/test/test_helpers_test.rb +2 -2
  102. data/test/tmp/app/views/devise/_links.erb +25 -0
  103. data/test/tmp/app/views/devise/confirmations/new.html.erb +15 -0
  104. data/test/tmp/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  105. data/test/tmp/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  106. data/test/tmp/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  107. data/test/tmp/app/views/devise/passwords/edit.html.erb +19 -0
  108. data/test/tmp/app/views/devise/passwords/new.html.erb +15 -0
  109. data/test/tmp/app/views/devise/registrations/edit.html.erb +22 -0
  110. data/test/tmp/app/views/devise/registrations/new.html.erb +17 -0
  111. data/test/tmp/app/views/devise/sessions/new.html.erb +15 -0
  112. data/test/tmp/app/views/devise/unlocks/new.html.erb +15 -0
  113. data/test/tmp/app/views/users/_links.erb +25 -0
  114. data/test/tmp/app/views/users/confirmations/new.html.erb +15 -0
  115. data/test/tmp/app/views/users/mailer/confirmation_instructions.html.erb +5 -0
  116. data/test/tmp/app/views/users/mailer/reset_password_instructions.html.erb +8 -0
  117. data/test/tmp/app/views/users/mailer/unlock_instructions.html.erb +7 -0
  118. data/test/tmp/app/views/users/passwords/edit.html.erb +19 -0
  119. data/test/tmp/app/views/users/passwords/new.html.erb +15 -0
  120. data/test/tmp/app/views/users/registrations/edit.html.erb +22 -0
  121. data/test/tmp/app/views/users/registrations/new.html.erb +17 -0
  122. data/test/tmp/app/views/users/sessions/new.html.erb +15 -0
  123. data/test/tmp/app/views/users/unlocks/new.html.erb +15 -0
  124. metadata +83 -27
  125. data/.gitignore +0 -12
  126. data/.travis.yml +0 -13
  127. data/Gemfile +0 -35
  128. data/Rakefile +0 -34
  129. data/devise.gemspec +0 -25
  130. data/lib/devise/controllers/internal_helpers.rb +0 -154
  131. data/lib/devise/controllers/shared_helpers.rb +0 -26
  132. data/test/schema_test.rb +0 -33
@@ -13,7 +13,7 @@ class RegistrationTest < ActionController::IntegrationTest
13
13
  fill_in 'password confirmation', :with => 'new_user123'
14
14
  click_button 'Sign up'
15
15
 
16
- assert_contain 'Welcome! You have signed up successfully.'
16
+ assert_contain 'You have signed up successfully'
17
17
  assert warden.authenticated?(:admin)
18
18
  assert_current_url "/admin_area/home"
19
19
 
@@ -50,7 +50,7 @@ class RegistrationTest < ActionController::IntegrationTest
50
50
  test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
51
51
  user_sign_up
52
52
 
53
- assert_contain 'You have signed up successfully. However, we could not sign you in because your account is unconfirmed.'
53
+ assert_contain 'A message with a confirmation link has been sent to your email address. Please open the link to activate your account.'
54
54
  assert_not_contain 'You have to confirm your account before continuing'
55
55
  assert_current_url "/"
56
56
 
@@ -291,3 +291,34 @@ class RegistrationTest < ActionController::IntegrationTest
291
291
  assert_equal User.count, 0
292
292
  end
293
293
  end
294
+
295
+ class ReconfirmableRegistrationTest < ActionController::IntegrationTest
296
+ test 'a signed in admin should see a more appropriate flash message when editing his account if reconfirmable is enabled' do
297
+ sign_in_as_admin
298
+ get edit_admin_registration_path
299
+
300
+ fill_in 'email', :with => 'admin.new@example.com'
301
+ fill_in 'current password', :with => '123456'
302
+ click_button 'Update'
303
+
304
+ assert_current_url '/admin_area/home'
305
+ assert_contain 'but we need to verify your new email address'
306
+
307
+ assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
308
+ end
309
+
310
+ test 'a signed in admin should not see a reconfirmation message if they did not change their password' do
311
+ sign_in_as_admin
312
+ get edit_admin_registration_path
313
+
314
+ fill_in 'password', :with => 'pas123'
315
+ fill_in 'password confirmation', :with => 'pas123'
316
+ fill_in 'current password', :with => '123456'
317
+ click_button 'Update'
318
+
319
+ assert_current_url '/admin_area/home'
320
+ assert_contain 'You updated your account successfully.'
321
+
322
+ assert Admin.first.valid_password?('pas123')
323
+ end
324
+ end
@@ -9,14 +9,6 @@ class RememberMeTest < ActionController::IntegrationTest
9
9
  user
10
10
  end
11
11
 
12
- def create_admin_and_remember
13
- admin = create_admin
14
- admin.remember_me!
15
- raw_cookie = Admin.serialize_into_cookie(admin)
16
- cookies['remember_admin_token'] = generate_signed_cookie(raw_cookie)
17
- admin
18
- end
19
-
20
12
  def generate_signed_cookie(raw_cookie)
21
13
  request = ActionDispatch::TestRequest.new
22
14
  request.cookie_jar.signed['raw_cookie'] = raw_cookie
@@ -117,34 +109,6 @@ class RememberMeTest < ActionController::IntegrationTest
117
109
  end
118
110
  end
119
111
 
120
- test 'if both extend_remember_period and remember_across_browsers are true, sends the same token with a new expire date' do
121
- swap Devise, :remember_across_browsers => true, :extend_remember_period => true, :remember_for => 1.year do
122
- admin = create_admin_and_remember
123
- token = admin.remember_token
124
-
125
- admin.remember_created_at = old = 10.minutes.ago
126
- admin.save!
127
-
128
- get root_path
129
- assert (cookie_expires("remember_admin_token") - 1.year) > (old + 5.minutes)
130
- assert_equal token, signed_cookie("remember_admin_token").last
131
- end
132
- end
133
-
134
- test 'if both extend_remember_period and remember_across_browsers are false, sends a new token with old expire date' do
135
- swap Devise, :remember_across_browsers => false, :extend_remember_period => false, :remember_for => 1.year do
136
- admin = create_admin_and_remember
137
- token = admin.remember_token
138
-
139
- admin.remember_created_at = old = 10.minutes.ago
140
- admin.save!
141
-
142
- get root_path
143
- assert (cookie_expires("remember_admin_token") - 1.year) < (old + 5.minutes)
144
- assert_not_equal token, signed_cookie("remember_admin_token").last
145
- end
146
- end
147
-
148
112
  test 'do not remember other scopes' do
149
113
  user = create_user_and_remember
150
114
  get root_path
@@ -182,20 +146,6 @@ class RememberMeTest < ActionController::IntegrationTest
182
146
  assert_not warden.authenticated?(:user)
183
147
  end
184
148
 
185
- test 'do not remember the admin anymore after forget' do
186
- admin = create_admin_and_remember
187
- get root_path
188
- assert warden.authenticated?(:admin)
189
-
190
- get destroy_admin_session_path
191
- assert_not warden.authenticated?(:admin)
192
- assert_nil admin.reload.remember_token
193
- assert_nil warden.cookies['remember_admin_token']
194
-
195
- get root_path
196
- assert_not warden.authenticated?(:admin)
197
- end
198
-
199
149
  test 'changing user password expires remember me token' do
200
150
  user = create_user_and_remember
201
151
  user.password = "another_password"
@@ -41,7 +41,7 @@ class SessionTimeoutTest < ActionController::IntegrationTest
41
41
  assert_not_nil last_request_at
42
42
 
43
43
  get users_path
44
- assert_redirected_to new_user_session_path
44
+ assert_redirected_to users_path
45
45
  assert_not warden.authenticated?(:user)
46
46
  end
47
47
 
@@ -68,12 +68,25 @@ class SessionTimeoutTest < ActionController::IntegrationTest
68
68
 
69
69
  get expire_user_path(user)
70
70
  get users_path
71
- assert_redirected_to new_user_session_path
71
+ assert_redirected_to users_path
72
72
  assert_not warden.authenticated?(:user)
73
73
  end
74
74
  end
75
75
 
76
76
  test 'error message with i18n' do
77
+ store_translations :en, :devise => {
78
+ :failure => { :user => { :timeout => 'Session expired!' } }
79
+ } do
80
+ user = sign_in_as_user
81
+
82
+ get expire_user_path(user)
83
+ get root_path
84
+ follow_redirect!
85
+ assert_contain 'Session expired!'
86
+ end
87
+ end
88
+
89
+ test 'error message with i18n with double redirect' do
77
90
  store_translations :en, :devise => {
78
91
  :failure => { :user => { :timeout => 'Session expired!' } }
79
92
  } do
@@ -82,15 +95,16 @@ class SessionTimeoutTest < ActionController::IntegrationTest
82
95
  get expire_user_path(user)
83
96
  get users_path
84
97
  follow_redirect!
98
+ follow_redirect!
85
99
  assert_contain 'Session expired!'
86
100
  end
87
101
  end
88
-
102
+
89
103
  test 'time out not triggered if remembered' do
90
104
  user = sign_in_as_user :remember_me => true
91
105
  get expire_user_path(user)
92
106
  assert_not_nil last_request_at
93
-
107
+
94
108
  get users_path
95
109
  assert_response :success
96
110
  assert warden.authenticated?(:user)
@@ -25,7 +25,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
25
25
  end
26
26
 
27
27
  test 'authenticate with valid authentication token key but does not store if stateless' do
28
- swap Devise, :token_authentication_key => :secret_token, :stateless_token => true do
28
+ swap Devise, :token_authentication_key => :secret_token, :skip_session_storage => [:token_auth] do
29
29
  sign_in_as_new_user_with_token
30
30
  assert warden.authenticated?(:user)
31
31
 
@@ -88,7 +88,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
88
88
  end
89
89
 
90
90
  test 'authenticate with valid authentication token key and do not store if stateless and timeoutable are enabled' do
91
- swap Devise, :token_authentication_key => :secret_token, :stateless_token => true, :timeout_in => (0.1).second do
91
+ swap Devise, :token_authentication_key => :secret_token, :skip_session_storage => [:token_auth], :timeout_in => (0.1).second do
92
92
  user = sign_in_as_new_user_with_token
93
93
  assert warden.authenticated?(:user)
94
94
 
@@ -112,7 +112,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
112
112
 
113
113
  assert_not_equal user1, user2
114
114
  visit users_path(Devise.token_authentication_key.to_s + '[$ne]' => user1.authentication_token)
115
- assert_nil warden.user(:user)
115
+ assert_nil warden.user(:user)
116
116
  end
117
117
  end
118
118
 
@@ -125,7 +125,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
125
125
  options[:auth_token] ||= user.authentication_token
126
126
 
127
127
  if options[:http_auth]
128
- header = "Basic #{ActiveSupport::Base64.encode64("#{VALID_AUTHENTICATION_TOKEN}:X")}"
128
+ header = "Basic #{Base64.encode64("#{VALID_AUTHENTICATION_TOKEN}:X")}"
129
129
  get users_path(:format => :xml), {}, "HTTP_AUTHORIZATION" => header
130
130
  else
131
131
  visit users_path(options[:auth_token_key].to_sym => options[:auth_token])
@@ -145,4 +145,4 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
145
145
  sign_in_as_new_user_with_token(:user => user)
146
146
  end
147
147
 
148
- end
148
+ end
@@ -36,11 +36,11 @@ class TrackableHooksTest < ActionController::IntegrationTest
36
36
  assert_equal "127.0.0.1", user.current_sign_in_ip
37
37
  assert_equal "127.0.0.1", user.last_sign_in_ip
38
38
  end
39
-
39
+
40
40
  test "current remote ip returns original ip behind a non transparent proxy" do
41
41
  user = create_user
42
-
43
- arbitrary_ip = '192.168.1.69'
42
+
43
+ arbitrary_ip = '200.121.1.69'
44
44
  sign_in_as_user do
45
45
  header 'HTTP_X_FORWARDED_FOR', arbitrary_ip
46
46
  end
@@ -63,7 +63,7 @@ class TrackableHooksTest < ActionController::IntegrationTest
63
63
  end
64
64
 
65
65
  test "does not update anything if user has signed out along the way" do
66
- swap Devise, :confirm_within => 0 do
66
+ swap Devise, :allow_unconfirmed_access_for => 0 do
67
67
  user = create_user(:confirm => false)
68
68
  sign_in_as_user
69
69
 
@@ -72,7 +72,7 @@ class TrackableHooksTest < ActionController::IntegrationTest
72
72
  assert_nil user.last_sign_in_at
73
73
  end
74
74
  end
75
-
75
+
76
76
  test "do not track if devise.skip_trackable is set" do
77
77
  user = create_user
78
78
  sign_in_as_user do
@@ -81,7 +81,7 @@ class TrackableHooksTest < ActionController::IntegrationTest
81
81
  user.reload
82
82
  assert_equal 0, user.sign_in_count
83
83
  visit destroy_user_session_path
84
-
84
+
85
85
  sign_in_as_user do
86
86
  header 'devise.skip_trackable', false
87
87
  end
data/test/mapping_test.rb CHANGED
@@ -51,12 +51,12 @@ class MappingTest < ActiveSupport::TestCase
51
51
 
52
52
  test 'has strategies depending on the model declaration' do
53
53
  assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
54
- assert_equal [:rememberable, :database_authenticatable], Devise.mappings[:admin].strategies
54
+ assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies
55
55
  end
56
56
 
57
57
  test 'has no input strategies depending on the model declaration' do
58
58
  assert_equal [:rememberable, :token_authenticatable], Devise.mappings[:user].no_input_strategies
59
- assert_equal [:rememberable], Devise.mappings[:admin].no_input_strategies
59
+ assert_equal [], Devise.mappings[:admin].no_input_strategies
60
60
  end
61
61
 
62
62
  test 'find scope for a given object' do
@@ -108,7 +108,6 @@ class MappingTest < ActiveSupport::TestCase
108
108
  assert mapping.authenticatable?
109
109
  assert mapping.recoverable?
110
110
  assert mapping.lockable?
111
- assert_not mapping.confirmable?
112
111
  assert_not mapping.omniauthable?
113
112
  end
114
113
 
@@ -80,8 +80,8 @@ class ConfirmableTest < ActiveSupport::TestCase
80
80
  end
81
81
 
82
82
  test 'should send confirmation instructions by email' do
83
- assert_email_sent do
84
- create_user
83
+ assert_email_sent "mynewuser@example.com" do
84
+ create_user :email => "mynewuser@example.com"
85
85
  end
86
86
  end
87
87
 
@@ -123,7 +123,7 @@ class ConfirmableTest < ActiveSupport::TestCase
123
123
 
124
124
  test 'should send email instructions for the user confirm its email' do
125
125
  user = create_user
126
- assert_email_sent do
126
+ assert_email_sent user.email do
127
127
  User.send_confirmation_instructions(:email => user.email)
128
128
  end
129
129
  end
@@ -164,19 +164,19 @@ class ConfirmableTest < ActiveSupport::TestCase
164
164
  end
165
165
 
166
166
  test 'confirm time should fallback to devise confirm in default configuration' do
167
- swap Devise, :confirm_within => 1.day do
167
+ swap Devise, :allow_unconfirmed_access_for => 1.day do
168
168
  user = new_user
169
169
  user.confirmation_sent_at = 2.days.ago
170
170
  assert_not user.active_for_authentication?
171
171
 
172
- Devise.confirm_within = 3.days
172
+ Devise.allow_unconfirmed_access_for = 3.days
173
173
  assert user.active_for_authentication?
174
174
  end
175
175
  end
176
176
 
177
177
  test 'should be active when confirmation sent at is not overpast' do
178
- swap Devise, :confirm_within => 5.days do
179
- Devise.confirm_within = 5.days
178
+ swap Devise, :allow_unconfirmed_access_for => 5.days do
179
+ Devise.allow_unconfirmed_access_for = 5.days
180
180
  user = create_user
181
181
 
182
182
  user.confirmation_sent_at = 4.days.ago
@@ -198,7 +198,7 @@ class ConfirmableTest < ActiveSupport::TestCase
198
198
  end
199
199
 
200
200
  test 'should not be active when confirm in is zero' do
201
- Devise.confirm_within = 0.days
201
+ Devise.allow_unconfirmed_access_for = 0.days
202
202
  user = create_user
203
203
  user.confirmation_sent_at = Date.today
204
204
  assert_not user.active_for_authentication?
@@ -236,3 +236,96 @@ class ConfirmableTest < ActiveSupport::TestCase
236
236
  end
237
237
  end
238
238
  end
239
+
240
+ class ReconfirmableTest < ActiveSupport::TestCase
241
+ test 'should not worry about validations on confirm even with reconfirmable' do
242
+ admin = create_admin
243
+ admin.reset_password_token = "a"
244
+ assert admin.confirm!
245
+ end
246
+
247
+ test 'should generate confirmation token after changing email' do
248
+ admin = create_admin
249
+ assert admin.confirm!
250
+ assert_nil admin.confirmation_token
251
+ assert admin.update_attributes(:email => 'new_test@example.com')
252
+ assert_not_nil admin.confirmation_token
253
+ end
254
+
255
+ test 'should regenerate confirmation token after changing email' do
256
+ admin = create_admin
257
+ assert admin.confirm!
258
+ assert admin.update_attributes(:email => 'old_test@example.com')
259
+ token = admin.confirmation_token
260
+ assert admin.update_attributes(:email => 'new_test@example.com')
261
+ assert_not_equal token, admin.confirmation_token
262
+ end
263
+
264
+ test 'should send confirmation instructions by email after changing email' do
265
+ admin = create_admin
266
+ assert admin.confirm!
267
+ assert_email_sent "new_test@example.com" do
268
+ assert admin.update_attributes(:email => 'new_test@example.com')
269
+ end
270
+ end
271
+
272
+ test 'should not send confirmation by email after changing password' do
273
+ admin = create_admin
274
+ assert admin.confirm!
275
+ assert_email_not_sent do
276
+ assert admin.update_attributes(:password => 'newpass', :password_confirmation => 'newpass')
277
+ end
278
+ end
279
+
280
+ test 'should stay confirmed when email is changed' do
281
+ admin = create_admin
282
+ assert admin.confirm!
283
+ assert admin.update_attributes(:email => 'new_test@example.com')
284
+ assert admin.confirmed?
285
+ end
286
+
287
+ test 'should update email only when it is confirmed' do
288
+ admin = create_admin
289
+ assert admin.confirm!
290
+ assert admin.update_attributes(:email => 'new_test@example.com')
291
+ assert_not_equal 'new_test@example.com', admin.email
292
+ assert admin.confirm!
293
+ assert_equal 'new_test@example.com', admin.email
294
+ end
295
+
296
+ test 'should not allow admin to get past confirmation email by resubmitting their new address' do
297
+ admin = create_admin
298
+ assert admin.confirm!
299
+ assert admin.update_attributes(:email => 'new_test@example.com')
300
+ assert_not_equal 'new_test@example.com', admin.email
301
+ assert admin.update_attributes(:email => 'new_test@example.com')
302
+ assert_not_equal 'new_test@example.com', admin.email
303
+ end
304
+
305
+ test 'should find a admin by send confirmation instructions with unconfirmed_email' do
306
+ admin = create_admin
307
+ assert admin.confirm!
308
+ assert admin.update_attributes(:email => 'new_test@example.com')
309
+ confirmation_admin = Admin.send_confirmation_instructions(:email => admin.unconfirmed_email)
310
+ assert_equal confirmation_admin, admin
311
+ end
312
+
313
+ test 'should return a new admin if no email or unconfirmed_email was found' do
314
+ confirmation_admin = Admin.send_confirmation_instructions(:email => "invalid@email.com")
315
+ assert_not confirmation_admin.persisted?
316
+ end
317
+
318
+ test 'should add error to new admin email if no email or unconfirmed_email was found' do
319
+ confirmation_admin = Admin.send_confirmation_instructions(:email => "invalid@email.com")
320
+ assert confirmation_admin.errors[:email]
321
+ assert_equal "not found", confirmation_admin.errors[:email].join
322
+ end
323
+
324
+ test 'should find admin with email in unconfirmed_emails' do
325
+ admin = create_admin
326
+ admin.unconfirmed_email = "new_test@email.com"
327
+ assert admin.save
328
+ admin = Admin.find_by_unconfirmed_email_with_errors(:email => "new_test@email.com")
329
+ assert admin.persisted?
330
+ end
331
+ end
@@ -28,6 +28,12 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
28
28
  assert_equal( { 'login' => 'foo@bar.com', "bool1" => true, "bool2" => false, "fixnum" => 123, "will_be_converted" => "1..10" }, conditions)
29
29
  end
30
30
 
31
+ test "param filter should not convert regular expressions to strings" do
32
+ conditions = { "regexp" => /expression/ }
33
+ conditions = Devise::ParamFilter.new([], []).filter(conditions)
34
+ assert_equal( { "regexp" => /expression/ }, conditions)
35
+ end
36
+
31
37
  test 'should respond to password and password confirmation' do
32
38
  user = new_user
33
39
  assert user.respond_to?(:password)
@@ -87,6 +93,13 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
87
93
  assert user.reload.valid_password?('pass321')
88
94
  end
89
95
 
96
+ test 'should update password with valid current password and :as option' do
97
+ user = create_user
98
+ assert user.update_with_password(:current_password => '123456',
99
+ :password => 'pass321', :password_confirmation => 'pass321', :as => :admin)
100
+ assert user.reload.valid_password?('pass321')
101
+ end
102
+
90
103
  test 'should add an error to current password when it is invalid' do
91
104
  user = create_user
92
105
  assert_not user.update_with_password(:current_password => 'other',
@@ -138,6 +151,12 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
138
151
  user.update_without_password(:email => 'new@example.com')
139
152
  assert_equal 'new@example.com', user.email
140
153
  end
154
+
155
+ test 'should update the user without password with :as option' do
156
+ user = create_user
157
+ user.update_without_password(:email => 'new@example.com', :as => :admin)
158
+ assert_equal 'new@example.com', user.email
159
+ end
141
160
 
142
161
  test 'should not update password without password' do
143
162
  user = create_user
@@ -31,7 +31,7 @@ class EncryptableTest < ActiveSupport::TestCase
31
31
 
32
32
  test 'should generate a base64 hash using SecureRandom for password salt' do
33
33
  swap_with_encryptor Admin, :sha1 do
34
- SecureRandom.expects(:base64).with(15).returns('01lI')
34
+ SecureRandom.expects(:base64).with(15).returns('01lI').twice
35
35
  salt = create_admin.password_salt
36
36
  assert_not_equal '01lI', salt
37
37
  assert_equal 4, salt.size
@@ -23,6 +23,19 @@ class LockableTest < ActiveSupport::TestCase
23
23
  assert_equal 0, user.reload.failed_attempts
24
24
  end
25
25
 
26
+ test "should increment failed_attempts on successfull validation if the user is already locked" do
27
+ user = create_user
28
+ user.confirm!
29
+
30
+ swap Devise, :maximum_attempts => 2 do
31
+ 3.times { user.valid_for_authentication?{ false } }
32
+ assert user.reload.access_locked?
33
+ end
34
+
35
+ user.valid_for_authentication?{ true }
36
+ assert_equal 4, user.reload.failed_attempts
37
+ end
38
+
26
39
  test "should not touch failed_attempts if lock_strategy is none" do
27
40
  user = create_user
28
41
  user.confirm!
@@ -195,31 +195,4 @@ class RecoverableTest < ActiveSupport::TestCase
195
195
  assert_equal "has expired, please request a new one", reset_password_user.errors[:reset_password_token].join
196
196
  end
197
197
  end
198
-
199
- test 'should save the model when the reset_password_sent_at doesnt exist' do
200
- user = create_user
201
- def user.respond_to?(meth, *)
202
- if meth == :reset_password_sent_at=
203
- false
204
- else
205
- super
206
- end
207
- end
208
- user.send_reset_password_instructions
209
- user.reload
210
- assert_not_nil user.reset_password_token
211
- end
212
-
213
- test 'should have valid period if does not respond to reset_password_sent_at' do
214
- user = create_user
215
- def user.respond_to?(meth, *)
216
- if meth == :reset_password_sent_at
217
- false
218
- else
219
- super
220
- end
221
- end
222
- assert user.reset_password_period_valid?
223
- end
224
-
225
198
  end