-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,3 +1,3 @@ | ||
"""Python toolbox of Ambient Digital containing an abundance of useful tools and gadgets.""" | ||
|
||
__version__ = "9.2.1" | ||
__version__ = "9.3.0" |
Empty file.
Empty file.
This file contains 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,20 @@ | ||
import re | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext as _ | ||
|
||
|
||
class SpecialCharValidator: | ||
""" | ||
The password must contain at least one special character (@#$%!^&*) | ||
""" | ||
|
||
def validate(self, password, user=None): | ||
if not re.findall("[@#$%!^&*]", password): | ||
raise ValidationError( | ||
_('The password has to contain one of the following special characters: "@#$%!^&*"'), | ||
code="password_no_symbol", | ||
) | ||
|
||
def get_help_text(self): | ||
return _('The password has to contain one of the following special characters: "@#$%!^&*"') |
This file contains 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,25 @@ | ||
# Validators | ||
|
||
This package provides additional validators. In the Django ecosystem, you can have two kinds of validators: [Password | ||
validators](https://docs.djangoproject.com/en/4.2/topics/auth/passwords/) and form validators. The first are used in the | ||
settings to add rules for your users passwords. The latter is employed in models and forms to validate user input. | ||
|
||
## Auth password validators | ||
|
||
### Special characters required | ||
|
||
Adding ths validator will require your users to add at least one of the following special characters to their | ||
password: `@#$%!^&*`. | ||
|
||
```python | ||
# Django settings | ||
|
||
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators | ||
AUTH_PASSWORD_VALIDATORS = [ | ||
... | ||
{ | ||
"NAME": "ambient_toolbox.validators.auth_password.SpecialCharValidator", | ||
}, | ||
] | ||
|
||
``` |
This file contains 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 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
Empty file.
Empty file.
This file contains 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,34 @@ | ||
from django.contrib.auth.models import User | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase, override_settings | ||
|
||
from ambient_toolbox.validators.auth_password.special_chars import SpecialCharValidator | ||
|
||
|
||
class SpecialCharValidatorTest(TestCase): | ||
def test_validate_happy_path(self): | ||
validator = SpecialCharValidator() | ||
self.assertIsNone(validator.validate("Admin0404!")) | ||
|
||
def test_validate_missing_special_char(self): | ||
validator = SpecialCharValidator() | ||
with self.assertRaisesMessage( | ||
ValidationError, 'The password has to contain one of the following special characters: "@#$%!^&*"' | ||
): | ||
validator.validate("EasyPassword") | ||
|
||
def test_get_help_text_regular(self): | ||
validator = SpecialCharValidator() | ||
self.assertEqual( | ||
validator.get_help_text(), 'The password has to contain one of the following special characters: "@#$%!^&*"' | ||
) | ||
|
||
@override_settings(AUTH_PASSWORD_VALIDATORS=["ambient_toolbox.validators.SpecialCharValidator"]) | ||
def test_functional_happy_path(self): | ||
user = User() | ||
self.assertIsNone(user.set_password("Admin0404!")) | ||
|
||
@override_settings(AUTH_PASSWORD_VALIDATORS=["ambient_toolbox.validators.auth_password.SpecialCharValidator"]) | ||
def test_functional_special_char_missing(self): | ||
user = User() | ||
self.assertFalse(user.set_password("EasyPassword")) |