File: test_init.py

package info (click to toggle)
python-unifi-discovery 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 364 kB
  • sloc: python: 721; makefile: 14; javascript: 8
file content (325 lines) | stat: -rw-r--r-- 11,466 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import asyncio
import contextlib
from unittest.mock import MagicMock, patch

import pytest
from aiohttp import ClientError, ClientSession, ContentTypeError, TCPConnector
from aioresponses import aioresponses

from unifi_discovery import (
    DISCOVERY_PORT,
    UBNT_REQUEST_PAYLOAD,
    AIOUnifiScanner,
    UnifiDevice,
    UnifiDiscovery,
    UnifiService,
    async_console_is_alive,
    create_udp_socket,
)


@pytest.fixture
def mock_aioresponse():
    with aioresponses() as m:
        yield m


@pytest.fixture
async def mock_discovery_aio_protocol():
    """Fixture to mock an asyncio connection."""
    loop = asyncio.get_running_loop()
    future = asyncio.Future()

    async def _wait_for_connection():
        transport, protocol = await future
        await asyncio.sleep(0)
        await asyncio.sleep(0)
        return transport, protocol

    async def _mock_create_datagram_endpoint(func, sock=None):
        protocol: UnifiDiscovery = func()
        transport = MagicMock()
        protocol.connection_made(transport)
        with contextlib.suppress(asyncio.InvalidStateError):
            future.set_result((transport, protocol))
        return transport, protocol

    with patch.object(loop, "create_datagram_endpoint", _mock_create_datagram_endpoint):
        yield _wait_for_connection


@pytest.mark.asyncio
async def test_async_scanner_specific_address(
    mock_discovery_aio_protocol, mock_aioresponse
):
    """Test scanner with a specific address."""
    scanner = AIOUnifiScanner()
    task = asyncio.ensure_future(
        scanner.async_scan(timeout=10, address="192.168.212.1")
    )
    _, protocol = await mock_discovery_aio_protocol()
    protocol.datagram_received(
        b"\x01\x00\x00\x8e\x02\x00\n\xe0c\xda\x00^\x08\xc0\xa8\xd4\x01\x01\x00\x06\xe0c\xda\x00^\x08\n\x00\x04\x00\x13\xe60\x0b\x00\x04Gate\x0c\x00\nUVC G4 Pro\x17\x00\x04\x00\x00\x00\x00\x03\x00'UVC.S5L.v4.46.18.67.ceacbaa.211202.1017\x10\x00\x02c\xa5 \x00$32f695ba-835b-5822-bc54-e290e1789ff1",
        ("192.168.212.1", DISCOVERY_PORT),
    )
    await task
    assert scanner.found_devices == [
        UnifiDevice(
            source_ip="192.168.212.1",
            hw_addr="e0:63:da:00:5e:08",
            ip_info=["e0:63:da:00:5e:08;192.168.212.1"],
            addr_entry=None,
            fw_version="UVC.S5L.v4.46.18.67.ceacbaa.211202.1017",
            mac_address=None,
            uptime=1304112,
            hostname="Gate",
            platform="UVC G4 Pro",
            model=None,
            signature_version="1",
        )
    ]


