Coverage for website/validators.py: 71%

17 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2025-09-13 15:29 -0300

1import re 

2 

3from django.core.exceptions import ValidationError 

4 

5 

6class LegacyPasswordValidator: 

7 """Compatibility password validator that enforces the project's legacy 

8 learning-oriented rules. 

9 

10 Rules enforced: 

11 - length >= 10 and <= 16 

12 - at least one uppercase letter 

13 - at least one digit 

14 - at least one special character (non-word or underscore) 

15 """ 

16 

17 def validate(self, password, user=None): 

18 # reference the `user` param to avoid unused-argument lint warnings 

19 _ = user 

20 if password is None: 

21 raise ValidationError("Senha inválida.") 

22 

23 if not (10 <= len(password) <= 16): 

24 raise ValidationError("A senha deve ter entre 10 e 16 caracteres (legado).") 

25 

26 if not re.search(r"[A-Z]", password): 

27 raise ValidationError("A senha deve conter ao menos uma letra maiúscula.") 

28 

29 if not re.search(r"\d", password): 

30 raise ValidationError("A senha deve conter ao menos um número.") 

31 

32 if not re.search(r"[\W_]", password): 

33 raise ValidationError("A senha deve conter ao menos um caractere especial.") 

34 

35 def get_help_text(self): 

36 return ( 

37 "A senha deve ter entre 10 e 16 caracteres, incluir uma letra maiúscula, " 

38 "um número e um caractere especial." 

39 )