From e6ecf4610bf04878211d494f55d56990a098064f Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Mon, 5 Oct 2026 17:23:15 +0530 Subject: [PATCH] fix(auth): require non-whitespace special character in password validation --- app/features/auth/security.py | 2 +- tests/test_password_validation.py | 39 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/test_password_validation.py diff --git a/app/features/auth/security.py b/app/features/auth/security.py index baedf6b..17a131f 100644 --- a/app/features/auth/security.py +++ b/app/features/auth/security.py @@ -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." ) diff --git a/tests/test_password_validation.py b/tests/test_password_validation.py new file mode 100644 index 0000000..b49e38a --- /dev/null +++ b/tests/test_password_validation.py @@ -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()