Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downcase authentication keys and humanize error message #4834

Merged
merged 3 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/devise/failure_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,14 @@ def i18n_message(default = nil)
options[:scope] = "devise.failure"
options[:default] = [message]
auth_keys = scope_class.authentication_keys
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key) }
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key).downcase }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add tests for it? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, created the pull request a bit to early!

Copy link
Author

@grantzau grantzau Apr 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edited the existing tests. They check, that the downcasing are done. There's not really a test to check the added humanize method though, not sure, if that's necessary?

Copy link
Collaborator

@feliperenan feliperenan Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add tests to ensure that it's the expected behavior.

options[:authentication_keys] = keys.join(I18n.translate(:"support.array.words_connector"))
options = i18n_options(options)
translated_message = I18n.t(:"#{scope}.#{message}", options)

I18n.t(:"#{scope}.#{message}", options)
# only call `#humanize` when the message is `:invalid` to ensure the original format
# of other messages - like `:does_not_exist` - is kept.
message == :invalid ? translated_message.humanize : translated_message
else
message.to_s
end
Expand Down
20 changes: 15 additions & 5 deletions test/failure_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,27 @@ def call_failure(env_params={})

test 'uses the proxy failure message as symbol' do
call_failure('warden' => OpenStruct.new(message: :invalid))
assert_equal 'Invalid Email or password.', @request.flash[:alert]
assert_equal 'Invalid email or password.', @request.flash[:alert]
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
end

test 'supports authentication_keys as a Hash for the flash message' do
swap Devise, authentication_keys: { email: true, login: true } do
call_failure('warden' => OpenStruct.new(message: :invalid))
assert_equal 'Invalid Email, Login or password.', @request.flash[:alert]
assert_equal 'Invalid email, login or password.', @request.flash[:alert]
end
end

test 'downcases authentication_keys for the flash message' do
call_failure('warden' => OpenStruct.new(message: :invalid))
assert_equal 'Invalid email or password.', @request.flash[:alert]
end

test 'humanizes the flash message' do
call_failure('warden' => OpenStruct.new(message: :invalid))
assert_equal @request.flash[:alert], @request.flash[:alert].humanize
end

test 'uses custom i18n options' do
call_failure('warden' => OpenStruct.new(message: :does_not_exist), app: FailureWithI18nOptions)
assert_equal 'User Steve does not exist', @request.flash[:alert]
Expand Down Expand Up @@ -250,7 +260,7 @@ def call_failure(env_params={})

test 'uses the failure message as response body' do
call_failure('formats' => Mime[:xml], 'warden' => OpenStruct.new(message: :invalid))
assert_match '<error>Invalid Email or password.</error>', @response.third.body
assert_match '<error>Invalid email or password.</error>', @response.third.body
end

context 'on ajax call' do
Expand Down Expand Up @@ -299,7 +309,7 @@ def call_failure(env_params={})
}
call_failure(env)
assert @response.third.body.include?('<h2>Log in</h2>')
assert @response.third.body.include?('Invalid Email or password.')
assert @response.third.body.include?('Invalid email or password.')
end

test 'calls the original controller if not confirmed email' do
Expand Down Expand Up @@ -334,7 +344,7 @@ def call_failure(env_params={})
}
call_failure(env)
assert @response.third.body.include?('<h2>Log in</h2>')
assert @response.third.body.include?('Invalid Email or password.')
assert @response.third.body.include?('Invalid email or password.')
assert_equal @request.env["SCRIPT_NAME"], '/sample'
assert_equal @request.env["PATH_INFO"], '/users/sign_in'
end
Expand Down
4 changes: 2 additions & 2 deletions test/integration/authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class AuthenticationKeysTest < Devise::IntegrationTest
test 'missing authentication keys cause authentication to abort' do
swap Devise, authentication_keys: [:subdomain] do
sign_in_as_user
assert_contain "Invalid Subdomain or password."
assert_contain "Invalid subdomain or password."
refute warden.authenticated?(:user)
end
end
Expand Down Expand Up @@ -596,7 +596,7 @@ class AuthenticationRequestKeysTest < Devise::IntegrationTest

swap Devise, request_keys: [:subdomain] do
sign_in_as_user
assert_contain "Invalid Email or password."
assert_contain "Invalid email or password."
refute warden.authenticated?(:user)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def resend_confirmation
fill_in 'password', with: 'invalid'
end

assert_contain 'Invalid Email or password'
assert_contain 'Invalid email or password'
refute warden.authenticated?(:user)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/integration/database_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest
fill_in 'password', with: 'abcdef'
end

assert_contain 'Invalid Email or password'
assert_contain 'Invalid email or password'
refute warden.authenticated?(:admin)
end

Expand All @@ -82,7 +82,7 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest
end

assert_not_contain 'Not found in database'
assert_contain 'Invalid Email or password.'
assert_contain 'Invalid email or password.'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/http_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HttpAuthenticationTest < Devise::IntegrationTest
sign_in_as_new_user_with_http("unknown")
assert_equal 401, status
assert_equal "application/xml; charset=utf-8", headers["Content-Type"]
assert_match "<error>Invalid Email or password.</error>", response.body
assert_match "<error>Invalid email or password.</error>", response.body
end

test 'returns a custom response with www-authenticate and chosen realm' do
Expand Down