Files
archy/apps/qortal/test_settings.py
ssmithx 4cc625e4d1 fix(qortal): enforce settings and honour icon-grid prompt policy
Parse settings with jq and require explicit safe booleans before listeners start. Reject missing/malformed configuration and multiple JSON documents. Honour prompt_before_launch in the icon-grid flow and cover false, true and legacy absent flags.

Validation: 17 focused frontend tests, 4 Python guard tests, shell syntax checks, Qortal manifest preflight and diff check passed. Image rebuild and live-node lifecycle acceptance remain pending.
2026-10-21 13:48:23 +00:00

55 lines
2.3 KiB
Python

"""Source-only security guard tests: python3 apps/qortal/test_settings.py."""
import json
from pathlib import Path
import subprocess
import tempfile
import unittest
GUARD = Path(__file__).with_name("validate-settings.sh")
SAFE = {"localAuthBypassEnabled": False, "apiRestricted": True}
class SettingsGuardTests(unittest.TestCase):
def check_settings(self, content, allowed):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "settings.json"
if content is not None:
path.write_text(content)
result = subprocess.run(
["sh", str(GUARD), str(path)], capture_output=True, text=True
)
self.assertEqual(result.returncode, 0 if allowed else 78)
self.assertEqual(result.stdout, "")
self.assertNotIn("private-config-marker", result.stderr)
def test_accepts_explicit_safe_settings(self):
for indent in (None, 2):
self.check_settings(json.dumps(SAFE, indent=indent), True)
self.check_settings('{"localAuthBypassEnabled":\nfalse,"apiRestricted":\ntrue}', True)
def test_rejects_unsafe_settings_regardless_of_formatting(self):
for key, value in (("localAuthBypassEnabled", True), ("apiRestricted", False)):
settings = {**SAFE, key: value}
for indent in (None, 2):
with self.subTest(key=key, indent=indent):
self.check_settings(json.dumps(settings, indent=indent), False)
self.check_settings('{"localAuthBypassEnabled":\ntrue,"apiRestricted":\nfalse}', False)
def test_rejects_missing_null_and_wrong_types(self):
for key in SAFE:
settings = dict(SAFE)
del settings[key]
self.check_settings(json.dumps(settings), False)
for value in (None, 0, 1, "true", "false", [], {}):
self.check_settings(json.dumps({**SAFE, key: value}), False)
def test_rejects_missing_malformed_and_multiple_documents(self):
for content in (None, "", "{private-config-marker", "{}", "[]", "null",
json.dumps(SAFE) + '\n' + json.dumps(SAFE)):
with self.subTest(content=content):
self.check_settings(content, False)
if __name__ == "__main__":
unittest.main()