@pytest.mark.asyncio
async def test_async_scanner_broadcast(mock_discovery_aio_protocol, mock_aioresponse):
    """Test scanner with a broadcast."""
    scanner = AIOUnifiScanner()
    mock_aioresponse.get("https://192.168.203.1/proxy/protect/api", status=401)
    mock_aioresponse.get(
        "https://192.168.203.1/api/system",
        payload={
            "hardware": {"shortname": "UDMPROSE"},
            "name": "UDM Pro SE",
            "mac": "245A4CDD6616",
            "isSingleUser": True,
            "isSsoEnabled": True,
            "directConnectDomain": "xyz.id.ui.direct",
        },
    )

    task = asyncio.ensure_future(scanner.async_scan(timeout=0.01))
    _, protocol = await mock_discovery_aio_protocol()
    protocol.datagram_received(
        UBNT_REQUEST_PAYLOAD,
        ("192.168.203.1", DISCOVERY_PORT),
    )
    protocol.datagram_received(
        b"",
        ("127.0.0.1", DISCOVERY_PORT),
    )
    protocol.datagram_received(
        None,
        ("127.0.0.1", DISCOVERY_PORT),
    )
    protocol.datagram_received(
        b"\x01\x00\x00\xa5\x01\x00\x06$ZLu\xba\xe6\x02\x00\n$ZLu\xba\xe6\xc0\xa8\xd5/\x03\x001UFP-UAP-B.MT7622_SOC.v0.4.0.4.340d302.220106.0349\x04\x00\x04\xc0\xa8\xd5/\x05\x00\x06$ZLu\xba\xe6\n\x00\x04\x00\x0c\xda/\x0b\x00\x11AlexanderTechRoom\x0c\x00\tUFP-UAP-B\x10\x00\x02\xa6 \x14\x00\x18Unifi-Protect-UAP-Bridge\x17\x00\x01\x00",
        ("192.168.213.252", DISCOVERY_PORT),
    )
    await task
    assert scanner.found_devices == [
        UnifiDevice(
            source_ip="192.168.203.1",
            hw_addr="24:5a:4c:dd:66:16",
            ip_info=None,
            addr_entry=None,
            fw_version=None,
            mac_address=None,
            uptime=None,
            hostname="UDM-Pro-SE",
            platform="UDMPROSE",
            model=None,
            signature_version="1",
            services={UnifiService.Protect: True},
            direct_connect_domain="xyz.id.ui.direct",
            is_sso_enabled=True,
            is_single_user=True,
        ),
        UnifiDevice(
            source_ip="192.168.213.252",
            hw_addr="24:5a:4c:75:ba:e6",
            ip_info=["24:5a:4c:75:ba:e6;192.168.213.47"],
            addr_entry="192.168.213.47",
            fw_version="UFP-UAP-B.MT7622_SOC.v0.4.0.4.340d302.220106.0349",
            mac_address="24:5a:4c:75:ba:e6",
            uptime=842287,
            hostname="AlexanderTechRoom",
            platform="UFP-UAP-B",
            model="Unifi-Protect-UAP-Bridge",
            signature_version="1",
            services={UnifiService.Protect: False},
            direct_connect_domain=None,
            is_sso_enabled=None,
            is_single_user=None,
        ),
    ]


@pytest.mark.asyncio
async def test_async_scanner_no_system_response(
    mock_discovery_aio_protocol, mock_aioresponse
):
    """Test scanner with a broadcast when the system api does not response."""
    scanner = AIOUnifiScanner()
    mock_aioresponse.get("https://192.168.203.1/proxy/protect/api", status=401)
    mock_aioresponse.get("https://192.168.203.1/api/system", status=404)

    task = asyncio.ensure_future(scanner.async_scan(timeout=0.01))
    _, protocol = await mock_discovery_aio_protocol()
    protocol.datagram_received(
        UBNT_REQUEST_PAYLOAD,
        ("192.168.203.1", DISCOVERY_PORT),
    )
    protocol.datagram_received(
        b"",
        ("127.0.0.1", DISCOVERY_PORT),
    )
    protocol.datagram_received(
        None,
        ("127.0.0.1", DISCOVERY_PORT),
    )
    protocol.datagram_received(
        b"\x01\x00\x00\xa5\x01\x00\x06$ZLu\xba\xe6\x02\x00\n$ZLu\xba\xe6\xc0\xa8\xd5/\x03\x001UFP-UAP-B.MT7622_SOC.v0.4.0.4.340d302.220106.0349\x04\x00\x04\xc0\xa8\xd5/\x05\x00\x06$ZLu\xba\xe6\n\x00\x04\x00\x0c\xda/\x0b\x00\x11AlexanderTechRoom\x0c\x00\tUFP-UAP-B\x10\x00\x02\xa6 \x14\x00\x18Unifi-Protect-UAP-Bridge\x17\x00\x01\x00",
        ("192.168.213.252", DISCOVERY_PORT),
    )
    await task
    assert scanner.found_devices == [
        UnifiDevice(
            source_ip="192.168.203.1",
            hw_addr=None,
            ip_info=None,
            addr_entry=None,
            fw_version=None,
            mac_address=None,
            uptime=None,
            hostname=None,
            platform=None,
            model=None,
            signature_version="1",
            services={UnifiService.Protect: True},
            direct_connect_domain=None,
            is_sso_enabled=None,
            is_single_user=None,
        ),
        UnifiDevice(
            source_ip="192.168.213.252",
            hw_addr="24:5a:4c:75:ba:e6",
            ip_info=["24:5a:4c:75:ba:e6;192.168.213.47"],
            addr_entry="192.168.213.47",
            fw_version="UFP-UAP-B.MT7622_SOC.v0.4.0.4.340d302.220106.0349",
            mac_address="24:5a:4c:75:ba:e6",
            uptime=842287,
            hostname="AlexanderTechRoom",
            platform="UFP-UAP-B",
            model="Unifi-Protect-UAP-Bridge",
            signature_version="1",
            services={UnifiService.Protect: False},
            direct_connect_domain=None,
            is_sso_enabled=None,
            is_single_user=None,
        ),
    ]


