|
| 1 | +import pytest |
| 2 | +from aioresponses import aioresponses, CallbackResult |
| 3 | + |
| 4 | +from fmd_api.client import FmdClient |
| 5 | +from fmd_api.device import Device |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.asyncio |
| 9 | +async def test_device_lock_without_message_sends_plain_lock(): |
| 10 | + client = FmdClient("https://fmd.example.com") |
| 11 | + client.access_token = "token" |
| 12 | + |
| 13 | + class DummySigner: |
| 14 | + def sign(self, message_bytes, pad, algo): |
| 15 | + return b"\xab" * 64 |
| 16 | + |
| 17 | + client.private_key = DummySigner() |
| 18 | + |
| 19 | + await client._ensure_session() |
| 20 | + device = Device(client, "test-device") |
| 21 | + |
| 22 | + with aioresponses() as m: |
| 23 | + # capture payload via callback |
| 24 | + captured = {} |
| 25 | + |
| 26 | + def cb(url, **kwargs): |
| 27 | + captured["json"] = kwargs.get("json") |
| 28 | + return CallbackResult(status=200, body="OK") |
| 29 | + |
| 30 | + m.post("https://fmd.example.com/api/v1/command", callback=cb) |
| 31 | + try: |
| 32 | + ok = await device.lock() |
| 33 | + assert ok is True |
| 34 | + assert captured["json"]["Data"] == "lock" |
| 35 | + finally: |
| 36 | + await client.close() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_device_lock_with_message_sanitizes_and_sends(): |
| 41 | + client = FmdClient("https://fmd.example.com") |
| 42 | + client.access_token = "token" |
| 43 | + |
| 44 | + class DummySigner: |
| 45 | + def sign(self, message_bytes, pad, algo): |
| 46 | + return b"\xab" * 64 |
| 47 | + |
| 48 | + client.private_key = DummySigner() |
| 49 | + |
| 50 | + await client._ensure_session() |
| 51 | + device = Device(client, "test-device") |
| 52 | + |
| 53 | + with aioresponses() as m: |
| 54 | + captured = {} |
| 55 | + |
| 56 | + def cb(url, **kwargs): |
| 57 | + captured["json"] = kwargs.get("json") |
| 58 | + return CallbackResult(status=200, body="OK") |
| 59 | + |
| 60 | + m.post("https://fmd.example.com/api/v1/command", callback=cb) |
| 61 | + try: |
| 62 | + ok = await device.lock(" Hello world; \n stay 'safe' \"pls\" ") |
| 63 | + assert ok is True |
| 64 | + sent = captured["json"]["Data"] |
| 65 | + assert sent.startswith("lock ") |
| 66 | + # Ensure removed quotes/semicolons/newlines and collapsed spaces |
| 67 | + assert '"' not in sent and "'" not in sent and ";" not in sent and "\n" not in sent |
| 68 | + assert " " not in sent |
| 69 | + assert sent.endswith("Hello world stay safe pls") |
| 70 | + finally: |
| 71 | + await client.close() |
0 commit comments