devise_token_auth_multi_email 0.9.0

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