cteward-ng/cteward_ng/tests/test_config.py

60 lines
1.6 KiB
Python
Raw Normal View History

2026-06-06 10:18:15 +02:00
"""Test configuration loading.
Verifies that config.py loads the JSON config and applies defaults
the same way startup.js does.
"""
import json
import os
import tempfile
import pytest
from cteward_ng.config import load_config
class TestLoadConfig:
def test_loads_json_file(self):
with tempfile.NamedTemporaryFile(
mode='w', suffix='.json', delete=False
) as fh:
json.dump({'mssql': {'password': 'test'}}, fh)
fh.flush()
config = load_config(fh.name)
os.unlink(fh.name)
assert config['mssql']['password'] == 'test'
def test_applies_defaults(self):
with tempfile.NamedTemporaryFile(
mode='w', suffix='.json', delete=False
) as fh:
json.dump({}, fh)
fh.flush()
config = load_config(fh.name)
os.unlink(fh.name)
assert 'mssql' in config
assert 'server' in config
assert 'auth' in config
assert 'bots' in config['auth']
assert 'flags' in config['auth']
2026-06-06 12:04:59 +02:00
def test_missing_file_returns_defaults(self):
2026-06-06 10:18:15 +02:00
config = load_config('/nonexistent/path.json')
2026-06-06 12:04:59 +02:00
assert 'mssql' in config
assert 'server' in config
assert 'auth' in config
2026-06-06 10:18:15 +02:00
2026-06-06 12:04:59 +02:00
def test_invalid_json_returns_defaults(self):
2026-06-06 10:18:15 +02:00
with tempfile.NamedTemporaryFile(
mode='w', suffix='.json', delete=False
) as fh:
fh.write('{invalid json}')
fh.flush()
config = load_config(fh.name)
os.unlink(fh.name)
2026-06-06 12:04:59 +02:00
assert 'mssql' in config
assert 'server' in config
assert 'auth' in config