Files
kernel-automation/tests/test_report_auth_config.py
T
patrickandClaude Opus 5.5 07749b01b0 Scaffold kernelwatch: fleet kernel status agent and dashboard
Agent: bash script run hourly by a hardened systemd timer on Debian 12/13
and Ubuntu 24.04. Collects running vs installed kernel (needrestart when
available), pending/security updates via a read-only apt simulation,
reboot-required state and apt list freshness, and POSTs them as JSON.

Server: stdlib-only Python + SQLite (system python3 >= 3.11). Token-bound
ingest API, login-protected dashboard with per-host status
(stale/reboot/update/ok) and history, admin CLI, systemd unit and installer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 10:15:06 -04:00

72 lines
2.6 KiB
Python

import os
import unittest
from helpers import tempdir
from kernelwatch import auth, config
from kernelwatch.report import ReportError, sanitize
class SanitizeTest(unittest.TestCase):
def test_drops_unknown_and_truncates(self):
out = sanitize({"kernel_running": "6.8.0-85-generic", "evil": "x", "os_id": "u" * 1000})
self.assertNotIn("evil", out)
self.assertEqual(len(out["os_id"]), 256)
def test_requires_kernel_running(self):
with self.assertRaises(ReportError):
sanitize({"os_id": "debian"})
def test_type_checks(self):
for field, bad in (("security_count", "3"), ("security_count", True),
("reboot_required", 1), ("kernels_installed", ["a", 2])):
with self.subTest(field=field, value=bad), self.assertRaises(ReportError):
sanitize({"kernel_running": "x", field: bad})
def test_nulls_allowed(self):
self.assertIsNone(sanitize({"kernel_running": "x", "needrestart_ksta": None})["needrestart_ksta"])
def test_not_an_object(self):
with self.assertRaises(ReportError):
sanitize([1, 2])
class AuthTest(unittest.TestCase):
def test_password_roundtrip(self):
h = auth.hash_password("correct horse battery staple")
self.assertTrue(auth.verify_password("correct horse battery staple", h))
self.assertFalse(auth.verify_password("wrong", h))
def test_garbage_hash(self):
self.assertFalse(auth.verify_password("x", "nonsense"))
self.assertFalse(auth.verify_password("x", "scrypt$a$b$c$d$e"))
def test_tokens_unique(self):
self.assertNotEqual(auth.new_token(), auth.new_token())
class ConfigTest(unittest.TestCase):
def write(self, tmp, text):
path = os.path.join(tmp, "server.toml")
with open(path, "w") as f:
f.write(text)
return path
def test_load(self):
with tempdir() as tmp:
cfg = config.load(self.write(tmp, "listen_port = 9000\nstale_after_hours = 2\n"))
self.assertEqual(cfg.listen_port, 9000)
self.assertEqual(cfg.stale_after_hours, 2.0)
def test_rejects_unknown_and_wrong_types(self):
with tempdir() as tmp:
with self.assertRaises(SystemExit):
config.load(self.write(tmp, "listen_prot = 9000\n"))
with self.assertRaises(SystemExit):
config.load(self.write(tmp, 'listen_port = "9000"\n'))
with self.assertRaises(SystemExit):
config.load(self.write(tmp, "secure_cookies = 1\n"))
if __name__ == "__main__":
unittest.main()