45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import json
|
|||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from pigway_frame_flow.errors import FlowError
|
||
|
|
|
||
|
|
|
||
|
|
def test_mqtt_uses_scope_and_durable_idempotency(client, service, monkeypatch):
|
||
|
|
token = client.post(
|
||
|
|
"/api/v1/clients",
|
||
|
|
json={"client_id": "mqtt", "name": "MQTT", "permissions": ["display:write"]},
|
||
|
|
).json()["token"]
|
||
|
|
monkeypatch.setenv("FRAME_FLOW_MQTT_TOKEN", token)
|
||
|
|
payload = json.dumps(
|
||
|
|
{"request_id": "stable-1", "content": {"type": "template", "template": "quote"}}
|
||
|
|
).encode()
|
||
|
|
first = service.mqtt.accept(payload)
|
||
|
|
assert service.mqtt.accept(payload) == first
|
||
|
|
assert service.registry.get("living-room").client.uploads == 0
|
||
|
|
assert service.operation(first["operation_id"])["status"] == "QUEUED"
|
||
|
|
with pytest.raises(FlowError):
|
||
|
|
service.mqtt.accept(payload, retained=True)
|
||
|
|
|
||
|
|
|
||
|
|
def test_mqtt_needs_authentication(service, monkeypatch):
|
||
|
|
monkeypatch.delenv("FRAME_FLOW_MQTT_TOKEN", raising=False)
|
||
|
|
with pytest.raises(FlowError):
|
||
|
|
service.mqtt.accept(b"{}")
|
||
|
|
|
||
|
|
|
||
|
|
def test_mqtt_disabled_by_default(client, service):
|
||
|
|
assert client.get("/api/v1/integrations/mqtt").json()["config"]["enabled"] is False
|
||
|
|
service.mqtt.start()
|
||
|
|
assert service.mqtt.client is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_mqtt_refuses_unapproved_plaintext(client):
|
||
|
|
assert (
|
||
|
|
client.put(
|
||
|
|
"/api/v1/integrations/mqtt",
|
||
|
|
json={"enabled": True, "host": "192.0.2.10", "tls": False},
|
||
|
|
).status_code
|
||
|
|
== 400
|
||
|
|
)
|