"""Break-glass administrator reset: a lost admin password is recovered with a server secret the host's owner sets (FPT_ADMIN_PASSWORD_RESET), applied once per value at start-up and audited. Seen live: the only copy of a deployment's admin password was lost, and the start-up bootstrap only ever creates an admin on an empty database.""" from __future__ import annotations import os import unittest from unittest import mock from coworker.tests.fakes import make_app class TestAdminReset(unittest.TestCase): def test_reset_once_per_value_and_audited(self): app, c, svc, _ = make_app() users = svc.users name = os.getenv("FPT_BOOTSTRAP_ADMIN_USER") or "admin" if not users.by_name(name): users.create(name, "Old-password-123!", "viewer", created_by="test") new = "Recovered-Pass-456!" with mock.patch.dict(os.environ, {"FPT_ADMIN_PASSWORD_RESET": new}): self.assertEqual(users.reset_admin_from_env(), name) u = users.by_name(name) self.assertEqual((u["role"], u["disabled"]), ("admin", 0)) self.assertTrue(users.authenticate(name, new)) self.assertIsNone(users.reset_admin_from_env()) # same value: applied once only with mock.patch.dict(os.environ, {}, clear=False): os.environ.pop("FPT_ADMIN_PASSWORD_RESET", None) self.assertIsNone(users.reset_admin_from_env()) # no secret: nothing happens if __name__ == "__main__": unittest.main()