Spaces:
Paused
Paused
Download tests/test_auth_unit.py from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 6.36 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/tests/test_auth_unit.py
- Command line
-
hf download hf://spaces/3SCError404/miracle/tests/test_auth_unit.py
-
curl -L -o test_auth_unit.py https://huggingface.co/spaces/3SCError404/miracle/resolve/main/tests/test_auth_unit.py
6.36 kB
| """ | |
| Unit tests for core/security.py — password hashing and JWT utilities. | |
| These tests are pure Python; they require no database, no network, and no | |
| server. They run fast and serve as a regression guard for the security | |
| primitives the rest of the application relies on. | |
| """ | |
| import pytest | |
| from datetime import timedelta | |
| from jose import jwt, JWTError | |
| from core.security import ( | |
| verify_password, | |
| get_password_hash, | |
| create_access_token, | |
| create_refresh_token, | |
| create_reset_token, | |
| decode_access_token, | |
| ) | |
| from core.config import get_settings | |
| settings = get_settings() | |
| # --------------------------------------------------------------------------- | |
| # Password hashing | |
| # --------------------------------------------------------------------------- | |
| class TestPasswordHashing: | |
| def test_hash_is_not_plaintext(self): | |
| hashed = get_password_hash("mypassword") | |
| assert hashed != "mypassword" | |
| def test_hash_starts_with_bcrypt_prefix(self): | |
| hashed = get_password_hash("mypassword") | |
| assert hashed.startswith("$2b$") or hashed.startswith("$2a$") | |
| def test_correct_password_verifies(self): | |
| hashed = get_password_hash("correct_horse") | |
| assert verify_password("correct_horse", hashed) is True | |
| def test_wrong_password_rejected(self): | |
| hashed = get_password_hash("correct_horse") | |
| assert verify_password("wrong_horse", hashed) is False | |
| def test_empty_password_rejected_against_non_empty_hash(self): | |
| hashed = get_password_hash("not_empty") | |
| assert verify_password("", hashed) is False | |
| def test_two_hashes_of_same_password_differ(self): | |
| pw = "same_password" | |
| h1 = get_password_hash(pw) | |
| h2 = get_password_hash(pw) | |
| assert h1 != h2 # bcrypt uses random salt | |
| def test_invalid_hash_returns_false(self): | |
| assert verify_password("anything", "not-a-valid-hash") is False | |
| # --------------------------------------------------------------------------- | |
| # Access token | |
| # --------------------------------------------------------------------------- | |
| class TestAccessToken: | |
| def test_token_is_string(self): | |
| token = create_access_token(subject="user-id-123") | |
| assert isinstance(token, str) and len(token) > 10 | |
| def test_token_contains_sub_claim(self): | |
| token = create_access_token(subject="user-id-abc") | |
| payload = decode_access_token(token) | |
| assert payload["sub"] == "user-id-abc" | |
| def test_token_type_is_access(self): | |
| token = create_access_token(subject="uid") | |
| payload = decode_access_token(token) | |
| assert payload["type"] == "access" | |
| def test_token_has_jti(self): | |
| token = create_access_token(subject="uid") | |
| payload = decode_access_token(token) | |
| assert "jti" in payload and payload["jti"] | |
| def test_workspace_id_embedded(self): | |
| ws_id = "workspace-xyz" | |
| token = create_access_token(subject="uid", workspace_id=ws_id) | |
| payload = decode_access_token(token) | |
| assert payload["workspace_id"] == ws_id | |
| def test_role_embedded(self): | |
| token = create_access_token(subject="uid", role="SC_ADMIN") | |
| payload = decode_access_token(token) | |
| assert payload["role"] == "SC_ADMIN" | |
| def test_custom_expiry_respected(self): | |
| import time | |
| token = create_access_token(subject="uid", expires_delta=timedelta(seconds=5)) | |
| payload = decode_access_token(token) | |
| # exp should be very close to now + 5s | |
| assert abs(payload["exp"] - (time.time() + 5)) < 3 | |
| def test_expired_token_raises(self): | |
| token = create_access_token(subject="uid", expires_delta=timedelta(seconds=-1)) | |
| with pytest.raises(JWTError): | |
| decode_access_token(token) | |
| def test_tampered_token_raises(self): | |
| token = create_access_token(subject="uid") | |
| tampered = token[:-4] + "XXXX" | |
| with pytest.raises(JWTError): | |
| decode_access_token(tampered) | |
| def test_different_tokens_have_unique_jti(self): | |
| t1 = create_access_token(subject="uid") | |
| t2 = create_access_token(subject="uid") | |
| jti1 = decode_access_token(t1)["jti"] | |
| jti2 = decode_access_token(t2)["jti"] | |
| assert jti1 != jti2 | |
| # --------------------------------------------------------------------------- | |
| # Refresh token | |
| # --------------------------------------------------------------------------- | |
| class TestRefreshToken: | |
| def test_token_type_is_refresh(self): | |
| token = create_refresh_token(subject="uid") | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| assert payload["type"] == "refresh" | |
| def test_refresh_token_has_jti(self): | |
| token = create_refresh_token(subject="uid") | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| assert "jti" in payload | |
| def test_refresh_token_cannot_be_used_as_access_token(self): | |
| """decode_access_token doesn't check type; the caller (deps.py) does. | |
| Verify the type field is present and wrong so the guard can act on it.""" | |
| token = create_refresh_token(subject="uid") | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| assert payload["type"] != "access" | |
| # --------------------------------------------------------------------------- | |
| # Reset token | |
| # --------------------------------------------------------------------------- | |
| class TestResetToken: | |
| def test_token_type_is_reset(self): | |
| token = create_reset_token(subject="user@example.com") | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| assert payload["type"] == "reset" | |
| def test_reset_token_subject_preserved(self): | |
| token = create_reset_token(subject="user@example.com") | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| assert payload["sub"] == "user@example.com" | |
| def test_reset_token_expires_in_roughly_15_minutes(self): | |
| import time | |
| token = create_reset_token(subject="uid") | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| # Default is 15 minutes; allow ±10 s margin | |
| expected = time.time() + 15 * 60 | |
| assert abs(payload["exp"] - expected) < 10 | |