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