File size: 1,864 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
from fastapi.testclient import TestClient

from app.integration_config import (
    IntegrationConfigStore,
    IntegrationConfigUpdate,
    OpcConnectionUpdate,
    get_config_store,
)
from app.main import app


def _password_store(tmp_path):
    store = IntegrationConfigStore(tmp_path / "config.json", tmp_path / "secret.json")
    current = store.load()
    store.save(
        IntegrationConfigUpdate(
            source_mode="opcua",
            connection=OpcConnectionUpdate(
                **current.connection.model_dump(exclude={"password_configured"}),
                password="internal-secret",
            ),
            tags=current.tags,
        )
    )
    return store


def test_runtime_requires_internal_token_when_password_exists(tmp_path, monkeypatch):
    store = _password_store(tmp_path)
    monkeypatch.delenv("INTEGRATION_INTERNAL_TOKEN", raising=False)
    app.dependency_overrides[get_config_store] = lambda: store
    try:
        response = TestClient(app).get("/api/integration/runtime")
        assert response.status_code == 503
        assert "internal-secret" not in response.text
    finally:
        app.dependency_overrides.clear()


def test_runtime_returns_password_only_with_matching_token(tmp_path, monkeypatch):
    store = _password_store(tmp_path)
    monkeypatch.setenv("INTEGRATION_INTERNAL_TOKEN", "node-red-token")
    app.dependency_overrides[get_config_store] = lambda: store
    try:
        client = TestClient(app)
        assert client.get("/api/integration/runtime").status_code == 401
        response = client.get(
            "/api/integration/runtime",
            headers={"X-Integration-Token": "node-red-token"},
        )
        assert response.status_code == 200
        assert response.json()["connection"]["password"] == "internal-secret"
    finally:
        app.dependency_overrides.clear()