namxam-devise 1.1.0.win

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.rdoc +455 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +118 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +311 -0
  6. data/Rakefile +55 -0
  7. data/TODO +3 -0
  8. data/app/controllers/devise/confirmations_controller.rb +33 -0
  9. data/app/controllers/devise/passwords_controller.rb +41 -0
  10. data/app/controllers/devise/registrations_controller.rb +57 -0
  11. data/app/controllers/devise/sessions_controller.rb +23 -0
  12. data/app/controllers/devise/unlocks_controller.rb +34 -0
  13. data/app/helpers/devise_helper.rb +17 -0
  14. data/app/mailers/devise/mailer.rb +71 -0
  15. data/app/views/devise/confirmations/new.html.erb +12 -0
  16. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  17. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  18. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  19. data/app/views/devise/passwords/edit.html.erb +16 -0
  20. data/app/views/devise/passwords/new.html.erb +12 -0
  21. data/app/views/devise/registrations/edit.html.erb +25 -0
  22. data/app/views/devise/registrations/new.html.erb +18 -0
  23. data/app/views/devise/sessions/new.html.erb +17 -0
  24. data/app/views/devise/shared/_links.erb +19 -0
  25. data/app/views/devise/unlocks/new.html.erb +12 -0
  26. data/config/locales/en.yml +39 -0
  27. data/lib/devise.rb +290 -0
  28. data/lib/devise/controllers/helpers.rb +231 -0
  29. data/lib/devise/controllers/internal_helpers.rb +98 -0
  30. data/lib/devise/controllers/scoped_views.rb +35 -0
  31. data/lib/devise/controllers/url_helpers.rb +41 -0
  32. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  33. data/lib/devise/encryptors/base.rb +20 -0
  34. data/lib/devise/encryptors/bcrypt.rb +19 -0
  35. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  36. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  37. data/lib/devise/encryptors/sha1.rb +25 -0
  38. data/lib/devise/encryptors/sha512.rb +25 -0
  39. data/lib/devise/failure_app.rb +107 -0
  40. data/lib/devise/hooks/activatable.rb +11 -0
  41. data/lib/devise/hooks/forgetable.rb +11 -0
  42. data/lib/devise/hooks/rememberable.rb +35 -0
  43. data/lib/devise/hooks/timeoutable.rb +22 -0
  44. data/lib/devise/hooks/trackable.rb +9 -0
  45. data/lib/devise/mapping.rb +103 -0
  46. data/lib/devise/models.rb +80 -0
  47. data/lib/devise/models/authenticatable.rb +126 -0
  48. data/lib/devise/models/confirmable.rb +164 -0
  49. data/lib/devise/models/database_authenticatable.rb +110 -0
  50. data/lib/devise/models/lockable.rb +165 -0
  51. data/lib/devise/models/recoverable.rb +81 -0
  52. data/lib/devise/models/registerable.rb +8 -0
  53. data/lib/devise/models/rememberable.rb +104 -0
  54. data/lib/devise/models/timeoutable.rb +26 -0
  55. data/lib/devise/models/token_authenticatable.rb +60 -0
  56. data/lib/devise/models/trackable.rb +30 -0
  57. data/lib/devise/models/validatable.rb +53 -0
  58. data/lib/devise/modules.rb +23 -0
  59. data/lib/devise/orm/active_record.rb +36 -0
  60. data/lib/devise/orm/mongoid.rb +29 -0
  61. data/lib/devise/path_checker.rb +18 -0
  62. data/lib/devise/rails.rb +69 -0
  63. data/lib/devise/rails/routes.rb +248 -0
  64. data/lib/devise/rails/warden_compat.rb +39 -0
  65. data/lib/devise/schema.rb +97 -0
  66. data/lib/devise/strategies/authenticatable.rb +111 -0
  67. data/lib/devise/strategies/base.rb +33 -0
  68. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  69. data/lib/devise/strategies/rememberable.rb +43 -0
  70. data/lib/devise/strategies/token_authenticatable.rb +49 -0
  71. data/lib/devise/test_helpers.rb +90 -0
  72. data/lib/devise/version.rb +3 -0
  73. data/lib/generators/active_record/devise_generator.rb +28 -0
  74. data/lib/generators/active_record/templates/migration.rb +29 -0
  75. data/lib/generators/devise/devise_generator.rb +17 -0
  76. data/lib/generators/devise/install_generator.rb +24 -0
  77. data/lib/generators/devise/orm_helpers.rb +23 -0
  78. data/lib/generators/devise/templates/README +25 -0
  79. data/lib/generators/devise/templates/devise.rb +139 -0
  80. data/lib/generators/devise/views_generator.rb +63 -0
  81. data/lib/generators/devise_install_generator.rb +4 -0
  82. data/lib/generators/devise_views_generator.rb +4 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/test/controllers/helpers_test.rb +213 -0
  85. data/test/controllers/internal_helpers_test.rb +51 -0
  86. data/test/controllers/url_helpers_test.rb +58 -0
  87. data/test/devise_test.rb +65 -0
  88. data/test/encryptors_test.rb +30 -0
  89. data/test/failure_app_test.rb +123 -0
  90. data/test/integration/authenticatable_test.rb +344 -0
  91. data/test/integration/confirmable_test.rb +104 -0
  92. data/test/integration/database_authenticatable_test.rb +38 -0
  93. data/test/integration/http_authenticatable_test.rb +49 -0
  94. data/test/integration/lockable_test.rb +109 -0
  95. data/test/integration/recoverable_test.rb +141 -0
  96. data/test/integration/registerable_test.rb +153 -0
  97. data/test/integration/rememberable_test.rb +91 -0
  98. data/test/integration/timeoutable_test.rb +80 -0
  99. data/test/integration/token_authenticatable_test.rb +88 -0
  100. data/test/integration/trackable_test.rb +64 -0
  101. data/test/mailers/confirmation_instructions_test.rb +80 -0
  102. data/test/mailers/reset_password_instructions_test.rb +68 -0
  103. data/test/mailers/unlock_instructions_test.rb +62 -0
  104. data/test/mapping_test.rb +85 -0
  105. data/test/models/confirmable_test.rb +221 -0
  106. data/test/models/database_authenticatable_test.rb +148 -0
  107. data/test/models/lockable_test.rb +188 -0
  108. data/test/models/recoverable_test.rb +138 -0
  109. data/test/models/rememberable_test.rb +176 -0
  110. data/test/models/timeoutable_test.rb +28 -0
  111. data/test/models/token_authenticatable_test.rb +37 -0
  112. data/test/models/trackable_test.rb +5 -0
  113. data/test/models/validatable_test.rb +99 -0
  114. data/test/models_test.rb +77 -0
  115. data/test/orm/active_record.rb +9 -0
  116. data/test/orm/mongoid.rb +10 -0
  117. data/test/rails_app/app/active_record/admin.rb +3 -0
  118. data/test/rails_app/app/active_record/shim.rb +2 -0
  119. data/test/rails_app/app/active_record/user.rb +7 -0
  120. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  121. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  122. data/test/rails_app/app/controllers/home_controller.rb +7 -0
  123. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  124. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  125. data/test/rails_app/app/controllers/sessions_controller.rb +6 -0
  126. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  127. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  128. data/test/rails_app/app/mongoid/admin.rb +6 -0
  129. data/test/rails_app/app/mongoid/shim.rb +16 -0
  130. data/test/rails_app/app/mongoid/user.rb +10 -0
  131. data/test/rails_app/config/application.rb +35 -0
  132. data/test/rails_app/config/boot.rb +13 -0
  133. data/test/rails_app/config/environment.rb +5 -0
  134. data/test/rails_app/config/environments/development.rb +19 -0
  135. data/test/rails_app/config/environments/production.rb +33 -0
  136. data/test/rails_app/config/environments/test.rb +33 -0
  137. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  138. data/test/rails_app/config/initializers/devise.rb +136 -0
  139. data/test/rails_app/config/initializers/inflections.rb +2 -0
  140. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  141. data/test/rails_app/config/routes.rb +47 -0
  142. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +27 -0
  143. data/test/rails_app/db/schema.rb +86 -0
  144. data/test/routes_test.rb +146 -0
  145. data/test/support/assertions.rb +24 -0
  146. data/test/support/helpers.rb +54 -0
  147. data/test/support/integration.rb +88 -0
  148. data/test/support/test_silencer.rb +5 -0
  149. data/test/support/webrat/integrations/rails.rb +32 -0
  150. data/test/test_helper.rb +21 -0
  151. data/test/test_helpers_test.rb +72 -0
  152. metadata +230 -0
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class UnlockInstructionsTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Devise.mailer_sender = 'test@example.com'
8
+ end
9
+
10
+ def user
11
+ @user ||= begin
12
+ user = create_user
13
+ user.lock_access!
14
+ user
15
+ end
16
+ end
17
+
18
+ def mail
19
+ @mail ||= begin
20
+ user
21
+ ActionMailer::Base.deliveries.last
22
+ end
23
+ end
24
+
25
+ test 'email sent after locking the user' do
26
+ assert_not_nil mail
27
+ end
28
+
29
+ test 'content type should be set to html' do
30
+ assert mail.content_type.include?('text/html')
31
+ end
32
+
33
+ test 'send unlock instructions to the user email' do
34
+ assert_equal [user.email], mail.to
35
+ end
36
+
37
+ test 'setup sender from configuration' do
38
+ assert_equal ['test@example.com'], mail.from
39
+ end
40
+
41
+ test 'setup subject from I18n' do
42
+ store_translations :en, :devise => { :mailer => { :unlock_instructions => { :subject => 'Yo unlock instructions' } } } do
43
+ assert_equal 'Yo unlock instructions', mail.subject
44
+ end
45
+ end
46
+
47
+ test 'subject namespaced by model' do
48
+ store_translations :en, :devise => { :mailer => { :unlock_instructions => { :user_subject => 'User Unlock Instructions' } } } do
49
+ assert_equal 'User Unlock Instructions', mail.subject
50
+ end
51
+ end
52
+
53
+ test 'body should have user info' do
54
+ assert_match(/#{user.email}/, mail.body.encoded)
55
+ end
56
+
57
+ test 'body should have link to unlock the account' do
58
+ host = ActionMailer::Base.default_url_options[:host]
59
+ unlock_url_regexp = %r{<a href=\"http://#{host}/users/unlock\?unlock_token=#{user.unlock_token}">}
60
+ assert_match unlock_url_regexp, mail.body.encoded
61
+ end
62
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class FakeRequest < Struct.new(:path_info, :params)
4
+ end
5
+
6
+ class MappingTest < ActiveSupport::TestCase
7
+ def fake_request(path, params={})
8
+ FakeRequest.new(path, params)
9
+ end
10
+
11
+ test 'store options' do
12
+ mapping = Devise.mappings[:user]
13
+ assert_equal User, mapping.to
14
+ assert_equal User.devise_modules, mapping.modules
15
+ assert_equal :users, mapping.plural
16
+ assert_equal :user, mapping.singular
17
+ assert_equal "users", mapping.path
18
+ end
19
+
20
+ test 'allows path to be given' do
21
+ assert_equal "admin_area", Devise.mappings[:admin].path
22
+ end
23
+
24
+ test 'allows custom singular to be given' do
25
+ assert_equal "accounts", Devise.mappings[:manager].path
26
+ end
27
+
28
+ test 'has strategies depending on the model declaration' do
29
+ assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
30
+ assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies
31
+ end
32
+
33
+ test 'find scope for a given object' do
34
+ assert_equal :user, Devise::Mapping.find_scope!(User)
35
+ assert_equal :user, Devise::Mapping.find_scope!(:user)
36
+ assert_equal :user, Devise::Mapping.find_scope!(User.new)
37
+ end
38
+
39
+ test 'find scope works with single table inheritance' do
40
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User))
41
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User).new)
42
+ end
43
+
44
+ test 'find scope raises an error if cannot be found' do
45
+ assert_raise RuntimeError do
46
+ Devise::Mapping.find_scope!(String)
47
+ end
48
+ end
49
+
50
+ test 'return default path names' do
51
+ mapping = Devise.mappings[:user]
52
+ assert_equal 'sign_in', mapping.path_names[:sign_in]
53
+ assert_equal 'sign_out', mapping.path_names[:sign_out]
54
+ assert_equal 'password', mapping.path_names[:password]
55
+ assert_equal 'confirmation', mapping.path_names[:confirmation]
56
+ assert_equal 'sign_up', mapping.path_names[:sign_up]
57
+ assert_equal 'unlock', mapping.path_names[:unlock]
58
+ end
59
+
60
+ test 'allow custom path names to be given' do
61
+ mapping = Devise.mappings[:manager]
62
+ assert_equal 'login', mapping.path_names[:sign_in]
63
+ assert_equal 'logout', mapping.path_names[:sign_out]
64
+ assert_equal 'secret', mapping.path_names[:password]
65
+ assert_equal 'verification', mapping.path_names[:confirmation]
66
+ assert_equal 'register', mapping.path_names[:sign_up]
67
+ assert_equal 'unblock', mapping.path_names[:unlock]
68
+ end
69
+
70
+ test 'magic predicates' do
71
+ mapping = Devise.mappings[:user]
72
+ assert mapping.authenticatable?
73
+ assert mapping.confirmable?
74
+ assert mapping.recoverable?
75
+ assert mapping.rememberable?
76
+ assert mapping.registerable?
77
+
78
+ mapping = Devise.mappings[:admin]
79
+ assert mapping.authenticatable?
80
+ assert mapping.recoverable?
81
+ assert mapping.lockable?
82
+ assert_not mapping.confirmable?
83
+ assert_not mapping.rememberable?
84
+ end
85
+ end
@@ -0,0 +1,221 @@
1
+ require 'test_helper'
2
+
3
+ class ConfirmableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should generate confirmation token after creating a record' do
10
+ assert_nil new_user.confirmation_token
11
+ assert_not_nil create_user.confirmation_token
12
+ end
13
+
14
+ test 'should never generate the same confirmation token for different users' do
15
+ confirmation_tokens = []
16
+ 3.times do
17
+ token = create_user.confirmation_token
18
+ assert !confirmation_tokens.include?(token)
19
+ confirmation_tokens << token
20
+ end
21
+ end
22
+
23
+ test 'should confirm a user by updating confirmed at' do
24
+ user = create_user
25
+ assert_nil user.confirmed_at
26
+ assert user.confirm!
27
+ assert_not_nil user.confirmed_at
28
+ end
29
+
30
+ test 'should clear confirmation token while confirming a user' do
31
+ user = create_user
32
+ assert_present user.confirmation_token
33
+ user.confirm!
34
+ assert_nil user.confirmation_token
35
+ end
36
+
37
+ test 'should verify whether a user is confirmed or not' do
38
+ assert_not new_user.confirmed?
39
+ user = create_user
40
+ assert_not user.confirmed?
41
+ user.confirm!
42
+ assert user.confirmed?
43
+ end
44
+
45
+ test 'should not confirm a user already confirmed' do
46
+ user = create_user
47
+ assert user.confirm!
48
+ assert_blank user.errors[:email]
49
+
50
+ assert_not user.confirm!
51
+ assert_equal "was already confirmed", user.errors[:email].join
52
+ end
53
+
54
+ test 'should find and confirm an user automatically' do
55
+ user = create_user
56
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
57
+ assert_equal confirmed_user, user
58
+ assert user.reload.confirmed?
59
+ end
60
+
61
+ test 'should return a new record with errors when a invalid token is given' do
62
+ confirmed_user = User.confirm_by_token('invalid_confirmation_token')
63
+ assert_not confirmed_user.persisted?
64
+ assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
65
+ end
66
+
67
+ test 'should return a new record with errors when a blank token is given' do
68
+ confirmed_user = User.confirm_by_token('')
69
+ assert_not confirmed_user.persisted?
70
+ assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
71
+ end
72
+
73
+ test 'should generate errors for a user email if user is already confirmed' do
74
+ user = create_user
75
+ user.confirmed_at = Time.now
76
+ user.save
77
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
78
+ assert confirmed_user.confirmed?
79
+ assert_equal "was already confirmed", confirmed_user.errors[:email].join
80
+ end
81
+
82
+ test 'should send confirmation instructions by email' do
83
+ assert_email_sent do
84
+ create_user
85
+ end
86
+ end
87
+
88
+ test 'should not send confirmation when trying to save an invalid user' do
89
+ assert_email_not_sent do
90
+ user = new_user
91
+ user.stubs(:valid?).returns(false)
92
+ user.save
93
+ end
94
+ end
95
+
96
+ test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
97
+ user = new_user
98
+ user.skip_confirmation!
99
+
100
+ assert_email_not_sent do
101
+ user.save!
102
+ assert_nil user.confirmation_token
103
+ assert_not_nil user.confirmed_at
104
+ end
105
+ end
106
+
107
+ test 'should find a user to send confirmation instructions' do
108
+ user = create_user
109
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
110
+ assert_equal confirmation_user, user
111
+ end
112
+
113
+ test 'should return a new user if no email was found' do
114
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
115
+ assert_not confirmation_user.persisted?
116
+ end
117
+
118
+ test 'should add error to new user email if no email was found' do
119
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
120
+ assert confirmation_user.errors[:email]
121
+ assert_equal "not found", confirmation_user.errors[:email].join
122
+ end
123
+
124
+ test 'should send email instructions for the user confirm it\'s email' do
125
+ user = create_user
126
+ assert_email_sent do
127
+ User.send_confirmation_instructions(:email => user.email)
128
+ end
129
+ end
130
+
131
+ test 'should always have confirmation token when email is sent' do
132
+ user = new_user
133
+ user.instance_eval { def confirmation_required?; false end }
134
+ user.save
135
+ user.send_confirmation_instructions
136
+ assert_not_nil user.reload.confirmation_token
137
+ end
138
+
139
+ test 'should not resend email instructions if the user change his email' do
140
+ user = create_user
141
+ user.email = 'new_test@example.com'
142
+ assert_email_not_sent do
143
+ user.save!
144
+ end
145
+ end
146
+
147
+ test 'should not reset confirmation status or token when updating email' do
148
+ user = create_user
149
+ user.confirm!
150
+ user.email = 'new_test@example.com'
151
+ user.save!
152
+
153
+ user.reload
154
+ assert user.confirmed?
155
+ assert_nil user.confirmation_token
156
+ end
157
+
158
+ test 'should not be able to send instructions if the user is already confirmed' do
159
+ user = create_user
160
+ user.confirm!
161
+ assert_not user.resend_confirmation_token
162
+ assert user.confirmed?
163
+ assert_equal 'was already confirmed', user.errors[:email].join
164
+ end
165
+
166
+ test 'confirm time should fallback to devise confirm in default configuration' do
167
+ swap Devise, :confirm_within => 1.day do
168
+ user = new_user
169
+ user.confirmation_sent_at = 2.days.ago
170
+ assert_not user.active?
171
+
172
+ Devise.confirm_within = 3.days
173
+ assert user.active?
174
+ end
175
+ end
176
+
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
180
+ user = create_user
181
+
182
+ user.confirmation_sent_at = 4.days.ago
183
+ assert user.active?
184
+
185
+ user.confirmation_sent_at = 5.days.ago
186
+ assert_not user.active?
187
+ end
188
+ end
189
+
190
+ test 'should be active when already confirmed' do
191
+ user = create_user
192
+ assert_not user.confirmed?
193
+ assert_not user.active?
194
+
195
+ user.confirm!
196
+ assert user.confirmed?
197
+ assert user.active?
198
+ end
199
+
200
+ test 'should not be active when confirm in is zero' do
201
+ Devise.confirm_within = 0.days
202
+ user = create_user
203
+ user.confirmation_sent_at = Date.today
204
+ assert_not user.active?
205
+ end
206
+
207
+ test 'should not be active without confirmation' do
208
+ user = create_user
209
+ user.confirmation_sent_at = nil
210
+ user.save
211
+ assert_not user.reload.active?
212
+ end
213
+
214
+ test 'should be active without confirmation when confirmation is not required' do
215
+ user = create_user
216
+ user.instance_eval { def confirmation_required?; false end }
217
+ user.confirmation_sent_at = nil
218
+ user.save
219
+ assert user.reload.active?
220
+ end
221
+ end
@@ -0,0 +1,148 @@
1
+ require 'test_helper'
2
+ require 'digest/sha1'
3
+
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
20
+ end
21
+
22
+ test 'should respond to password and password confirmation' do
23
+ user = new_user
24
+ assert user.respond_to?(:password)
25
+ assert user.respond_to?(:password_confirmation)
26
+ end
27
+
28
+ test 'should generate encrypted password and salt while setting password' do
29
+ user = new_user
30
+ assert_present user.password_salt
31
+ assert_present user.encrypted_password
32
+ end
33
+
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
52
+ end
53
+
54
+ test 'should not generate encrypted password if password is blank' do
55
+ assert_blank new_user(:password => nil).encrypted_password
56
+ assert_blank new_user(:password => '').encrypted_password
57
+ end
58
+
59
+ test 'should encrypt password again if password has changed' do
60
+ user = create_user
61
+ encrypted_password = user.encrypted_password
62
+ user.password = user.password_confirmation = 'new_password'
63
+ user.save!
64
+ assert_not_equal encrypted_password, user.encrypted_password
65
+ end
66
+
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
+ test 'should test for a valid password' do
96
+ user = create_user
97
+ assert user.valid_password?('123456')
98
+ assert_not user.valid_password?('654321')
99
+ end
100
+
101
+ test 'should respond to current password' do
102
+ assert new_user.respond_to?(:current_password)
103
+ end
104
+
105
+ test 'should update password with valid current password' do
106
+ user = create_user
107
+ assert user.update_with_password(:current_password => '123456',
108
+ :password => 'pass321', :password_confirmation => 'pass321')
109
+ assert user.reload.valid_password?('pass321')
110
+ end
111
+
112
+ test 'should add an error to current password when it is invalid' do
113
+ user = create_user
114
+ assert_not user.update_with_password(:current_password => 'other',
115
+ :password => 'pass321', :password_confirmation => 'pass321')
116
+ assert user.reload.valid_password?('123456')
117
+ assert_match "is invalid", user.errors[:current_password].join
118
+ end
119
+
120
+ test 'should add an error to current password when it is blank' do
121
+ user = create_user
122
+ assert_not user.update_with_password(:password => 'pass321',
123
+ :password_confirmation => 'pass321')
124
+ assert user.reload.valid_password?('123456')
125
+ assert_match "can't be blank", user.errors[:current_password].join
126
+ end
127
+
128
+ test 'should ignore password and its confirmation if they are blank' do
129
+ user = create_user
130
+ assert user.update_with_password(:current_password => '123456', :email => "new@email.com")
131
+ assert_equal "new@email.com", user.email
132
+ end
133
+
134
+ test 'should not update password with invalid confirmation' do
135
+ user = create_user
136
+ assert_not user.update_with_password(:current_password => '123456',
137
+ :password => 'pass321', :password_confirmation => 'other')
138
+ assert user.reload.valid_password?('123456')
139
+ end
140
+
141
+ test 'should clean up password fields on failure' do
142
+ user = create_user
143
+ assert_not user.update_with_password(:current_password => '123456',
144
+ :password => 'pass321', :password_confirmation => 'other')
145
+ assert user.password.blank?
146
+ assert user.password_confirmation.blank?
147
+ end
148
+ end