Skip to content

Commit 356b094

Browse files
grantzaucarlosantoniodasilva
authored andcommitted
Downcase authentication keys and humanize error message (#4834)
"Invalid Email or password." is grammatically incorrect, a change introduced a while ago by #4014. Signed-off-by: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
1 parent 9a149ff commit 356b094

6 files changed

Lines changed: 28 additions & 16 deletions

File tree

lib/devise/failure_app.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ def i18n_message(default = nil)
111111
options[:scope] = "devise.failure"
112112
options[:default] = [message]
113113
auth_keys = scope_class.authentication_keys
114-
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key) }
114+
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key).downcase }
115115
options[:authentication_keys] = keys.join(I18n.t(:"support.array.words_connector"))
116116
options = i18n_options(options)
117-
118-
I18n.t(:"#{scope}.#{message}", **options)
117+
translated_message = I18n.t(:"#{scope}.#{message}", **options)
118+
# only call `#humanize` when the message is `:invalid` to ensure the original format
119+
# of other messages - like `:does_not_exist` - is kept.
120+
message == :invalid ? translated_message.humanize : translated_message
119121
else
120122
message.to_s
121123
end

test/failure_app_test.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,27 @@ def call_failure(env_params = {})
184184

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

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

198+
test 'downcases authentication_keys for the flash message' do
199+
call_failure('warden' => OpenStruct.new(message: :invalid))
200+
assert_equal 'Invalid email or password.', @request.flash[:alert]
201+
end
202+
203+
test 'humanizes the flash message' do
204+
call_failure('warden' => OpenStruct.new(message: :invalid))
205+
assert_equal @request.flash[:alert], @request.flash[:alert].humanize
206+
end
207+
198208
test 'uses custom i18n options' do
199209
call_failure('warden' => OpenStruct.new(message: :does_not_exist), app: FailureWithI18nOptions)
200210
assert_equal 'User Steve does not exist', @request.flash[:alert]
@@ -288,7 +298,7 @@ def call_failure(env_params = {})
288298

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

294304
test 'respects the i18n locale passed via warden options when responding to HTTP request' do
@@ -343,7 +353,7 @@ def call_failure(env_params = {})
343353
}
344354
call_failure(env)
345355
assert_includes @response.third.body, '<h2>Log in</h2>'
346-
assert_includes @response.third.body, 'Invalid Email or password.'
356+
assert_includes @response.third.body, 'Invalid email or password.'
347357
end
348358

349359
test 'calls the original controller if not confirmed email' do
@@ -378,7 +388,7 @@ def call_failure(env_params = {})
378388
}
379389
call_failure(env)
380390
assert_includes @response.third.body, '<h2>Log in</h2>'
381-
assert_includes @response.third.body, 'Invalid Email or password.'
391+
assert_includes @response.third.body, 'Invalid email or password.'
382392
assert_equal '/sample', @request.env["SCRIPT_NAME"]
383393
assert_equal '/users/sign_in', @request.env["PATH_INFO"]
384394
end
@@ -409,7 +419,7 @@ def call_failure(env_params = {})
409419
call_failure(env)
410420

411421
assert_equal 422, @response.first
412-
assert_includes @response.third.body, 'Invalid Email or password.'
422+
assert_includes @response.third.body, 'Invalid email or password.'
413423
end
414424
end
415425

@@ -435,7 +445,7 @@ def call_failure(env_params = {})
435445
call_failure(env)
436446

437447
assert_equal 200, @response.first
438-
assert_includes @response.third.body, 'Invalid Email or password.'
448+
assert_includes @response.third.body, 'Invalid email or password.'
439449
end
440450

441451
test 'users default hardcoded responder `redirect_status` for the status code since responders version does not support configuring it' do

test/integration/authenticatable_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ class AuthenticationKeysTest < Devise::IntegrationTest
563563
test 'missing authentication keys cause authentication to abort' do
564564
swap Devise, authentication_keys: [:subdomain] do
565565
sign_in_as_user
566-
assert_contain "Invalid Subdomain or password."
566+
assert_contain "Invalid subdomain or password."
567567
assert_not warden.authenticated?(:user)
568568
end
569569
end
@@ -602,7 +602,7 @@ class AuthenticationRequestKeysTest < Devise::IntegrationTest
602602

603603
swap Devise, request_keys: [:subdomain] do
604604
sign_in_as_user
605-
assert_contain "Invalid Email or password."
605+
assert_contain "Invalid email or password."
606606
assert_not warden.authenticated?(:user)
607607
end
608608
end

test/integration/confirmable_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def resend_confirmation
151151
fill_in 'password', with: 'invalid'
152152
end
153153

154-
assert_contain 'Invalid Email or password'
154+
assert_contain 'Invalid email or password'
155155
assert_not warden.authenticated?(:user)
156156
end
157157
end

test/integration/database_authenticatable_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest
7070
fill_in 'password', with: 'abcdef'
7171
end
7272

73-
assert_contain 'Invalid Email or password'
73+
assert_contain 'Invalid email or password'
7474
assert_not warden.authenticated?(:admin)
7575
end
7676

@@ -82,7 +82,7 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest
8282
end
8383

8484
assert_not_contain 'Not found in database'
85-
assert_contain 'Invalid Email or password.'
85+
assert_contain 'Invalid email or password.'
8686
end
8787
end
8888
end

test/integration/http_authenticatable_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class HttpAuthenticationTest < Devise::IntegrationTest
5252
sign_in_as_new_user_with_http("unknown")
5353
assert_equal 401, status
5454
assert_equal "application/json; charset=utf-8", headers["Content-Type"]
55-
assert_match '"error":"Invalid Email or password."', response.body
55+
assert_match '"error":"Invalid email or password."', response.body
5656
end
5757

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

0 commit comments

Comments
 (0)