devise 1.1.9 → 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (121) hide show
  1. data/CHANGELOG.rdoc +34 -26
  2. data/README.rdoc +134 -100
  3. data/app/controllers/devise/confirmations_controller.rb +1 -1
  4. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  5. data/app/controllers/devise/passwords_controller.rb +1 -1
  6. data/app/controllers/devise/registrations_controller.rb +59 -6
  7. data/app/controllers/devise/sessions_controller.rb +3 -2
  8. data/app/controllers/devise/unlocks_controller.rb +1 -1
  9. data/app/helpers/devise_helper.rb +4 -2
  10. data/app/mailers/devise/mailer.rb +27 -10
  11. data/app/views/devise/confirmations/new.html.erb +1 -1
  12. data/app/views/devise/passwords/edit.html.erb +2 -2
  13. data/app/views/devise/passwords/new.html.erb +1 -1
  14. data/app/views/devise/registrations/edit.html.erb +1 -1
  15. data/app/views/devise/registrations/new.html.erb +1 -1
  16. data/app/views/devise/sessions/new.html.erb +1 -1
  17. data/app/views/devise/shared/_links.erb +6 -0
  18. data/app/views/devise/unlocks/new.html.erb +1 -1
  19. data/config/locales/en.yml +9 -2
  20. data/lib/devise.rb +116 -58
  21. data/lib/devise/controllers/helpers.rb +103 -107
  22. data/lib/devise/controllers/internal_helpers.rb +23 -7
  23. data/lib/devise/controllers/scoped_views.rb +4 -6
  24. data/lib/devise/controllers/url_helpers.rb +3 -5
  25. data/lib/devise/encryptors/base.rb +1 -1
  26. data/lib/devise/encryptors/restful_authentication_sha1.rb +4 -4
  27. data/lib/devise/failure_app.rb +29 -21
  28. data/lib/devise/hooks/forgetable.rb +2 -1
  29. data/lib/devise/hooks/rememberable.rb +11 -9
  30. data/lib/devise/mapping.rb +12 -5
  31. data/lib/devise/models.rb +0 -14
  32. data/lib/devise/models/authenticatable.rb +40 -30
  33. data/lib/devise/models/confirmable.rb +11 -15
  34. data/lib/devise/models/database_authenticatable.rb +23 -35
  35. data/lib/devise/models/encryptable.rb +65 -0
  36. data/lib/devise/models/lockable.rb +8 -7
  37. data/lib/devise/models/omniauthable.rb +23 -0
  38. data/lib/devise/models/recoverable.rb +5 -3
  39. data/lib/devise/models/registerable.rb +13 -0
  40. data/lib/devise/models/rememberable.rb +38 -30
  41. data/lib/devise/models/timeoutable.rb +20 -3
  42. data/lib/devise/models/token_authenticatable.rb +19 -7
  43. data/lib/devise/models/validatable.rb +16 -4
  44. data/lib/devise/modules.rb +15 -8
  45. data/lib/devise/omniauth.rb +47 -0
  46. data/lib/devise/omniauth/config.rb +30 -0
  47. data/lib/devise/omniauth/test_helpers.rb +57 -0
  48. data/lib/devise/omniauth/url_helpers.rb +29 -0
  49. data/lib/devise/orm/active_record.rb +2 -0
  50. data/lib/devise/orm/mongoid.rb +4 -2
  51. data/lib/devise/rails.rb +26 -46
  52. data/lib/devise/rails/routes.rb +64 -20
  53. data/lib/devise/rails/warden_compat.rb +18 -20
  54. data/lib/devise/schema.rb +13 -14
  55. data/lib/devise/strategies/authenticatable.rb +33 -7
  56. data/lib/devise/strategies/database_authenticatable.rb +1 -1
  57. data/lib/devise/strategies/rememberable.rb +1 -1
  58. data/lib/devise/strategies/token_authenticatable.rb +6 -2
  59. data/lib/devise/test_helpers.rb +11 -1
  60. data/lib/devise/version.rb +1 -1
  61. data/lib/generators/active_record/templates/migration.rb +1 -0
  62. data/lib/generators/devise/orm_helpers.rb +3 -2
  63. data/lib/generators/templates/devise.rb +70 -39
  64. data/test/controllers/helpers_test.rb +43 -67
  65. data/test/controllers/internal_helpers_test.rb +29 -8
  66. data/test/controllers/url_helpers_test.rb +2 -1
  67. data/test/failure_app_test.rb +56 -21
  68. data/test/generators/generators_test_helper.rb +4 -0
  69. data/test/generators/install_generator_test.rb +14 -0
  70. data/test/generators/views_generator_test.rb +37 -0
  71. data/test/integration/authenticatable_test.rb +147 -62
  72. data/test/integration/database_authenticatable_test.rb +22 -0
  73. data/test/integration/http_authenticatable_test.rb +12 -2
  74. data/test/integration/omniauthable_test.rb +107 -0
  75. data/test/integration/recoverable_test.rb +39 -20
  76. data/test/integration/registerable_test.rb +30 -4
  77. data/test/integration/rememberable_test.rb +57 -34
  78. data/test/integration/timeoutable_test.rb +10 -1
  79. data/test/integration/token_authenticatable_test.rb +12 -17
  80. data/test/mailers/confirmation_instructions_test.rb +4 -0
  81. data/test/mailers/reset_password_instructions_test.rb +4 -0
  82. data/test/mailers/unlock_instructions_test.rb +4 -0
  83. data/test/mapping_test.rb +37 -3
  84. data/test/models/confirmable_test.rb +3 -3
  85. data/test/models/database_authenticatable_test.rb +14 -71
  86. data/test/models/encryptable_test.rb +65 -0
  87. data/test/models/lockable_test.rb +17 -1
  88. data/test/models/recoverable_test.rb +17 -0
  89. data/test/models/rememberable_test.rb +186 -125
  90. data/test/models/token_authenticatable_test.rb +1 -13
  91. data/test/models_test.rb +5 -5
  92. data/test/omniauth/url_helpers_test.rb +47 -0
  93. data/test/rails_app/app/active_record/admin.rb +4 -1
  94. data/test/rails_app/app/active_record/user.rb +5 -4
  95. data/test/rails_app/app/controllers/{sessions_controller.rb → admins/sessions_controller.rb} +1 -1
  96. data/test/rails_app/app/controllers/home_controller.rb +9 -0
  97. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
  98. data/test/rails_app/app/mongoid/admin.rb +4 -1
  99. data/test/rails_app/app/mongoid/shim.rb +16 -3
  100. data/test/rails_app/app/mongoid/user.rb +5 -5
  101. data/test/rails_app/config/initializers/devise.rb +52 -28
  102. data/test/rails_app/config/routes.rb +14 -6
  103. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +21 -17
  104. data/test/rails_app/db/schema.rb +17 -51
  105. data/test/rails_app/lib/shared_admin.rb +9 -0
  106. data/test/rails_app/lib/shared_user.rb +23 -0
  107. data/test/routes_test.rb +42 -9
  108. data/test/support/integration.rb +3 -3
  109. data/test/support/webrat/integrations/rails.rb +7 -0
  110. data/test/test_helper.rb +2 -0
  111. data/test/test_helpers_test.rb +29 -0
  112. metadata +60 -30
  113. data/Gemfile +0 -27
  114. data/Gemfile.lock +0 -115
  115. data/Rakefile +0 -55
  116. data/TODO +0 -3
  117. data/lib/devise/encryptors/bcrypt.rb +0 -19
  118. data/lib/generators/devise_install_generator.rb +0 -4
  119. data/lib/generators/devise_views_generator.rb +0 -4
  120. data/test/indifferent_hash.rb +0 -33
  121. data/test/support/test_silencer.rb +0 -5
