Skip to content

Commit

Permalink
v9.3.0 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
GitRon authored Nov 24, 2023
1 parent 023542e commit dd171e4
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

**9.3.0** (2023-11-21)
* Added auth password validator for special chars `SpecialCharValidator`

**9.2.1** (2023-11-21)
* Fixed crash in GitLab coverage service due to `check=True`

Expand Down
2 changes: 1 addition & 1 deletion ambient_toolbox/__init__.py
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.
20 changes: 20 additions & 0 deletions ambient_toolbox/validators/auth_password/special_chars.py
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: "@#$%!^&*"')
25 changes: 25 additions & 0 deletions docs/features/validators.md
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",
},
]

```
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The package is published at pypi under the following link: `https://pypi.org/pro
features/services.md
features/tests.md
features/utils.rst
features/validators.md
features/view-layer.rst
features/changelog.md

Expand Down
4 changes: 4 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@
TIME_ZONE = "UTC"

LOCALE_PATHS = [str(BASE_PATH) + "/ambient_toolbox/locale"]

PASSWORD_HASHERS = [
"django.contrib.auth.hashers.MD5PasswordHasher",
]
Empty file added tests/validators/__init__.py
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions tests/validators/auth_password/test_special_chars.py
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"))

0 comments on commit dd171e4

Please sign in to comment.