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

Translate error messages in the activatable and timeoutable hooks #5767

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/devise/hooks/activatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
if record && record.respond_to?(:active_for_authentication?) && !record.active_for_authentication?
scope = options[:scope]
warden.logout(scope)
throw :warden, scope: scope, message: record.inactive_message
throw :warden, scope: scope, message: record.inactive_message, locale: options[:locale]
end
end
2 changes: 1 addition & 1 deletion lib/devise/hooks/timeoutable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
record.timedout?(last_request_at) &&
!proxy.remember_me_is_active?(record)
Devise.sign_out_all_scopes ? proxy.sign_out : proxy.sign_out(scope)
throw :warden, scope: scope, message: :timeout
throw :warden, scope: scope, message: :timeout, locale: options[:locale]
end

unless env['devise.skip_trackable']
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/devise_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DeviseHelperTest < Devise::IntegrationTest
mongoid: model_labels
}

I18n.available_locales
I18n.backend.eager_load!
I18n.backend.store_translations(:en, translations)
end

Expand Down
22 changes: 22 additions & 0 deletions test/integration/activatable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'test_helper'

class ActivatableTest < Devise::IntegrationTest
test 'shows the localized error message for inactive accounts' do
store_translations(
en: { devise: { failure: { unconfirmed: 'Unconfirmed account.' } } },
de: { devise: { failure: { unconfirmed: 'Unbestätigtes Konto!' } } }
) do
I18n.with_locale(:de) do
user = create_user(confirm: false, confirmation_sent_at: 1.hour.ago)
get new_user_session_path
fill_in 'email', with: user.email
fill_in 'password', with: user.password
click_button 'Log In'

assert_contain('Unbestätigtes Konto!')
end
end
end
end
4 changes: 3 additions & 1 deletion test/integration/authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ class AuthenticationRedirectTest < Devise::IntegrationTest
end

test 'require_no_authentication should set the already_authenticated flash message as admin' do
store_translations :en, devise: { failure: { admin: { already_authenticated: 'You are already signed in as admin.' } } } do
store_translations en: {
devise: { failure: { admin: { already_authenticated: 'You are already signed in as admin.' } } }
} do
sign_in_as_admin
visit new_admin_session_path
assert_equal "You are already signed in as admin.", flash[:alert]
Expand Down
4 changes: 2 additions & 2 deletions test/integration/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def resend_confirmation
end

