devise_token_auth_skycocker_fork 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +97 -0
  4. data/Rakefile +42 -0
  5. data/app/controllers/devise_token_auth/application_controller.rb +79 -0
  6. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +44 -0
  7. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +178 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +39 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +239 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +185 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +198 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +131 -0
  13. data/app/controllers/devise_token_auth/token_validations_controller.rb +31 -0
  14. data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
  15. data/app/models/devise_token_auth/concerns/active_record_support.rb +34 -0
  16. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  17. data/app/models/devise_token_auth/concerns/user.rb +262 -0
  18. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
  19. data/app/validators/devise_token_auth/email_validator.rb +23 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  24. data/config/locales/da-DK.yml +50 -0
  25. data/config/locales/de.yml +49 -0
  26. data/config/locales/en.yml +50 -0
  27. data/config/locales/es.yml +49 -0
  28. data/config/locales/fr.yml +49 -0
  29. data/config/locales/it.yml +46 -0
  30. data/config/locales/ja.yml +46 -0
  31. data/config/locales/nl.yml +30 -0
  32. data/config/locales/pl.yml +48 -0
  33. data/config/locales/pt-BR.yml +46 -0
  34. data/config/locales/pt.yml +48 -0
  35. data/config/locales/ro.yml +46 -0
  36. data/config/locales/ru.yml +50 -0
  37. data/config/locales/sq.yml +46 -0
  38. data/config/locales/sv.yml +50 -0
  39. data/config/locales/uk.yml +59 -0
  40. data/config/locales/vi.yml +50 -0
  41. data/config/locales/zh-CN.yml +46 -0
  42. data/config/locales/zh-HK.yml +48 -0
  43. data/config/locales/zh-TW.yml +48 -0
  44. data/lib/devise_token_auth.rb +13 -0
  45. data/lib/devise_token_auth/blacklist.rb +2 -0
  46. data/lib/devise_token_auth/controllers/helpers.rb +161 -0
  47. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  48. data/lib/devise_token_auth/engine.rb +90 -0
  49. data/lib/devise_token_auth/errors.rb +8 -0
  50. data/lib/devise_token_auth/rails/routes.rb +116 -0
  51. data/lib/devise_token_auth/url.rb +41 -0
  52. data/lib/devise_token_auth/version.rb +5 -0
  53. data/lib/generators/devise_token_auth/USAGE +31 -0
  54. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  55. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  56. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  57. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  58. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +50 -0
  59. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +56 -0
  60. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  61. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +63 -0
  62. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  63. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  64. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  65. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  66. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  67. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  68. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  69. data/test/controllers/demo_group_controller_test.rb +151 -0
  70. data/test/controllers/demo_mang_controller_test.rb +284 -0
  71. data/test/controllers/demo_user_controller_test.rb +629 -0
  72. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +127 -0
  73. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +376 -0
  74. data/test/controllers/devise_token_auth/passwords_controller_test.rb +639 -0
  75. data/test/controllers/devise_token_auth/registrations_controller_test.rb +880 -0
  76. data/test/controllers/devise_token_auth/sessions_controller_test.rb +541 -0
  77. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  78. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  79. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  80. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  81. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  82. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  83. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  84. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  85. data/test/dummy/README.rdoc +28 -0
  86. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  87. data/test/dummy/app/active_record/mang.rb +5 -0
  88. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  89. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  90. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  91. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  92. data/test/dummy/app/active_record/user.rb +6 -0
  93. data/test/dummy/app/controllers/application_controller.rb +18 -0
  94. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  95. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  96. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  97. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  98. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  99. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  100. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  101. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  102. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  103. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  104. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  105. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  106. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  107. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  108. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  109. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  110. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  111. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  112. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  113. data/test/dummy/app/mongoid/mang.rb +53 -0
  114. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  115. data/test/dummy/app/mongoid/scoped_user.rb +57 -0
  116. data/test/dummy/app/mongoid/unconfirmable_user.rb +51 -0
  117. data/test/dummy/app/mongoid/unregisterable_user.rb +54 -0
  118. data/test/dummy/app/mongoid/user.rb +56 -0
  119. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  120. data/test/dummy/config.ru +18 -0
  121. data/test/dummy/config/application.rb +48 -0
  122. data/test/dummy/config/application.yml.bk +0 -0
  123. data/test/dummy/config/boot.rb +11 -0
  124. data/test/dummy/config/environment.rb +7 -0
  125. data/test/dummy/config/environments/development.rb +46 -0
  126. data/test/dummy/config/environments/production.rb +84 -0
  127. data/test/dummy/config/environments/test.rb +50 -0
  128. data/test/dummy/config/initializers/assets.rb +10 -0
  129. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  130. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  131. data/test/dummy/config/initializers/devise.rb +17 -0
  132. data/test/dummy/config/initializers/devise_token_auth.rb +24 -0
  133. data/test/dummy/config/initializers/figaro.rb +3 -0
  134. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  135. data/test/dummy/config/initializers/inflections.rb +18 -0
  136. data/test/dummy/config/initializers/mime_types.rb +6 -0
  137. data/test/dummy/config/initializers/omniauth.rb +10 -0
  138. data/test/dummy/config/initializers/session_store.rb +5 -0
  139. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  140. data/test/dummy/config/routes.rb +55 -0
  141. data/test/dummy/config/spring.rb +3 -0
  142. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +65 -0
  143. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +64 -0
  144. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  145. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  146. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +62 -0
  147. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +63 -0
  148. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +63 -0
  149. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +63 -0
  150. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +63 -0
  151. data/test/dummy/db/schema.rb +200 -0
  152. data/test/dummy/lib/migration_database_helper.rb +43 -0
  153. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  154. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +50 -0
  155. data/test/dummy/tmp/generators/db/migrate/20181030122248_devise_token_auth_create_users.rb +56 -0
  156. data/test/factories/users.rb +40 -0
  157. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  158. data/test/lib/devise_token_auth/url_test.rb +26 -0
  159. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  160. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  161. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  162. data/test/models/concerns/mongoid_support_test.rb +31 -0
  163. data/test/models/only_email_user_test.rb +37 -0
  164. data/test/models/user_test.rb +140 -0
  165. data/test/support/controllers/routes.rb +43 -0
  166. data/test/test_helper.rb +103 -0
  167. metadata +443 -0
