test: unit tests for licensing, LAN IP, TLS identity and rediscovery
- local_backend/tests (pytest, 48 tests, <1s): license token verify/evaluate (tamper, other site/key, offline, grace, workday deferral, clock, transition), netinfo detection on fake sysfs (VPN/tunnel/WiFi/link-local), LAN-IP resolver priority + mismatch + staleness + override validation, TLS identity create/restart/renew-same-key/foreign cert (+ openssl pin check) - waiter_pwa/tests (node --test, 7 tests): subnet candidates, scan picks only this venue, early stop, progress - requirements-dev.txt (pytest), npm test, .pytest_cache ignored Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
"""Offline-capable licensing rules (services/license.py, KI-006).
|
||||
|
||||
Tokens are signed with a throwaway key here — the production signing key is
|
||||
never needed; verify_token() takes the public key as a parameter.
|
||||
"""
|
||||
import base64
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
||||
|
||||
from services.license import LICENSE_PUBLIC_KEY, evaluate, verify_token
|
||||
|
||||
KEY = Ed25519PrivateKey.generate()
|
||||
PUB = base64.b64encode(KEY.public_key().public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)).decode()
|
||||
ISSUED = datetime.fromisoformat("2026-09-28T12:00:00+00:00")
|
||||
EXPIRES = datetime.fromisoformat("2027-09-28T00:00:00+00:00")
|
||||
|
||||
|
||||
def b64(data: bytes) -> str:
|
||||
return base64.urlsafe_b64encode(data).rstrip(b"=").decode()
|
||||
|
||||
|
||||
def make_token(signer=KEY, **overrides) -> str:
|
||||
payload = {"v": 1, "site_id": "site-A", "active": True, "locked": False, "lock_reason": None,
|
||||
"expires_at": EXPIRES.isoformat(), "issued_at": ISSUED.isoformat(), **overrides}
|
||||
body = b64(json.dumps(payload, separators=(",", ":"), sort_keys=True).encode())
|
||||
return f"{body}.{b64(signer.sign(body.encode()))}"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def payload():
|
||||
return verify_token(make_token(), "site-A", PUB)
|
||||
|
||||
|
||||
# ── verify_token ─────────────────────────────────────────────────────────────
|
||||
|
||||
def test_genuine_token_accepted(payload):
|
||||
assert payload is not None and payload["expires_at"] == EXPIRES.isoformat()
|
||||
|
||||
|
||||
def test_edited_expiry_rejected(payload):
|
||||
_, sig = make_token().split(".")
|
||||
forged = b64(json.dumps({**payload, "expires_at": "2099-01-01T00:00:00+00:00"},
|
||||
separators=(",", ":"), sort_keys=True).encode())
|
||||
assert verify_token(f"{forged}.{sig}", "site-A", PUB) is None
|
||||
|
||||
|
||||
def test_token_for_another_site_rejected():
|
||||
assert verify_token(make_token(site_id="site-B"), "site-A", PUB) is None
|
||||
|
||||
|
||||
def test_token_signed_by_another_key_rejected():
|
||||
assert verify_token(make_token(signer=Ed25519PrivateKey.generate()), "site-A", PUB) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("junk", [None, "", "not.a.token", "abc"])
|
||||
def test_garbage_rejected(junk):
|
||||
assert verify_token(junk, "site-A", PUB) is None
|
||||
|
||||
|
||||
def test_production_public_key_is_ed25519():
|
||||
assert len(base64.b64decode(LICENSE_PUBLIC_KEY)) == 32
|
||||
|
||||
|
||||
# ── evaluate: offline ────────────────────────────────────────────────────────
|
||||
|
||||
def test_364_days_offline_still_licensed(payload):
|
||||
r = evaluate(payload, ISSUED + timedelta(days=364), ISSUED, workday_open=False)
|
||||
assert (r["licensed"], r["license_problem"]) == (True, None)
|
||||
|
||||
|
||||
# ── evaluate: expiry + grace, never mid-service ──────────────────────────────
|
||||
|
||||
def test_two_days_after_expiry_is_grace(payload):
|
||||
r = evaluate(payload, EXPIRES + timedelta(days=2), ISSUED, workday_open=False)
|
||||
assert (r["licensed"], r["grace_over"], r["grace_days_remaining"]) == (True, False, 3)
|
||||
|
||||
|
||||
def test_grace_over_without_workday_blocks(payload):
|
||||
r = evaluate(payload, EXPIRES + timedelta(days=6), ISSUED, workday_open=False)
|
||||
assert (r["licensed"], r["license_problem"], r["grace_over"]) == (False, "expired", True)
|
||||
|
||||
|
||||
def test_grace_over_with_open_workday_keeps_running(payload):
|
||||
r = evaluate(payload, EXPIRES + timedelta(days=6), ISSUED, workday_open=True)
|
||||
assert (r["licensed"], r["grace_over"]) == (True, True)
|
||||
|
||||
|
||||
def test_deactivated_site_blocks():
|
||||
p = verify_token(make_token(active=False), "site-A", PUB)
|
||||
r = evaluate(p, ISSUED + timedelta(days=1), ISSUED, workday_open=False)
|
||||
assert (r["licensed"], r["license_problem"]) == (False, "inactive")
|
||||
|
||||
|
||||
# ── evaluate: clock set back ─────────────────────────────────────────────────
|
||||
|
||||
def test_clock_before_newest_activity_blocks(payload):
|
||||
floor = ISSUED + timedelta(days=100)
|
||||
r = evaluate(payload, floor - timedelta(days=30), floor, workday_open=False)
|
||||
assert (r["licensed"], r["license_problem"]) == (False, "clock")
|
||||
|
||||
|
||||
def test_small_clock_skew_tolerated(payload):
|
||||
floor = ISSUED + timedelta(days=100)
|
||||
assert evaluate(payload, floor - timedelta(hours=12), floor, workday_open=False)["licensed"]
|
||||
|
||||
|
||||
def test_clock_before_signed_issue_time_blocks(payload):
|
||||
r = evaluate(payload, ISSUED - timedelta(days=5), ISSUED, workday_open=False)
|
||||
assert r["license_problem"] == "clock"
|
||||
|
||||
|
||||
# ── evaluate: no genuine token ───────────────────────────────────────────────
|
||||
|
||||
def test_transition_old_cloud_recent_ok():
|
||||
r = evaluate(None, ISSUED, None, False, legacy_ok_at=ISSUED - timedelta(hours=10))
|
||||
assert (r["licensed"], r["license_problem"]) == (True, None)
|
||||
|
||||
|
||||
def test_transition_old_cloud_stale():
|
||||
r = evaluate(None, ISSUED, None, False, legacy_ok_at=ISSUED - timedelta(hours=80))
|
||||
assert (r["licensed"], r["license_problem"]) == (False, "unverified")
|
||||
|
||||
|
||||
def test_never_licensed_blocks():
|
||||
r = evaluate(None, ISSUED, None, False)
|
||||
assert (r["licensed"], r["license_problem"]) == (False, "unverified")
|
||||
Reference in New Issue
Block a user