Files
douyi_guaji_main/tests/test_license_server_api.py
T

185 lines
6.4 KiB
Python

from __future__ import annotations
def test_license_server_api_activates_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
db_path = tmp_path / "license.db"
service = LicenseService(db_path=db_path, private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=5, count=1, customer="api-user")[0]
client = TestClient(api.create_app(service=service))
health = client.get("/api/v1/health")
resp = client.post(
"/api/v1/activate",
json={"card_key": card.code, "machine_fingerprint": "machine-api"},
)
assert health.status_code == 200
assert health.json()["ok"] is True
assert resp.status_code == 200
body = resp.json()
assert body["ok"] is True
assert body["license"]["payload"]["customer"] == "api-user"
assert body["license"]["payload"]["machine_fingerprint"] == "machine-api"
def test_license_server_api_rejects_unknown_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service))
resp = client.post(
"/api/v1/activate",
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
)
assert resp.status_code == 404
assert resp.json()["reason"] == "not_found"
def test_admin_page_requires_token(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service, admin_token="secret"))
resp = client.get("/admin")
assert resp.status_code == 403
def test_admin_page_can_create_and_revoke_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service, admin_token="secret"))
created = client.post(
"/admin/create",
auth=("admin", "secret"),
cookies={api.ADMIN_CSRF_COOKIE: "csrf-token"},
data={"csrf_token": "csrf-token", "days": "8", "count": "1", "customer": "web-admin"},
)
card = created.text.split("DG-", 1)[1].split("<", 1)[0]
card = "DG-" + card.strip()
revoked = client.post(
"/admin/revoke",
auth=("admin", "secret"),
cookies={api.ADMIN_CSRF_COOKIE: "csrf-token"},
data={"csrf_token": "csrf-token", "card_key": card},
)
assert created.status_code == 200
assert "web-admin" in created.text
assert card.startswith("DG-")
assert revoked.status_code == 200
assert "已禁用" in revoked.text
assert service.activate(card, machine_fingerprint="machine-a").reason == "revoked"
def test_admin_page_shows_stats_cards_and_audit_logs(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
active = service.create_cards(days=5, count=1, customer="active-user")[0]
revoked = service.create_cards(days=2, count=1, customer="revoked-user")[0]
service.activate(active.code, machine_fingerprint="machine-a")
service.revoke(revoked.code)
client = TestClient(api.create_app(service=service, admin_token="secret"))
resp = client.get("/admin", auth=("admin", "secret"))
assert resp.status_code == 200
body = resp.text
assert "总卡密" in body
assert "活跃卡密" in body
assert "已绑定设备" in body
assert "active-user" in body
assert "revoked-user" in body
assert "最近审计日志" in body
assert "activate" in body
assert "revoke" in body
def test_admin_query_token_is_rejected(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service, admin_token="secret"))
resp = client.get("/admin?token=secret")
assert resp.status_code == 403
def test_admin_create_requires_csrf(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service, admin_token="secret"))
resp = client.post(
"/admin/create",
auth=("admin", "secret"),
data={"days": "8", "count": "1", "customer": "web-admin"},
)
assert resp.status_code == 200
assert "CSRF 校验失败" in resp.text
def test_api_rate_limits_repeated_failed_activation(tmp_path, monkeypatch):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
monkeypatch.setattr(api, "RATE_LIMIT_MAX_REQUESTS", 2)
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service))
for _ in range(2):
assert client.post(
"/api/v1/activate",
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
).status_code == 404
limited = client.post(
"/api/v1/activate",
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
)
assert limited.status_code == 429
assert limited.json()["reason"] == "rate_limited"