@@ -76,5 +76,14 @@ class SessionTimeoutTest < ActionController::IntegrationTest
76
76
  assert_contain 'Session expired!'
77
77
  end
78
78
  end
79
-
79
+
80
+ test 'time out not triggered if remembered' do
81
+ user = sign_in_as_user :remember_me => true
82
+ get expire_user_path(user)
83
+ assert_not_nil last_request_at
84
+
85
+ get users_path
86
+ assert_response :success
87
+ assert warden.authenticated?(:user)
88
+ end
80
89
  end
@@ -7,12 +7,23 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
7
7
  sign_in_as_new_user_with_token
8
8
 
9
9
  assert_response :success
10
- assert_template 'users/index'
10
+ assert_current_url "/users?secret_token=#{VALID_AUTHENTICATION_TOKEN}"
11
11
  assert_contain 'Welcome'
12
12
  assert warden.authenticated?(:user)
13
13
  end
14
14
  end
15
15
 
16
+ test 'authenticate with valid authentication token key but does not store if stateless' do
17
+ swap Devise, :token_authentication_key => :secret_token, :stateless_token => true do
18
+ sign_in_as_new_user_with_token
19
+ assert warden.authenticated?(:user)
20
+
21
+ get users_path
22
+ assert_redirected_to new_user_session_path
23
+ assert_not warden.authenticated?(:user)
24
+ end
25
+ end
26
+
16
27
  test 'authenticate with valid authentication token key and value through http' do
