Spaces:
Running on Zero
Running on Zero
File size: 941 Bytes
32c5da4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import pytest
from backend.app.core.security import ApiSecurity
def test_security_allows_without_required_key(monkeypatch) -> None:
monkeypatch.setattr("backend.app.core.security.API_KEYS", "")
sec = ApiSecurity()
principal = sec.authenticate(api_key=None, client_id="local")
assert principal.role == "operator"
def test_security_rejects_invalid_key(monkeypatch) -> None:
monkeypatch.setattr("backend.app.core.security.API_KEYS", "abc")
sec = ApiSecurity()
with pytest.raises(Exception):
sec.authenticate(api_key="wrong", client_id="local")
def test_security_role_mapping(monkeypatch) -> None:
monkeypatch.setattr("backend.app.core.security.API_KEYS", "k1:viewer,k2:admin")
sec = ApiSecurity()
viewer = sec.authenticate(api_key="k1", client_id="local")
admin = sec.authenticate(api_key="k2", client_id="local")
assert viewer.role == "viewer"
assert admin.role == "admin"
|