From 7980c571b36d641d48036f75ad4c6edadcb48ed5 Mon Sep 17 00:00:00 2001 From: faucomte97 Date: Wed, 31 Jan 2024 17:37:33 +0000 Subject: [PATCH] Feedback and pylint --- codeforlife/user/auth/password_validators/base.py | 3 ++- .../user/auth/password_validators/independent.py | 12 ++++++------ codeforlife/user/auth/password_validators/student.py | 12 ++++++------ codeforlife/user/auth/password_validators/teacher.py | 12 ++++++------ .../{password_validator.py => base.py} | 1 - .../auth/password_validators/test_independent.py | 8 +++++++- .../tests/auth/password_validators/test_student.py | 5 ++++- .../tests/auth/password_validators/test_teacher.py | 12 ++++++++++-- 8 files changed, 41 insertions(+), 24 deletions(-) rename codeforlife/user/tests/auth/password_validators/{password_validator.py => base.py} (97%) diff --git a/codeforlife/user/auth/password_validators/base.py b/codeforlife/user/auth/password_validators/base.py index 929d69c7..caad97de 100644 --- a/codeforlife/user/auth/password_validators/base.py +++ b/codeforlife/user/auth/password_validators/base.py @@ -8,6 +8,7 @@ from ...models.user import User -class BasePasswordValidator: +class PasswordValidator: + """Base class for all password validators""" def validate(self, password: str, user: t.Optional[User] = None): raise NotImplementedError() diff --git a/codeforlife/user/auth/password_validators/independent.py b/codeforlife/user/auth/password_validators/independent.py index 074b750e..1ccfc034 100644 --- a/codeforlife/user/auth/password_validators/independent.py +++ b/codeforlife/user/auth/password_validators/independent.py @@ -8,19 +8,19 @@ from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ -from .base import BasePasswordValidator +from .base import PasswordValidator -class IndependentPasswordValidator(BasePasswordValidator): - def __init__(self): - self.min_length = 8 +class IndependentPasswordValidator(PasswordValidator): def validate(self, password, user=None): if user.teacher is None and user.student is None: - if len(password) < self.min_length: + min_length = 8 + + if len(password) < min_length: raise ValidationError( _( - f"Your password must be at least {self.min_length} " + f"Your password must be at least {min_length} " f"characters long." ), code="password_too_short", diff --git a/codeforlife/user/auth/password_validators/student.py b/codeforlife/user/auth/password_validators/student.py index 41d3d447..e3e4047e 100644 --- a/codeforlife/user/auth/password_validators/student.py +++ b/codeforlife/user/auth/password_validators/student.py @@ -6,19 +6,19 @@ from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ -from .base import BasePasswordValidator +from .base import PasswordValidator -class StudentPasswordValidator(BasePasswordValidator): - def __init__(self): - self.min_length = 6 +class StudentPasswordValidator(PasswordValidator): def validate(self, password, user=None): if user.student is not None: - if len(password) < self.min_length: + min_length = 6 + + if len(password) < min_length: raise ValidationError( _( - f"Your password must be at least {self.min_length} " + f"Your password must be at least {min_length} " f"characters long." ), code="password_too_short", diff --git a/codeforlife/user/auth/password_validators/teacher.py b/codeforlife/user/auth/password_validators/teacher.py index ecf0d41b..e7b29d1b 100644 --- a/codeforlife/user/auth/password_validators/teacher.py +++ b/codeforlife/user/auth/password_validators/teacher.py @@ -8,19 +8,19 @@ from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ -from .base import BasePasswordValidator +from .base import PasswordValidator -class TeacherPasswordValidator(BasePasswordValidator): - def __init__(self): - self.min_length = 10 +class TeacherPasswordValidator(PasswordValidator): def validate(self, password, user=None): if user.teacher is not None: - if len(password) < self.min_length: + min_length = 10 + + if len(password) < min_length: raise ValidationError( _( - f"Your password needs to be at least {self.min_length} " + f"Your password needs to be at least {min_length} " f"characters long." ), code="password_too_short", diff --git a/codeforlife/user/tests/auth/password_validators/password_validator.py b/codeforlife/user/tests/auth/password_validators/base.py similarity index 97% rename from codeforlife/user/tests/auth/password_validators/password_validator.py rename to codeforlife/user/tests/auth/password_validators/base.py index 2a221b2d..27bbb466 100644 --- a/codeforlife/user/tests/auth/password_validators/password_validator.py +++ b/codeforlife/user/tests/auth/password_validators/base.py @@ -35,7 +35,6 @@ def __enter__(self, *args, **kwargs): def __exit__(self, *args, **kwargs): value = self.context.__exit__(*args, **kwargs) - print(self.context) assert self.context.exception.code == code return value diff --git a/codeforlife/user/tests/auth/password_validators/test_independent.py b/codeforlife/user/tests/auth/password_validators/test_independent.py index 2b52fe4c..df077540 100644 --- a/codeforlife/user/tests/auth/password_validators/test_independent.py +++ b/codeforlife/user/tests/auth/password_validators/test_independent.py @@ -3,7 +3,7 @@ Created on 30/01/2024 at 12:36:00(+00:00). """ -from .password_validator import PasswordValidatorTestCase +from .base import PasswordValidatorTestCase from ....auth.password_validators import IndependentPasswordValidator from ....models.user import User @@ -14,28 +14,34 @@ def setUpClass(cls): cls.user = User.objects.filter( new_teacher__isnull=True, new_student__isnull=True ).first() + assert cls.user is not None + cls.validator = IndependentPasswordValidator() super(TestIndependentPasswordValidator, cls).setUpClass() def test_validate__password_too_short(self): + """Check password validator rejects too short password""" password = "fxwSn4}" with self.assert_raises_validation_error("password_too_short"): self.validator.validate(password, self.user) def test_validate__password_no_uppercase(self): + """Check password validator rejects password without uppercase""" password = ">28v*@a)-{" with self.assert_raises_validation_error("password_no_uppercase"): self.validator.validate(password, self.user) def test_validate__password_no_lowercase(self): + """Check password validator rejects password without lowercase""" password = "F:6]LH!_5>" with self.assert_raises_validation_error("password_no_lowercase"): self.validator.validate(password, self.user) def test_validate__password_no_digit(self): + """Check password validator rejects password without digit""" password = "{$#FJdxGvs" with self.assert_raises_validation_error("password_no_digit"): diff --git a/codeforlife/user/tests/auth/password_validators/test_student.py b/codeforlife/user/tests/auth/password_validators/test_student.py index 7485751f..c198dc17 100644 --- a/codeforlife/user/tests/auth/password_validators/test_student.py +++ b/codeforlife/user/tests/auth/password_validators/test_student.py @@ -3,7 +3,7 @@ Created on 30/01/2024 at 12:36:00(+00:00). """ -from .password_validator import PasswordValidatorTestCase +from .base import PasswordValidatorTestCase from ....auth.password_validators import StudentPasswordValidator from ....models.user import User @@ -12,10 +12,13 @@ class TestStudentPasswordValidator(PasswordValidatorTestCase): @classmethod def setUpClass(cls): cls.user = User.objects.filter(new_student__isnull=False).first() + assert cls.user is not None + cls.validator = StudentPasswordValidator() super(TestStudentPasswordValidator, cls).setUpClass() def test_validate__password_too_short(self): + """Check password validator rejects too short password""" password = "fxwSn" with self.assert_raises_validation_error("password_too_short"): diff --git a/codeforlife/user/tests/auth/password_validators/test_teacher.py b/codeforlife/user/tests/auth/password_validators/test_teacher.py index cbcdaeef..0f9d041e 100644 --- a/codeforlife/user/tests/auth/password_validators/test_teacher.py +++ b/codeforlife/user/tests/auth/password_validators/test_teacher.py @@ -3,7 +3,7 @@ Created on 30/01/2024 at 12:36:00(+00:00). """ -from .password_validator import PasswordValidatorTestCase +from .base import PasswordValidatorTestCase from ....auth.password_validators import TeacherPasswordValidator from ....models.user import User @@ -11,35 +11,43 @@ class TestTeacherPasswordValidator(PasswordValidatorTestCase): @classmethod def setUpClass(cls): - cls.user = User.objects.first() + cls.user = User.objects.filter(new_teacher__isnull=False).first() + assert cls.user is not None + cls.validator = TeacherPasswordValidator() super(TestTeacherPasswordValidator, cls).setUpClass() def test_validate__password_too_short(self): + """Check password validator rejects too short password""" password = "fxwSn4}PW" with self.assert_raises_validation_error("password_too_short"): self.validator.validate(password, self.user) def test_validate__password_no_uppercase(self): + """Check password validator rejects password without uppercase""" password = ">28v*@a)-{" with self.assert_raises_validation_error("password_no_uppercase"): self.validator.validate(password, self.user) def test_validate__password_no_lowercase(self): + """Check password validator rejects password without lowercase""" password = "F:6]LH!_5>" with self.assert_raises_validation_error("password_no_lowercase"): self.validator.validate(password, self.user) def test_validate__password_no_digit(self): + """Check password validator rejects password without digit""" password = "{$#FJdxGvs" with self.assert_raises_validation_error("password_no_digit"): self.validator.validate(password, self.user) def test_validate__password_no_special_character(self): + """Check password validator rejects password without special + character""" password = "kR48SsAwrE" with self.assert_raises_validation_error(