65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
|
|
import os
|
||
|
|
from datetime import datetime, timedelta, timezone
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
|
||
|
|
class DummyConfigManager:
|
||
|
|
def __init__(self, values):
|
||
|
|
self._values = values
|
||
|
|
|
||
|
|
def get_config_value(self, section, key, fallback=None):
|
||
|
|
return self._values.get((section, key), fallback)
|
||
|
|
|
||
|
|
|
||
|
|
def test_check_validity_accepts_candidate_machine_id(monkeypatch):
|
||
|
|
from devices.utils.license_manager import LicenseManager
|
||
|
|
|
||
|
|
lm = LicenseManager(config_manager=DummyConfigManager({}))
|
||
|
|
monkeypatch.setattr(lm, "get_machine_id_candidates", lambda: ["MID-PRIMARY", "MID-ALT"])
|
||
|
|
|
||
|
|
license_data = {
|
||
|
|
"product": "BodyBalanceEvaluation",
|
||
|
|
"license_id": "L1",
|
||
|
|
"license_type": "full",
|
||
|
|
"machine_id": "MID-ALT",
|
||
|
|
"issued_at": datetime.now(timezone.utc).isoformat(),
|
||
|
|
"expires_at": (datetime.now(timezone.utc) + timedelta(days=1)).isoformat().replace("+00:00", "Z"),
|
||
|
|
"signature": "x",
|
||
|
|
"features": {"export": True},
|
||
|
|
}
|
||
|
|
|
||
|
|
ok, msg = lm.check_validity(license_data, machine_id="MID-PRIMARY", grace_days=0)
|
||
|
|
assert ok is True, msg
|
||
|
|
|
||
|
|
|
||
|
|
def test_resolve_license_paths_falls_back_to_persistent(monkeypatch, tmp_path):
|
||
|
|
from devices.utils.license_manager import LicenseManager
|
||
|
|
|
||
|
|
programdata = tmp_path / "programdata"
|
||
|
|
persistent_dir = programdata / "BodyCheck" / "license"
|
||
|
|
persistent_dir.mkdir(parents=True)
|
||
|
|
(persistent_dir / "license.json").write_text('{"a":1}', encoding="utf-8")
|
||
|
|
(persistent_dir / "license_public_key.pem").write_text("PUB", encoding="utf-8")
|
||
|
|
|
||
|
|
cfg_dir = tmp_path / "cfg"
|
||
|
|
cfg_license_path = str(cfg_dir / "license.json")
|
||
|
|
cfg_pub_path = str(cfg_dir / "license_public_key.pem")
|
||
|
|
|
||
|
|
cfg = DummyConfigManager(
|
||
|
|
{
|
||
|
|
("LICENSE", "path"): cfg_license_path,
|
||
|
|
("LICENSE", "public_key"): cfg_pub_path,
|
||
|
|
("LICENSE", "grace_days"): "3",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
lm = LicenseManager(config_manager=cfg)
|
||
|
|
|
||
|
|
monkeypatch.setenv("PROGRAMDATA", str(programdata))
|
||
|
|
monkeypatch.setattr("platform.system", lambda: "Windows")
|
||
|
|
|
||
|
|
license_path, pub_path, grace_days = lm._resolve_license_paths()
|
||
|
|
assert license_path == str(persistent_dir / "license.json")
|
||
|
|
assert pub_path == str(persistent_dir / "license_public_key.pem")
|
||
|
|
assert grace_days == 3
|