devise_token_auth_multitenancy 1.1.3.alpha1

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