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

Improve messages returned to users #4576

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions kobo/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,17 @@
)
}),
(
'JSON object of guidance messages presented to users when they '
'click the "Problems with the token" link after being prompted for '
'their verification token. Markdown syntax is supported, and '
'`##support email##` will be replaced with the value of the '
'`SUPPORT_EMAIL` setting on this page.\n'
'Guidance message presented when users click the '
'"Problems with the token" link.\n\n'
'`##support email##` is a placeholder for the `SUPPORT_EMAIL` '
'setting.\n'
'Markdown syntax is supported.\n'
'The “default” message will be used if no translations are provided.'
' The “default” should be in English.\n'
'To add messages in other languages, follow the example of '
'`some-other-language`, but use a valid language code (e.g. `fr` '
'for French).'
'“some-other-language“, but replace “some-other-language“ with a '
'valid language code (e.g. “fr“ for French).'

),
# Use custom field for schema validation
'i18n_text_jsonfield_schema'
Expand Down Expand Up @@ -437,11 +440,14 @@
),
}),
(
'Create custom guidance text for password complexity. '
'The contents of the message should reflect the password complexity rules '
'as set in Constance. '
'"default" is the fallback language, and will be used if no translation are provided '
'and should be in English.'
'Guidance message presented when users create or modify a password. '
'It should reflect the defined password rules.\n\n'
'Markdown syntax is supported.\n'
'The “default” message will be used if no translations are provided.'
' The “default” should be in English.\n'
'To add messages in other languages, follow the example of '
'“some-other-language“, but replace “some-other-language“ with a '
'valid language code (e.g. “fr“ for French).'
),
'i18n_text_jsonfield_schema',
),
Expand Down
22 changes: 21 additions & 1 deletion kpi/password_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def get_help_text(self):

class UserAttributeSimilarityValidator(BaseUserAttributeSimilarityValidator):

I18N_EXTRA_ATTRIBUTES_MAPPING = {
'full_name': t('Full name'),
'organization': t('Organization name'),
}

def validate(self, password, user=None):
if not config.ENABLE_PASSWORD_USER_ATTRIBUTE_SIMILARITY_VALIDATION:
return
Expand All @@ -128,7 +133,22 @@ def validate(self, password, user=None):
user_extra_details.data.get('organization', ''),
)

super().validate(password, user)
try:
super().validate(password, user)
except ValidationError as e:
# if field is one of the extra attributes, raise another error
# with the translated version
message = e.messages[0]
for (
extra_attribute,
translation,
) in self.I18N_EXTRA_ATTRIBUTES_MAPPING.items():
if extra_attribute in message:
raise ValidationError(
message.replace(extra_attribute, translation.lower())
)

raise e


class CustomRulesValidator:
Expand Down
6 changes: 3 additions & 3 deletions kpi/tests/test_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_user_attribute_similarity_failed(self):

password = 'spongy bob'
error = self._run_validation(password)
assert 'The password is too similar to the full_name' in error
assert 'The password is too similar to the full name' in error

password = 'some@username.com'
error = self._run_validation(password)
Expand All @@ -194,15 +194,15 @@ def test_new_user_attribute_similarity_failed(self):

password = 'johnnydoe'
error = self._run_validation(password, new_user)
assert 'The password is too similar to the full_name' in error
assert 'The password is too similar to the full name' in error

password = 'jd_2023'
error = self._run_validation(password, new_user)
assert 'The password is too similar to the email' in error

password = 'unkn0wnBus1ness'
error = self._run_validation(password, new_user)
assert 'The password is too similar to the organization' in error
assert 'The password is too similar to the organization name' in error

@override_config(
ENABLE_PASSWORD_USER_ATTRIBUTE_SIMILARITY_VALIDATION=True,
Expand Down