feat: add server managed auto updates
This commit is contained in:
1 parent
ba2e159cad
commit
5c7d341d9b
6 files changed
+466
-6
No files matched your search
@@ -5,6 +5,9 @@ import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
|
||||
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
|
||||
|
||||
|
||||
def test_compare_versions_handles_v_prefix_and_patch_order():
|
||||
@@ -30,6 +33,26 @@ def test_load_settings_is_disabled_without_manifest_url(tmp_path, monkeypatch):
|
||||
assert settings.manifest_url == ""
|
||||
|
||||
|
||||
def test_load_settings_uses_default_server_for_frozen_exe(tmp_path, monkeypatch):
|
||||
from douyin.core import path_config
|
||||
from douyin.updater import force_update
|
||||
from douyin.updater.force_update import load_update_settings
|
||||
|
||||
cfg = tmp_path / "config.json"
|
||||
cfg.write_text(json.dumps({}), encoding="utf-8")
|
||||
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg))
|
||||
monkeypatch.setattr(force_update.sys, "frozen", True, raising=False)
|
||||
monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False)
|
||||
monkeypatch.delenv("DOUYIN_AUTO_UPDATE_ENABLED", raising=False)
|
||||
|
||||
settings = load_update_settings()
|
||||
|
||||
assert settings.enabled is True
|
||||
assert settings.manifest_url == force_update.DEFAULT_UPDATE_MANIFEST_URL
|
||||
assert settings.require_https is False
|
||||
assert settings.require_signed_manifest is True
|
||||
|
||||
|
||||
def test_forced_update_downloads_verifies_and_launches_installer(tmp_path):
|
||||
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
||||
|
||||
@@ -81,6 +104,73 @@ def test_forced_update_downloads_verifies_and_launches_installer(tmp_path):
|
||||
assert any(arg.startswith("/LOG=") for arg in launched[0])
|
||||
|
||||
|
||||
def test_forced_update_accepts_signed_manifest(tmp_path, monkeypatch):
|
||||
from douyin.updater import force_update
|
||||
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
||||
|
||||
private_key = serialization.load_pem_private_key(TEST_PRIVATE_KEY_PEM, password=None)
|
||||
monkeypatch.setattr(force_update, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
||||
payload = b"signed installer"
|
||||
expected_sha = hashlib.sha256(payload).hexdigest()
|
||||
manifest = {
|
||||
"enabled": True,
|
||||
"version": "3.3.4",
|
||||
"url": "http://103.236.55.219:18100/api/v1/update/download/DouyinGuaji-3.3.4-Setup.exe",
|
||||
"filename": "DouyinGuaji-3.3.4-Setup.exe",
|
||||
"sha256": expected_sha,
|
||||
"force": True,
|
||||
}
|
||||
manifest["signature"] = force_update.base64.b64encode(
|
||||
private_key.sign(force_update._canonical_payload_bytes(dict(manifest)))
|
||||
).decode("ascii")
|
||||
launched: list[list[str]] = []
|
||||
|
||||
result = perform_forced_update(
|
||||
UpdateSettings(
|
||||
enabled=True,
|
||||
manifest_url="http://103.236.55.219:18100/api/v1/update/manifest",
|
||||
download_dir=tmp_path,
|
||||
require_https=False,
|
||||
require_signed_manifest=True,
|
||||
),
|
||||
current_version="3.3.3",
|
||||
fetch_manifest=lambda url, timeout: manifest,
|
||||
download_file=lambda url, dest, timeout: dest.write_bytes(payload),
|
||||
launch_installer=lambda cmd: launched.append(cmd),
|
||||
)
|
||||
|
||||
assert result.status == "update_started"
|
||||
assert launched
|
||||
|
||||
|
||||
def test_forced_update_rejects_bad_signed_manifest(tmp_path, monkeypatch):
|
||||
from douyin.updater import force_update
|
||||
from douyin.updater.force_update import UpdateError, UpdateSettings, perform_forced_update
|
||||
|
||||
monkeypatch.setattr(force_update, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
||||
manifest = {
|
||||
"version": "3.3.4",
|
||||
"url": "http://103.236.55.219:18100/api/v1/update/download/app.exe",
|
||||
"sha256": "0" * 64,
|
||||
"signature": "bad",
|
||||
}
|
||||
|
||||
with pytest.raises(UpdateError, match="签名"):
|
||||
perform_forced_update(
|
||||
UpdateSettings(
|
||||
enabled=True,
|
||||
manifest_url="http://103.236.55.219:18100/api/v1/update/manifest",
|
||||
download_dir=tmp_path,
|
||||
require_https=False,
|
||||
require_signed_manifest=True,
|
||||
),
|
||||
current_version="3.3.3",
|
||||
fetch_manifest=lambda url, timeout: manifest,
|
||||
download_file=lambda url, dest, timeout: None,
|
||||
launch_installer=lambda cmd: None,
|
||||
)
|
||||
|
||||
|
||||
def test_launch_installer_on_windows_waits_for_parent_before_setup(monkeypatch, tmp_path):
|
||||
from douyin.updater import force_update
|
||||
|
||||
|
||||
@@ -121,6 +121,64 @@ def test_admin_page_can_create_and_revoke_card(tmp_path):
|
||||
assert service.activate(card, machine_fingerprint="machine-a").reason == "revoked"
|
||||
|
||||
|
||||
def test_update_manifest_defaults_to_no_update_with_signature(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("/api/v1/update/manifest")
|
||||
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["enabled"] is False
|
||||
assert body["version"] == "0.0.0"
|
||||
assert isinstance(body["signature"], str)
|
||||
|
||||
|
||||
def test_admin_can_publish_update_and_download_installer(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"))
|
||||
client.post("/admin/login", data={"admin_token": "secret"}, follow_redirects=False)
|
||||
payload = b"installer bytes"
|
||||
|
||||
published = client.post(
|
||||
"/admin/update/publish",
|
||||
cookies={api.ADMIN_CSRF_COOKIE: "csrf-token"},
|
||||
data={
|
||||
"csrf_token": "csrf-token",
|
||||
"version": "3.3.4",
|
||||
"notes": "smoke",
|
||||
"force": "1",
|
||||
},
|
||||
files={"installer": ("DouyinGuaji-3.3.4-Setup.exe", payload, "application/octet-stream")},
|
||||
)
|
||||
manifest = client.get("/api/v1/update/manifest")
|
||||
body = manifest.json()
|
||||
download = client.get("/api/v1/update/download/DouyinGuaji-3.3.4-Setup.exe")
|
||||
|
||||
assert published.status_code == 200
|
||||
assert "已发布更新 V3.3.4" in published.text
|
||||
assert manifest.status_code == 200
|
||||
assert body["enabled"] is True
|
||||
assert body["version"] == "3.3.4"
|
||||
assert body["sha256"]
|
||||
assert body["url"].endswith("/api/v1/update/download/DouyinGuaji-3.3.4-Setup.exe")
|
||||
assert isinstance(body["signature"], str)
|
||||
assert download.status_code == 200
|
||||
assert download.content == payload
|
||||
|
||||
|
||||
def test_admin_page_shows_stats_cards_and_audit_logs(tmp_path):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
Reference in new issue
Block a user