from __future__ import annotations import json import zipfile from datetime import datetime, timedelta, timezone from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM def test_build_diagnostic_log_archive_tails_logs_only(tmp_path, monkeypatch): from douyin.core import path_config from douyin.diagnostics import log_upload logs_dir = tmp_path / "logs" logs_dir.mkdir() (logs_dir / "unified.log").write_text("a" * 40, encoding="utf-8") (logs_dir / "actions.log").write_text("action\n", encoding="utf-8") (tmp_path / "license.dat").write_text("secret", encoding="utf-8") monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir)) archive = log_upload.build_diagnostic_log_archive( output_dir=tmp_path, max_source_bytes=10, max_archive_bytes=1024 * 1024, ) with zipfile.ZipFile(archive) as zf: names = set(zf.namelist()) assert "logs/unified.log" in names assert "logs/actions.log" in names assert "license.dat" not in names assert zf.read("logs/unified.log") == b"a" * 10 def test_license_server_log_upload_overwrites_same_card(tmp_path): from fastapi.testclient import TestClient from license_server import api from license_server.core import LicenseService service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM) card = service.create_cards(days=5, count=1, customer="log-user")[0] activated = service.activate(card.code, machine_fingerprint="machine-api") assert activated.license_doc is not None client = TestClient(api.create_app(service=service)) first = client.post( "/api/v1/logs/upload", data={"license": json.dumps(activated.license_doc, ensure_ascii=False)}, files={"log_file": ("one.zip", b"first", "application/zip")}, ) second = client.post( "/api/v1/logs/upload", data={"license": json.dumps(activated.license_doc, ensure_ascii=False)}, files={"log_file": ("two.zip", b"second", "application/zip")}, ) entries = service.list_diagnostic_logs() stored = service.diagnostic_log_path(entries[0].key) assert first.status_code == 200 assert second.status_code == 200 assert len(entries) == 1 assert entries[0].filename == "two.zip" assert stored is not None assert stored.read_bytes() == b"second" def test_upload_diagnostic_logs_posts_signed_license_and_file(tmp_path, monkeypatch): from tools.license_dev import create_dev_license from douyin.core import path_config from douyin.license import license_manager from douyin.diagnostics import log_upload logs_dir = tmp_path / "logs" logs_dir.mkdir() (logs_dir / "unified.log").write_text("hello\n", encoding="utf-8") license_path = tmp_path / "license.dat" monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM) monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a") monkeypatch.setattr(log_upload, "get_default_license_path", lambda: license_path) monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir)) create_dev_license( output_path=license_path, machine_fingerprint="machine-a", expires_at=datetime.now(timezone.utc) + timedelta(days=3), private_key_pem=TEST_PRIVATE_KEY_PEM, ) calls = {} def fake_post(url, *, license_doc, file_path, timeout_seconds=20): calls["url"] = url calls["license_doc"] = license_doc calls["file_path"] = file_path return {"ok": True, "message": "ok"} result = log_upload.upload_diagnostic_logs( upload_url="https://license.example.test/api/v1/logs/upload", http_post_file=fake_post, ) assert result.ok is True assert calls["license_doc"]["payload"]["machine_fingerprint"] == "machine-a" assert calls["file_path"].exists()