gonow-devise 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/CHANGELOG.rdoc +506 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +335 -0
  4. data/app/controllers/devise/confirmations_controller.rb +33 -0
  5. data/app/controllers/devise/oauth_callbacks_controller.rb +4 -0
  6. data/app/controllers/devise/passwords_controller.rb +41 -0
  7. data/app/controllers/devise/registrations_controller.rb +109 -0
  8. data/app/controllers/devise/sessions_controller.rb +24 -0
  9. data/app/controllers/devise/unlocks_controller.rb +34 -0
  10. data/app/helpers/devise_helper.rb +19 -0
  11. data/app/mailers/devise/mailer.rb +88 -0
  12. data/app/views/devise/confirmations/new.html.erb +12 -0
  13. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  14. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  15. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  16. data/app/views/devise/passwords/edit.html.erb +16 -0
  17. data/app/views/devise/passwords/new.html.erb +12 -0
  18. data/app/views/devise/registrations/edit.html.erb +25 -0
  19. data/app/views/devise/registrations/new.html.erb +18 -0
  20. data/app/views/devise/sessions/new.html.erb +17 -0
  21. data/app/views/devise/shared/_links.erb +25 -0
  22. data/app/views/devise/unlocks/new.html.erb +12 -0
  23. data/config/locales/en.yml +46 -0
  24. data/lib/devise.rb +372 -0
  25. data/lib/devise/controllers/helpers.rb +228 -0
  26. data/lib/devise/controllers/internal_helpers.rb +113 -0
  27. data/lib/devise/controllers/scoped_views.rb +33 -0
  28. data/lib/devise/controllers/url_helpers.rb +39 -0
  29. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  30. data/lib/devise/encryptors/base.rb +20 -0
  31. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  32. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  33. data/lib/devise/encryptors/sha1.rb +25 -0
  34. data/lib/devise/encryptors/sha512.rb +25 -0
  35. data/lib/devise/failure_app.rb +126 -0
  36. data/lib/devise/hooks/activatable.rb +11 -0
  37. data/lib/devise/hooks/forgetable.rb +12 -0
  38. data/lib/devise/hooks/rememberable.rb +45 -0
  39. data/lib/devise/hooks/timeoutable.rb +22 -0
  40. data/lib/devise/hooks/trackable.rb +9 -0
  41. data/lib/devise/mapping.rb +105 -0
  42. data/lib/devise/models.rb +66 -0
  43. data/lib/devise/models/authenticatable.rb +143 -0
  44. data/lib/devise/models/confirmable.rb +160 -0
  45. data/lib/devise/models/database_authenticatable.rb +94 -0
  46. data/lib/devise/models/encryptable.rb +65 -0
  47. data/lib/devise/models/lockable.rb +168 -0
  48. data/lib/devise/models/oauthable.rb +49 -0
  49. data/lib/devise/models/recoverable.rb +83 -0
  50. data/lib/devise/models/registerable.rb +21 -0
  51. data/lib/devise/models/rememberable.rb +122 -0
  52. data/lib/devise/models/timeoutable.rb +43 -0
  53. data/lib/devise/models/token_authenticatable.rb +72 -0
  54. data/lib/devise/models/trackable.rb +30 -0
  55. data/lib/devise/models/validatable.rb +64 -0
  56. data/lib/devise/modules.rb +30 -0
  57. data/lib/devise/oauth.rb +41 -0
  58. data/lib/devise/oauth/config.rb +33 -0
  59. data/lib/devise/oauth/helpers.rb +18 -0
  60. data/lib/devise/oauth/internal_helpers.rb +182 -0
  61. data/lib/devise/oauth/test_helpers.rb +29 -0
  62. data/lib/devise/oauth/url_helpers.rb +35 -0
  63. data/lib/devise/orm/active_record.rb +38 -0
  64. data/lib/devise/orm/mongoid.rb +31 -0
  65. data/lib/devise/path_checker.rb +18 -0
  66. data/lib/devise/rails.rb +68 -0
  67. data/lib/devise/rails/routes.rb +260 -0
  68. data/lib/devise/rails/warden_compat.rb +41 -0
  69. data/lib/devise/schema.rb +96 -0
  70. data/lib/devise/strategies/authenticatable.rb +150 -0
  71. data/lib/devise/strategies/base.rb +15 -0
  72. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  73. data/lib/devise/strategies/rememberable.rb +51 -0
  74. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  75. data/lib/devise/test_helpers.rb +100 -0
  76. data/lib/devise/version.rb +3 -0
  77. data/lib/generators/active_record/devise_generator.rb +28 -0
  78. data/lib/generators/active_record/templates/migration.rb +30 -0
  79. data/lib/generators/devise/devise_generator.rb +17 -0
  80. data/lib/generators/devise/install_generator.rb +24 -0
  81. data/lib/generators/devise/orm_helpers.rb +24 -0
  82. data/lib/generators/devise/views_generator.rb +63 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/lib/generators/templates/README +25 -0
  85. data/lib/generators/templates/devise.rb +168 -0
  86. data/test/controllers/helpers_test.rb +205 -0
  87. data/test/controllers/internal_helpers_test.rb +56 -0
  88. data/test/controllers/url_helpers_test.rb +59 -0
  89. data/test/devise_test.rb +65 -0
  90. data/test/encryptors_test.rb +30 -0
  91. data/test/failure_app_test.rb +148 -0
  92. data/test/generators/generators_test_helper.rb +4 -0
  93. data/test/generators/install_generator_test.rb +14 -0
  94. data/test/generators/views_generator_test.rb +37 -0
  95. data/test/integration/authenticatable_test.rb +424 -0
  96. data/test/integration/confirmable_test.rb +104 -0
  97. data/test/integration/database_authenticatable_test.rb +38 -0
  98. data/test/integration/http_authenticatable_test.rb +64 -0
  99. data/test/integration/lockable_test.rb +109 -0
  100. data/test/integration/oauthable_test.rb +244 -0
  101. data/test/integration/recoverable_test.rb +134 -0
  102. data/test/integration/registerable_test.rb +180 -0
  103. data/test/integration/rememberable_test.rb +179 -0
  104. data/test/integration/timeoutable_test.rb +89 -0
  105. data/test/integration/token_authenticatable_test.rb +99 -0
  106. data/test/integration/trackable_test.rb +64 -0
  107. data/test/mailers/confirmation_instructions_test.rb +84 -0
  108. data/test/mailers/reset_password_instructions_test.rb +72 -0
  109. data/test/mailers/unlock_instructions_test.rb +66 -0
  110. data/test/mapping_test.rb +95 -0
  111. data/test/models/confirmable_test.rb +221 -0
  112. data/test/models/database_authenticatable_test.rb +82 -0
  113. data/test/models/encryptable_test.rb +65 -0
  114. data/test/models/lockable_test.rb +204 -0
  115. data/test/models/oauthable_test.rb +21 -0
  116. data/test/models/recoverable_test.rb +155 -0
  117. data/test/models/rememberable_test.rb +271 -0
  118. data/test/models/timeoutable_test.rb +28 -0
  119. data/test/models/token_authenticatable_test.rb +37 -0
  120. data/test/models/trackable_test.rb +5 -0
  121. data/test/models/validatable_test.rb +99 -0
  122. data/test/models_test.rb +77 -0
  123. data/test/oauth/config_test.rb +44 -0
  124. data/test/oauth/url_helpers_test.rb +47 -0
  125. data/test/orm/active_record.rb +9 -0
  126. data/test/orm/mongoid.rb +10 -0
  127. data/test/rails_app/app/active_record/admin.rb +6 -0
  128. data/test/rails_app/app/active_record/shim.rb +2 -0
  129. data/test/rails_app/app/active_record/user.rb +8 -0
  130. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  131. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  132. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  133. data/test/rails_app/app/controllers/home_controller.rb +12 -0
  134. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  135. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  136. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  137. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  138. data/test/rails_app/app/mongoid/admin.rb +9 -0
  139. data/test/rails_app/app/mongoid/shim.rb +24 -0
  140. data/test/rails_app/app/mongoid/user.rb +10 -0
  141. data/test/rails_app/config/application.rb +35 -0
  142. data/test/rails_app/config/boot.rb +13 -0
  143. data/test/rails_app/config/environment.rb +5 -0
  144. data/test/rails_app/config/environments/development.rb +19 -0
  145. data/test/rails_app/config/environments/production.rb +33 -0
  146. data/test/rails_app/config/environments/test.rb +33 -0
  147. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  148. data/test/rails_app/config/initializers/devise.rb +172 -0
  149. data/test/rails_app/config/initializers/inflections.rb +2 -0
  150. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  151. data/test/rails_app/config/routes.rb +54 -0
  152. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  153. data/test/rails_app/db/schema.rb +52 -0
  154. data/test/rails_app/lib/shared_admin.rb +9 -0
  155. data/test/rails_app/lib/shared_user.rb +48 -0
  156. data/test/routes_test.rb +189 -0
  157. data/test/support/assertions.rb +24 -0
  158. data/test/support/helpers.rb +60 -0
  159. data/test/support/integration.rb +88 -0
  160. data/test/support/webrat/integrations/rails.rb +24 -0
  161. data/test/test_helper.rb +23 -0
  162. data/test/test_helpers_test.rb +101 -0
  163. metadata +357 -0
