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,7 @@
1
+ require 'test_helper'
2
+
3
+ class OmniauthableTest < ActiveSupport::TestCase
4
+ test 'required_fields should contain the fields that Devise uses' do
5
+ assert_same_content Devise::Models::Omniauthable.required_fields(User), []
6
+ end
7
+ end
@@ -0,0 +1,205 @@
1
+ require 'test_helper'
2
+
3
+ class RecoverableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should not generate reset password token after creating a record' do
10
+ assert_nil new_user.reset_password_token
11
+ end
12
+
13
+ test 'should never generate the same reset password token for different users' do
14
+ reset_password_tokens = []
15
+ 3.times do
16
+ user = create_user
17
+ user.send_reset_password_instructions
18
+ token = user.reset_password_token
19
+ assert !reset_password_tokens.include?(token)
20
+ reset_password_tokens << token
21
+ end
22
+ end
23
+
24
+ test 'should reset password and password confirmation from params' do
25
+ user = create_user
26
+ user.reset_password!('123456789', '987654321')
27
+ assert_equal '123456789', user.password
28
+ assert_equal '987654321', user.password_confirmation
29
+ end
30
+
31
+ test 'should reset password and save the record' do
32
+ assert create_user.reset_password!('123456789', '123456789')
33
+ end
34
+
35
+ test 'should clear reset password token while reseting the password' do
36
+ user = create_user
37
+ assert_nil user.reset_password_token
38
+
39
+ user.send_reset_password_instructions
40
+ assert_present user.reset_password_token
41
+ assert user.reset_password!('123456789', '123456789')
42
+ assert_nil user.reset_password_token
43
+ end
44
+
45
+ test 'should not clear reset password token if record is invalid' do
46
+ user = create_user
47
+ user.send_reset_password_instructions
48
+ assert_present user.reset_password_token
49
+ assert_not user.reset_password!('123456789', '987654321')
50
+ assert_present user.reset_password_token
51
+ end
52
+
53
+ test 'should not reset password with invalid data' do
54
+ user = create_user
55
+ user.stubs(:valid?).returns(false)
56
+ assert_not user.reset_password!('123456789', '987654321')
57
+ end
58
+
59
+ test 'should reset reset password token and send instructions by email' do
60
+ user = create_user
61
+ assert_email_sent do
62
+ token = user.reset_password_token
63
+ user.send_reset_password_instructions
64
+ assert_not_equal token, user.reset_password_token
65
+ end
66
+ end
67
+
68
+ test 'should find a user to send instructions by email' do
69
+ user = create_user
70
+ reset_password_user = User.send_reset_password_instructions(email: user.email)
71
+ assert_equal reset_password_user, user
72
+ end
73
+
74
+ test 'should return a new record with errors if user was not found by e-mail' do
75
+ reset_password_user = User.send_reset_password_instructions(email: "invalid@example.com")
76
+ assert_not reset_password_user.persisted?
77
+ assert_equal "not found", reset_password_user.errors[:email].join
78
+ end
79
+
80
+ test 'should find a user to send instructions by authentication_keys' do
81
+ swap Devise, authentication_keys: [:username, :email] do
82
+ user = create_user
83
+ reset_password_user = User.send_reset_password_instructions(email: user.email, username: user.username)
84
+ assert_equal reset_password_user, user
85
+ end
86
+ end
87
+
88
+ test 'should require all reset_password_keys' do
89
+ swap Devise, reset_password_keys: [:username, :email] do
90
+ user = create_user
91
+ reset_password_user = User.send_reset_password_instructions(email: user.email)
92
+ assert_not reset_password_user.persisted?
93
+ assert_equal "can't be blank", reset_password_user.errors[:username].join
94
+ end
95
+ end
96
+
97
+ test 'should reset reset_password_token before send the reset instructions email' do
98
+ user = create_user
99
+ token = user.reset_password_token
100
+ User.send_reset_password_instructions(email: user.email)
101
+ assert_not_equal token, user.reload.reset_password_token
102
+ end
103
+
104
+ test 'should send email instructions to the user reset their password' do
105
+ user = create_user
106
+ assert_email_sent do
107
+ User.send_reset_password_instructions(email: user.email)
108
+ end
109
+ end
110
+
111
+ test 'should find a user to reset their password based on the raw token' do
112
+ user = create_user
113
+ raw = user.send_reset_password_instructions
114
+
115
+ reset_password_user = User.reset_password_by_token(reset_password_token: raw)
116
+ assert_equal reset_password_user, user
117
+ end
118
+
119
+ test 'should return a new record with errors if no reset_password_token is found' do
120
+ reset_password_user = User.reset_password_by_token(reset_password_token: 'invalid_token')
121
+ assert_not reset_password_user.persisted?
122
+ assert_equal "is invalid", reset_password_user.errors[:reset_password_token].join
123
+ end
124
+
125
+ test 'should return a new record with errors if reset_password_token is blank' do
126
+ reset_password_user = User.reset_password_by_token(reset_password_token: '')
127
+ assert_not reset_password_user.persisted?
128
+ assert_match "can't be blank", reset_password_user.errors[:reset_password_token].join
129
+ end
130
+
131
+ test 'should return a new record with errors if password is blank' do
132
+ user = create_user
133
+ raw = user.send_reset_password_instructions
134
+
135
+ reset_password_user = User.reset_password_by_token(reset_password_token: raw, password: '')
136
+ assert_not reset_password_user.errors.empty?
137
+ assert_match "can't be blank", reset_password_user.errors[:password].join
138
+ assert_equal raw, reset_password_user.reset_password_token
139
+ end
140
+
141
+ test 'should reset successfully user password given the new password and confirmation' do
142
+ user = create_user
143
+ old_password = user.password
144
+ raw = user.send_reset_password_instructions
145
+
146
+ reset_password_user = User.reset_password_by_token(
147
+ reset_password_token: raw,
148
+ password: 'new_password',
149
+ password_confirmation: 'new_password'
150
+ )
151
+ assert_nil reset_password_user.reset_password_token
152
+
153
+ user.reload
154
+ assert_not user.valid_password?(old_password)
155
+ assert user.valid_password?('new_password')
156
+ assert_nil user.reset_password_token
157
+ end
158
+
159
+ test 'should not reset password after reset_password_within time' do
160
+ swap Devise, reset_password_within: 1.hour do
161
+ user = create_user
162
+ raw = user.send_reset_password_instructions
163
+
164
+ old_password = user.password
165
+ user.reset_password_sent_at = 2.days.ago
166
+ user.save!
167
+
168
+ reset_password_user = User.reset_password_by_token(
169
+ reset_password_token: raw,
170
+ password: 'new_password',
171
+ password_confirmation: 'new_password'
172
+ )
173
+ user.reload
174
+
175
+ assert user.valid_password?(old_password)
176
+ assert_not user.valid_password?('new_password')
177
+ assert_equal "has expired, please request a new one", reset_password_user.errors[:reset_password_token].join
178
+ end
179
+ end
180
+
181
+ test 'required_fields should contain the fields that Devise uses' do
182
+ assert_same_content Devise::Models::Recoverable.required_fields(User), [
183
+ :reset_password_sent_at,
184
+ :reset_password_token
185
+ ]
186
+ end
187
+
188
+ test 'should return a user based on the raw token' do
189
+ user = create_user
190
+ raw = user.send_reset_password_instructions
191
+
192
+ assert_equal User.with_reset_password_token(raw), user
193
+ end
194
+
195
+ test 'should return the same reset password token as generated' do
196
+ user = create_user
197
+ raw = user.send_reset_password_instructions
198
+ assert_equal Devise.token_generator.digest(self.class, :reset_password_token, raw), user.reset_password_token
199
+ end
200
+
201
+ test 'should return nil if a user based on the raw token is not found' do
202
+ assert_equal User.with_reset_password_token('random-token'), nil
203
+ end
204
+
205
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class RegisterableTest < ActiveSupport::TestCase
4
+ test 'required_fields should contain the fields that Devise uses' do
5
+ assert_same_content Devise::Models::Registerable.required_fields(User), []
6
+ end
7
+ end
@@ -0,0 +1,198 @@
1
+ require 'test_helper'
2
+
3
+ class RememberableTest < ActiveSupport::TestCase
4
+ def resource_class
5
+ User
6
+ end
7
+
8
+ def create_resource
9
+ create_user
10
+ end
11
+
12
+ test 'remember_me should not generate a new token if using salt' do
13
+ user = create_user
14
+ user.expects(:valid?).never
15
+ user.remember_me!
16
+ end
17
+
18
+ test 'forget_me should not clear remember token if using salt' do
19
+ user = create_user
20
+ user.remember_me!
21
+ user.expects(:valid?).never
22
+ user.forget_me!
23
+ end
24
+
25
+ test 'can generate remember token' do
26
+ user = create_user
27
+ user.singleton_class.send(:attr_accessor, :remember_token)
28
+ User.to_adapter.expects(:find_first).returns(nil)
29
+ user.remember_me!
30
+ assert user.remember_token
31
+ end
32
+
33
+ test 'serialize into cookie' do
34
+ user = create_user
35
+ user.remember_me!
36
+ assert_equal [user.to_key, user.authenticatable_salt], User.serialize_into_cookie(user)
37
+ end
38
+
39
+ test 'serialize from cookie' do
40
+ user = create_user
41
+ user.remember_me!
42
+ assert_equal user, User.serialize_from_cookie(user.to_key, user.authenticatable_salt)
43
+ end
44
+
45
+ test 'raises a RuntimeError if authenticatable_salt is nil' do
46
+ user = User.new
47
+ user.encrypted_password = nil
48
+ assert_raise RuntimeError do
49
+ user.rememberable_value
50
+ end
51
+ end
52
+
53
+ test 'should respond to remember_me attribute' do
54
+ assert resource_class.new.respond_to?(:remember_me)
55
+ assert resource_class.new.respond_to?(:remember_me=)
56
+ end
57
+
58
+ test 'forget_me should clear remember_created_at if expire_all_remember_me_on_sign_out is true' do
59
+ swap Devise, expire_all_remember_me_on_sign_out: true do
60
+ resource = create_resource
61
+ resource.remember_me!
62
+ assert_not_nil resource.remember_created_at
63
+
64
+ resource.forget_me!
65
+ assert_nil resource.remember_created_at
66
+ end
67
+ end
68
+
69
+ test 'forget_me should not clear remember_created_at if expire_all_remember_me_on_sign_out is false' do
70
+ swap Devise, expire_all_remember_me_on_sign_out: false do
71
+ resource = create_resource
72
+ resource.remember_me!
73
+
74
+ assert_not_nil resource.remember_created_at
75
+
76
+ resource.forget_me!
77
+ assert_not_nil resource.remember_created_at
78
+ end
79
+ end
80
+
81
+ test 'forget_me should not try to update resource if it has been destroyed' do
82
+ resource = create_resource
83
+ resource.expects(:remember_created_at).never
84
+ resource.expects(:save).never
85
+
86
+ resource.destroy
87
+ resource.forget_me!
88
+ end
89
+
90
+ test 'remember is expired if not created at timestamp is set' do
91
+ assert create_resource.remember_expired?
92
+ end
93
+
94
+ test 'serialize should return nil if no resource is found' do
95
+ assert_nil resource_class.serialize_from_cookie([0], "123")
96
+ end
97
+
98
+ test 'remember me return nil if is a valid resource with invalid token' do
99
+ resource = create_resource
100
+ assert_nil resource_class.serialize_from_cookie([resource.id], "123")
101
+ end
102
+
103
+ test 'remember for should fallback to devise remember for default configuration' do
104
+ swap Devise, remember_for: 1.day do
105
+ resource = create_resource
106
+ resource.remember_me!
107
+ assert_not resource.remember_expired?
108
+ end
109
+ end
110
+
111
+ test 'remember expires at should sum date of creation with remember for configuration' do
112
+ swap Devise, remember_for: 3.days do
113
+ resource = create_resource
114
+ resource.remember_me!
115
+ assert_equal 3.days.from_now.to_date, resource.remember_expires_at.to_date
116
+
117
+ Devise.remember_for = 5.days
118
+ assert_equal 5.days.from_now.to_date, resource.remember_expires_at.to_date
119
+ end
120
+ end
121
+
122
+ test 'remember should be expired if remember_for is zero' do
123
+ swap Devise, remember_for: 0.days do
124
+ Devise.remember_for = 0.days
125
+ resource = create_resource
126
+ resource.remember_me!
127
+ assert resource.remember_expired?
128
+ end
129
+ end
130
+
131
+ test 'remember should be expired if it was created before limit time' do
132
+ swap Devise, remember_for: 1.day do
133
+ resource = create_resource
134
+ resource.remember_me!
135
+ resource.remember_created_at = 2.days.ago
136
+ resource.save
137
+ assert resource.remember_expired?
138
+ end
139
+ end
140
+
141
+ test 'remember should not be expired if it was created within the limit time' do
142
+ swap Devise, remember_for: 30.days do
143
+ resource = create_resource
144
+ resource.remember_me!
145
+ resource.remember_created_at = (30.days.ago + 2.minutes)
146
+ resource.save
147
+ assert_not resource.remember_expired?
148
+ end
149
+ end
150
+
151
+ test 'if extend_remember_period is false, remember_me! should generate a new timestamp if expired' do
152
+ swap Devise, remember_for: 5.minutes do
153
+ resource = create_resource
154
+ resource.remember_me!(false)
155
+ assert resource.remember_created_at
156
+
157
+ resource.remember_created_at = old = 10.minutes.ago
158
+ resource.save
159
+
160
+ resource.remember_me!(false)
161
+ assert_not_equal old.to_i, resource.remember_created_at.to_i
162
+ end
163
+ end
164
+
165
+ test 'if extend_remember_period is false, remember_me! should not generate a new timestamp' do
166
+ swap Devise, remember_for: 1.year do
167
+ resource = create_resource
168
+ resource.remember_me!(false)
169
+ assert resource.remember_created_at
170
+
171
+ resource.remember_created_at = old = 10.minutes.ago.utc
172
+ resource.save
173
+
174
+ resource.remember_me!(false)
175
+ assert_equal old.to_i, resource.remember_created_at.to_i
176
+ end
177
+ end
178
+
179
+ test 'if extend_remember_period is true, remember_me! should always generate a new timestamp' do
180
+ swap Devise, remember_for: 1.year do
181
+ resource = create_resource
182
+ resource.remember_me!(true)
183
+ assert resource.remember_created_at
184
+
185
+ resource.remember_created_at = old = 10.minutes.ago
186
+ resource.save
187
+
188
+ resource.remember_me!(true)
189
+ assert_not_equal old, resource.remember_created_at
190
+ end
191
+ end
192
+
193
+ test 'should have the required_fields array' do
194
+ assert_same_content Devise::Models::Rememberable.required_fields(User), [
195
+ :remember_created_at
196
+ ]
197
+ end
198
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class SerializableTest < ActiveSupport::TestCase
4
+ setup do
5
+ @user = create_user
6
+ end
7
+
8
+ test 'should not include unsafe keys on XML' do
9
+ assert_match(/email/, @user.to_xml)
10
+ assert_no_match(/confirmation-token/, @user.to_xml)
11
+ end
12
+
13
+ test 'should not include unsafe keys on XML even if a new except is provided' do
14
+ assert_no_match(/email/, @user.to_xml(except: :email))
15
+ assert_no_match(/confirmation-token/, @user.to_xml(except: :email))
16
+ end
17
+
18
+ test 'should include unsafe keys on XML if a force_except is provided' do
19
+ assert_no_match(/<email/, @user.to_xml(force_except: :email))
20
+ assert_match(/confirmation-token/, @user.to_xml(force_except: :email))
21
+ end
22
+
23
+ test 'should not include unsafe keys on JSON' do
24
+ keys = from_json().keys.select{ |key| !key.include?("id") }
25
+ assert_equal %w(created_at email facebook_token updated_at username), keys.sort
26
+ end
27
+
28
+ test 'should not include unsafe keys on JSON even if a new except is provided' do
29
+ assert_no_key "email", from_json(except: :email)
30
+ assert_no_key "confirmation_token", from_json(except: :email)
31
+ end
32
+
33
+ test 'should include unsafe keys on JSON if a force_except is provided' do
34
+ assert_no_key "email", from_json(force_except: :email)
35
+ assert_key "confirmation_token", from_json(force_except: :email)
36
+ end
37
+
38
+ def assert_key(key, subject)
39
+ assert subject.key?(key), "Expected #{subject.inspect} to have key #{key.inspect}"
40
+ end
41
+
42
+ def assert_no_key(key, subject)
43
+ assert !subject.key?(key), "Expected #{subject.inspect} to not have key #{key.inspect}"
44
+ end
45
+
46
+ def from_json(options=nil)
47
+ ActiveSupport::JSON.decode(@user.to_json(options))["user"]
48
+ end
49
+ end