17
28
  swap Devise, :token_authentication_key => :secret_token do
18
29
  sign_in_as_new_user_with_token(:http_auth => true)
@@ -65,22 +76,6 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
65
76
  end
66
77
  end
67
78
 
68
- test 'should not be subject to injection' do
69
- swap Devise, :token_authentication_key => :secret_token do
70
- user1 = create_user()
71
-
72
- # Clean up user cache
73
- @user = nil
74
-
75
- user2 = create_user(:email => "another@test.com")
76
- user2.update_attribute(:authentication_token, "ANOTHERTOKEN")
77
-
78
- assert_not_equal user1, user2
79
- visit users_path(Devise.token_authentication_key.to_s + '[$ne]' => user1.authentication_token)
80
- assert_nil warden.user(:user)
81
- end
82
- end
83
-
84
79
  private
85
80
 
86
81
  def sign_in_as_new_user_with_token(options = {})
@@ -35,6 +35,10 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
35
35
  assert_equal ['test@example.com'], mail.from
36
36
  end
37
37
 
38
+ test 'setup reply to as copy from sender' do
39
+ assert_equal ['test@example.com'], mail.reply_to
40
+ end
41
+
38
42
  test 'setup subject from I18n' do
39
43
  store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :subject => 'Account Confirmation' } } } do
40
44
  assert_equal 'Account Confirmation', mail.subject
@@ -38,6 +38,10 @@ class ResetPasswordInstructionsTest < ActionMailer::TestCase
38
38
  assert_equal ['test@example.com'], mail.from
39
39
  end
40
40
 
41
+ test 'setup reply to as copy from sender' do
42
+ assert_equal ['test@example.com'], mail.reply_to
43
+ end
44
+
41
45
  test 'setup subject from I18n' do
42
46
  store_translations :en, :devise => { :mailer => { :reset_password_instructions => { :subject => 'Reset instructions' } } } do
43
47
  assert_equal 'Reset instructions', mail.subject
@@ -38,6 +38,10 @@ class UnlockInstructionsTest < ActionMailer::TestCase
38
38
  assert_equal ['test@example.com'], mail.from
39
39
  end
40
40
 
41
+ test 'setup reply to as copy from sender' do
42
+ assert_equal ['test@example.com'], mail.reply_to
43
+ end
44
+
41
45
  test 'setup subject from I18n' do
42
46
  store_translations :en, :devise => { :mailer => { :unlock_instructions => { :subject => 'Yo unlock instructions' } } } do
43
47
  assert_equal 'Yo unlock instructions', mail.subject
@@ -12,22 +12,42 @@ class MappingTest < ActiveSupport::TestCase
12
12
  mapping = Devise.mappings[:user]
13
13
  assert_equal User, mapping.to
14
14
  assert_equal User.devise_modules, mapping.modules
15
- assert_equal :users, mapping.plural
15
+ assert_equal "users", mapping.scoped_path
16
16
  assert_equal :user, mapping.singular
17
17
  assert_equal "users", mapping.path
18
+ assert_equal "/users", mapping.fullpath
19
+ end
20
+
21
+ test 'store options with namespace' do
22
+ mapping = Devise.mappings[:publisher_account]
23
+ assert_equal Admin, mapping.to
24
+ assert_equal "publisher/accounts", mapping.scoped_path
25
+ assert_equal :publisher_account, mapping.singular
26
+ assert_equal "accounts", mapping.path
27
+ assert_equal "/publisher/accounts", mapping.fullpath
18
28
  end
