Issue 20 : fix require non-whitespace special character in password validation #9
|
|
@ -56,7 +56,7 @@ def validate_password_strength(password: str) -> None:
|
|||
raise WeakPasswordError(
|
||||
"Password must contain at least one number."
|
||||
)
|
||||
if not any(not c.isalnum() for c in password):
|
||||
if not any(not c.isalnum() and not c.isspace() for c in password):
|
||||
raise WeakPasswordError(
|
||||
"Password must contain at least one special character."
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
import unittest
|
||||
from app.features.auth.security import validate_password_strength
|
||||
from app.features.auth.errors import WeakPasswordError
|
||||
|
||||
class TestPasswordValidation(unittest.TestCase):
|
||||
def test_space_instead_of_special_char_fails(self):
|
||||
with self.assertRaises(WeakPasswordError) as ctx:
|
||||
validate_password_strength("Siraj 12345")
|
||||
self.assertIn("special character", str(ctx.exception))
|
||||
|
||||
def test_too_short_fails(self):
|
||||
with self.assertRaises(WeakPasswordError) as ctx:
|
||||
validate_password_strength("Short1!")
|
||||
self.assertIn("8 characters", str(ctx.exception))
|
||||
|
||||
def test_no_uppercase_fails(self):
|
||||
with self.assertRaises(WeakPasswordError) as ctx:
|
||||
validate_password_strength("lowercase1!")
|
||||
self.assertIn("uppercase", str(ctx.exception))
|
||||
|
||||
def test_no_digit_fails(self):
|
||||
with self.assertRaises(WeakPasswordError) as ctx:
|
||||
validate_password_strength("NoNumbers!")
|
||||
self.assertIn("number", str(ctx.exception))
|
||||
|
||||
def test_no_special_char_fails(self):
|
||||
with self.assertRaises(WeakPasswordError) as ctx:
|
||||
validate_password_strength("NoSpecial123")
|
||||
self.assertIn("special character", str(ctx.exception))
|
||||
|
||||
def test_valid_passwords_pass(self):
|
||||
# Must not raise any exceptions
|
||||
validate_password_strength("Siraj@12345")
|
||||
validate_password_strength("ValidPassword123!")
|
||||
validate_password_strength("P@ssw0rd2026")
|
||||
validate_password_strength("Strong#Pass1")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Reference in New Issue