85 lines
2.9 KiB
Python
85 lines
2.9 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 DEV_PRIVATE_KEY_PEM, LicenseService
|
|
|
|
db_path = tmp_path / "license.db"
|
|
service = LicenseService(db_path=db_path, private_key_pem=DEV_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 DEV_PRIVATE_KEY_PEM, LicenseService
|
|
|
|
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=DEV_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 DEV_PRIVATE_KEY_PEM, LicenseService
|
|
|
|
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=DEV_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 DEV_PRIVATE_KEY_PEM, LicenseService
|
|
|
|
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=DEV_PRIVATE_KEY_PEM)
|
|
client = TestClient(api.create_app(service=service, admin_token="secret"))
|
|
|
|
created = client.post(
|
|
"/admin/create?token=secret",
|
|
data={"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?token=secret", data={"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"
|