19
29
 
20
30
  test 'allows path to be given' do
21
31
  assert_equal "admin_area", Devise.mappings[:admin].path
22
32
  end
23
33
 
34
+ test 'sign_out_via defaults to :get' do
35
+ assert_equal :get, Devise.mappings[:user].sign_out_via
36
+ end
37
+
38
+ test 'allows custom sign_out_via to be given' do
39
+ assert_equal :delete, Devise.mappings[:sign_out_via_delete].sign_out_via
40
+ assert_equal :post, Devise.mappings[:sign_out_via_post].sign_out_via
41
+ assert_equal [:delete, :post], Devise.mappings[:sign_out_via_delete_or_post].sign_out_via
42
+ end
43
+
24
44
  test 'allows custom singular to be given' do
25
45
  assert_equal "accounts", Devise.mappings[:manager].path
26
46
  end
27
47
 
28
48
  test 'has strategies depending on the model declaration' do
29
49
  assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
30
- assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies
50
+ assert_equal [:rememberable, :database_authenticatable], Devise.mappings[:admin].strategies
31
51
  end
32
52
 
33
53
  test 'find scope for a given object' do
@@ -80,6 +100,20 @@ class MappingTest < ActiveSupport::TestCase
80
100
  assert mapping.recoverable?
81
101
  assert mapping.lockable?
82
102
  assert_not mapping.confirmable?
83
- assert_not mapping.rememberable?
103
+ assert_not mapping.omniauthable?
84
104
  end
105
+
106
+ test 'find mapping by path' do
107
+ assert_raise RuntimeError do
108
+ Devise::Mapping.find_by_path!('/accounts/facebook/callback')
109
+ end
110
+
111
+ assert_nothing_raised do
112
+ Devise::Mapping.find_by_path!('/:locale/accounts/login')
113
+ end
114
+
115
+ assert_nothing_raised do
116
+ Devise::Mapping.find_by_path!('/accounts/facebook/callback', :path)
117
+ end
118
+ end
85
119
  end
@@ -48,7 +48,7 @@ class ConfirmableTest < ActiveSupport::TestCase
48
48
  assert_blank user.errors[:email]
49
49
 
50
50
  assert_not user.confirm!
51
- assert_equal "was already confirmed", user.errors[:email].join
51
+ assert_equal "was already confirmed, please try signing in", user.errors[:email].join
52
52
  end
53
53
 
54
54
  test 'should find and confirm an user automatically' do
@@ -76,7 +76,7 @@ class ConfirmableTest < ActiveSupport::TestCase
76
76
  user.save
77
77
  confirmed_user = User.confirm_by_token(user.confirmation_token)
78
78
  assert confirmed_user.confirmed?
79
- assert_equal "was already confirmed", confirmed_user.errors[:email].join
79
+ assert_equal "was already confirmed, please try signing in", confirmed_user.errors[:email].join
80
80
  end
81
81
 
82
82
  test 'should send confirmation instructions by email' do
@@ -160,7 +160,7 @@ class ConfirmableTest < ActiveSupport::TestCase
160
160
  user.confirm!
161
161
  assert_not user.resend_confirmation_token
162
162
  assert user.confirmed?
163
- assert_equal 'was already confirmed', user.errors[:email].join
163
+ assert_equal 'was already confirmed, please try signing in', user.errors[:email].join
164
164
  end
165
165
 
166
166
  test 'confirm time should fallback to devise confirm in default configuration' do
@@ -2,53 +2,31 @@ require 'test_helper'
2
2
  require 'digest/sha1'
3
3
 
4
4
  class DatabaseAuthenticatableTest < ActiveSupport::TestCase
5
-
6
- def encrypt_password(user, pepper=User.pepper, stretches=User.stretches, encryptor=User.encryptor_class)
7
- encryptor.digest('123456', stretches, user.password_salt, pepper)
8
- end
9
-
10
- def swap_with_encryptor(klass, encryptor, options={})
11
- klass.instance_variable_set(:@encryptor_class, nil)
12
-
13
- swap klass, options.merge(:encryptor => encryptor) do
14
- begin
15
- yield
16
- ensure
17
- klass.instance_variable_set(:@encryptor_class, nil)
18
- end
19
- end
5
+ test 'should downcase case insensitive keys when saving' do
6
+ # case_insensitive_keys is set to :email by default.
7
+ email = 'Foo@Bar.com'
8
+ user = new_user(:email => email)
9
+
10
+ assert_equal email, user.email
11
+ user.save!
12
+ assert_equal email.downcase, user.email
20
13
  end