@@ -0,0 +1,880 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ # was the web request successful?
6
+ # was the user redirected to the right page?
7
+ # was the user successfully authenticated?
8
+ # was the correct object stored in the response?
9
+ # was the appropriate message delivered in the json payload?
10
+
11
+ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::IntegrationTest
12
+ describe DeviseTokenAuth::RegistrationsController do
13
+ describe 'Validate non-empty body' do
14
+ before do
15
+ # need to post empty data
16
+ post '/auth', params: {}
17
+
18
+ @resource = assigns(:resource)
19
+ @data = JSON.parse(response.body)
20
+ end
21
+
22
+ test 'request should fail' do
23
+ assert_equal 422, response.status
24
+ end
25
+
26
+ test 'returns error message' do
27
+ assert_not_empty @data['errors']
28
+ end
29
+
30
+ test 'return error status' do
31
+ assert_equal 'error', @data['status']
32
+ end
33
+
34
+ test 'user should not have been saved' do
35
+ assert @resource.nil?
36
+ end
37
+ end
38
+
39
+ describe 'Successful registration' do
40
+ before do
41
+ @mails_sent = ActionMailer::Base.deliveries.count
42
+
43
+ post '/auth',
44
+ params: {
45
+ email: Faker::Internet.email,
46
+ password: 'secret123',
47
+ password_confirmation: 'secret123',
48
+ confirm_success_url: Faker::Internet.url,
49
+ unpermitted_param: '(x_x)'
50
+ }
51
+
52
+ @resource = assigns(:resource)
53
+ @data = JSON.parse(response.body)
54
+ @mail = ActionMailer::Base.deliveries.last
55
+ end
56
+
57
+ test 'request should be successful' do
58
+ assert_equal 200, response.status
59
+ end
60
+
61
+ test 'user should have been created' do
62
+ assert @resource.id
63
+ end
64
+
65
+ test 'user should not be confirmed' do
66
+ assert_nil @resource.confirmed_at
67
+ end
68
+
69
+ test 'new user data should be returned as json' do
70
+ assert @data['data']['email']
71
+ end
72
+
73
+ test 'new user should receive confirmation email' do
74
+ assert_equal @resource.email, @mail['to'].to_s
75
+ end
76
+
77
+ test 'new user password should not be returned' do
78
+ assert_nil @data['data']['password']
79
+ end
80
+
81
+ test 'only one email was sent' do
82
+ assert_equal @mails_sent + 1, ActionMailer::Base.deliveries.count
83
+ end
84
+ end
85
+
86
+ describe 'using "+" in email' do
87
+ test 'can use + sign in email addresses' do
88
+ @plus_email = 'ak+testing@gmail.com'
89
+
90
+ post '/auth',
91
+ params: { email: @plus_email,
92
+ password: 'secret123',
93
+ password_confirmation: 'secret123',
94
+ confirm_success_url: Faker::Internet.url }
95
+
96
+ @resource = assigns(:resource)
97
+
98
+ assert_equal @plus_email, @resource.email
99
+ end
100
+ end
101
+
102
+ describe 'Using redirect_whitelist' do
103
+ before do
104
+ @good_redirect_url = Faker::Internet.url
105
+ @bad_redirect_url = Faker::Internet.url
106
+ DeviseTokenAuth.redirect_whitelist = [@good_redirect_url]
107
+ end
108
+
109
+ teardown do
110
+ DeviseTokenAuth.redirect_whitelist = nil
111
+ end
112
+
113
+ test 'request to whitelisted redirect should be successful' do
114
+ post '/auth',
115
+ params: { email: Faker::Internet.email,
116
+ password: 'secret123',
117
+ password_confirmation: 'secret123',
118
+ confirm_success_url: @good_redirect_url,
119
+ unpermitted_param: '(x_x)' }
120
+
121
+ assert_equal 200, response.status
122
+ end
123
+
124
+ test 'request to non-whitelisted redirect should fail' do
125
+ post '/auth',
126
+ params: { email: Faker::Internet.email,
127
+ password: 'secret123',
128
+ password_confirmation: 'secret123',
129
+ confirm_success_url: @bad_redirect_url,
130
+ unpermitted_param: '(x_x)' }
131
+ @data = JSON.parse(response.body)
132
+
133
+ assert_equal 422, response.status
134
+ assert @data['errors']
135
+ assert_equal @data['errors'],
136
+ [I18n.t('devise_token_auth.registrations.redirect_url_not_allowed',
137
+ redirect_url: @bad_redirect_url)]
138
+ end
139
+ end
140
+
141
+ describe 'failure if not redirecturl' do
142
+ test 'request should fail if not redirect_url' do
143
+ post '/auth',
144
+ params: { email: Faker::Internet.email,
145
+ password: 'secret123',
146
+ password_confirmation: 'secret123',
147
+ unpermitted_param: '(x_x)' }
148
+
149
+ assert_equal 422, response.status
150
+ end
151
+
152
+ test 'request to non-whitelisted redirect should fail' do
153
+ post '/auth',
154
+ params: { email: Faker::Internet.email,
155
+ password: 'secret123',
156
+ password_confirmation: 'secret123',
157
+ unpermitted_param: '(x_x)' }
158
+ @data = JSON.parse(response.body)
159
+
160
+ assert @data['errors']
161
+ assert_equal @data['errors'], [I18n.t('devise_token_auth.registrations.missing_confirm_success_url')]
162
+ end
163
+ end
164
+
165
+ describe 'Using default_confirm_success_url' do
166
+ before do
167
+ @mails_sent = ActionMailer::Base.deliveries.count
168
+ @redirect_url = Faker::Internet.url
169
+
170
+ DeviseTokenAuth.default_confirm_success_url = @redirect_url
171
+
172
+ assert_difference 'ActionMailer::Base.deliveries.size', 1 do
173
+ post '/auth', params: { email: Faker::Internet.email,
174
+ password: 'secret123',
175
+ password_confirmation: 'secret123',
176
+ unpermitted_param: '(x_x)' }
177
+ end
178
+
179
+ @resource = assigns(:resource)
180
+ @data = JSON.parse(response.body)
181
+ @mail = ActionMailer::Base.deliveries.last
182
+ @sent_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)(&|\")/)[1])
183
+ end
184
+
185
+ teardown do
186
+ DeviseTokenAuth.default_confirm_success_url = nil
187
+ end
188
+
189
+ test 'request should be successful' do
190
+ assert_equal 200, response.status
191
+ end
192
+
193
+ test 'email contains the default redirect url' do
194
+ assert_equal @redirect_url, @sent_redirect_url
195
+ end
196
+ end
197
+
198
+ describe 'using namespaces' do
199
+ before do
200
+ @mails_sent = ActionMailer::Base.deliveries.count
201
+
202
+ post '/api/v1/auth', params: {
203
+ email: Faker::Internet.email,
204
+ password: 'secret123',
205
+ password_confirmation: 'secret123',
206
+ confirm_success_url: Faker::Internet.url,
207
+ unpermitted_param: '(x_x)'
208
+ }
209
+
210
+ @resource = assigns(:resource)
211
+ @data = JSON.parse(response.body)
212
+ @mail = ActionMailer::Base.deliveries.last
213
+ end
214
+
215
+ test 'request should be successful' do
216
+ assert_equal 200, response.status
217
+ end
218
+
219
+ test 'user should have been created' do
220
+ assert @resource.id
221
+ end
222
+ end
223
+
224
+ describe 'case-insensitive email' do
225
+ before do
226
+ @resource_class = User
227
+ @request_params = {
228
+ email: 'AlternatingCase@example.com',
229
+ password: 'secret123',
230
+ password_confirmation: 'secret123',
231
+ confirm_success_url: Faker::Internet.url
232
+ }
233
+ end
234
+
235
+ test 'success should downcase uid if configured' do
236
+ @resource_class.case_insensitive_keys = [:email]
237
+ post '/auth', params: @request_params
238
+ assert_equal 200, response.status
239
+ @data = JSON.parse(response.body)
240
+ assert_equal 'alternatingcase@example.com', @data['data']['uid']
241
+ end
242
+
243
+ test 'request should not downcase uid if not configured' do
244
+ @resource_class.case_insensitive_keys = []
245
+ post '/auth', params: @request_params
246
+ assert_equal 200, response.status
247
+ @data = JSON.parse(response.body)
248
+ assert_equal 'AlternatingCase@example.com', @data['data']['uid']
249
+ end
250
+ end
251
+
252
+ describe 'Adding extra params' do
253
+ before do
254
+ @redirect_url = Faker::Internet.url
255
+ @operating_thetan = 2
256
+
257
+ post '/auth',
258
+ params: { email: Faker::Internet.email,
259
+ password: 'secret123',
260
+ password_confirmation: 'secret123',
261
+ confirm_success_url: @redirect_url,
262
+ favorite_color: @fav_color,
263
+ operating_thetan: @operating_thetan }
264
+
265
+ @resource = assigns(:resource)
266
+ @data = JSON.parse(response.body)
267
+ @mail = ActionMailer::Base.deliveries.last
268
+
269
+ @mail_reset_token = @mail.body.match(/confirmation_token=([^&]*)&/)[1]
270
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=(.*)\"/)[1])
271
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
272
+ end
273
+
274
+ test 'redirect_url is included as param in email' do
275
+ assert_equal @redirect_url, @mail_redirect_url
276
+ end
277
+
278
+ test 'additional sign_up params should be considered' do
279
+ assert_equal @operating_thetan, @resource.operating_thetan
280
+ end
281
+
282
+ test 'config_name param is included in the confirmation email link' do
283
+ assert @mail_config_name
284
+ end
285
+
286
+ test "client config name falls back to 'default'" do
287
+ assert_equal 'default', @mail_config_name
288
+ end
289
+ end
290
+
291
+ describe 'bad email' do
292
+ before do
293
+ post '/auth',
294
+ params: { email: 'false_email@',
295
+ password: 'secret123',
296
+ password_confirmation: 'secret123',
297
+ confirm_success_url: Faker::Internet.url }
298
+
299
+ @resource = assigns(:resource)
300
+ @data = JSON.parse(response.body)
301
+ end
302
+
303
+ test 'request should not be successful' do
304
+ assert_equal 422, response.status
305
+ end
306
+
307
+ test 'user should not have been created' do
308
+ refute @resource.persisted?
309
+ end
310
+
311
+ test 'error should be returned in the response' do
312
+ assert @data['errors'].length
313
+ end
314
+
315
+ test 'full_messages should be included in error hash' do
316
+ assert @data['errors']['full_messages'].length
317
+ end
318
+ end
319
+
320
+ describe 'missing email' do
321
+ before do
322
+ post '/auth',
323
+ params: { password: 'secret123',
324
+ password_confirmation: 'secret123',
325
+ confirm_success_url: Faker::Internet.url }
326
+
327
+ @resource = assigns(:resource)
328
+ @data = JSON.parse(response.body)
329
+ end
330
+
331
+ test 'request should not be successful' do
332
+ assert_equal 422, response.status
333
+ end
334
+
335
+ test 'user should not have been created' do
336
+ refute @resource.persisted?
337
+ end
338
+
339
+ test 'error should be returned in the response' do
340
+ assert @data['errors'].length
341
+ end
342
+
343
+ test 'full_messages should be included in error hash' do
344
+ assert @data['errors']['full_messages'].length
345
+ end
346
+ end
347
+
348
+ describe 'Mismatched passwords' do
349
+ before do
350
+ post '/auth',
351
+ params: { email: Faker::Internet.email,
352
+ password: 'secret123',
353
+ password_confirmation: 'bogus',
354
+ confirm_success_url: Faker::Internet.url }
355
+
356
+ @resource = assigns(:resource)
357
+ @data = JSON.parse(response.body)
358
+ end
359
+
360
+ test 'request should not be successful' do
361
+ assert_equal 422, response.status
362
+ end
363
+
364
+ test 'user should have been created' do
365
+ refute @resource.persisted?
366
+ end
367
+
368
+ test 'error should be returned in the response' do
369
+ assert @data['errors'].length
370
+ end
371
+
372
+ test 'full_messages should be included in error hash' do
373
+ assert @data['errors']['full_messages'].length
374
+ end
375
+ end
376
+
377
+ describe 'Existing users' do
378
+ before do
379
+ @existing_user = create(:user, :confirmed)
380
+
381
+ post '/auth',
382
+ params: { email: @existing_user.email,
383
+ password: 'secret123',
384
+ password_confirmation: 'secret123',
385
+ confirm_success_url: Faker::Internet.url }
386
+
387
+ @resource = assigns(:resource)
388
+ @data = JSON.parse(response.body)
389
+ end
390
+
391
+ test 'request should not be successful' do
392
+ assert_equal 422, response.status
393
+ end
394
+
395
+ test 'user should have been created' do
396
+ refute @resource.persisted?
397
+ end
398
+
399
+ test 'error should be returned in the response' do
400
+ assert @data['errors'].length
401
+ end
402
+ end
403
+
404
+ describe 'Destroy user account' do
405
+ describe 'success' do
406
+ before do
407
+ @existing_user = create(:user, :confirmed)
408
+ @auth_headers = @existing_user.create_new_auth_token
409
+ @client_id = @auth_headers['client']
410
+
411
+ # ensure request is not treated as batch request
412
+ age_token(@existing_user, @client_id)
413
+
414
+ delete '/auth', params: {}, headers: @auth_headers
415
+
416
+ @data = JSON.parse(response.body)
417
+ end
418
+
419
+ test 'request is successful' do
420
+ assert_equal 200, response.status
421
+ end
422
+
423
+ test 'message should be returned' do
424
+ assert @data['message']
425
+ assert_equal @data['message'],
426
+ I18n.t('devise_token_auth.registrations.account_with_uid_destroyed',
427
+ uid: @existing_user.uid)
428
+ end
429
+ test 'existing user should be deleted' do
430
+ refute User.where(id: @existing_user.id).first
431
+ end
432
+ end
433
+
434
+ describe 'failure: no auth headers' do
435
+ before do
436
+ delete '/auth'
437
+ @data = JSON.parse(response.body)
438
+ end
439
+
440
+ test 'request returns 404 (not found) status' do
441
+ assert_equal 404, response.status
442
+ end
443
+
444
+ test 'error should be returned' do
445
+ assert @data['errors'].length
446
+ assert_equal @data['errors'], [I18n.t('devise_token_auth.registrations.account_to_destroy_not_found')]
447
+ end
448
+ end
449
+ end
450
+
451
+ describe 'Update user account' do
452
+ describe 'existing user' do
453
+ before do
454
+ @existing_user = create(:user, :confirmed)
455
+ @auth_headers = @existing_user.create_new_auth_token
456
+ @client_id = @auth_headers['client']
457
+
458
+ # ensure request is not treated as batch request
459
+ age_token(@existing_user, @client_id)
460
+ end
461
+
462
+ describe 'without password check' do
463
+ describe 'success' do
464
+ before do
465
+ # test valid update param
466
+ @resource_class = User
467
+ @new_operating_thetan = 1_000_000
468
+ @email = 'AlternatingCase2@example.com'
469
+ @request_params = {
470
+ operating_thetan: @new_operating_thetan,
471
+ email: @email
472
+ }
473
+ end
474
+
475
+ test 'Request was successful' do
476
+ put '/auth', params: @request_params, headers: @auth_headers
477
+ assert_equal 200, response.status
478
+ end
479
+
480
+ test 'Case sensitive attributes update' do
481
+ @resource_class.case_insensitive_keys = []
482
+ put '/auth', params: @request_params, headers: @auth_headers
483
+ @data = JSON.parse(response.body)
484
+ @existing_user.reload
485
+ assert_equal @new_operating_thetan,
486
+ @existing_user.operating_thetan
487
+ assert_equal @email, @existing_user.email
488
+ assert_equal @email, @existing_user.uid
489
+ end
490
+
491
+ test 'Case insensitive attributes update' do
492
+ @resource_class.case_insensitive_keys = [:email]
493
+ put '/auth', params: @request_params, headers: @auth_headers
494
+ @data = JSON.parse(response.body)
495
+ @existing_user.reload
496
+ assert_equal @new_operating_thetan, @existing_user.operating_thetan
497
+ assert_equal @email.downcase, @existing_user.email
498
+ assert_equal @email.downcase, @existing_user.uid
499
+ end
500
+
501
+ test 'Supply current password' do
502
+ @request_params[:current_password] = @existing_user.password
503
+ @request_params[:email] = @existing_user.email
504
+
505
+ put '/auth', params: @request_params, headers: @auth_headers
506
+ @data = JSON.parse(response.body)
507
+ @existing_user.reload
508
+ assert_equal @existing_user.email, @request_params[:email]
509
+ end
510
+ end
511
+
512
+ describe 'validate non-empty body' do
513
+ before do
514
+ # get the email so we can check it wasn't updated
515
+ @email = @existing_user.email
516
+ put '/auth', params: {}, headers: @auth_headers
517
+
518
+ @data = JSON.parse(response.body)
519
+ @existing_user.reload
520
+ end
521
+
522
+ test 'request should fail' do
523
+ assert_equal 422, response.status
524
+ end
525
+
526
+ test 'returns error message' do
527
+ assert_not_empty @data['errors']
528
+ end
529
+
530
+ test 'return error status' do
531
+ assert_equal 'error', @data['status']
532
+ end
533
+
534
+ test 'user should not have been saved' do
535
+ assert_equal @email, @existing_user.email
536
+ end
537
+ end
538
+
539
+ describe 'error' do
540
+ before do
541
+ # test invalid update param
542
+ @new_operating_thetan = 'blegh'
543
+ put '/auth',
544
+ params: { operating_thetan: @new_operating_thetan },
545
+ headers: @auth_headers
546
+
547
+ @data = JSON.parse(response.body)
548
+ @existing_user.reload
549
+ end
550
+
551
+ test 'Request was NOT successful' do
552
+ assert_equal 422, response.status
553
+ end
554
+
555
+ test 'Errors were provided with response' do
556
+ assert @data['errors'].length
557
+ end
558
+ end
559
+ end
560
+
561
+ describe 'with password check for password update only' do
562
+ before do
563
+ DeviseTokenAuth.check_current_password_before_update = :password
564
+ end
565
+
566
+ after do
567
+ DeviseTokenAuth.check_current_password_before_update = false
568
+ end
569
+
570
+ describe 'success without password update' do
571
+ before do
572
+ # test valid update param
573
+ @resource_class = User
574
+ @new_operating_thetan = 1_000_000
575
+ @email = 'AlternatingCase2@example.com'
576
+ @request_params = {
577
+ operating_thetan: @new_operating_thetan,
578
+ email: @email
579
+ }
580
+ end
581
+
582
+ test 'Request was successful' do
583
+ put '/auth', params: @request_params, headers: @auth_headers
584
+ assert_equal 200, response.status
585
+ end
586
+ end
587
+
588
+ describe 'success with password update' do
589
+ before do
590
+ @existing_user.update password: 'secret123', password_confirmation: 'secret123'
591
+ @request_params = {
592
+ password: 'the_new_secret456',
593
+ password_confirmation: 'the_new_secret456',
594
+ current_password: 'secret123'
595
+ }
596
+ end
597
+
598
+ test 'Request was successful' do
599
+ put '/auth', params: @request_params, headers: @auth_headers
600
+ assert_equal 200, response.status
601
+ end
602
+ end
603
+
604
+ describe 'error with password mismatch' do
605
+ before do
606
+ @existing_user.update password: 'secret123',
607
+ password_confirmation: 'secret123'
608
+ @request_params = {
609
+ password: 'the_new_secret456',
610
+ password_confirmation: 'the_new_secret456',
611
+ current_password: 'not_so_secret321'
612
+ }
613
+ end
614
+
615
+ test 'Request was NOT successful' do
616
+ put '/auth', params: @request_params, headers: @auth_headers
617
+ assert_equal 422, response.status
618
+ end
619
+ end
620
+ end
621
+
622
+ describe 'with password check for all attributes' do
623
+ before do
624
+ DeviseTokenAuth.check_current_password_before_update = :password
625
+ @new_operating_thetan = 1_000_000
626
+ @email = 'AlternatingCase2@example.com'
627
+ end
628
+
629
+ after do
630
+ DeviseTokenAuth.check_current_password_before_update = false
631
+ end
632
+
633
+ describe 'success with password update' do
634
+ before do
635
+ @existing_user.update password: 'secret123',
636
+ password_confirmation: 'secret123'
637
+ @request_params = {
638
+ operating_thetan: @new_operating_thetan,
639
+ email: @email,
640
+ current_password: 'secret123'
641
+ }
642
+ end
643
+
644
+ test 'Request was successful' do
645
+ put '/auth', params: @request_params, headers: @auth_headers
646
+ assert_equal 200, response.status
647
+ end
648
+ end
649
+
650
+ describe 'error with password mismatch' do
651
+ before do
652
+ @existing_user.update password: 'secret123',
653
+ password_confirmation: 'secret123'
654
+ @request_params = {
655
+ operating_thetan: @new_operating_thetan,
656
+ email: @email,
657
+ current_password: 'not_so_secret321'
658
+ }
659
+ end
660
+
661
+ test 'Request was NOT successful' do
662
+ put '/auth', params: @request_params, headers: @auth_headers
663
+ assert_equal 422, response.status
664
+ end
665
+ end
666
+ end
667
+ end
668
+
669
+ describe 'invalid user' do
670
+ before do
671
+ @existing_user = create(:user, :confirmed)
672
+ @auth_headers = @existing_user.create_new_auth_token
673
+ @client_id = @auth_headers['client']
674
+
675
+ # ensure request is not treated as batch request
676
+ expire_token(@existing_user, @client_id)
677
+
678
+ # test valid update param
679
+ @new_operating_thetan = 3
680
+
681
+ put '/auth',
682
+ params: {
683
+ operating_thetan: @new_operating_thetan
684
+ },
685
+ headers: @auth_headers
686
+
687
+ @data = JSON.parse(response.body)
688
+ @existing_user.reload
689
+ end
690
+
691
+ test 'Response should return 404 status' do
692
+ assert_equal 404, response.status
693
+ end
694
+
695
+ test 'error should be returned' do
696
+ assert @data['errors'].length
697
+ assert_equal @data['errors'], [I18n.t('devise_token_auth.registrations.user_not_found')]
698
+ end
699
+
700
+ test 'User should not be updated' do
701
+ refute_equal @new_operating_thetan, @existing_user.operating_thetan
702
+ end
703
+ end
704
+ end
705
+
706
+ describe 'Ouath user has existing email' do
707
+ before do
708
+ @existing_user = create(:user, :facebook, :confirmed)
709
+
710
+ post '/auth',
711
+ params: { email: @existing_user.email,
712
+ password: 'secret123',
713
+ password_confirmation: 'secret123',
714
+ confirm_success_url: Faker::Internet.url }
715
+
716
+ @resource = assigns(:resource)
717
+ @data = JSON.parse(response.body)
718
+ end
719
+
720
+ test 'request should be successful' do
721
+ assert_equal 200, response.status
722
+ end
723
+
724
+ test 'user should have been created' do
725
+ assert @resource.id
726
+ end
727
+
728
+ test 'new user data should be returned as json' do
729
+ assert @data['data']['email']
730
+ end
731
+ end
732
+
733
+ describe 'Alternate user class' do
734
+ before do
735
+ post '/mangs',
736
+ params: { email: Faker::Internet.email,
737
+ password: 'secret123',
738
+ password_confirmation: 'secret123',
739
+ confirm_success_url: Faker::Internet.url }
740
+
741
+ @resource = assigns(:resource)
742
+ @data = JSON.parse(response.body)
743
+ @mail = ActionMailer::Base.deliveries.last
744
+ end
745
+
746
+ test 'request should be successful' do
747
+ assert_equal 200, response.status
748
+ end
749
+
750
+ test 'use should be a Mang' do
751
+ assert_equal 'Mang', @resource.class.name
752
+ end
753
+
754
+ test 'Mang should be destroyed' do
755
+ @resource.skip_confirmation!
756
+ @resource.save!
757
+ @auth_headers = @resource.create_new_auth_token
758
+ @client_id = @auth_headers['client']
759
+
760
+ # ensure request is not treated as batch request
761
+ age_token(@resource, @client_id)
762
+
763
+ delete '/mangs',
764
+ params: {},
765
+ headers: @auth_headers
766
+
767
+ assert_equal 200, response.status
768
+ refute Mang.where(id: @resource.id).first
769
+ end
770
+ end
771
+
772
+ describe 'Passing client config name' do
773
+ before do
774
+ @config_name = 'altUser'
775
+
776
+ post '/mangs',
777
+ params: { email: Faker::Internet.email,
778
+ password: 'secret123',
779
+ password_confirmation: 'secret123',
780
+ confirm_success_url: Faker::Internet.url,
781
+ config_name: @config_name }
782
+
783
+ @resource = assigns(:resource)
784
+ @data = JSON.parse(response.body)
785
+ @mail = ActionMailer::Base.deliveries.last
786
+
787
+ @resource.reload
788
+
789
+ @mail_reset_token = @mail.body.match(/confirmation_token=([^&]*)&/)[1]
790
+ @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=(.*)\"/)[1])
791
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
792
+ end
793
+
794
+ test 'config_name param is included in the confirmation email link' do
795
+ assert_equal @config_name, @mail_config_name
796
+ end
797
+ end
798
+
799
+ describe 'Excluded :registrations module' do
800
+ test 'UnregisterableUser should not be able to access registration routes' do
801
+ assert_raises(ActionController::RoutingError) do
802
+ post '/unregisterable_user_auth',
803
+ params: { email: Faker::Internet.email,
804
+ password: 'secret123',
805
+ password_confirmation: 'secret123',
806
+ confirm_success_url: Faker::Internet.url }
807
+ end
808
+ end
809
+ end
810
+
811
+ describe 'Skipped confirmation' do
812
+ setup do
813
+ User.set_callback(:create, :before, :skip_confirmation!)
814
+
815
+ post '/auth',
816
+ params: { email: Faker::Internet.email,
817
+ password: 'secret123',
818
+ password_confirmation: 'secret123',
819
+ confirm_success_url: Faker::Internet.url }
820
+
821
+ @resource = assigns(:resource)
822
+ @token = response.headers['access-token']
823
+ @client_id = response.headers['client']
824
+ end
825
+
826
+ teardown do
827
+ User.skip_callback(:create, :before, :skip_confirmation!)
828
+ end
829
+
830
+ test 'user was created' do
831
+ assert @resource
832
+ end
833
+
834
+ test 'user was confirmed' do
835
+ assert @resource.confirmed?
836
+ end
837
+
838
+ test 'auth headers were returned in response' do
839
+ assert response.headers['access-token']
840
+ assert response.headers['token-type']
841
+ assert response.headers['client']
842
+ assert response.headers['expiry']
843
+ assert response.headers['uid']
844
+ end
845
+
846
+ test 'response token is valid' do
847
+ assert @resource.valid_token?(@token, @client_id)
848
+ end
849
+ end
850
+
851
+ describe 'User with only :database_authenticatable and :registerable included' do
852
+ setup do
853
+ @mails_sent = ActionMailer::Base.deliveries.count
854
+
855
+ post '/only_email_auth',
856
+ params: { email: Faker::Internet.email,
857
+ password: 'secret123',
858
+ password_confirmation: 'secret123',
859
+ confirm_success_url: Faker::Internet.url,
860
+ unpermitted_param: '(x_x)' }
861
+
862
+ @resource = assigns(:resource)
863
+ @data = JSON.parse(response.body)
864
+ @mail = ActionMailer::Base.deliveries.last
865
+ end
866
+
867
+ test 'user was created' do
868
+ assert @resource.id
869
+ end
870
+
871
+ test 'email confirmation was not sent' do
872
+ assert_equal @mails_sent, ActionMailer::Base.deliveries.count
873
+ end
874
+
875
+ test 'user is confirmed' do
876
+ assert @resource.confirmed?
877
+ end
878
+ end
879
+ end
880
+ end