Ignore non-object prefs JSON (#1257)
This commit is contained in:
parent
84d54d9853
commit
ba6da17a92
2 changed files with 22 additions and 1 deletions
|
|
@ -12,7 +12,8 @@ def _load():
|
|||
"""Load the raw prefs file (internal use only)."""
|
||||
try:
|
||||
with open(PREFS_FILE, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
data = json.load(f)
|
||||
return data if isinstance(data, dict) else {}
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
|
|
|
|||
20
tests/test_prefs_routes.py
Normal file
20
tests/test_prefs_routes.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import json
|
||||
|
||||
import routes.prefs_routes as prefs_routes
|
||||
|
||||
|
||||
def test_load_ignores_non_object_prefs_file(tmp_path, monkeypatch):
|
||||
prefs_file = tmp_path / "user_prefs.json"
|
||||
prefs_file.write_text(json.dumps(["not", "a", "prefs", "object"]), encoding="utf-8")
|
||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", str(prefs_file))
|
||||
|
||||
assert prefs_routes._load() == {}
|
||||
assert prefs_routes._load_for_user("alice") == {}
|
||||
|
||||
|
||||
def test_load_keeps_object_prefs_file(tmp_path, monkeypatch):
|
||||
prefs_file = tmp_path / "user_prefs.json"
|
||||
prefs_file.write_text(json.dumps({"theme": "dark"}), encoding="utf-8")
|
||||
monkeypatch.setattr(prefs_routes, "PREFS_FILE", str(prefs_file))
|
||||
|
||||
assert prefs_routes._load_for_user("alice") == {"theme": "dark"}
|
||||
Loading…
Add table
Reference in a new issue