"""Tests for configuration loading functionality."""
import json
import os
import tempfile
from pathlib import Path
from unittest import mock
import pytest
def test_load_config_from_env_vars():
"""Test loading configuration from environment variables."""
from splitwise_mcp.config import load_config
env_vars = {
"SPLITWISE_API_KEY": "test_api_key",
"SPLITWISE_GROUP_ID": "12345",
"SPLITWISE_PAYER_ID": "111",
"SPLITWISE_PARTNER_ID": "222",
}
with mock.patch.dict(os.environ, env_vars, clear=False):
config = load_config()
assert config["api_key"] == "test_api_key"
assert config["group_id"] == "12345"
assert config["payer_id"] == "111"
assert config["partner_id"] == "222"
def test_load_config_from_config_path_env():
"""Test loading configuration from SPLITWISE_CONFIG_PATH."""
from splitwise_mcp.config import load_config
config_data = {
"splitwise": {
"api_key": "file_api_key",
"group_id": "67890",
"payer_id": "333",
"partner_id": "444",
}
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(config_data, f)
config_path = f.name
try:
# Clear any existing env vars that would take priority
env_vars = {
"SPLITWISE_CONFIG_PATH": config_path,
}
clear_vars = ["SPLITWISE_API_KEY", "SPLITWISE_GROUP_ID", "SPLITWISE_PAYER_ID", "SPLITWISE_PARTNER_ID"]
with mock.patch.dict(os.environ, env_vars, clear=False):
# Remove the individual env vars if they exist
for var in clear_vars:
os.environ.pop(var, None)
config = load_config()
assert config["api_key"] == "file_api_key"
assert config["group_id"] == "67890"
assert config["payer_id"] == "333"
assert config["partner_id"] == "444"
finally:
os.unlink(config_path)
def test_load_config_from_local_config_json(tmp_path, monkeypatch):
"""Test loading configuration from ./config.json."""
from splitwise_mcp.config import load_config
config_data = {
"splitwise": {
"api_key": "local_api_key",
"group_id": "11111",
"payer_id": "555",
"partner_id": "666",
}
}
config_file = tmp_path / "config.json"
config_file.write_text(json.dumps(config_data))
# Change to the temp directory
monkeypatch.chdir(tmp_path)
# Clear env vars
for var in ["SPLITWISE_API_KEY", "SPLITWISE_GROUP_ID", "SPLITWISE_PAYER_ID", "SPLITWISE_PARTNER_ID", "SPLITWISE_CONFIG_PATH"]:
monkeypatch.delenv(var, raising=False)
config = load_config()
assert config["api_key"] == "local_api_key"
assert config["group_id"] == "11111"
assert config["payer_id"] == "555"
assert config["partner_id"] == "666"
def test_env_vars_override_config_file():
"""Test that environment variables take priority over config file."""
from splitwise_mcp.config import load_config
config_data = {
"splitwise": {
"api_key": "file_api_key",
"group_id": "file_group",
"payer_id": "file_payer",
"partner_id": "file_partner",
}
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(config_data, f)
config_path = f.name
try:
env_vars = {
"SPLITWISE_CONFIG_PATH": config_path,
"SPLITWISE_API_KEY": "env_api_key", # This should override
"SPLITWISE_GROUP_ID": "env_group", # This should override
"SPLITWISE_PAYER_ID": "env_payer", # This should override
"SPLITWISE_PARTNER_ID": "env_partner", # This should override
}
with mock.patch.dict(os.environ, env_vars, clear=False):
config = load_config()
# Env vars should take priority
assert config["api_key"] == "env_api_key"
assert config["group_id"] == "env_group"
assert config["payer_id"] == "env_payer"
assert config["partner_id"] == "env_partner"
finally:
os.unlink(config_path)
def test_configuration_error_when_missing_fields(tmp_path, monkeypatch):
"""Test ConfigurationError raised when required fields are missing."""
from splitwise_mcp.config import load_config, ConfigurationError
# Change to temp dir with no config.json
monkeypatch.chdir(tmp_path)
# Clear all env vars
for var in ["SPLITWISE_API_KEY", "SPLITWISE_GROUP_ID", "SPLITWISE_PAYER_ID", "SPLITWISE_PARTNER_ID", "SPLITWISE_CONFIG_PATH"]:
monkeypatch.delenv(var, raising=False)
with pytest.raises(ConfigurationError) as exc_info:
load_config()
# Error message should mention missing fields
error_msg = str(exc_info.value)
assert "SPLITWISE_API_KEY" in error_msg or "api_key" in error_msg
def test_configuration_error_when_partial_env_vars(monkeypatch):
"""Test ConfigurationError raised when only some env vars are set."""
from splitwise_mcp.config import load_config, ConfigurationError
# Only set one env var
monkeypatch.setenv("SPLITWISE_API_KEY", "test_key")
monkeypatch.delenv("SPLITWISE_GROUP_ID", raising=False)
monkeypatch.delenv("SPLITWISE_PAYER_ID", raising=False)
monkeypatch.delenv("SPLITWISE_PARTNER_ID", raising=False)
monkeypatch.delenv("SPLITWISE_CONFIG_PATH", raising=False)
# No local config.json either
monkeypatch.chdir("/tmp")
with pytest.raises(ConfigurationError):
load_config()
def test_configuration_error_message_is_helpful(tmp_path, monkeypatch):
"""Test that ConfigurationError provides helpful guidance."""
from splitwise_mcp.config import load_config, ConfigurationError
monkeypatch.chdir(tmp_path)
for var in ["SPLITWISE_API_KEY", "SPLITWISE_GROUP_ID", "SPLITWISE_PAYER_ID", "SPLITWISE_PARTNER_ID", "SPLITWISE_CONFIG_PATH"]:
monkeypatch.delenv(var, raising=False)
with pytest.raises(ConfigurationError) as exc_info:
load_config()
error_msg = str(exc_info.value)
# Should mention how to fix the issue
assert "config.json" in error_msg.lower() or "environment" in error_msg.lower()