devise_token_auth 0.1.42 → 0.1.43.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -38
  3. data/app/controllers/devise_token_auth/application_controller.rb +15 -0
  4. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +38 -0
  5. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +21 -19
  6. data/app/controllers/devise_token_auth/confirmations_controller.rb +12 -7
  7. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +3 -7
  8. data/app/controllers/devise_token_auth/passwords_controller.rb +16 -26
  9. data/app/controllers/devise_token_auth/registrations_controller.rb +6 -2
  10. data/app/controllers/devise_token_auth/sessions_controller.rb +3 -14
  11. data/app/controllers/devise_token_auth/unlocks_controller.rb +105 -0
  12. data/app/models/devise_token_auth/concerns/user.rb +29 -12
  13. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +5 -8
  14. data/app/views/devise/mailer/unlock_instructions.html.erb +1 -1
  15. data/app/views/devise_token_auth/omniauth_external_window.html.erb +1 -1
  16. data/config/initializers/devise.rb +1 -1
  17. data/config/locales/da-DK.yml +50 -0
  18. data/config/locales/en.yml +4 -0
  19. data/lib/devise_token_auth/controllers/helpers.rb +30 -20
  20. data/lib/devise_token_auth/rails/routes.rb +1 -1
  21. data/lib/devise_token_auth/url.rb +1 -1
  22. data/lib/devise_token_auth/version.rb +1 -1
  23. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +2 -1
  24. data/lib/generators/devise_token_auth/templates/user.rb +1 -1
  25. data/test/controllers/custom/custom_confirmations_controller_test.rb +5 -10
  26. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +15 -16
  27. data/test/controllers/custom/custom_passwords_controller_test.rb +28 -34
  28. data/test/controllers/custom/custom_registrations_controller_test.rb +23 -21
  29. data/test/controllers/custom/custom_sessions_controller_test.rb +16 -18
  30. data/test/controllers/custom/custom_token_validations_controller_test.rb +13 -11
  31. data/test/controllers/demo_group_controller_test.rb +19 -5
  32. data/test/controllers/demo_mang_controller_test.rb +37 -16
  33. data/test/controllers/demo_user_controller_test.rb +70 -38
  34. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +40 -22
  35. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +108 -82
  36. data/test/controllers/devise_token_auth/passwords_controller_test.rb +127 -145
  37. data/test/controllers/devise_token_auth/registrations_controller_test.rb +258 -274
  38. data/test/controllers/devise_token_auth/sessions_controller_test.rb +112 -117
  39. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +14 -15
  40. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +194 -0
  41. data/test/controllers/overrides/confirmations_controller_test.rb +8 -9
  42. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +16 -12
  43. data/test/controllers/overrides/passwords_controller_test.rb +19 -15
  44. data/test/controllers/overrides/registrations_controller_test.rb +7 -7
  45. data/test/controllers/overrides/sessions_controller_test.rb +6 -6
  46. data/test/controllers/overrides/token_validations_controller_test.rb +7 -4
  47. data/test/dummy/app/controllers/auth_origin_controller.rb +2 -2
  48. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +0 -2
  49. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +11 -7
  50. data/test/dummy/app/controllers/overrides/passwords_controller.rb +10 -8
  51. data/test/dummy/app/controllers/overrides/sessions_controller.rb +1 -1
  52. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +2 -2
  53. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +2 -2
  54. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +1 -1
  55. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +1 -1
  56. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +2 -1
  57. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +1 -1
  58. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +2 -1
  59. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +2 -1
  60. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +2 -1
  61. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +2 -1
  62. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +2 -1
  63. data/test/dummy/db/schema.rb +157 -162
  64. data/test/dummy/tmp/generators/app/models/user.rb +1 -1
  65. data/test/dummy/tmp/generators/db/migrate/{20170517171822_devise_token_auth_create_users.rb → 20171014052631_devise_token_auth_create_users.rb} +2 -1
  66. data/test/lib/generators/devise_token_auth/install_generator_test.rb +5 -1
  67. data/test/models/user_test.rb +35 -1
  68. data/test/test_helper.rb +17 -11
  69. metadata +85 -80
@@ -21,51 +21,50 @@ class DeviseTokenAuth::TokenValidationsControllerTest < ActionDispatch::Integrat
21
21
 