test 'error message is configurable by resource name' do
store_translations :en, devise: {
failure: { user: { unconfirmed: "Not confirmed user" } }
store_translations en: {
devise: { failure: { user: { unconfirmed: "Not confirmed user" } } }
} do
sign_in_as_user(confirm: false)
assert_contain 'Not confirmed user'
Expand Down
12 changes: 9 additions & 3 deletions test/integration/database_authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest
end

test 'sign in with invalid email should return to sign in form with error message' do
store_translations :en, devise: { failure: { admin: { not_found_in_database: 'Invalid email address' } } } do
store_translations en: {
devise: { failure: { admin: { not_found_in_database: 'Invalid email address' } } }
} do
sign_in_as_admin do
fill_in 'email', with: '[email protected]'
end
Expand All @@ -76,7 +78,9 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest

test 'when in paranoid mode and without a valid e-mail' do
swap Devise, paranoid: true do
store_translations :en, devise: { failure: { not_found_in_database: 'Not found in database' } } do
store_translations en: {
devise: { failure: { not_found_in_database: 'Not found in database' } }
} do
sign_in_as_user do
fill_in 'email', with: '[email protected]'
end
Expand All @@ -88,7 +92,9 @@ class DatabaseAuthenticationTest < Devise::IntegrationTest
end

test 'error message is configurable by resource name' do
store_translations :en, devise: { failure: { admin: { invalid: "Invalid credentials" } } } do
store_translations en: {
devise: { failure: { admin: { invalid: "Invalid credentials" } } }
} do
sign_in_as_admin do
fill_in 'password', with: 'abcdef'
end
Expand Down
10 changes: 2 additions & 8 deletions test/integration/lockable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ def send_unlock_request
end

test 'error message is configurable by resource name' do
store_translations :en, devise: {
failure: {user: {locked: "You are locked!"}}
} do

store_translations en: { devise: { failure: { user: { locked: "You are locked!"} } } } do
user = create_user(locked: true)
user.failed_attempts = User.maximum_attempts + 1
user.save!
Expand All @@ -117,10 +114,7 @@ def send_unlock_request
end

test "user should not be able to sign in when locked" do
store_translations :en, devise: {
failure: {user: {locked: "You are locked!"}}
} do

store_translations en: { devise: { failure: { user: { locked: "You are locked!"} } } } do
user = create_user(locked: true)
user.failed_attempts = User.maximum_attempts + 1
user.save!
Expand Down
26 changes: 20 additions & 6 deletions test/integration/timeoutable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def last_request_at
end

test 'error message with i18n' do
store_translations :en, devise: {
failure: { user: { timeout: 'Session expired!' } }
} do
store_translations en: { devise: { failure: { user: { timeout: 'Session expired!' } } } } do
user = sign_in_as_user

get expire_user_path(user)
Expand All @@ -154,9 +152,7 @@ def last_request_at
end

test 'error message with i18n with double redirect' do
store_translations :en, devise: {
failure: { user: { timeout: 'Session expired!' } }
} do
store_translations en: { devise: { failure: { user: { timeout: 'Session expired!' } } } } do
user = sign_in_as_user

get expire_user_path(user)
Expand All @@ -167,6 +163,24 @@ def last_request_at
end
end

test 'shows the localized error message on session timeout' do
# Set up the error message in the default and in a different locale
store_translations(
en: { devise: { failure: { user: { timeout: 'Session expired!' } } } },
de: { devise: { failure: { timeout: 'Sitzung abgelaufen!' } } }
) do
I18n.with_locale(:de) do
user = sign_in_as_user

get expire_user_path(user)
get users_path
follow_redirect!
follow_redirect!
assert_contain('Sitzung abgelaufen!')
end
end
end

test 'time out not triggered if remembered' do
user = sign_in_as_user remember_me: true
get expire_user_path(user)
Expand Down
8 changes: 6 additions & 2 deletions test/mailers/confirmation_instructions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ def mail
end

test 'set up subject from I18n' do
store_translations :en, devise: { mailer: { confirmation_instructions: { subject: 'Account Confirmation' } } } do
store_translations en: {
devise: { mailer: { confirmation_instructions: { subject: 'Account Confirmation' } } }
} do
assert_equal 'Account Confirmation', mail.subject
end
end

test 'subject namespaced by model' do
store_translations :en, devise: { mailer: { confirmation_instructions: { user_subject: 'User Account Confirmation' } } } do
store_translations en: {
devise: { mailer: { confirmation_instructions: { user_subject: 'User Account Confirmation' } } }
} do
assert_equal 'User Account Confirmation', mail.subject
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/mailers/email_changed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ def mail
end

test 'set up subject from I18n' do
store_translations :en, devise: { mailer: { email_changed: { subject: 'Email Has Changed' } } } do
store_translations en: {
devise: { mailer: { email_changed: { subject: 'Email Has Changed' } } }
} do
assert_equal 'Email Has Changed', mail.subject
end
end

test 'subject namespaced by model' do
store_translations :en, devise: { mailer: { email_changed: { user_subject: 'User Email Has Changed' } } } do
store_translations en: {
devise: { mailer: { email_changed: { user_subject: 'User Email Has Changed' } } }
} do
assert_equal 'User Email Has Changed', mail.subject
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/mailers/reset_password_instructions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ def mail
end

test 'set up subject from I18n' do
store_translations :en, devise: { mailer: { reset_password_instructions: { subject: 'Reset instructions' } } } do
store_translations en: {
devise: { mailer: { reset_password_instructions: { subject: 'Reset instructions' } } }
} do
assert_equal 'Reset instructions', mail.subject
end
end

test 'subject namespaced by model' do
store_translations :en, devise: { mailer: { reset_password_instructions: { user_subject: 'User Reset Instructions' } } } do
store_translations en: {
devise: { mailer: { reset_password_instructions: { user_subject: 'User Reset Instructions' } } }
} do
assert_equal 'User Reset Instructions', mail.subject
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/mailers/unlock_instructions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ def mail
end

test 'set up subject from I18n' do
store_translations :en, devise: { mailer: { unlock_instructions: { subject: 'Yo unlock instructions' } } } do
store_translations en: {
devise: { mailer: { unlock_instructions: { subject: 'Yo unlock instructions' } } }
} do
assert_equal 'Yo unlock instructions', mail.subject
end
end

test 'subject namespaced by model' do
store_translations :en, devise: { mailer: { unlock_instructions: { user_subject: 'User Unlock Instructions' } } } do
store_translations en: {
devise: { mailer: { unlock_instructions: { user_subject: 'User Unlock Instructions' } } }
} do
assert_equal 'User Unlock Instructions', mail.subject
end
end
Expand Down
13 changes: 6 additions & 7 deletions test/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ def setup_mailer
ActionMailer::Base.deliveries = []
end

def store_translations(locale, translations, &block)
# Calling 'available_locales' before storing the translations to ensure
# that the I18n backend will be initialized before we store our custom
# translations, so they will always override the translations for the
# YML file.
I18n.available_locales
I18n.backend.store_translations(locale, translations)
def store_translations(translations)
# Eager-loading the backend before storing the translations ensures that the I18n backend will
# always be initialized before we store our custom translations, so the test-specific
# translations override the translations from the YML files.
I18n.backend.eager_load!
translations.each { |locale, entries| I18n.backend.store_translations(locale, entries) }
yield
ensure
I18n.reload!
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require "orm/#{DEVISE_ORM}"

I18n.load_path.concat Dir["#{File.dirname(__FILE__)}/support/locale/*.yml"]
# Allow setting test-specific locales even if they are not defined in the locale YAML files.
I18n.enforce_available_locales = false

require 'mocha/minitest'
require 'timecop'
Expand Down