21
-
14
+
22
15
  test 'should respond to password and password confirmation' do
23
16
  user = new_user
24
17
  assert user.respond_to?(:password)
25
18
  assert user.respond_to?(:password_confirmation)
26
19
  end
27
20
 
28
- test 'should generate encrypted password and salt while setting password' do
21
+ test 'should generate encrypted password while setting password' do
29
22
  user = new_user
30
- assert_present user.password_salt
31
23
  assert_present user.encrypted_password
32
24
  end
33
25
 
34
- test 'should not change password salt when updating' do
35
- user = create_user
36
- salt = user.password_salt
37
- user.expects(:password_salt=).never
38
- user.save!
39
- assert_equal salt, user.password_salt
40
- end
41
-
42
- test 'should generate a base64 hash using SecureRandom for password salt' do
43
- swap_with_encryptor User, :sha1 do
44
- ActiveSupport::SecureRandom.expects(:base64).with(15).returns('friendly_token')
45
- assert_equal 'friendly_token', new_user.password_salt
46
- end
47
- end
48
-
49
- test 'should not generate salt if password is blank' do
50
- assert_blank new_user(:password => nil).password_salt
51
- assert_blank new_user(:password => '').password_salt
26
+ test 'allow authenticatable_salt to work even with nil encrypted password' do
27
+ user = User.new
28
+ user.encrypted_password = nil
29
+ assert_nil user.authenticatable_salt
52
30
  end
53
31
 
54
32
  test 'should not generate encrypted password if password is blank' do
@@ -64,47 +42,12 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
64
42
  assert_not_equal encrypted_password, user.encrypted_password
65
43
  end
66
44
 
67
- test 'should fallback to sha1 as default encryption' do
68
- user = new_user
69
- assert_equal encrypt_password(user), user.encrypted_password
70
- end
71
-
72
- test 'should fallback to devise pepper default configuration' do
73
- begin
74
- Devise.pepper = ''
75
- user = new_user
76
- assert_equal encrypt_password(user), user.encrypted_password
77
- assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
78
-
79
- Devise.pepper = 'new_pepper'
80
- user = new_user
81
- assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
82
- assert_not_equal encrypt_password(user, 'another_pepper'), user.encrypted_password
83
- ensure
84
- Devise.pepper = nil
85
- end
86
- end
87
-
88
- test 'should respect encryptor configuration' do
89
- swap_with_encryptor User, :sha512 do
90
- user = create_user
91
- assert_equal user.encrypted_password, encrypt_password(user, User.pepper, User.stretches, ::Devise::Encryptors::Sha512)
92
- end
93
- end
94
-
95
45
  test 'should test for a valid password' do
96
46
  user = create_user
97
47
  assert user.valid_password?('123456')
98
48
  assert_not user.valid_password?('654321')
99
49
  end
100
50
 
101
- test 'should not validate password when salt is nil' do
102
- admin = create_admin
103
- admin.password_salt = nil
104
- admin.save
105
- assert_not admin.valid_password?('123456')
106
- end
107
-
108
51
  test 'should respond to current password' do
109
52
  assert new_user.respond_to?(:current_password)
