-
Notifications
You must be signed in to change notification settings - Fork 0
Bugfix user birth day has no limit #231
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39302e4
Refactor `BaseSerializerTestCase` based tests
8501730
Refactor strings
f149ce5
Add validation of user age (for user UserSerializer) and unit tests f…
e385fc7
Add custom `UserAgeValidator` and unit tests for it
25cc3bf
[M] Move user's age validation to model
e62ad3c
Add note to realease-notes
kbeker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,47 @@ | ||
| # pylint: disable=line-too-long | ||
| from enum import Enum | ||
|
|
||
| from django.utils.translation import ugettext_lazy | ||
| from django.utils.translation import ugettext_lazy as _ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 👍 👍 |
||
|
|
||
| from users.common import constants | ||
| from utils.mixins import NotCallableMixin | ||
|
|
||
|
|
||
| class ConfirmationMessages: | ||
| SUCCESSFUL_UPDATE_USER_MESSAGE = ugettext_lazy("Account has been successfully updated!") | ||
| SUCCESSFUL_USER_PASSWORD_CHANGE_MESSAGE = ugettext_lazy("Your password has been successfully updated!") | ||
| FAILED_USER_PASSWORD_CHANGE_MESSAGE = ugettext_lazy("Please correct the error below.") | ||
| SUCCESSFUL_UPDATE_USER_MESSAGE = _("Account has been successfully updated!") | ||
| SUCCESSFUL_USER_PASSWORD_CHANGE_MESSAGE = _("Your password has been successfully updated!") | ||
| FAILED_USER_PASSWORD_CHANGE_MESSAGE = _("Please correct the error below.") | ||
|
|
||
|
|
||
| class PermissionsMessage: | ||
| NONE_ADMIN_USER = ugettext_lazy("You are not allowed to enter - for administration only.") | ||
| NONE_ADMIN_OR_OWNER_USER = ugettext_lazy("It's none of your business.") | ||
| NONE_ADMIN_USER = _("You are not allowed to enter - for administration only.") | ||
| NONE_ADMIN_OR_OWNER_USER = _("It's none of your business.") | ||
|
|
||
|
|
||
| class CustomUserAdminText: | ||
| PERSONAL_INFO = ugettext_lazy("Personal info") | ||
| STATUS = ugettext_lazy("Status") | ||
| PERMISSIONS = ugettext_lazy("Permissions") | ||
| IMPORTANT_DATES = ugettext_lazy("Important dates") | ||
| PERSONAL_INFO = _("Personal info") | ||
| STATUS = _("Status") | ||
| PERMISSIONS = _("Permissions") | ||
| IMPORTANT_DATES = _("Important dates") | ||
|
|
||
|
|
||
| class CustomUserModelText: | ||
| VERBOSE_NAME_USER = ugettext_lazy("user") | ||
| VERBOSE_NAME_PLURAL_USERS = ugettext_lazy("users") | ||
|
|
||
| EMAIL_ADDRESS = ugettext_lazy("email address") | ||
| FIRST_NAME = ugettext_lazy("first name") | ||
| LAST_NAME = ugettext_lazy("last name") | ||
| IS_STAFF = ugettext_lazy("staff status") | ||
| STAFF_HELP_TEXT = ugettext_lazy("Designates whether the user can log into this admin site.") | ||
| IS_ACTIVE = ugettext_lazy("active") | ||
| ACTIVE_HELP_TEXT = ugettext_lazy( | ||
| VERBOSE_NAME_USER = _("user") | ||
| VERBOSE_NAME_PLURAL_USERS = _("users") | ||
|
|
||
| EMAIL_ADDRESS = _("email address") | ||
| FIRST_NAME = _("first name") | ||
| LAST_NAME = _("last name") | ||
| IS_STAFF = _("staff status") | ||
| STAFF_HELP_TEXT = _("Designates whether the user can log into this admin site.") | ||
| IS_ACTIVE = _("active") | ||
| ACTIVE_HELP_TEXT = _( | ||
| "Designates whether this user should be treated as active. Unselect this instead of deleting accounts." | ||
| ) | ||
| DATE_JOINED = ugettext_lazy("date joined") | ||
| DATE_OF_BIRTH = ugettext_lazy("date of birth") | ||
| UPDATED_AT = ugettext_lazy("updated at") | ||
| PHONE_REGEX_MESSAGE = ugettext_lazy( | ||
| "Phone number must be entered in the format: '999999999'. Up to 15 digits allowed." | ||
| ) | ||
| DATE_JOINED = _("date joined") | ||
| DATE_OF_BIRTH = _("date of birth") | ||
| UPDATED_AT = _("updated at") | ||
| PHONE_REGEX_MESSAGE = _("Phone number must be entered in the format: '999999999'. Up to 15 digits allowed.") | ||
|
|
||
|
|
||
| class ValidationErrorText: | ||
|
|
@@ -56,28 +54,27 @@ class ValidationErrorText: | |
| "Please enter an e-mail address with a valid domain (" + ", ".join(constants.VALID_EMAIL_DOMAIN_LIST) + ")" | ||
| ) | ||
| VALIDATION_ERROR_EMAIL_MESSAGE_DOMAIN_SHORT = "Please enter an e-mail address with a valid domain" | ||
| VALIDATION_ERROR_SIGNUP_EMAIL_MESSAGE = ugettext_lazy("A user is already registered with this e-mail address.") | ||
| VALIDATION_ERROR_SIGNUP_PASSWORD_MESSAGE = ugettext_lazy("The two password fields didn't match.") | ||
| VALIDATION_ERROR_SIGNUP_EMAIL_MESSAGE = _("A user is already registered with this e-mail address.") | ||
| VALIDATION_ERROR_SIGNUP_PASSWORD_MESSAGE = _("The two password fields didn't match.") | ||
| VALIDATION_ERROR_AGE_NOT_ACCEPTED = _("User can't be below 18 or above 99 years old.") | ||
|
|
||
|
|
||
| class CustomUserCountryText: | ||
| POLAND = ugettext_lazy("Poland") | ||
| UNITED_STATES = ugettext_lazy("United States") | ||
| UNITED_KINGDOM = ugettext_lazy("United Kingdom") | ||
| GERMANY = ugettext_lazy("Germany") | ||
| FRANCE = ugettext_lazy("France") | ||
| POLAND = _("Poland") | ||
| UNITED_STATES = _("United States") | ||
| UNITED_KINGDOM = _("United Kingdom") | ||
| GERMANY = _("Germany") | ||
| FRANCE = _("France") | ||
|
|
||
|
|
||
| class CustomUserUserTypeText: | ||
| EMPLOYEE = ugettext_lazy("Employee") | ||
| MANAGER = ugettext_lazy("Manager") | ||
| ADMIN = ugettext_lazy("Admin") | ||
| EMPLOYEE = _("Employee") | ||
| MANAGER = _("Manager") | ||
| ADMIN = _("Admin") | ||
|
|
||
|
|
||
| class SuccessInfoAfterRegistrationText(NotCallableMixin, Enum): | ||
| CONGRATULATIONS = ugettext_lazy("Congratulations!") | ||
| ACCOUNT_CREATED = ugettext_lazy("Your account has been successfully created! Now you can sign in!") | ||
| REDIRECTION_INFO = ugettext_lazy( | ||
| "You will be redirected in few seconds to the login site or press a button to make it faster." | ||
| ) | ||
| OKAY_BUTTON = ugettext_lazy("Okay!") | ||
| CONGRATULATIONS = _("Congratulations!") | ||
| ACCOUNT_CREATED = _("Your account has been successfully created! Now you can sign in!") | ||
| REDIRECTION_INFO = _("You will be redirected in few seconds to the login site or press a button to make it faster.") | ||
| OKAY_BUTTON = _("Okay!") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Generated by Django 2.1.8 on 2019-05-27 11:53 | ||
|
|
||
| from django.db import migrations, models | ||
| import users.validators | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('users', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='customuser', | ||
| name='date_of_birth', | ||
| field=models.DateField(blank=True, null=True, validators=[users.validators.UserAgeValidator(18, 99)], verbose_name='date of birth'), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import datetime | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from django.test import TestCase | ||
| from freezegun import freeze_time | ||
|
|
||
| from users.validators import UserAgeValidator | ||
|
|
||
|
|
||
| @freeze_time("2019-05-27") | ||
| class TestUserAgeValidator(TestCase): | ||
| def setUp(self): | ||
| self.user_age_validator = UserAgeValidator( | ||
| UserAgeValidator.MINIMAL_ACCETABLE_AGLE, UserAgeValidator.MAXIMAL_ACCEPTABLE_AGE | ||
| ) | ||
|
|
||
| def test_that_users_age_can_be_none(self): | ||
| self._assert_date_of_birth_is_valid(None) | ||
|
|
||
| def test_that_users_age_can_be_equal_to_lower_limit(self): | ||
| # user is exactly 18 years old | ||
| date_of_birth = datetime.datetime.strptime("2001-05-27", "%Y-%m-%d").date() | ||
| self._assert_date_of_birth_is_valid(date_of_birth) | ||
|
|
||
| def test_that_users_age_can_be_equal_to_upper_limit(self): | ||
| # user is exactly 100 years old - 1 day (still 99) | ||
| date_of_birth = datetime.datetime.strptime("1919-05-28", "%Y-%m-%d").date() | ||
| self._assert_date_of_birth_is_valid(date_of_birth) | ||
|
|
||
| def test_that_when_users_age_is_below_lower_limit_validation_error_is_raised(self): | ||
| # user is exactly 18 years old - 1 day (still 17) | ||
| date_of_birth = datetime.datetime.strptime("2001-05-28", "%Y-%m-%d").date() | ||
| with self.assertRaises(ValidationError): | ||
| self.user_age_validator(date_of_birth) | ||
|
|
||
| def test_that_when_users_age_is_above_upper_limit_validation_error_is_raised(self): | ||
| # user is exactly 100 years old | ||
| date_of_birth = datetime.datetime.strptime("1919-05-27", "%Y-%m-%d").date() | ||
| with self.assertRaises(ValidationError): | ||
| self.user_age_validator(date_of_birth) | ||
|
|
||
| def _assert_date_of_birth_is_valid(self, date_of_birth): | ||
| try: | ||
| self.user_age_validator(date_of_birth) | ||
| except Exception as e: # pylint:disable=broad-except | ||
| self.fail(f"Unexpected exception {str(e)} has occurred!") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