fix: require license gate before gui entry

This commit is contained in:
Your Name committed 2026-06-28 08:50:05 +08:00
1 parent 407c3b37c2
commit f5a77867ee
8 files changed
+268 -9

No files matched your search

+57
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
import io
from types import SimpleNamespace
import pytest
@@ -16,6 +17,9 @@ class _DummyHandler:
self.calls = []
self.client_address = ("127.0.0.1", 12345)
self.static_calls = []
self.response_status = None
self.response_headers = []
self.wfile = io.BytesIO()
def _read_json(self):
return self._body
@@ -28,6 +32,15 @@ class _DummyHandler:
self.calls.append((status, {"error": "send_error"}))
return None
def send_response(self, status):
self.response_status = status
def send_header(self, name, value):
self.response_headers.append((name, value))
def end_headers(self):
return None
def _serve_static(self, rel_path):
self.static_calls.append(rel_path)
return None
@@ -101,6 +114,22 @@ def _get(path: str, monkeypatch, query: str = ""):
return handler.calls[-1]
def _license_ok(monkeypatch):
class OkStatus:
ok = True
monkeypatch.setattr(routes, "check_license", lambda: OkStatus())
def _license_missing(monkeypatch):
class MissingStatus:
ok = False
reason = "missing"
message = "缺少授权文件"
monkeypatch.setattr(routes, "check_license", lambda: MissingStatus())
@pytest.mark.parametrize(
"raw,expected",
[
@@ -154,6 +183,34 @@ def test_license_status_route_returns_public_payload(monkeypatch):
assert obj == {"ok": True, "reason": "ok", "customer": "demo"}
def test_web_root_requires_license_gate_before_static_app(monkeypatch):
_license_missing(monkeypatch)
handler = _DummyHandler()
runner = _DummyRunner()
u = SimpleNamespace(path="/", query="")
routes.handle_get(handler, u, runner, "token")
html = handler.wfile.getvalue().decode("utf-8")
assert handler.response_status == 200
assert "卡密验证" in html
assert "输入卡密" in html
assert "window.location.reload" in html
assert handler.static_calls == []
def test_web_root_enters_static_app_when_license_exists(monkeypatch):
_license_ok(monkeypatch)
handler = _DummyHandler()
runner = _DummyRunner()
u = SimpleNamespace(path="/", query="")
routes.handle_get(handler, u, runner, "token")
assert handler.static_calls == ["/"]
assert handler.wfile.getvalue() == b""
def test_license_install_route_accepts_code(monkeypatch):
calls = {}