110
53
  end
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class EncryptableTest < ActiveSupport::TestCase
4
+ def encrypt_password(admin, pepper=Admin.pepper, stretches=Admin.stretches, encryptor=Admin.encryptor_class)
5
+ encryptor.digest('123456', stretches, admin.password_salt, pepper)
6
+ end
7
+
8
+ def swap_with_encryptor(klass, encryptor, options={})
9
+ klass.instance_variable_set(:@encryptor_class, nil)
10
+
11
+ swap klass, options.merge(:encryptor => encryptor) do
12
+ begin
13
+ yield
14
+ ensure
15
+ klass.instance_variable_set(:@encryptor_class, nil)
16
+ end
17
+ end
18
+ end
19
+
20
+ test 'should generate salt while setting password' do
21
+ assert_present create_admin.password_salt
22
+ end
23
+
24
+ test 'should not change password salt when updating' do
25
+ admin = create_admin
26
+ salt = admin.password_salt
27
+ admin.expects(:password_salt=).never
28
+ admin.save!
29
+ assert_equal salt, admin.password_salt
30
+ end
31
+
32
+ test 'should generate a base64 hash using SecureRandom for password salt' do
33
+ swap_with_encryptor Admin, :sha1 do
34
+ ActiveSupport::SecureRandom.expects(:base64).with(44).returns('friendly_token')
35
+ assert_equal 'friendly_token', create_admin.password_salt
36
+ end
37
+ end
38
+
39
+ test 'should not generate salt if password is blank' do
40
+ assert_blank create_admin(:password => nil).password_salt
41
+ assert_blank create_admin(:password => '').password_salt
42
+ end
43
+
44
+ test 'should encrypt password again if password has changed' do
45
+ admin = create_admin
46
+ encrypted_password = admin.encrypted_password
47
+ admin.password = admin.password_confirmation = 'new_password'
48
+ admin.save!
49
+ assert_not_equal encrypted_password, admin.encrypted_password
50
+ end
51
+
52
+ test 'should respect encryptor configuration' do
53
+ swap_with_encryptor Admin, :sha512 do
54
+ admin = create_admin
55
+ assert_equal admin.encrypted_password, encrypt_password(admin, Admin.pepper, Admin.stretches, ::Devise::Encryptors::Sha512)
56
+ end
57
+ end
58
+
59
+ test 'should not validate password when salt is nil' do
60
+ admin = create_admin
61
+ admin.password_salt = nil
62
+ admin.save
63
+ assert_not admin.valid_password?('123456')
64
+ end
65
+ end
@@ -178,11 +178,27 @@ class LockableTest < ActiveSupport::TestCase
178
178
  assert_equal 'not found', unlock_user.errors[:email].join
179
179
  end
180
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
+
181
198
  test 'should not be able to send instructions if the user is not locked' do
182
199
  user = create_user
183
200
  assert_not user.resend_unlock_token
184
201
  assert_not user.access_locked?
185
202
  assert_equal 'was not locked', user.errors[:email].join
186
203
  end
187
-
188
204
  end
@@ -85,6 +85,23 @@ class RecoverableTest < ActiveSupport::TestCase
85
85
  assert_not reset_password_user.persisted?
86
86
  assert_equal "not found", reset_password_user.errors[:email].join
87
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
88
105
 
89
106
  test 'should reset reset_password_token before send the reset instructions email' do
90
107
  user = create_user
@@ -1,218 +1,279 @@
1
1
  require 'test_helper'
2
2
 
3
- class RememberableTest < ActiveSupport::TestCase
4
- test 'should respond to remember_me attribute' do
5
- user = new_user
6
- assert user.respond_to?(:remember_me)
7
- end
3
+ module SharedRememberableTest
4
+ extend ActiveSupport::Testing::Declarative
8
5
 
9
- test 'remember_me should generate a new token and save the record without validating' do
10
- user = create_user
11
- user.expects(:valid?).never
12
- token = user.remember_token
13
- user.remember_me!
14
- assert_not_equal token, user.remember_token
15
- assert_not user.changed?
16
- end
17
-
18
- test 'forget_me should clear remember token and save the record without validating' do
19
- user = create_user
20
- user.remember_me!
21
- assert_not user.remember_token.nil?
22
- user.expects(:valid?).never
23
- user.forget_me!
24
- assert user.remember_token.nil?
25
- assert_not user.changed?
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=)
26
9
  end
27
10
 
28
11
  test 'forget_me should clear remember_created_at' do
29
- user = create_user
30
- user.remember_me!
31
- assert_not user.remember_created_at.nil?
32
- user.forget_me!
33
- assert user.remember_created_at.nil?
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?
34
17
  end
35
18
 
36
- test 'forget should do nothing if no remember token exists' do
37
- user = create_user
38
- user.expects(:save).never
39
- user.forget_me!
19
+ test 'remember is expired if not created at timestamp is set' do
20
+ assert create_resource.remember_expired?
40
21
  end
41
22
 
