mirror of
https://github.com/ruvnet/RuView
synced 2026-06-19 11:53:19 +00:00
81cc241b9e
The Rust port at v2/ has been the primary codebase since the rename in #427. The Python implementation at v1/ is no longer the active target; the only load-bearing path is the deterministic proof bundle at v1/data/proof/ (per ADR-011 / ADR-028 witness verification). Move the whole Python tree into archive/v1/ and document the policy in archive/README.md: no new features, bug fixes only when they affect a still-load-bearing path (currently just the proof), CI continues to verify the proof on every push and PR. Path references updated in 26 files via path-pattern sed (only matches v1/<known-child> patterns, never bare v1 or API URLs like /api/v1/). Two double-prefix typos (archive/archive/v1/) caught and hand-fixed in verify-pipeline.yml and ADR-011. Validated: - Python proof verify.py imports cleanly at archive/v1/data/proof/ (numpy/scipy still required; CI installs requirements-lock.txt from archive/v1/ now) - cargo test --workspace --no-default-features → 1,539 passed, 0 failed, 8 ignored (unaffected by Python tree relocation) - ESP32-S3 on COM7 untouched (no firmware paths changed) After-merge: contributors should re-run any local `python v1/...` commands as `python archive/v1/...` (CLAUDE.md and CHANGELOG already updated).
138 lines
5.3 KiB
Python
138 lines
5.3 KiB
Python
"""Tests for AuthMiddleware and TokenManager."""
|
|
|
|
import pytest
|
|
import os
|
|
from unittest.mock import MagicMock, AsyncMock, patch
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
class TestTokenManager:
|
|
def test_create_token(self, mock_settings):
|
|
from src.middleware.auth import TokenManager
|
|
tm = TokenManager(mock_settings)
|
|
token = tm.create_access_token({"sub": "user1"})
|
|
assert isinstance(token, str)
|
|
assert len(token) > 0
|
|
|
|
def test_verify_valid_token(self, mock_settings):
|
|
from src.middleware.auth import TokenManager
|
|
tm = TokenManager(mock_settings)
|
|
token = tm.create_access_token({"sub": "user1", "role": "admin"})
|
|
payload = tm.verify_token(token)
|
|
assert payload["sub"] == "user1"
|
|
assert payload["role"] == "admin"
|
|
|
|
def test_verify_invalid_token(self, mock_settings):
|
|
from src.middleware.auth import TokenManager, AuthenticationError
|
|
tm = TokenManager(mock_settings)
|
|
with pytest.raises(AuthenticationError):
|
|
tm.verify_token("invalid.token.here")
|
|
|
|
def test_decode_claims(self, mock_settings):
|
|
from src.middleware.auth import TokenManager
|
|
tm = TokenManager(mock_settings)
|
|
token = tm.create_access_token({"sub": "user1"})
|
|
claims = tm.decode_token_claims(token)
|
|
assert claims is not None
|
|
assert claims["sub"] == "user1"
|
|
|
|
def test_decode_claims_invalid(self, mock_settings):
|
|
from src.middleware.auth import TokenManager
|
|
tm = TokenManager(mock_settings)
|
|
claims = tm.decode_token_claims("bad-token")
|
|
assert claims is None
|
|
|
|
def test_token_has_expiry(self, mock_settings):
|
|
from src.middleware.auth import TokenManager
|
|
tm = TokenManager(mock_settings)
|
|
token = tm.create_access_token({"sub": "user1"})
|
|
payload = tm.verify_token(token)
|
|
assert "exp" in payload
|
|
assert "iat" in payload
|
|
|
|
|
|
class TestUserManager:
|
|
def test_create_user(self):
|
|
from src.middleware.auth import UserManager
|
|
um = UserManager()
|
|
assert um.get_user("nonexistent") is None
|
|
|
|
def test_hash_password(self):
|
|
from src.middleware.auth import UserManager
|
|
hashed = UserManager.hash_password("secret123")
|
|
assert hashed != "secret123"
|
|
assert len(hashed) > 20
|
|
|
|
def test_verify_password(self):
|
|
from src.middleware.auth import UserManager
|
|
hashed = UserManager.hash_password("secret123")
|
|
assert UserManager.verify_password("secret123", hashed) is True
|
|
assert UserManager.verify_password("wrong", hashed) is False
|
|
|
|
|
|
class TestTokenBlacklist:
|
|
def test_add_and_check(self):
|
|
from src.api.middleware.auth import TokenBlacklist
|
|
bl = TokenBlacklist()
|
|
bl.add_token("tok123")
|
|
assert bl.is_blacklisted("tok123") is True
|
|
assert bl.is_blacklisted("tok456") is False
|
|
|
|
def test_blacklisted_token_rejected(self, mock_settings):
|
|
from src.middleware.auth import TokenManager, AuthenticationError
|
|
from src.api.middleware.auth import token_blacklist
|
|
|
|
tm = TokenManager(mock_settings)
|
|
token = tm.create_access_token({"sub": "user1"})
|
|
# Token should be valid
|
|
tm.verify_token(token)
|
|
# Blacklist it
|
|
token_blacklist.add_token(token)
|
|
with pytest.raises(AuthenticationError, match="revoked"):
|
|
tm.verify_token(token)
|
|
# Cleanup
|
|
token_blacklist._blacklisted_tokens.discard(token)
|
|
|
|
|
|
class TestAuthMiddleware:
|
|
def test_public_paths(self, mock_settings):
|
|
with patch("src.api.middleware.auth.get_settings", return_value=mock_settings):
|
|
from src.api.middleware.auth import AuthMiddleware
|
|
app = MagicMock()
|
|
mw = AuthMiddleware(app)
|
|
assert mw._is_public_path("/health") is True
|
|
assert mw._is_public_path("/docs") is True
|
|
assert mw._is_public_path("/api/v1/pose/analyze") is False
|
|
|
|
def test_protected_paths(self, mock_settings):
|
|
with patch("src.api.middleware.auth.get_settings", return_value=mock_settings):
|
|
from src.api.middleware.auth import AuthMiddleware
|
|
app = MagicMock()
|
|
mw = AuthMiddleware(app)
|
|
assert mw._is_protected_path("/api/v1/pose/analyze") is True
|
|
assert mw._is_protected_path("/health") is False
|
|
|
|
def test_extract_token_from_header(self, mock_settings):
|
|
with patch("src.api.middleware.auth.get_settings", return_value=mock_settings):
|
|
from src.api.middleware.auth import AuthMiddleware
|
|
app = MagicMock()
|
|
mw = AuthMiddleware(app)
|
|
request = MagicMock()
|
|
request.headers = {"authorization": "Bearer mytoken123"}
|
|
request.query_params = {}
|
|
request.cookies = {}
|
|
token = mw._extract_token(request)
|
|
assert token == "mytoken123"
|
|
|
|
def test_extract_token_missing(self, mock_settings):
|
|
with patch("src.api.middleware.auth.get_settings", return_value=mock_settings):
|
|
from src.api.middleware.auth import AuthMiddleware
|
|
app = MagicMock()
|
|
mw = AuthMiddleware(app)
|
|
request = MagicMock()
|
|
request.headers = {}
|
|
request.query_params = {}
|
|
request.cookies = {}
|
|
token = mw._extract_token(request)
|
|
assert token is None
|