22
22
  # ensure that request is not treated as batch request
23
23
  age_token(@resource, @client_id)
24
-
25
24
  end
26
25
 
27
26
  describe 'vanilla user' do
28
27
  before do
29
- get '/auth/validate_token', {}, @auth_headers
28
+ get '/auth/validate_token', params: {}, headers: @auth_headers
30
29
  @resp = JSON.parse(response.body)
31
30
  end
32
31
 
33
- test "token valid" do
32
+ test 'token valid' do
34
33
  assert_equal 200, response.status
35
34
  end
36
35
  end
37
36
 
38
37
  describe 'using namespaces' do
39
38
  before do
40
- get '/api/v1/auth/validate_token', {}, @auth_headers
39
+ get '/api/v1/auth/validate_token', params: {}, headers: @auth_headers
41
40
  @resp = JSON.parse(response.body)
42
41
  end
43
42
 
44
- test "token valid" do
43
+ test 'token valid' do
45
44
  assert_equal 200, response.status
46
45
  end
47
46
  end
48
47
 
49
48
  describe 'failure' do
50
49
  before do
51
- get '/api/v1/auth/validate_token', {}, @auth_headers.merge({"access-token" => "12345"})
50
+ get '/api/v1/auth/validate_token',
51
+ params: {},
52
+ headers: @auth_headers.merge('access-token' => '12345')
52
53
  @resp = JSON.parse(response.body)
53
54
  end
54
55
 
55
- test "request should fail" do
56
+ test 'request should fail' do
56
57
  assert_equal 401, response.status
57
58
  end
58
59
 
59
- test "response should contain errors" do
60
+ test 'response should contain errors' do
60
61
  assert @resp['errors']
61
- assert_equal @resp['errors'], [I18n.t("devise_token_auth.token_validations.invalid")]
62
+ assert_equal @resp['errors'], [I18n.t('devise_token_auth.token_validations.invalid')]
62
63
  end
63
64
  end
64
-
65
65
  end
66
66
 
67
67
  describe 'using namespaces with unused resource' do
68
-
69
68
  before do
70
69
  @resource = scoped_users(:confirmed_email_user)
71
70
  @resource.skip_confirmation!
@@ -81,11 +80,11 @@ class DeviseTokenAuth::TokenValidationsControllerTest < ActionDispatch::Integrat
81
80
  age_token(@resource, @client_id)
82
81
  end
83
82
 
84
- test "should be successful" do
85
- get '/api_v2/auth/validate_token', {}, @auth_headers
83
+ test 'should be successful' do
84
+ get '/api_v2/auth/validate_token',
85
+ params: {},
86
+ headers: @auth_headers
86
87
  assert_equal 200, response.status
87
88
  end
88
-
89
89
  end
90
-
91
90
  end