42
- test 'serialize into cookie' do
43
- user = create_user
44
- user.remember_me!
45
- assert_equal [user.id, user.remember_token], User.serialize_into_cookie(user)
46
- end
47
-
48
- test 'serialize from cookie' do
49
- user = create_user
50
- user.remember_me!
51
- assert_equal user, User.serialize_from_cookie(user.id, user.remember_token)
23
+ test 'serialize should return nil if no resource is found' do
24
+ assert_nil resource_class.serialize_from_cookie([0], "123")
52
25
  end
53
26
 
54
- test 'serialize should return nil if no user is found' do
55
- assert_nil User.serialize_from_cookie(0, "123")
56
- end
57
-
58
- test 'remember me return nil if is a valid user with invalid token' do
59
- user = create_user
60
- assert_nil User.serialize_from_cookie(user.id, "123")
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")
61
30
  end
62
31
 
63
32
  test 'remember for should fallback to devise remember for default configuration' do
64
33
  swap Devise, :remember_for => 1.day do
65
- user = create_user
66
- user.remember_me!
67
- assert_not user.remember_expired?
34
+ resource = create_resource
35
+ resource.remember_me!
36
+ assert_not resource.remember_expired?
68
37
  end
69
38
  end
70
39
 
71
40
  test 'remember expires at should sum date of creation with remember for configuration' do
72
41
  swap Devise, :remember_for => 3.days do
73
- user = create_user
74
- user.remember_me!
75
- assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date
42
+ resource = create_resource
43
+ resource.remember_me!
44
+ assert_equal 3.days.from_now.to_date, resource.remember_expires_at.to_date
76
45
 
77
46
  Devise.remember_for = 5.days
78
- assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date
47
+ assert_equal 5.days.from_now.to_date, resource.remember_expires_at.to_date
79
48
  end
80
49
  end
81
50
 
82
51
  test 'remember should be expired if remember_for is zero' do
83
52
  swap Devise, :remember_for => 0.days do
84
53
  Devise.remember_for = 0.days
85
- user = create_user
86
- user.remember_me!
87
- assert user.remember_expired?
54
+ resource = create_resource
55
+ resource.remember_me!
56
+ assert resource.remember_expired?
88
57
  end
89
58
  end
90
59
 
91
60
  test 'remember should be expired if it was created before limit time' do
92
61
  swap Devise, :remember_for => 1.day do
93
- user = create_user
94
- user.remember_me!
95
- user.remember_created_at = 2.days.ago
96
- user.save
97
- assert user.remember_expired?
62
+ resource = create_resource
63
+ resource.remember_me!
64
+ resource.remember_created_at = 2.days.ago
65
+ resource.save
66
+ assert resource.remember_expired?
98
67
  end
99
68
  end
100
69
 
101
70
  test 'remember should not be expired if it was created whitin the limit time' do
102
71
  swap Devise, :remember_for => 30.days do
103
- user = create_user
104
- user.remember_me!
105
- user.remember_created_at = (30.days.ago + 2.minutes)
106
- user.save
107
- assert_not user.remember_expired?
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?
108
77
  end
109
78
  end
110
79
 
111
80
  test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
112
81
  swap Devise, :remember_for => 5.minutes do
113
- user = create_user
114
- user.remember_me!(false)
115
- assert user.remember_created_at
82
+ resource = create_resource
83
+ resource.remember_me!(false)
84
+ assert resource.remember_created_at
116
85
 
117
- user.remember_created_at = old = 10.minutes.ago
118
- user.save
86
+ resource.remember_created_at = old = 10.minutes.ago
87
+ resource.save
119
88
 
120
- user.remember_me!(false)
121
- assert_not_equal old.to_i, user.remember_created_at.to_i
89
+ resource.remember_me!(false)
90
+ assert_not_equal old.to_i, resource.remember_created_at.to_i
122
91
  end
123
92
  end
124
93
 
125
94
  test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
126
95
  swap Devise, :remember_for => 1.year do
127
- user = create_user
128
- user.remember_me!(false)
129
- assert user.remember_created_at
96
+ resource = create_resource
97
+ resource.remember_me!(false)
98
+ assert resource.remember_created_at
130
99
 
