Skip to content

Commit

Permalink
Feedback and pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed Jan 31, 2024
1 parent 92ebe0c commit 7980c57
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
3 changes: 2 additions & 1 deletion codeforlife/user/auth/password_validators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
12 changes: 6 additions & 6 deletions codeforlife/user/auth/password_validators/independent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions codeforlife/user/auth/password_validators/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions codeforlife/user/auth/password_validators/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"):
Expand Down
12 changes: 10 additions & 2 deletions codeforlife/user/tests/auth/password_validators/test_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,51 @@
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


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(
Expand Down

0 comments on commit 7980c57

Please sign in to comment.