@@ -0,0 +1,194 @@
1
+ require 'test_helper'
2
+
3
+ # was the web request successful?
4
+ # was the user redirected to the right page?
5
+ # was the user successfully authenticated?
6
+ # was the correct object stored in the response?
7
+ # was the appropriate message delivered in the json payload?
8
+
9
+ class DeviseTokenAuth::UnlocksControllerTest < ActionController::TestCase
10
+ describe DeviseTokenAuth::UnlocksController do
11
+ setup do
12
+ @request.env['devise.mapping'] = Devise.mappings[:lockable_user]
13
+ end
14
+
15
+ teardown do
16
+ @request.env['devise.mapping'] = Devise.mappings[:user]
17
+ end
18
+
19
+ before do
20
+ @original_lock_strategy = Devise.lock_strategy
21
+ @original_unlock_strategy = Devise.unlock_strategy
22
+ @original_maximum_attempts = Devise.maximum_attempts
23
+ Devise.lock_strategy = :failed_attempts
24
+ Devise.unlock_strategy = :email
25
+ Devise.maximum_attempts = 5
26
+ end
27
+
28
+ after do
29
+ Devise.lock_strategy = @original_lock_strategy
30
+ Devise.maximum_attempts = @original_maximum_attempts
31
+ Devise.unlock_strategy = @original_unlock_strategy
32
+ end
33
+
34
+ describe 'Unlocking user' do
35
+ before do
36
+ @resource = lockable_users(:unlocked_user)
37
+ end
38
+
39
+ describe 'request unlock without email' do
40
+ before do
41
+ @auth_headers = @resource.create_new_auth_token
42
+ @new_password = Faker::Internet.password
43
+
44
+ post :create
45
+ @data = JSON.parse(response.body)
46
+ end
47
+
48
+ test 'response should fail' do
49
+ assert_equal 401, response.status
50
+ end
51
+ test 'error message should be returned' do
52
+ assert @data['errors']
53
+ assert_equal @data['errors'], [I18n.t('devise_token_auth.passwords.missing_email')]
54
+ end
55
+ end
56
+
57
+ describe 'request unlock' do
58
+ describe 'unknown user should return 404' do
59
+ before do
60
+ post :create, params: { email: 'chester@cheet.ah' }
61
+ @data = JSON.parse(response.body)
62
+ end
63
+ test 'unknown user should return 404' do
64
+ assert_equal 404, response.status
65
+ end
66
+
67
+ test 'errors should be returned' do
68
+ assert @data['errors']
69
+ assert_equal @data['errors'],
70
+ [I18n.t('devise_token_auth.passwords.user_not_found',
71
+ email: 'chester@cheet.ah')]
72
+ end
73
+ end
74
+
75
+ describe 'successfully requested unlock' do
76
+ before do
77
+ post :create, params: { email: @resource.email }
78
+
79
+ @data = JSON.parse(response.body)
80
+ end
81
+
82
+ test 'response should not contain extra data' do
83
+ assert_nil @data['data']
84
+ end
85
+ end
86
+
87
+ describe 'case-sensitive email' do
88
+ before do
89
+ post :create, params: { email: @resource.email }
90
+
91
+ @mail = ActionMailer::Base.deliveries.last
92
+ @resource.reload
93
+ @data = JSON.parse(response.body)
94
+
95
+ @mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
96
+ @mail_reset_token = @mail.body.match(/unlock_token=(.*)\"/)[1]
97
+ end
98
+
99
+ test 'response should return success status' do
100
+ assert_equal 200, response.status
101
+ end
102
+
103
+ test 'response should contains message' do
104
+ assert_equal @data['message'], I18n.t('devise_token_auth.unlocks.sended', email: @resource.email)
105
+ end
106
+
107
+ test 'action should send an email' do
108
+ assert @mail
109
+ end
110
+
111
+ test 'the email should be addressed to the user' do
112
+ assert_equal @mail.to.first, @resource.email
113
+ end
114
+
115
+ test 'the client config name should fall back to "default"' do
116
+ assert_equal 'default', @mail_config_name
117
+ end
118
+
119
+ test 'the email body should contain a link with reset token as a query param' do
120
+ user = LockableUser.unlock_access_by_token(@mail_reset_token)
121
+ assert_equal user.id, @resource.id
122
+ end
123
+
124
+ describe 'unlock link failure' do
125
+ test 'response should return 404' do
126
+ assert_raises(ActionController::RoutingError) do
127
+ get :show, params: { unlock_token: 'bogus' }
128
+ end
129
+ end
130
+ end
131
+
132
+ describe 'password reset link success' do
133
+ before do
134
+ get :show, params: { unlock_token: @mail_reset_token }
135
+
136
+ @resource.reload
137
+
138
+ raw_qs = response.location.split('?')[1]
139
+ @qs = Rack::Utils.parse_nested_query(raw_qs)
140
+
141
+ @access_token = @qs['access-token']
142
+ @client = @qs['client']
143
+ @client_id = @qs['client_id']
144
+ @expiry = @qs['expiry']
145
+ @token = @qs['token']
146
+ @uid = @qs['uid']
147
+ @unlock = @qs['unlock']
148
+ end
149
+
150
+ test 'respones should have success redirect status' do
151
+ assert_equal 302, response.status
152
+ end
153
+
154
+ test 'response should contain auth params' do
155
+ assert @access_token
156
+ assert @client
157
+ assert @client_id
158
+ assert @expiry
159
+ assert @token
160
+ assert @uid
161
+ assert @unlock
162
+ end
163
+
164
+ test 'response auth params should be valid' do
165
+ assert @resource.valid_token?(@token, @client_id)
166
+ assert @resource.valid_token?(@access_token, @client)
167
+ end
168
+ end
169
+ end
170
+
171
+ describe 'case-insensitive email' do
172
+ before do
173
+ @resource_class = LockableUser
174
+ @request_params = {
175
+ email: @resource.email.upcase
176
+ }
177
+ end
178
+
179
+ test 'response should return success status if configured' do
180
+ @resource_class.case_insensitive_keys = [:email]
181
+ post :create, params: @request_params
182
+ assert_equal 200, response.status
183
+ end
184
+
185
+ test 'response should return failure status if not configured' do
186
+ @resource_class.case_insensitive_keys = []
187
+ post :create, params: @request_params
188
+ assert_equal 404, response.status
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
@@ -10,14 +10,12 @@ class Overrides::ConfirmationsControllerTest < ActionDispatch::IntegrationTest
10
10
  describe Overrides::ConfirmationsController do
11
11
  before do
12
12
  @redirect_url = Faker::Internet.url
13
- @new_user = evil_users(:unconfirmed_email_user)
13
+ @new_user = evil_users(:unconfirmed_email_user)
14
14
 
15
15
  # generate + send email
16
- @new_user.send_confirmation_instructions({
17
- redirect_url: @redirect_url
18
- })
16
+ @new_user.send_confirmation_instructions(redirect_url: @redirect_url)
19
17
 
20
- @mail = ActionMailer::Base.deliveries.last
18
+ @mail = ActionMailer::Base.deliveries.last
21
19
  @confirmation_path = @mail.body.match(/localhost([^\"]*)\"/)[1]
22
20
 
23
21
  # visit confirmation link
@@ -27,16 +25,17 @@ class Overrides::ConfirmationsControllerTest < ActionDispatch::IntegrationTest
27
25
  @new_user.reload
28
26
  end
29
27
 
30
- test "user is confirmed" do
28
+ test 'user is confirmed' do
31
29
  assert @new_user.confirmed?
32
30
  end
33
31
 
34
- test "user can be authenticated via confirmation link" do
32
+ test 'user can be authenticated via confirmation link' do
35
33
  # hard coded in override controller
36
- override_proof_str = "(^^,)"
34
+ override_proof_str = '(^^,)'
37
35
 
38
36
  # ensure present in redirect URL
39
- override_proof_param = URI.unescape(response.headers["Location"].match(/override_proof=([^&]*)&/)[1])
37
+ override_proof_param = URI.unescape(response.headers['Location']
38
+ .match(/override_proof=([^&]*)&/)[1])
40
39
 
41
40
  assert_equal override_proof_str, override_proof_param
42
41
  end
@@ -10,22 +10,25 @@ class Overrides::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTe
10
10
  describe Overrides::OmniauthCallbacksController do
11
11
  setup do
12
12
  OmniAuth.config.test_mode = true
13
- OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
14
- :provider => 'facebook',
15
- :uid => '123545',
16
- :info => {
13
+ OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
14
+ provider: 'facebook',
15
+ uid: '123545',
16
+ info: {
17
17
  name: 'chong',
18
18
  email: 'chongbong@aol.com'
19
19
  }
20
- })
20
+ )
21
21
 
22
- @favorite_color = "gray"
22
+ @favorite_color = 'gray'
23
23
 
24
- get_via_redirect '/evil_user_auth/facebook', {
25
- auth_origin_url: Faker::Internet.url,
26
- favorite_color: @favorite_color,
27
- omniauth_window_type: 'newWindow'
28
- }
24
+ get '/evil_user_auth/facebook',
25
+ params: {
26
+ auth_origin_url: Faker::Internet.url,
27
+ favorite_color: @favorite_color,
28
+ omniauth_window_type: 'newWindow'
29
+ }
30
+
31
+ follow_all_redirects!
29
32
 
30
33
  @resource = assigns(:resource)
31
34
  end
@@ -35,7 +38,8 @@ class Overrides::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTe
35
38
  end
36
39
 
37
40
  test 'controller was overridden' do
38
- assert_equal @resource.nickname, Overrides::OmniauthCallbacksController::DEFAULT_NICKNAME
41
+ assert_equal @resource.nickname,
42
+ Overrides::OmniauthCallbacksController::DEFAULT_NICKNAME
39
43
  end
40
44
 
41
45
  test 'whitelisted param was allowed' do
@@ -12,10 +12,11 @@ class Overrides::PasswordsControllerTest < ActionDispatch::IntegrationTest
12
12
  @resource = evil_users(:confirmed_email_user)
13
13
  @redirect_url = Faker::Internet.url
14
14
 
15
- post "/evil_user_auth/password", {
16
- email: @resource.email,
17
- redirect_url: @redirect_url
18
- }
15
+ post '/evil_user_auth/password',
16
+ params: {
17
+ email: @resource.email,
18
+ redirect_url: @redirect_url
19
+ }
19
20
 
20
21
  @mail = ActionMailer::Base.deliveries.last
21
22
  @resource.reload
@@ -24,22 +25,23 @@ class Overrides::PasswordsControllerTest < ActionDispatch::IntegrationTest
24
25
  @mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
25
26
  @mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
26
27
 
27
- get '/evil_user_auth/password/edit', {
28
- reset_password_token: @mail_reset_token,
29
- redirect_url: @mail_redirect_url
30
- }
28
+ get '/evil_user_auth/password/edit',
29
+ params: { reset_password_token: @mail_reset_token,
30
+ redirect_url: @mail_redirect_url }
31
31
 
32
32
  @resource.reload
33
33
 
34
34
  raw_qs = response.location.split('?')[1]
35
35
  @qs = Rack::Utils.parse_nested_query(raw_qs)
36
36
 
37
- @client_id = @qs["client_id"]
38
- @expiry = @qs["expiry"]
39
- @reset_password = @qs["reset_password"]
40
- @token = @qs["token"]
41
- @uid = @qs["uid"]
42
- @override_proof = @qs["override_proof"]
37
+ @access_token = @qs['access-token']
38
+ @client = @qs['client']
39
+ @client_id = @qs['client_id']
40
+ @expiry = @qs['expiry']
41
+ @override_proof = @qs['override_proof']
42
+ @reset_password = @qs['reset_password']
43
+ @token = @qs['token']
44
+ @uid = @qs['uid']
43
45
  end
44
46
 
45
47
  test 'response should have success redirect status' do
@@ -47,12 +49,14 @@ class Overrides::PasswordsControllerTest < ActionDispatch::IntegrationTest
47
49
  end
48
50
 
49
51
  test 'response should contain auth params + override proof' do
52
+ assert @access_token
53
+ assert @client
50
54
  assert @client_id
51
55
  assert @expiry
56
+ assert @override_proof
52
57
  assert @reset_password
53
58
  assert @token
54
59
  assert @uid
55
- assert @override_proof
56
60
  end
57
61
 
58
62
  test 'override proof is correct' do
@@ -12,18 +12,17 @@ class Overrides::RegistrationsControllerTest < ActionDispatch::IntegrationTest
12
12
  @existing_user = evil_users(:confirmed_email_user)
13
13
  @auth_headers = @existing_user.create_new_auth_token
14
14
  @client_id = @auth_headers['client']
15
- @favorite_color = "pink"
16
-
15
+ @favorite_color = 'pink'
17
16
 
18
17
  # ensure request is not treated as batch request
19
18
  age_token(@existing_user, @client_id)
20
19
 
21
20
  # test valid update param
22
- @new_operating_thetan = 1000000
21
+ @new_operating_thetan = 1_000_000
23
22
 
24
- put '/evil_user_auth', {
25
- favorite_color: @favorite_color
26
- }, @auth_headers
23
+ put '/evil_user_auth',
24
+ params: { favorite_color: @favorite_color },
25
+ headers: @auth_headers
27
26
 
28
27
  @data = JSON.parse(response.body)
29
28
  @existing_user.reload
@@ -34,7 +33,8 @@ class Overrides::RegistrationsControllerTest < ActionDispatch::IntegrationTest
34
33
  end
35
34
 
36
35
  test 'controller was overridden' do
37
- assert_equal Overrides::RegistrationsController::OVERRIDE_PROOF, @data["override_proof"]
36
+ assert_equal Overrides::RegistrationsController::OVERRIDE_PROOF,
37
+ @data['override_proof']
38
38
  end
39
39
  end
40
40
  end