@@ -0,0 +1,95 @@
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 'sign_out_via defaults to :get' do
25
+ assert_equal :get, Devise.mappings[:user].sign_out_via
26
+ end
27
+
28
+ test 'allows custom sign_out_via to be given' do
29
+ assert_equal :delete, Devise.mappings[:sign_out_via_delete].sign_out_via
30
+ assert_equal :post, Devise.mappings[:sign_out_via_post].sign_out_via
31
+ assert_equal [:delete, :post], Devise.mappings[:sign_out_via_delete_or_post].sign_out_via
32
+ end
33
+
34
+ test 'allows custom singular to be given' do
35
+ assert_equal "accounts", Devise.mappings[:manager].path
36
+ end
37
+
38
+ test 'has strategies depending on the model declaration' do
39
+ assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
40
+ assert_equal [:rememberable, :database_authenticatable], Devise.mappings[:admin].strategies
41
+ end
42
+
43
+ test 'find scope for a given object' do
44
+ assert_equal :user, Devise::Mapping.find_scope!(User)
45
+ assert_equal :user, Devise::Mapping.find_scope!(:user)
46
+ assert_equal :user, Devise::Mapping.find_scope!(User.new)
47
+ end
48
+
49
+ test 'find scope works with single table inheritance' do
50
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User))
51
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User).new)
52
+ end
53
+
54
+ test 'find scope raises an error if cannot be found' do
55
+ assert_raise RuntimeError do
56
+ Devise::Mapping.find_scope!(String)
57
+ end
58
+ end
59
+
60
+ test 'return default path names' do
61
+ mapping = Devise.mappings[:user]
62
+ assert_equal 'sign_in', mapping.path_names[:sign_in]
63
+ assert_equal 'sign_out', mapping.path_names[:sign_out]
64
+ assert_equal 'password', mapping.path_names[:password]
65
+ assert_equal 'confirmation', mapping.path_names[:confirmation]
66
+ assert_equal 'sign_up', mapping.path_names[:sign_up]
67
+ assert_equal 'unlock', mapping.path_names[:unlock]
68
+ end
69
+
70
+ test 'allow custom path names to be given' do
71
+ mapping = Devise.mappings[:manager]
72
+ assert_equal 'login', mapping.path_names[:sign_in]
73
+ assert_equal 'logout', mapping.path_names[:sign_out]
74
+ assert_equal 'secret', mapping.path_names[:password]
75
+ assert_equal 'verification', mapping.path_names[:confirmation]
76
+ assert_equal 'register', mapping.path_names[:sign_up]
77
+ assert_equal 'unblock', mapping.path_names[:unlock]
78
+ end
79
+
80
+ test 'magic predicates' do
81
+ mapping = Devise.mappings[:user]
82
+ assert mapping.authenticatable?
83
+ assert mapping.confirmable?
84
+ assert mapping.recoverable?
85
+ assert mapping.rememberable?
86
+ assert mapping.registerable?
87
+
88
+ mapping = Devise.mappings[:admin]
89
+ assert mapping.authenticatable?
90
+ assert mapping.recoverable?
91
+ assert mapping.lockable?
92
+ assert_not mapping.confirmable?
93
+ assert_not mapping.oauthable?
94
+ end
95
+ 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, please try signing in", 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, please try signing in", 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, please try signing in', 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,82 @@
1
+ require 'test_helper'
2
+ require 'digest/sha1'
3
+
4
+ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
5
+ test 'should respond to password and password confirmation' do
6
+ user = new_user
7
+ assert user.respond_to?(:password)
8
+ assert user.respond_to?(:password_confirmation)
9
+ end
10
+
11
+ test 'should generate encrypted password while setting password' do
12
+ user = new_user
13
+ assert_present user.encrypted_password
14
+ end
15
+
16
+ test 'should not generate encrypted password if password is blank' do
17
+ assert_blank new_user(:password => nil).encrypted_password
18
+ assert_blank new_user(:password => '').encrypted_password
19
+ end
20
+
21
+ test 'should encrypt password again if password has changed' do
22
+ user = create_user
23
+ encrypted_password = user.encrypted_password
24
+ user.password = user.password_confirmation = 'new_password'
25
+ user.save!
26
+ assert_not_equal encrypted_password, user.encrypted_password
27
+ end
28
+
29
+ test 'should test for a valid password' do
30
+ user = create_user
31
+ assert user.valid_password?('123456')
32
+ assert_not user.valid_password?('654321')
33
+ end
34
+
35
+ test 'should respond to current password' do
36
+ assert new_user.respond_to?(:current_password)
37
+ end
38
+
39
+ test 'should update password with valid current password' do
40
+ user = create_user
41
+ assert user.update_with_password(:current_password => '123456',
42
+ :password => 'pass321', :password_confirmation => 'pass321')
43
+ assert user.reload.valid_password?('pass321')
44
+ end
45
+
46
+ test 'should add an error to current password when it is invalid' do
47
+ user = create_user
48
+ assert_not user.update_with_password(:current_password => 'other',
49
+ :password => 'pass321', :password_confirmation => 'pass321')
50
+ assert user.reload.valid_password?('123456')
51
+ assert_match "is invalid", user.errors[:current_password].join
52
+ end
53
+
54
+ test 'should add an error to current password when it is blank' do
55
+ user = create_user
56
+ assert_not user.update_with_password(:password => 'pass321',
57
+ :password_confirmation => 'pass321')
58
+ assert user.reload.valid_password?('123456')
59
+ assert_match "can't be blank", user.errors[:current_password].join
60
+ end
61
+
62
+ test 'should ignore password and its confirmation if they are blank' do
63
+ user = create_user
64
+ assert user.update_with_password(:current_password => '123456', :email => "new@email.com")
65
+ assert_equal "new@email.com", user.email
66
+ end
67
+
68
+ test 'should not update password with invalid confirmation' do
69
+ user = create_user
70
+ assert_not user.update_with_password(:current_password => '123456',
71
+ :password => 'pass321', :password_confirmation => 'other')
72
+ assert user.reload.valid_password?('123456')
73
+ end
74
+
75
+ test 'should clean up password fields on failure' do
76
+ user = create_user
77
+ assert_not user.update_with_password(:current_password => '123456',
78
+ :password => 'pass321', :password_confirmation => 'other')
79
+ assert user.password.blank?
80
+ assert user.password_confirmation.blank?
81
+ end
82
+ 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