Spaces:
Sleeping
Sleeping
File size: 2,772 Bytes
aec7019 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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()
|