deviseOne 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.travis.yml +38 -0
  4. data/.yardopts +9 -0
  5. data/CHANGELOG.md +1117 -0
  6. data/CONTRIBUTING.md +14 -0
  7. data/Gemfile +29 -0
  8. data/Gemfile.lock +199 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.md +529 -0
  11. data/Rakefile +35 -0
  12. data/app/controllers/devise/confirmations_controller.rb +47 -0
  13. data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
  14. data/app/controllers/devise/passwords_controller.rb +71 -0
  15. data/app/controllers/devise/registrations_controller.rb +143 -0
  16. data/app/controllers/devise/sessions_controller.rb +166 -0
  17. data/app/controllers/devise/unlocks_controller.rb +46 -0
  18. data/app/controllers/devise_controller.rb +193 -0
  19. data/app/helpers/devise_helper.rb +25 -0
  20. data/app/mailers/devise/mailer.rb +20 -0
  21. data/app/views/devise/confirmations/new.html.erb +16 -0
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  23. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  24. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  25. data/app/views/devise/passwords/edit.html.erb +25 -0
  26. data/app/views/devise/passwords/new.html.erb +16 -0
  27. data/app/views/devise/registrations/edit.html.erb +39 -0
  28. data/app/views/devise/registrations/new.html.erb +29 -0
  29. data/app/views/devise/sessions/new.html.erb +27 -0
  30. data/app/views/devise/shared/_links.html.erb +21 -0
  31. data/app/views/devise/unlocks/new.html.erb +16 -0
  32. data/config/locales/en.yml +70 -0
  33. data/devise.gemspec +33 -0
  34. data/devise.png +0 -0
  35. data/gemfiles/Gemfile.rails-3.2-stable +29 -0
  36. data/gemfiles/Gemfile.rails-3.2-stable.lock +169 -0
  37. data/gemfiles/Gemfile.rails-4.0-stable +29 -0
  38. data/gemfiles/Gemfile.rails-4.0-stable.lock +165 -0
  39. data/gemfiles/Gemfile.rails-4.1-stable +29 -0
  40. data/gemfiles/Gemfile.rails-4.1-stable.lock +170 -0
  41. data/lib/devise.rb +499 -0
  42. data/lib/devise/controllers/helpers.rb +284 -0
  43. data/lib/devise/controllers/rememberable.rb +47 -0
  44. data/lib/devise/controllers/scoped_views.rb +17 -0
  45. data/lib/devise/controllers/sign_in_out.rb +102 -0
  46. data/lib/devise/controllers/store_location.rb +58 -0
  47. data/lib/devise/controllers/url_helpers.rb +69 -0
  48. data/lib/devise/delegator.rb +16 -0
  49. data/lib/devise/failure_app.rb +212 -0
  50. data/lib/devise/hooks/activatable.rb +10 -0
  51. data/lib/devise/hooks/csrf_cleaner.rb +7 -0
  52. data/lib/devise/hooks/forgetable.rb +9 -0
  53. data/lib/devise/hooks/lockable.rb +7 -0
  54. data/lib/devise/hooks/proxy.rb +21 -0
  55. data/lib/devise/hooks/rememberable.rb +7 -0
  56. data/lib/devise/hooks/timeoutable.rb +35 -0
  57. data/lib/devise/hooks/trackable.rb +9 -0
  58. data/lib/devise/mailers/helpers.rb +90 -0
  59. data/lib/devise/mapping.rb +175 -0
  60. data/lib/devise/models.rb +119 -0
  61. data/lib/devise/models/authenticatable.rb +290 -0
  62. data/lib/devise/models/confirmable.rb +305 -0
  63. data/lib/devise/models/database_authenticatable.rb +164 -0
  64. data/lib/devise/models/lockable.rb +196 -0
  65. data/lib/devise/models/omniauthable.rb +27 -0
  66. data/lib/devise/models/recoverable.rb +157 -0
  67. data/lib/devise/models/registerable.rb +25 -0
  68. data/lib/devise/models/rememberable.rb +142 -0
  69. data/lib/devise/models/timeoutable.rb +49 -0
  70. data/lib/devise/models/trackable.rb +38 -0
  71. data/lib/devise/models/validatable.rb +66 -0
  72. data/lib/devise/modules.rb +28 -0
  73. data/lib/devise/omniauth.rb +28 -0
  74. data/lib/devise/omniauth/config.rb +45 -0
  75. data/lib/devise/omniauth/url_helpers.rb +18 -0
  76. data/lib/devise/orm/active_record.rb +3 -0
  77. data/lib/devise/orm/mongoid.rb +3 -0
  78. data/lib/devise/parameter_filter.rb +40 -0
  79. data/lib/devise/parameter_sanitizer.rb +99 -0
  80. data/lib/devise/rails.rb +56 -0
  81. data/lib/devise/rails/routes.rb +495 -0
  82. data/lib/devise/rails/warden_compat.rb +22 -0
  83. data/lib/devise/strategies/authenticatable.rb +173 -0
  84. data/lib/devise/strategies/base.rb +20 -0
  85. data/lib/devise/strategies/database_authenticatable.rb +24 -0
  86. data/lib/devise/strategies/rememberable.rb +59 -0
  87. data/lib/devise/test_helpers.rb +132 -0
  88. data/lib/devise/time_inflector.rb +14 -0
  89. data/lib/devise/token_generator.rb +70 -0
  90. data/lib/devise/version.rb +3 -0
  91. data/lib/generators/active_record/devise_generator.rb +91 -0
  92. data/lib/generators/active_record/templates/migration.rb +18 -0
  93. data/lib/generators/active_record/templates/migration_existing.rb +25 -0
  94. data/lib/generators/devise/controllers_generator.rb +44 -0
  95. data/lib/generators/devise/devise_generator.rb +26 -0
  96. data/lib/generators/devise/install_generator.rb +29 -0
  97. data/lib/generators/devise/orm_helpers.rb +51 -0
  98. data/lib/generators/devise/views_generator.rb +135 -0
  99. data/lib/generators/mongoid/devise_generator.rb +55 -0
  100. data/lib/generators/templates/README +35 -0
  101. data/lib/generators/templates/controllers/README +14 -0
  102. data/lib/generators/templates/controllers/confirmations_controller.rb +28 -0
  103. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +28 -0
  104. data/lib/generators/templates/controllers/passwords_controller.rb +32 -0
  105. data/lib/generators/templates/controllers/registrations_controller.rb +60 -0
  106. data/lib/generators/templates/controllers/sessions_controller.rb +25 -0
  107. data/lib/generators/templates/controllers/unlocks_controller.rb +28 -0
  108. data/lib/generators/templates/devise.rb +263 -0
  109. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  110. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  111. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  112. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +16 -0
  113. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  114. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  115. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +27 -0
  116. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  117. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  118. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +16 -0
  119. data/script/cached-bundle +49 -0
  120. data/script/s3-put +71 -0
  121. data/test/controllers/custom_registrations_controller_test.rb +35 -0
  122. data/test/controllers/custom_strategy_test.rb +62 -0
  123. data/test/controllers/helpers_test.rb +316 -0
  124. data/test/controllers/internal_helpers_test.rb +129 -0
  125. data/test/controllers/load_hooks_controller_test.rb +19 -0
  126. data/test/controllers/passwords_controller_test.rb +31 -0
  127. data/test/controllers/sessions_controller_test.rb +102 -0
  128. data/test/controllers/url_helpers_test.rb +65 -0
  129. data/test/delegator_test.rb +19 -0
  130. data/test/devise_test.rb +107 -0
  131. data/test/failure_app_test.rb +275 -0
  132. data/test/generators/active_record_generator_test.rb +109 -0
  133. data/test/generators/controllers_generator_test.rb +48 -0
  134. data/test/generators/devise_generator_test.rb +39 -0
  135. data/test/generators/install_generator_test.rb +13 -0
  136. data/test/generators/mongoid_generator_test.rb +23 -0
  137. data/test/generators/views_generator_test.rb +96 -0
  138. data/test/helpers/devise_helper_test.rb +49 -0
  139. data/test/integration/authenticatable_test.rb +731 -0
  140. data/test/integration/confirmable_test.rb +324 -0
  141. data/test/integration/database_authenticatable_test.rb +94 -0
  142. data/test/integration/http_authenticatable_test.rb +105 -0
  143. data/test/integration/lockable_test.rb +239 -0
  144. data/test/integration/omniauthable_test.rb +133 -0
  145. data/test/integration/recoverable_test.rb +334 -0
  146. data/test/integration/registerable_test.rb +361 -0
  147. data/test/integration/rememberable_test.rb +176 -0
  148. data/test/integration/timeoutable_test.rb +189 -0
  149. data/test/integration/trackable_test.rb +92 -0
  150. data/test/mailers/confirmation_instructions_test.rb +115 -0
  151. data/test/mailers/reset_password_instructions_test.rb +96 -0
  152. data/test/mailers/unlock_instructions_test.rb +91 -0
  153. data/test/mapping_test.rb +128 -0
  154. data/test/models/authenticatable_test.rb +23 -0
  155. data/test/models/confirmable_test.rb +461 -0
  156. data/test/models/database_authenticatable_test.rb +249 -0
  157. data/test/models/lockable_test.rb +328 -0
  158. data/test/models/omniauthable_test.rb +7 -0
  159. data/test/models/recoverable_test.rb +205 -0
  160. data/test/models/registerable_test.rb +7 -0
  161. data/test/models/rememberable_test.rb +198 -0
  162. data/test/models/serializable_test.rb +49 -0
  163. data/test/models/timeoutable_test.rb +51 -0
  164. data/test/models/trackable_test.rb +41 -0
  165. data/test/models/validatable_test.rb +127 -0
  166. data/test/models_test.rb +144 -0
  167. data/test/omniauth/config_test.rb +57 -0
  168. data/test/omniauth/url_helpers_test.rb +54 -0
  169. data/test/orm/active_record.rb +10 -0
  170. data/test/orm/mongoid.rb +13 -0
  171. data/test/parameter_sanitizer_test.rb +81 -0
  172. data/test/rails_app/Rakefile +6 -0
  173. data/test/rails_app/app/active_record/admin.rb +6 -0
  174. data/test/rails_app/app/active_record/shim.rb +2 -0
  175. data/test/rails_app/app/active_record/user.rb +6 -0
  176. data/test/rails_app/app/active_record/user_on_engine.rb +7 -0
  177. data/test/rails_app/app/active_record/user_on_main_app.rb +7 -0
  178. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  179. data/test/rails_app/app/controllers/admins_controller.rb +11 -0
  180. data/test/rails_app/app/controllers/application_controller.rb +12 -0
  181. data/test/rails_app/app/controllers/application_with_fake_engine.rb +30 -0
  182. data/test/rails_app/app/controllers/custom/registrations_controller.rb +21 -0
  183. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  184. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  185. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  186. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  187. data/test/rails_app/app/controllers/users_controller.rb +31 -0
  188. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  189. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +3 -0
  190. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  191. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +4 -0
  192. data/test/rails_app/app/mongoid/admin.rb +29 -0
  193. data/test/rails_app/app/mongoid/shim.rb +23 -0
  194. data/test/rails_app/app/mongoid/user.rb +39 -0
  195. data/test/rails_app/app/mongoid/user_on_engine.rb +39 -0
  196. data/test/rails_app/app/mongoid/user_on_main_app.rb +39 -0
  197. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  198. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  199. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  200. data/test/rails_app/app/views/home/index.html.erb +1 -0
  201. data/test/rails_app/app/views/home/join.html.erb +1 -0
  202. data/test/rails_app/app/views/home/private.html.erb +1 -0
  203. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  204. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  205. data/test/rails_app/app/views/users/edit_form.html.erb +1 -0
  206. data/test/rails_app/app/views/users/index.html.erb +1 -0
  207. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  208. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  209. data/test/rails_app/bin/bundle +3 -0
  210. data/test/rails_app/bin/rails +4 -0
  211. data/test/rails_app/bin/rake +4 -0
  212. data/test/rails_app/config.ru +4 -0
  213. data/test/rails_app/config/application.rb +40 -0
  214. data/test/rails_app/config/boot.rb +14 -0
  215. data/test/rails_app/config/database.yml +18 -0
  216. data/test/rails_app/config/environment.rb +5 -0
  217. data/test/rails_app/config/environments/development.rb +30 -0
  218. data/test/rails_app/config/environments/production.rb +80 -0
  219. data/test/rails_app/config/environments/test.rb +36 -0
  220. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  221. data/test/rails_app/config/initializers/devise.rb +180 -0
  222. data/test/rails_app/config/initializers/inflections.rb +2 -0
  223. data/test/rails_app/config/initializers/secret_token.rb +8 -0
  224. data/test/rails_app/config/initializers/session_store.rb +1 -0
  225. data/test/rails_app/config/routes.rb +122 -0
  226. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  227. data/test/rails_app/db/schema.rb +55 -0
  228. data/test/rails_app/lib/shared_admin.rb +17 -0
  229. data/test/rails_app/lib/shared_user.rb +29 -0
  230. data/test/rails_app/lib/shared_user_without_omniauth.rb +13 -0
  231. data/test/rails_app/public/404.html +26 -0
  232. data/test/rails_app/public/422.html +26 -0
  233. data/test/rails_app/public/500.html +26 -0
  234. data/test/rails_app/public/favicon.ico +0 -0
  235. data/test/routes_test.rb +264 -0
  236. data/test/support/action_controller/record_identifier.rb +10 -0
  237. data/test/support/assertions.rb +39 -0
  238. data/test/support/helpers.rb +73 -0
  239. data/test/support/integration.rb +92 -0
  240. data/test/support/locale/en.yml +8 -0
  241. data/test/support/mongoid.yml +6 -0
  242. data/test/support/webrat/integrations/rails.rb +24 -0
  243. data/test/test_helper.rb +34 -0
  244. data/test/test_helpers_test.rb +163 -0
  245. data/test/test_models.rb +33 -0
  246. metadata +531 -0
