Issue 20 : fix require non-whitespace special character in password validation #9

Merged
amit merged 1 commits from fix/issue-20-password-special-character-validation into main 2026-10-06 12:25:35 +00:00
2 changed files with 40 additions and 1 deletions
Showing only changes of commit e6ecf4610b - Show all commits

View File

@ -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."
)

View File

@ -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()