Files
douyi_guaji_main/tests/test_license_manager.py
T

266 lines
8.8 KiB
Python

from __future__ import annotations
import json
from datetime import datetime, timedelta, timezone
import pytest
def test_signed_license_is_accepted_for_current_machine(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="local-test",
)
status = license_manager.check_license(license_path)
assert status.ok is True
assert status.reason == "ok"
assert status.payload["customer"] == "local-test"
def test_license_rejects_different_machine(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-b")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
)
status = license_manager.check_license(license_path)
assert status.ok is False
assert status.reason == "machine_mismatch"
def test_license_rejects_expired_file(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) - timedelta(days=1),
)
status = license_manager.check_license(license_path)
assert status.ok is False
assert status.reason == "expired"
def test_license_rejects_tampered_payload(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
)
raw = json.loads(license_path.read_text(encoding="utf-8"))
raw["payload"]["customer"] = "tampered"
license_path.write_text(json.dumps(raw, ensure_ascii=False), encoding="utf-8")
status = license_manager.check_license(license_path)
assert status.ok is False
assert status.reason == "invalid_signature"
def test_missing_license_is_allowed_in_source_mode(tmp_path, monkeypatch):
from douyin.license import license_manager
monkeypatch.delenv("DOUYIN_LICENSE_REQUIRED", raising=False)
status = license_manager.check_license(tmp_path / "missing.dat")
assert status.ok is True
assert status.reason == "dev_bypass"
def test_missing_license_can_be_required(tmp_path, monkeypatch):
from douyin.license import license_manager
monkeypatch.setenv("DOUYIN_LICENSE_REQUIRED", "1")
status = license_manager.check_license(tmp_path / "missing.dat")
assert status.ok is False
assert status.reason == "missing"
def test_install_license_accepts_json_text(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
source_path = tmp_path / "source_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="json-text",
)
result = license_manager.install_license_text(
source_path.read_text(encoding="utf-8"),
target_path,
)
assert result.ok is True
assert result.reason == "ok"
assert target_path.exists()
assert license_manager.check_license(target_path).payload["customer"] == "json-text"
def test_install_license_accepts_activation_code(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license, encode_activation_code
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
source_path = tmp_path / "source_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="activation-code",
)
code = encode_activation_code(source_path.read_text(encoding="utf-8"))
result = license_manager.install_license_text(code, target_path)
assert result.ok is True
assert target_path.exists()
assert license_manager.check_license(target_path).payload["customer"] == "activation-code"
def test_install_license_rejects_invalid_text(tmp_path):
from douyin.license import license_manager
target_path = tmp_path / "license.dat"
result = license_manager.install_license_text("not-a-license", target_path)
assert result.ok is False
assert result.reason == "invalid_format"
assert not target_path.exists()
def test_activate_license_code_installs_server_license(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
source_path = tmp_path / "server_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="online-user",
)
server_doc = json.loads(source_path.read_text(encoding="utf-8"))
calls = {}
def fake_post(url, payload, timeout_seconds=10):
calls["url"] = url
calls["payload"] = payload
calls["timeout_seconds"] = timeout_seconds
return {"ok": True, "license": server_doc, "reason": "ok", "message": "授权有效"}
result = license_manager.install_license_or_activate(
"DG-AAAA-BBBB-CCCC",
target_path,
activation_url="https://license.example.test/api/v1/activate",
http_post=fake_post,
)
assert result.ok is True
assert target_path.exists()
assert calls["payload"] == {
"card_key": "DG-AAAA-BBBB-CCCC",
"machine_fingerprint": "machine-a",
}
assert license_manager.check_license(target_path).payload["customer"] == "online-user"
def test_plain_card_uses_default_activation_url(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(
license_manager,
"get_default_activation_url",
lambda: "https://license.example.test/api/v1/activate",
)
source_path = tmp_path / "server_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="default-url-user",
)
server_doc = json.loads(source_path.read_text(encoding="utf-8"))
calls = {}
def fake_post(url, payload, timeout_seconds=10):
calls["url"] = url
calls["payload"] = payload
return {"ok": True, "license": server_doc}
result = license_manager.install_license_or_activate(
"DG-DEFAULT-TEST",
target_path,
http_post=fake_post,
)
assert result.ok is True
assert calls["url"] == "https://license.example.test/api/v1/activate"
assert calls["payload"]["card_key"] == "DG-DEFAULT-TEST"
def test_license_status_public_payload_hides_signature(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="public-status",
)
payload = license_manager.get_license_status_payload(license_path)
assert payload["ok"] is True
assert payload["reason"] == "ok"
assert payload["customer"] == "public-status"
assert "signature" not in payload
assert payload["machine_fingerprint"]