"""Tests for configuration loading.""" import tempfile from pathlib import Path import pytest from bouncer.config import load MINIMAL_CONFIG = """\ [bouncer] password = "secret" [proxy] host = "127.0.0.1" port = 1080 [networks.test] host = "irc.example.com" """ FULL_CONFIG = """\ [bouncer] bind = "0.0.0.0" port = 6668 password = "hunter2" [bouncer.backlog] max_messages = 5000 replay_on_connect = false [proxy] host = "10.0.0.1" port = 9050 [networks.libera] host = "irc.libera.chat" port = 6697 tls = true nick = "testbot" user = "testuser" realname = "Test Bot" channels = ["#test", "#dev"] autojoin = true [networks.oftc] host = "irc.oftc.net" port = 6697 tls = true nick = "testbot" channels = ["#debian"] """ def _write_config(content: str) -> Path: f = tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) f.write(content) f.close() return Path(f.name) class TestLoad: def test_minimal(self): cfg = load(_write_config(MINIMAL_CONFIG)) assert cfg.bouncer.password == "secret" assert cfg.bouncer.bind == "127.0.0.1" assert cfg.bouncer.port == 6667 assert cfg.bouncer.backlog.max_messages == 10000 assert cfg.proxy.host == "127.0.0.1" assert "test" in cfg.networks net = cfg.networks["test"] assert net.host == "irc.example.com" assert net.port == 6667 assert net.tls is False def test_full(self): cfg = load(_write_config(FULL_CONFIG)) assert cfg.bouncer.bind == "0.0.0.0" assert cfg.bouncer.port == 6668 assert cfg.bouncer.backlog.max_messages == 5000 assert cfg.bouncer.backlog.replay_on_connect is False assert cfg.proxy.port == 9050 assert len(cfg.networks) == 2 libera = cfg.networks["libera"] assert libera.tls is True assert libera.port == 6697 assert libera.channels == ["#test", "#dev"] assert libera.nick == "testbot" def test_no_networks_raises(self): config = """\ [bouncer] password = "x" [proxy] """ with pytest.raises(ValueError, match="at least one network"): load(_write_config(config)) def test_slash_in_network_name_raises(self): config = """\ [bouncer] password = "x" [proxy] [networks."lib/era"] host = "irc.example.com" """ with pytest.raises(ValueError, match="must not contain '/'"): load(_write_config(config)) def test_tls_default_port(self): config = """\ [bouncer] password = "x" [proxy] [networks.test] host = "irc.example.com" tls = true """ cfg = load(_write_config(config)) assert cfg.networks["test"].port == 6697