@pytest.mark.asyncio
async def test_async_scanner_system_api_missing_mac(
    mock_discovery_aio_protocol, mock_aioresponse
):
    """Test scanner with a broadcast when the system api responds but no mac."""
    scanner = AIOUnifiScanner()
    mock_aioresponse.get("https://192.168.203.1/proxy/protect/api", status=401)
    mock_aioresponse.get(
        "https://192.168.203.1/api/system",
        payload={
            "hardware": {"shortname": "UCKP"},
            "name": "UniFi-CloudKey-Gen2-Plus",
        },
    )
    task = asyncio.ensure_future(scanner.async_scan(timeout=0.01))
    _, protocol = await mock_discovery_aio_protocol()
    protocol.datagram_received(
        UBNT_REQUEST_PAYLOAD,
        ("192.168.203.1", DISCOVERY_PORT),
    )
    await task
    assert scanner.found_devices == [
        UnifiDevice(
            source_ip="192.168.203.1",
            hw_addr=None,
            ip_info=None,
            addr_entry=None,
            fw_version=None,
            mac_address=None,
            uptime=None,
            hostname="UniFi-CloudKey-Gen2-Plus",
            platform="UCKP",
            model=None,
            signature_version="1",
            services={UnifiService.Protect: True},
            direct_connect_domain=None,
            is_sso_enabled=None,
            is_single_user=None,
        )
    ]


@pytest.mark.asyncio
async def test_async_scanner_system_api_returns_html(
    mock_discovery_aio_protocol, mock_aioresponse, caplog
):
    """Test scanner with a broadcast when the system api responds but no mac."""
    scanner = AIOUnifiScanner()
    mock_aioresponse.get("https://192.168.203.1/proxy/protect/api", status=401)
    mock_aioresponse.get(
        "https://192.168.203.1/api/system",
        exception=ContentTypeError,
    )
    task = asyncio.ensure_future(scanner.async_scan(timeout=0.01))
    _, protocol = await mock_discovery_aio_protocol()
    protocol.datagram_received(
        UBNT_REQUEST_PAYLOAD,
        ("192.168.203.1", DISCOVERY_PORT),
    )
    await task
    assert scanner.found_devices == [
        UnifiDevice(
            source_ip="192.168.203.1",
            hw_addr=None,
            ip_info=None,
            addr_entry=None,
            fw_version=None,
            mac_address=None,
            uptime=None,
            hostname=None,
            platform=None,
            model=None,
            signature_version="1",
            services={UnifiService.Protect: True},
            direct_connect_domain=None,
            is_sso_enabled=None,
            is_single_user=None,
        )
    ]


@pytest.mark.asyncio
async def test_async_scanner_falls_back_to_any_source_port_if_socket_in_use():
    """Test port fallback."""
    hold_socket = create_udp_socket(DISCOVERY_PORT)
    assert hold_socket.getsockname() == ("0.0.0.0", DISCOVERY_PORT)
    random_socket = create_udp_socket(DISCOVERY_PORT)
    assert random_socket.getsockname() is not None


@pytest.mark.asyncio
async def test_async_console_is_alive(mock_aioresponse):
    """Test if a console is alive."""
    mock_aioresponse.get("https://1.2.3.1/api/system", status=401)
    mock_aioresponse.get("https://1.2.3.2/api/system", status=200)
    mock_aioresponse.get("https://1.2.3.3/api/system", exception=ClientError)
    mock_aioresponse.get("https://1.2.3.4/api/system", exception=asyncio.TimeoutError)

    async with ClientSession(connector=TCPConnector(ssl=False)) as session:
        assert await async_console_is_alive(session, "1.2.3.1") is True
        assert await async_console_is_alive(session, "1.2.3.2") is True
        assert await async_console_is_alive(session, "1.2.3.3") is False
        assert await async_console_is_alive(session, "1.2.3.4") is False