mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-17 10:33:15 +01:00
Generate auth-gateway test fixtures from the Django config exporter
This commit is contained in:
@@ -17,25 +17,32 @@ assert _PROCESS_CONFIG_SPEC and _PROCESS_CONFIG_SPEC.loader
|
||||
_PROCESS_CONFIG_SPEC.loader.exec_module(_PROCESS_CONFIG_MODULE)
|
||||
build_caddyfile = _PROCESS_CONFIG_MODULE.build_caddyfile
|
||||
|
||||
# Generated by the Django exporter: UPDATE_AUTH_GATEWAY_FIXTURES=1 python manage.py test app_gateway
|
||||
FIXTURES_CONFIG_DIR = Path(__file__).resolve().parent / "fixtures" / "config_files"
|
||||
|
||||
|
||||
class AuthGatewayConfigTests(unittest.TestCase):
|
||||
def test_existing_config_loads_and_resolves_routes(self):
|
||||
config_dir = Path(__file__).resolve().parents[2] / "caddy" / "config_files"
|
||||
runtime_config = load_runtime_config(config_dir)
|
||||
runtime_config = load_runtime_config(FIXTURES_CONFIG_DIR)
|
||||
|
||||
context = resolve_request_context(runtime_config, "app1-dev.local", "/admin/settings")
|
||||
context = resolve_request_context(runtime_config, "dashboard.example.test", "/admin/settings")
|
||||
self.assertIsNotNone(context)
|
||||
self.assertEqual(context.policy_name, "senha-totp")
|
||||
self.assertEqual(context.policy_name, "password-totp")
|
||||
|
||||
context = resolve_request_context(runtime_config, "app1-dev.local", "/api/status")
|
||||
context = resolve_request_context(runtime_config, "dashboard.example.test", "/api/status")
|
||||
self.assertIsNotNone(context)
|
||||
self.assertEqual(context.policy_name, "ips-conhecidos")
|
||||
self.assertEqual(context.policy_name, "trusted-ips")
|
||||
|
||||
policy = build_effective_policy(runtime_config, "senha-totp")
|
||||
context = resolve_request_context(runtime_config, "dashboard.example.test", "/")
|
||||
self.assertIsNotNone(context)
|
||||
self.assertEqual(context.policy_name, "staff-password")
|
||||
|
||||
policy = build_effective_policy(runtime_config, "password-totp")
|
||||
self.assertIsNotNone(policy)
|
||||
self.assertEqual(policy.required_factors, ["totp", "password"])
|
||||
self.assertEqual(policy.required_factors, ["password", "totp"])
|
||||
self.assertEqual(policy.allowed_users, {"alice", "bob"})
|
||||
|
||||
def test_invalid_oidc_provider_fails_loudly(self):
|
||||
def test_invalid_oidc_provider_disables_policy(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
tmp_path = Path(tmpdir)
|
||||
(tmp_path / "wireguard_webadmin.json").write_text(
|
||||
@@ -51,16 +58,19 @@ class AuthGatewayConfigTests(unittest.TestCase):
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
load_runtime_config(tmp_path)
|
||||
with self.assertLogs("auth_gateway.config_loader", level="WARNING"):
|
||||
runtime_config = load_runtime_config(tmp_path)
|
||||
|
||||
self.assertNotIn("oidc", runtime_config.auth_methods)
|
||||
self.assertEqual(runtime_config.policies["default"].policy_type, "error")
|
||||
|
||||
def test_ip_rules_respect_json_order(self):
|
||||
config_dir = Path(__file__).resolve().parents[2] / "caddy" / "config_files"
|
||||
runtime_config = load_runtime_config(config_dir)
|
||||
method = runtime_config.auth_methods["iplist1"]
|
||||
self.assertTrue(evaluate_ip_rules("192.168.0.8", method.rules))
|
||||
self.assertTrue(evaluate_ip_rules("192.168.0.12", method.rules))
|
||||
self.assertFalse(evaluate_ip_rules("10.10.10.10", method.rules))
|
||||
runtime_config = load_runtime_config(FIXTURES_CONFIG_DIR)
|
||||
method = runtime_config.auth_methods["ip-allowlist"]
|
||||
self.assertTrue(evaluate_ip_rules("10.10.0.1", method.rules))
|
||||
self.assertTrue(evaluate_ip_rules("10.20.0.8", method.rules))
|
||||
self.assertTrue(evaluate_ip_rules("10.20.0.12", method.rules))
|
||||
self.assertFalse(evaluate_ip_rules("10.99.0.1", method.rules))
|
||||
|
||||
def test_totp_verification_accepts_valid_tokens(self):
|
||||
secret = "JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP"
|
||||
@@ -68,10 +78,12 @@ class AuthGatewayConfigTests(unittest.TestCase):
|
||||
self.assertTrue(verify_totp(secret, token))
|
||||
|
||||
def test_totp_verification_rejects_short_secrets(self):
|
||||
self.assertFalse(verify_totp("JBSWY3DPEHPK3PXP", "123456"))
|
||||
with self.assertLogs("uvicorn.error", level="WARNING"):
|
||||
self.assertFalse(verify_totp("JBSWY3DPEHPK3PXP", "123456"))
|
||||
|
||||
def test_totp_verification_rejects_invalid_secrets(self):
|
||||
self.assertFalse(verify_totp("NOT-A-BASE32-SECRET!", "123456"))
|
||||
with self.assertLogs("uvicorn.error", level="WARNING"):
|
||||
self.assertFalse(verify_totp("NOT-A-BASE32-SECRET!", "123456"))
|
||||
|
||||
def test_caddyfile_uses_boundary_matchers_and_clears_identity_headers(self):
|
||||
caddyfile = build_caddyfile(
|
||||
|
||||
Reference in New Issue
Block a user