@@ -0,0 +1,249 @@
1
+ require 'test_helper'
2
+ require 'test_models'
3
+ require 'digest/sha1'
4
+
5
+ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
6
+ test 'should downcase case insensitive keys when saving' do
7
+ # case_insensitive_keys is set to :email by default.
8
+ email = 'Foo@Bar.com'
9
+ user = new_user(email: email)
10
+
11
+ assert_equal email, user.email
12
+ user.save!
13
+ assert_equal email.downcase, user.email
14
+ end
15
+
16
+ test 'should downcase case insensitive keys that refer to virtual attributes when saving' do
17
+ email = 'Foo@Bar1.com'
18
+ confirmation = 'Foo@Bar1.com'
19
+ attributes = valid_attributes(email: email, email_confirmation: confirmation)
20
+ user = UserWithVirtualAttributes.new(attributes)
21
+
22
+ assert_equal confirmation, user.email_confirmation
23
+ user.save!
24
+ assert_equal confirmation.downcase, user.email_confirmation
25
+ end
26
+
27
+ test 'should not mutate value assigned to case insensitive key' do
28
+ email = 'Foo@Bar.com'
29
+ original_email = email.dup
30
+ user = new_user(email: email)
31
+
32
+ user.save!
33
+ assert_equal original_email, email
34
+ end
35
+
36
+ test 'should remove whitespace from strip whitespace keys when saving' do
37
+ # strip_whitespace_keys is set to :email by default.
38
+ email = ' foo@bar.com '
39
+ user = new_user(email: email)
40
+
41
+ assert_equal email, user.email
42
+ user.save!
43
+ assert_equal email.strip, user.email
44
+ end
45
+
46
+ test 'should not mutate value assigned to string whitespace key' do
47
+ email = ' foo@bar.com '
48
+ original_email = email.dup
49
+ user = new_user(email: email)
50
+
51
+ user.save!
52
+ assert_equal original_email, email
53
+ end
54
+
55
+ test "doesn't throw exception when globally configured strip_whitespace_keys are not present on a model" do
56
+ swap Devise, strip_whitespace_keys: [:fake_key] do
57
+ assert_nothing_raised { create_user }
58
+ end
59
+ end
60
+
61
+ test "doesn't throw exception when globally configured case_insensitive_keys are not present on a model" do
62
+ swap Devise, case_insensitive_keys: [:fake_key] do
63
+ assert_nothing_raised { create_user }
64
+ end
65
+ end
66
+
67
+ test "param filter should not convert booleans and integer to strings" do
68
+ conditions = { "login" => "foo@bar.com", "bool1" => true, "bool2" => false, "fixnum" => 123, "will_be_converted" => (1..10) }
69
+ conditions = Devise::ParameterFilter.new([], []).filter(conditions)
70
+ assert_equal( { "login" => "foo@bar.com", "bool1" => "true", "bool2" => "false", "fixnum" => "123", "will_be_converted" => "1..10" }, conditions)
71
+ end
72
+
73
+ test 'param filter should filter case_insensitive_keys as insensitive' do
74
+ conditions = {'insensitive' => 'insensitive_VAL', 'sensitive' => 'sensitive_VAL'}
75
+ conditions = Devise::ParameterFilter.new(['insensitive'], []).filter(conditions)
76
+ assert_equal( {'insensitive' => 'insensitive_val', 'sensitive' => 'sensitive_VAL'}, conditions )
77
+ end
78
+
79
+ test 'param filter should filter strip_whitespace_keys stripping whitespaces' do
80
+ conditions = {'strip_whitespace' => ' strip_whitespace_val ', 'do_not_strip_whitespace' => ' do_not_strip_whitespace_val '}
81
+ conditions = Devise::ParameterFilter.new([], ['strip_whitespace']).filter(conditions)
82
+ assert_equal( {'strip_whitespace' => 'strip_whitespace_val', 'do_not_strip_whitespace' => ' do_not_strip_whitespace_val '}, conditions )
83
+ end
84
+
85
+ test 'should respond to password and password confirmation' do
86
+ user = new_user
87
+ assert user.respond_to?(:password)
88
+ assert user.respond_to?(:password_confirmation)
89
+ end
90
+
91
+ test 'should generate encrypted password while setting password' do
92
+ user = new_user
93
+ assert_present user.encrypted_password
94
+ end
95
+
96
+ test 'should support custom encryption methods' do
97
+ user = UserWithCustomEncryption.new(password: '654321')
98
+ assert_equal user.encrypted_password, '123456'
99
+ end
100
+
101
+ test 'allow authenticatable_salt to work even with nil encrypted password' do
102
+ user = User.new
103
+ user.encrypted_password = nil
104
+ assert_nil user.authenticatable_salt
105
+ end
106
+
107
+ test 'should not generate encrypted password if password is blank' do
108
+ assert_blank new_user(password: nil).encrypted_password
109
+ assert_blank new_user(password: '').encrypted_password
110
+ end
111
+
112
+ test 'should encrypt password again if password has changed' do
113
+ user = create_user
114
+ encrypted_password = user.encrypted_password
115
+ user.password = user.password_confirmation = 'new_password'
116
+ user.save!
117
+ assert_not_equal encrypted_password, user.encrypted_password
118
+ end
119
+
120
+ test 'should test for a valid password' do
121
+ user = create_user
122
+ assert user.valid_password?('12345678')
123
+ assert_not user.valid_password?('654321')
124
+ end
125
+
126
+ test 'should not raise error with an empty password' do
127
+ user = create_user
128
+ user.encrypted_password = ''
129
+ assert_nothing_raised { user.valid_password?('12345678') }
130
+ end
131
+
132
+ test 'should be an invalid password if the user has an empty password' do
133
+ user = create_user
134
+ user.encrypted_password = ''
135
+ assert_not user.valid_password?('654321')
136
+ end
137
+
138
+ test 'should respond to current password' do
139
+ assert new_user.respond_to?(:current_password)
140
+ end
141
+
142
+ test 'should update password with valid current password' do
143
+ user = create_user
144
+ assert user.update_with_password(current_password: '12345678',
145
+ password: 'pass4321', password_confirmation: 'pass4321')
146
+ assert user.reload.valid_password?('pass4321')
147
+ end
148
+
149
+ test 'should add an error to current password when it is invalid' do
150
+ user = create_user
151
+ assert_not user.update_with_password(current_password: 'other',
152
+ password: 'pass4321', password_confirmation: 'pass4321')
153
+ assert user.reload.valid_password?('12345678')
154
+ assert_match "is invalid", user.errors[:current_password].join
155
+ end
156
+
157
+ test 'should add an error to current password when it is blank' do
158
+ user = create_user
159
+ assert_not user.update_with_password(password: 'pass4321',
160
+ password_confirmation: 'pass4321')
161
+ assert user.reload.valid_password?('12345678')
162
+ assert_match "can't be blank", user.errors[:current_password].join
163
+ end
164
+
165
+ test 'should run validations even when current password is invalid or blank' do
166
+ user = UserWithValidation.create!(valid_attributes)
167
+ user.save
168
+ assert user.persisted?
169
+ assert_not user.update_with_password(username: "")
170
+ assert_match "usertest", user.reload.username
171
+ assert_match "can't be blank", user.errors[:username].join
172
+ end
173
+
174
+ test 'should ignore password and its confirmation if they are blank' do
175
+ user = create_user
176
+ assert user.update_with_password(current_password: '12345678', email: "new@example.com")
177
+ assert_equal "new@example.com", user.email
178
+ end
179
+
180
+ test 'should not update password with invalid confirmation' do
181
+ user = create_user
182
+ assert_not user.update_with_password(current_password: '12345678',
183
+ password: 'pass4321', password_confirmation: 'other')
184
+ assert user.reload.valid_password?('12345678')
185
+ end
186
+
187
+ test 'should clean up password fields on failure' do
188
+ user = create_user
189
+ assert_not user.update_with_password(current_password: '12345678',
190
+ password: 'pass4321', password_confirmation: 'other')
191
+ assert user.password.blank?
192
+ assert user.password_confirmation.blank?
193
+ end
194
+
195
+ test 'should update the user without password' do
196
+ user = create_user
197
+ user.update_without_password(email: 'new@example.com')
198
+ assert_equal 'new@example.com', user.email
199
+ end
200
+
201
+ test 'should not update password without password' do
202
+ user = create_user
203
+ user.update_without_password(password: 'pass4321', password_confirmation: 'pass4321')
204
+ assert !user.reload.valid_password?('pass4321')
205
+ assert user.valid_password?('12345678')
206
+ end
207
+
208
+ test 'should destroy user if current password is valid' do
209
+ user = create_user
210
+ assert user.destroy_with_password('12345678')
211
+ assert !user.persisted?
212
+ end
213
+
214
+ test 'should not destroy user with invalid password' do
215
+ user = create_user
216
+ assert_not user.destroy_with_password('other')
217
+ assert user.persisted?
218
+ assert_match "is invalid", user.errors[:current_password].join
219
+ end
220
+
221
+ test 'should not destroy user with blank password' do
222
+ user = create_user
223
+ assert_not user.destroy_with_password(nil)
224
+ assert user.persisted?
225
+ assert_match "can't be blank", user.errors[:current_password].join
226
+ end
227
+
228
+ test 'downcase_keys with validation' do
229
+ User.create(email: "HEllO@example.com", password: "123456")
230
+ user = User.create(email: "HEllO@example.com", password: "123456")
231
+ assert !user.valid?
232
+ end
233
+
234
+ test 'required_fields should be encryptable_password and the email field by default' do
235
+ assert_same_content Devise::Models::DatabaseAuthenticatable.required_fields(User), [
236
+ :email,
237
+ :encrypted_password
238
+ ]
239
+ end
240
+
241
+ test 'required_fields should be encryptable_password and the login when the login is on authentication_keys' do
242
+ swap Devise, authentication_keys: [:login] do
243
+ assert_same_content Devise::Models::DatabaseAuthenticatable.required_fields(User), [
244
+ :encrypted_password,
245
+ :login
246
+ ]
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,328 @@
1
+ require 'test_helper'
2
+
3
+ class LockableTest < ActiveSupport::TestCase
4
+ def setup
5
+ setup_mailer
6
+ end
7
+
8
+ test "should respect maximum attempts configuration" do
9
+ user = create_user
10
+ user.confirm!
11
+ swap Devise, maximum_attempts: 2 do
12
+ 2.times { user.valid_for_authentication?{ false } }
13
+ assert user.reload.access_locked?
14
+ end
15
+ end
16
+
17
+ test "should increment failed_attempts on successfull validation if the user is already locked" do
18
+ user = create_user
19
+ user.confirm!
20
+
21
+ swap Devise, maximum_attempts: 2 do
22
+ 2.times { user.valid_for_authentication?{ false } }
23
+ assert user.reload.access_locked?
24
+ end
25
+
26
+ user.valid_for_authentication?{ true }
27
+ assert_equal 3, user.reload.failed_attempts
28
+ end
29
+
30
+ test "should not touch failed_attempts if lock_strategy is none" do
31
+ user = create_user
32
+ user.confirm!
33
+ swap Devise, lock_strategy: :none, maximum_attempts: 2 do
34
+ 3.times { user.valid_for_authentication?{ false } }
35
+ assert !user.access_locked?
36
+ assert_equal 0, user.failed_attempts
37
+ end
38
+ end
39
+
40
+ test 'should be valid for authentication with a unlocked user' do
41
+ user = create_user
42
+ user.lock_access!
43
+ user.unlock_access!
44
+ assert user.valid_for_authentication?{ true }
45
+ end
46
+
47
+ test "should verify whether a user is locked or not" do
48
+ user = create_user
49
+ assert_not user.access_locked?
50
+ user.lock_access!
51
+ assert user.access_locked?
52
+ end
53
+
54
+ test "active_for_authentication? should be the opposite of locked?" do
55
+ user = create_user
56
+ user.confirm!
57
+ assert user.active_for_authentication?
58
+ user.lock_access!
59
+ assert_not user.active_for_authentication?
60
+ end
61
+
62
+ test "should unlock a user by cleaning locked_at, failed_attempts and unlock_token" do
63
+ user = create_user
64
+ user.lock_access!
65
+ assert_not_nil user.reload.locked_at
66
+ assert_not_nil user.reload.unlock_token
67
+
68
+ user.unlock_access!
69
+ assert_nil user.reload.locked_at
70
+ assert_nil user.reload.unlock_token
71
+ assert_equal 0, user.reload.failed_attempts
72
+ end
73
+
74
+ test "new user should not be locked and should have zero failed_attempts" do
75
+ assert_not new_user.access_locked?
76
+ assert_equal 0, create_user.failed_attempts
77
+ end
78
+
79
+ test "should unlock user after unlock_in period" do
80
+ swap Devise, unlock_in: 3.hours do
81
+ user = new_user
82
+ user.locked_at = 2.hours.ago
83
+ assert user.access_locked?
84
+
85
+ Devise.unlock_in = 1.hour
86
+ assert_not user.access_locked?
87
+ end
88
+ end
89
+
90
+ test "should not unlock in 'unlock_in' if :time unlock strategy is not set" do
91
+ swap Devise, unlock_strategy: :email do
92
+ user = new_user
93
+ user.locked_at = 2.hours.ago
94
+ assert user.access_locked?
95
+ end
96
+ end
97
+
98
+ test "should set unlock_token when locking" do
99
+ user = create_user
100
+ assert_nil user.unlock_token
101
+ user.lock_access!
102
+ assert_not_nil user.unlock_token
103
+ end
104
+
105
+ test "should never generate the same unlock token for different users" do
106
+ unlock_tokens = []
107
+ 3.times do
108
+ user = create_user
109
+ user.lock_access!
110
+ token = user.unlock_token
111
+ assert !unlock_tokens.include?(token)
112
+ unlock_tokens << token
113
+ end
114
+ end
115
+
116
+ test "should not generate unlock_token when :email is not an unlock strategy" do
117
+ swap Devise, unlock_strategy: :time do
118
+ user = create_user
119
+ user.lock_access!
120
+ assert_nil user.unlock_token
121
+ end
122
+ end
123
+
124
+ test "should send email with unlock instructions when :email is an unlock strategy" do
125
+ swap Devise, unlock_strategy: :email do
126
+ user = create_user
127
+ assert_email_sent do
128
+ user.lock_access!
129
+ end
130
+ end
131
+ end
132
+
133
+ test "doesn't send email when you pass option send_instructions to false" do
134
+ swap Devise, unlock_strategy: :email do
135
+ user = create_user
136
+ assert_email_not_sent do
137
+ user.lock_access! send_instructions: false
138
+ end
139
+ end
140
+ end
141
+
142
+ test "sends email when you pass options other than send_instructions" do
143
+ swap Devise, unlock_strategy: :email do
144
+ user = create_user
145
+ assert_email_sent do
146
+ user.lock_access! foo: :bar, bar: :foo
147
+ end
148
+ end
149
+ end
150
+
151
+ test "should not send email with unlock instructions when :email is not an unlock strategy" do
152
+ swap Devise, unlock_strategy: :time do
153
+ user = create_user
154
+ assert_email_not_sent do
155
+ user.lock_access!
156
+ end
157
+ end
158
+ end
159
+
160
+ test 'should find and unlock a user automatically based on raw token' do
161
+ user = create_user
162
+ raw = user.send_unlock_instructions
163
+ locked_user = User.unlock_access_by_token(raw)
164
+ assert_equal locked_user, user
165
+ assert_not user.reload.access_locked?
166
+ end
167
+
168
+ test 'should return a new record with errors when a invalid token is given' do
169
+ locked_user = User.unlock_access_by_token('invalid_token')
170
+ assert_not locked_user.persisted?
171
+ assert_equal "is invalid", locked_user.errors[:unlock_token].join
172
+ end
173
+
174
+ test 'should return a new record with errors when a blank token is given' do
175
+ locked_user = User.unlock_access_by_token('')
176
+ assert_not locked_user.persisted?
177
+ assert_equal "can't be blank", locked_user.errors[:unlock_token].join
178
+ end
179
+
180
+ test 'should find a user to send unlock instructions' do
181
+ user = create_user
182
+ user.lock_access!
183
+ unlock_user = User.send_unlock_instructions(email: user.email)
184
+ assert_equal unlock_user, user
185
+ end
186
+
187
+ test 'should return a new user if no email was found' do
188
+ unlock_user = User.send_unlock_instructions(email: "invalid@example.com")
189
+ assert_not unlock_user.persisted?
190
+ end
191
+
192
+ test 'should add error to new user email if no email was found' do
193
+ unlock_user = User.send_unlock_instructions(email: "invalid@example.com")
194
+ assert_equal 'not found', unlock_user.errors[:email].join
195
+ end
196
+
197
+ test 'should find a user to send unlock instructions by authentication_keys' do
198
+ swap Devise, authentication_keys: [:username, :email] do
199
+ user = create_user
200
+ unlock_user = User.send_unlock_instructions(email: user.email, username: user.username)
201
+ assert_equal unlock_user, user
202
+ end
203
+ end
204
+
205
+ test 'should require all unlock_keys' do
206
+ swap Devise, unlock_keys: [:username, :email] do
207
+ user = create_user
208
+ unlock_user = User.send_unlock_instructions(email: user.email)
209
+ assert_not unlock_user.persisted?
210
+ assert_equal "can't be blank", unlock_user.errors[:username].join
211
+ end
212
+ end
213
+
214
+ test 'should not be able to send instructions if the user is not locked' do
215
+ user = create_user
216
+ assert_not user.resend_unlock_instructions
217
+ assert_not user.access_locked?
218
+ assert_equal 'was not locked', user.errors[:email].join
219
+ end
220
+
221
+ test 'should not be able to send instructions if the user if not locked and have username as unlock key' do
222
+ swap Devise, unlock_keys: [:username] do
223
+ user = create_user
224
+ assert_not user.resend_unlock_instructions
225
+ assert_not user.access_locked?
226
+ assert_equal 'was not locked', user.errors[:username].join
227
+ end
228
+ end
229
+
230
+ test 'should unlock account if lock has expired and increase attempts on failure' do
231
+ swap Devise, unlock_in: 1.minute do
232
+ user = create_user
233
+ user.confirm!
234
+
235
+ user.failed_attempts = 2
236
+ user.locked_at = 2.minutes.ago
237
+
238
+ user.valid_for_authentication? { false }
239
+ assert_equal 1, user.failed_attempts
240
+ end
241
+ end
242
+
243
+ test 'should unlock account if lock has expired on success' do
244
+ swap Devise, unlock_in: 1.minute do
245
+ user = create_user
246
+ user.confirm!
247
+
248
+ user.failed_attempts = 2
249
+ user.locked_at = 2.minutes.ago
250
+
251
+ user.valid_for_authentication? { true }
252
+ assert_equal 0, user.failed_attempts
253
+ assert_nil user.locked_at
254
+ end
255
+ end
256
+
257
+ test 'required_fields should contain the all the fields when all the strategies are enabled' do
258
+ swap Devise, unlock_strategy: :both do
259
+ swap Devise, lock_strategy: :failed_attempts do
260
+ assert_same_content Devise::Models::Lockable.required_fields(User), [
261
+ :failed_attempts,
262
+ :locked_at,
263
+ :unlock_token
264
+ ]
265
+ end
266
+ end
267
+ end
268
+
269
+ test 'required_fields should contain only failed_attempts and locked_at when the strategies are time and failed_attempts are enabled' do
270
+ swap Devise, unlock_strategy: :time do
271
+ swap Devise, lock_strategy: :failed_attempts do
272
+ assert_same_content Devise::Models::Lockable.required_fields(User), [
273
+ :failed_attempts,
274
+ :locked_at
275
+ ]
276
+ end
277
+ end
278
+ end
279
+
280
+ test 'required_fields should contain only failed_attempts and unlock_token when the strategies are token and failed_attempts are enabled' do
281
+ swap Devise, unlock_strategy: :email do
282
+ swap Devise, lock_strategy: :failed_attempts do
283
+ assert_same_content Devise::Models::Lockable.required_fields(User), [
284
+ :failed_attempts,
285
+ :unlock_token
286
+ ]
287
+ end
288
+ end
289
+ end
290
+
291
+ test 'should not return a locked unauthenticated message if in paranoid mode' do
292
+ swap Devise, paranoid: :true do
293
+ user = create_user
294
+ user.failed_attempts = Devise.maximum_attempts + 1
295
+ user.lock_access!
296
+
297
+ assert_equal :invalid, user.unauthenticated_message
298
+ end
299
+ end
300
+
301
+ test 'should return last attempt message if user made next-to-last attempt of password entering' do
302
+ swap Devise, last_attempt_warning: true, lock_strategy: :failed_attempts do
303
+ user = create_user
304
+ user.failed_attempts = Devise.maximum_attempts - 2
305
+ assert_equal :invalid, user.unauthenticated_message
306
+
307
+ user.failed_attempts = Devise.maximum_attempts - 1
308
+ assert_equal :last_attempt, user.unauthenticated_message
309
+
310
+ user.failed_attempts = Devise.maximum_attempts
311
+ assert_equal :locked, user.unauthenticated_message
312
+ end
313
+ end
314
+
315
+ test 'should not return last attempt message if last_attempt_warning is disabled' do
316
+ swap Devise, last_attempt_warning: false, lock_strategy: :failed_attempts do
317
+ user = create_user
318
+ user.failed_attempts = Devise.maximum_attempts - 1
319
+ assert_equal :invalid, user.unauthenticated_message
320
+ end
321
+ end
322
+
323
+ test 'should return locked message if user was programatically locked' do
324
+ user = create_user
325
+ user.lock_access!
326
+ assert_equal :locked, user.unauthenticated_message
327
+ end
328
+ end