131
- user.remember_created_at = old = 10.minutes.ago.utc
132
- user.save
100
+ resource.remember_created_at = old = 10.minutes.ago.utc
101
+ resource.save
133
102
 
134
- user.remember_me!(false)
135
- assert_equal old.to_i, user.remember_created_at.to_i
103
+ resource.remember_me!(false)
104
+ assert_equal old.to_i, resource.remember_created_at.to_i
136
105
  end
137
106
  end
138
107
 
139
108
  test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
140
109
  swap Devise, :remember_for => 1.year do
141
- user = create_user
142
- user.remember_me!(true)
143
- assert user.remember_created_at
110
+ resource = create_resource
111
+ resource.remember_me!(true)
112
+ assert resource.remember_created_at
144
113
 
145
- user.remember_created_at = old = 10.minutes.ago
146
- user.save
114
+ resource.remember_created_at = old = 10.minutes.ago
115
+ resource.save
147
116
 
148
- user.remember_me!(true)
149
- assert_not_equal old, user.remember_created_at
117
+ resource.remember_me!(true)
118
+ assert_not_equal old, resource.remember_created_at
150
119
  end
151
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.to_key, 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.to_key, admin.remember_token)
163
+ end
152
164
 
153
165
  test 'if remember_across_browsers is true, remember_me! should create a new token if no token exists' do
154
166
  swap Devise, :remember_across_browsers => true, :remember_for => 1.year do
155
- user = create_user
156
- assert_equal nil, user.remember_token
157
- user.remember_me!
158
- assert_not_equal nil, user.remember_token
167
+ admin = create_admin
168
+ assert_equal nil, admin.remember_token
169
+ admin.remember_me!
170
+ assert_not_equal nil, admin.remember_token
159
171
  end
160
172
  end
161
173
 
162
174
  test 'if remember_across_browsers is true, remember_me! should create a new token if a token exists but has expired' do
163
175
  swap Devise, :remember_across_browsers => true, :remember_for => 1.day do
164
- user = create_user
165
- user.remember_me!
166
- user.remember_created_at = 2.days.ago
167
- user.save
168
- token = user.remember_token
169
- user.remember_me!
170
- assert_not_equal token, user.remember_token
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
171
183
  end
172
184
  end
173
185
 
174
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
175
187
  swap Devise, :remember_across_browsers => true, :remember_for => 2.days do
176
- user = create_user
177
- user.remember_me!
178
- user.remember_created_at = 1.day.ago
179
- user.save
180
- token = user.remember_token
181
- user.remember_me!
182
- assert_equal token, user.remember_token
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
183
195
  end
184
196
  end
185
197
 
186
198
  test 'if remember_across_browsers is false, remember_me! should create a new token if no token exists' do
187
199
  swap Devise, :remember_across_browsers => false do
188
- user = create_user
189
- assert_equal nil, user.remember_token
190
- user.remember_me!
191
- assert_not_equal nil, user.remember_token
200
+ admin = create_admin
201
+ assert_equal nil, admin.remember_token
202
+ admin.remember_me!
203
+ assert_not_equal nil, admin.remember_token
192
204
  end
193
205
  end
194
206
 
195
207
  test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists but has expired' do
196
208
  swap Devise, :remember_across_browsers => false, :remember_for => 1.day do
197
- user = create_user
198
- user.remember_me!
199
- user.remember_created_at = 2.days.ago
200
- user.save
201
- token = user.remember_token
202
- user.remember_me!
203
- assert_not_equal token, user.remember_token
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
204
216
  end
205
217
  end
206
218
 
207
219
  test 'if remember_across_browsers is false, remember_me! should create a new token if a token exists and has not expired' do
208
220
  swap Devise, :remember_across_browsers => false, :remember_for => 2.days do
209
- user = create_user
210
- user.remember_me!
211
- user.remember_created_at = 1.day.ago
212
- user.save
213
- token = user.remember_token
214
- user.remember_me!
215
- assert_not_equal token, user.remember_token
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.to_key, 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.to_key, user.authenticatable_salt)
270
+ end
271
+
272
+ test 'raises a RuntimeError if authenticatable_salt is nil' do
273
+ user = User.new
274
+ user.encrypted_password = nil
275
+ assert_raise RuntimeError do
276
+ user.rememberable_value
216
277
  end
217
278
  end
218
279
  end