| import os |
| import sys |
| import unittest |
|
|
| |
| current_file_path = os.path.abspath(__file__) |
| security_dir = os.path.dirname(current_file_path) |
| tests_dir = os.path.dirname(security_dir) |
| backend_dir = os.path.dirname(tests_dir) |
| project_root = os.path.dirname(backend_dir) |
|
|
| print(f"DEBUG: current_file_path: {current_file_path}") |
| print(f"DEBUG: backend_dir: {backend_dir}") |
| print(f"DEBUG: project_root: {project_root}") |
|
|
| sys.path.insert(0, backend_dir) |
| sys.path.insert(0, project_root) |
|
|
| print(f"DEBUG: sys.path[:2]: {sys.path[:2]}") |
|
|
| class TestSecurityFixes(unittest.TestCase): |
| def test_bcrypt_hard_import(self): |
| """Ensure bcrypt is now a hard import and not optional""" |
| try: |
| import core.auth as auth |
| self.assertTrue(auth.BCRYPT_AVAILABLE) |
| except ImportError as e: |
| self.fail(f"bcrypt should be a required dependency now: {e}") |
|
|
| def test_verify_password_failure_on_plain_text(self): |
| """Ensure plain-text passwords no longer verify against themselves""" |
| from core.auth import verify_password |
|
|
| |
| |
| self.assertFalse(verify_password("password", "password")) |
|
|
| def test_password_truncation(self): |
| """Ensure passwords are truncated to 71 bytes for bcrypt safety""" |
| from core.auth import get_password_hash, verify_password |
| long_password = "a" * 100 |
| password_hash = get_password_hash(long_password) |
| |
| |
| self.assertTrue(verify_password(long_password, password_hash)) |
| |
| |
| self.assertTrue(verify_password("a" * 71, password_hash)) |
| |
| |
| self.assertFalse(verify_password("b" + "a" * 70, password_hash)) |
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|