Skip to content

Commit

Permalink
Make paranoid option return success status code and message regardles…
Browse files Browse the repository at this point in the history
…s of result (#1524)
  • Loading branch information
Keith Doggett authored Mar 16, 2022
1 parent 23d6b81 commit 798255e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def render_create_success

def render_not_found_error
if Devise.paranoid
render_error(404, I18n.t('devise_token_auth.confirmations.sended_paranoid'))
render_create_success
else
render_error(404, I18n.t('devise_token_auth.confirmations.user_not_found', email: @email))
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def password_resource_params

def render_not_found_error
if Devise.paranoid
render_error(404, I18n.t('devise_token_auth.passwords.sended_paranoid'))
render_create_success
else
render_error(404, I18n.t('devise_token_auth.passwords.user_not_found', email: @email))
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/devise_token_auth/unlocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def render_show_error

def render_not_found_error
if Devise.paranoid
render_error(404, I18n.t('devise_token_auth.unlocks.sended_paranoid'))
render_create_success
else
render_error(404, I18n.t('devise_token_auth.unlocks.user_not_found', email: @email))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,30 @@ def token_and_client_config_from(body)
test 'response should contain message' do
assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended_paranoid', email: @resource.email)
end

test 'response should return success status' do
assert_equal 200, response.status
end
end

describe 'on failure' do
before do
swap Devise, paranoid: true do
@email = '[email protected]'
post :create,
params: { email: '[email protected]',
params: { email: @email,
redirect_url: @redirect_url },
xhr: true
@data = JSON.parse(response.body)
end
end

test 'response should contain errors' do
assert_equal @data['errors'], [I18n.t('devise_token_auth.confirmations.sended_paranoid')]
test 'response should not contain errors' do
assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended_paranoid', email: @email)
end

test 'response should return success status' do
assert_equal 200, response.status
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/controllers/devise_token_auth/passwords_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
end
end

test 'unknown user should return 404' do
assert_equal 404, response.status
test 'response should return success status' do
assert_equal 200, response.status
end

test 'errors should be returned' do
assert @data['errors']
assert_equal @data['errors'],
[I18n.t('devise_token_auth.passwords.sended_paranoid')]
test 'response should contain message' do
assert_equal \
@data['message'],
I18n.t('devise_token_auth.passwords.sended_paranoid')
end
end
end
Expand Down
34 changes: 28 additions & 6 deletions test/controllers/devise_token_auth/unlocks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ class DeviseTokenAuth::UnlocksControllerTest < ActionController::TestCase
end
end

test 'unknown user should return 404' do
assert_equal 404, response.status
test 'should always return success' do
assert_equal 200, response.status
end

test 'errors should be returned' do
assert @data['errors']
assert_equal @data['errors'], [I18n.t('devise_token_auth.unlocks.sended_paranoid')]
test 'errors should not be returned' do
assert @data['success']
assert_equal \
@data['message'],
I18n.t('devise_token_auth.unlocks.sended_paranoid')
end
end

describe 'successfully requested unlock' do
describe 'successfully requested unlock without paranoid mode' do
before do
post :create, params: { email: @resource.email }

Expand All @@ -103,6 +105,26 @@ class DeviseTokenAuth::UnlocksControllerTest < ActionController::TestCase
end
end

describe 'successfully requested unlock with paranoid mode' do
before do
swap Devise, paranoid: true do
post :create, params: { email: @resource.email }
@data = JSON.parse(response.body)
end
end

test 'should always return success' do
assert_equal 200, response.status
end

test 'errors should not be returned' do
assert @data['success']
assert_equal \
@data['message'],
I18n.t('devise_token_auth.unlocks.sended_paranoid')
end
end

describe 'case-sensitive email' do
before do
post :create, params: { email: @resource.email }
Expand Down

0 comments on commit 798255e

Please sign in to comment.