refactor(api): answer every API key question from one KeyPolicy

This commit is contained in:
imSp4rky
2026-11-25 21:14:09 -07:00
parent 2da0043d17
commit 9febbb20c5
29 changed files with 721 additions and 646 deletions
+9 -13
View File
@@ -1,6 +1,6 @@
import pytest
from unshackle.core.api import handlers
from unshackle.core.api import handlers, key_policy
from unshackle.core.api.errors import APIError
@@ -71,7 +71,7 @@ def test_single_account_always_same(accounts):
def test_client_managed_gets_nothing(monkeypatch, accounts):
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
monkeypatch.setattr(key_policy, "load_service_config", lambda s: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: object()))
monkeypatch.setattr(handlers, "instantiate_service", lambda *a: "svc")
@@ -86,13 +86,9 @@ def test_per_key_gate(accounts, monkeypatch):
monkeypatch.setattr(handlers, "request_secret_key", lambda r: r.headers["X-Secret-Key"])
handlers.config.serve["users"] = {"k1": {}, "k2": {"server_accounts": True}, "k3": {"server_accounts": ["EX"]}}
assert handlers.server_accounts_allowed(Req("k1"), "EX") is False
assert handlers.server_accounts_allowed(Req("k2"), "EX") is True
assert handlers.server_accounts_allowed(Req("k3"), "EX") is True
assert handlers.server_accounts_allowed(Req("k3"), "SINGLE") is False
assert handlers.server_accounts_allowed(Req("admin"), "EX") is True
assert handlers.server_account_for(Req("k1"), "EX") is False
assert handlers.server_account_for(Req("k2"), "OTHER") is False
assert handlers.server_account_for(handlers.request_policy(Req("k1")), "EX") is False
assert handlers.server_account_for(handlers.request_policy(Req("k2")), "EX") is True
assert handlers.server_account_for(handlers.request_policy(Req("k2")), "OTHER") is False
def test_rotation_per_region_pool(accounts):
@@ -101,7 +97,7 @@ def test_rotation_per_region_pool(accounts):
def test_server_account_ignores_client_credentials(monkeypatch, accounts):
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
monkeypatch.setattr(key_policy, "load_service_config", lambda s: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: object()))
monkeypatch.setattr(handlers, "instantiate_service", lambda *a: "svc")
@@ -125,7 +121,7 @@ def test_server_cdm_ignores_client_cdm_type(monkeypatch):
"""A server_cdm tier licenses with the server's own device, so the client's CDM type must
not swap it for a stub of the other DRM system."""
seen = []
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
monkeypatch.setattr(key_policy, "load_service_config", lambda s: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda s, p, t=None, app_config=None: seen.append(t))
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: object()))
monkeypatch.setattr(handlers, "instantiate_service", lambda *a: "svc")
@@ -155,7 +151,7 @@ def _lang_option_module():
def _create_with_real_ctx(monkeypatch, data):
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
monkeypatch.setattr(key_policy, "load_service_config", lambda s: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: _lang_option_module()))
svc, _, _ = handlers.create_service_instance("OTHER", "t", data, None, [], None)
@@ -230,7 +226,7 @@ def test_malformed_dl_params_are_ignored(monkeypatch, dl_params):
def test_profile_reaches_the_parent_ctx_params(monkeypatch):
monkeypatch.setattr(handlers, "load_service_config", lambda s: {})
monkeypatch.setattr(key_policy, "load_service_config", lambda s: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
monkeypatch.setattr(handlers.Services, "load", staticmethod(lambda s: _lang_option_module()))
svc, _, _ = handlers.create_service_instance("OTHER", "t", {}, None, [], "work")