Hearth/tests/test_config.py

103 lines
2.6 KiB
Python

import textwrap
import pytest
from hearth.config import ConfigError, load_config
VALID_YAML = textwrap.dedent(
"""
participants:
alerts:
tier: full_duplex
email: alerts@levkine.ca
display_name: "Ilia"
imap_host: mail.levkine.ca
smtp_host: mail.levkine.ca
username: alerts@levkine.ca
password_env: TEST_PASSWORD_ALERTS
friend:
tier: send_only
email: friend@example.com
display_name: "Friend"
"""
)
def write_yaml(tmp_path, content):
path = tmp_path / "hearth.yml"
path.write_text(content)
return path
def test_load_valid_config(tmp_path):
path = write_yaml(tmp_path, VALID_YAML)
config = load_config(path)
assert set(config.all_keys()) == {"alerts", "friend"}
assert config.full_duplex_keys() == ["alerts"]
assert config.is_known_email("ALERTS@levkine.ca") == "alerts"
assert config.is_known_email("nobody@nowhere.com") is None
def test_missing_file_raises(tmp_path):
with pytest.raises(ConfigError):
load_config(tmp_path / "does-not-exist.yml")
def test_duplicate_email_rejected(tmp_path):
content = textwrap.dedent(
"""
participants:
alerts:
tier: full_duplex
email: alerts@levkine.ca
imap_host: mail.levkine.ca
smtp_host: mail.levkine.ca
username: alerts@levkine.ca
password_env: TEST_PASSWORD_ALERTS
duplicate:
tier: send_only
email: alerts@levkine.ca
display_name: "Dup"
"""
)
path = write_yaml(tmp_path, content)
with pytest.raises(ConfigError):
load_config(path)
def test_full_duplex_requires_creds_fields(tmp_path):
content = textwrap.dedent(
"""
participants:
broken:
tier: full_duplex
email: broken@levkine.ca
"""
)
path = write_yaml(tmp_path, content)
with pytest.raises(ConfigError):
load_config(path)
def test_requires_at_least_one_full_duplex(tmp_path):
content = textwrap.dedent(
"""
participants:
friend:
tier: send_only
email: friend@example.com
"""
)
path = write_yaml(tmp_path, content)
with pytest.raises(ConfigError):
load_config(path)
def test_send_only_participant_cannot_authenticate(tmp_path):
path = write_yaml(tmp_path, VALID_YAML)
config = load_config(path)
friend = config.get("friend")
assert friend.can_authenticate is False
with pytest.raises(ConfigError):
_ = friend.password