pump / backend /tests /test_integration_config.py
Codex
Deploy efficiency estimate fix cf55248
aec7019
Raw History Blame Contribute Delete
2.77 kB
import json
import pytest
from fastapi.testclient import TestClient
from pydantic import ValidationError
from app.integration_config import (
IntegrationConfig,
IntegrationConfigStore,
IntegrationConfigUpdate,
OpcConnectionUpdate,
default_config,
get_config_store,
)
from app.main import app
def make_store(tmp_path):
return IntegrationConfigStore(
tmp_path / "integration_config.json",
tmp_path / "opcua_secret.json",
)
def test_default_config_has_complete_station_tag_roster():
config = default_config()
assert config.source_mode == "simulation"
assert len(config.tags) == 41
assert any(tag.key == "level" and tag.required for tag in config.tags)
assert any(tag.key == "pump5.freq" for tag in config.tags)
assert any(tag.key == "pump5.cmd" and tag.writable for tag in config.tags)
def test_store_persists_config_and_keeps_password_out_of_public_json(tmp_path):
store = make_store(tmp_path)
current = store.load()
connection = OpcConnectionUpdate(
**current.connection.model_dump(exclude={"password_configured"}),
password="secret-value",
)
update = IntegrationConfigUpdate(
source_mode="opcua",
connection=connection,
tags=current.tags,
)
saved = store.save(update)
assert saved.revision == 2
assert saved.source_mode == "opcua"
assert saved.connection.password_configured is True
assert store.get_password() == "secret-value"
public_payload = json.loads(store.config_path.read_text(encoding="utf-8"))
assert "password" not in public_payload["connection"]
assert "secret-value" not in store.config_path.read_text(encoding="utf-8")
assert "secret-value" in store.secret_path.read_text(encoding="utf-8")
def test_duplicate_enabled_node_ids_are_rejected():
config = default_config()
config.tags[1].node_id = config.tags[0].node_id
with pytest.raises(ValidationError):
IntegrationConfig.model_validate(config.model_dump())
def test_config_api_roundtrip(tmp_path):
store = make_store(tmp_path)
app.dependency_overrides[get_config_store] = lambda: store
try:
client = TestClient(app)
response = client.get("/api/integration/config")
assert response.status_code == 200
payload = response.json()
assert payload["source_mode"] == "simulation"
payload["source_mode"] = "opcua"
payload["connection"].pop("password_configured", None)
response = client.put("/api/integration/config", json=payload)
assert response.status_code == 200
assert response.json()["source_mode"] == "opcua"
assert response.json()["revision"] == 2
finally:
app.dependency_overrides.clear()