"""The devices a UniFi controller manages, from either API, in the contract's words.""" from __future__ import annotations from typing import Any import pytest from conftest import load from napalm_ubiquiti.parsers.managed import ( managed_from_classic, managed_from_integration, managed_from_pending, ) class TestIntegration: def test_an_adopted_switch(self) -> None: device = load("integration/v10.6.106-devices.json")["data"][0] assert managed_from_integration(device, "default") == { "mac": "fc:ec:da:11:22:33", "name": "Switch 16", "model": "US16P150", "role": "switch", "state": "online", "site": "default", "ip": "192.168.0.57", "id": "5b5e2c17-0d3c-3c47-a0f1-6c1d2f1e0001", "firmware": "4.0.66.10832", } def test_an_ap_and_a_gateway_that_also_switches(self) -> None: data = load("integration/v10.6.106-devices.json")["data"] assert managed_from_integration(data[1], "default")["role"] == "access_point" assert managed_from_integration(data[3], "default")["role"] == "gateway" @pytest.mark.parametrize( ("state", "word"), [ ("ONLINE", "online"), ("OFFLINE", "offline"), ("CONNECTION_INTERRUPTED", "offline"), ("ISOLATED", "offline"), ("PENDING_ADOPTION", "pending"), ("UPDATING", "busy"), ("GETTING_READY", "busy"), ("ADOPTING", "busy"), ("DELETING", "busy"), ("U5G_INCORRECT_TOPOLOGY", "offline"), ("SOMETHING_NEW", "offline"), ], ) def test_states(self, state: str, word: str) -> None: device = { "macAddress": "f4:92:bf:00:00:01", "model": "U6LR", "state": state, "features": [], } assert managed_from_integration(device, "default")["state"] == word def test_a_device_without_features_or_address(self) -> None: device = { "macAddress": "F4:92:BF:00:00:01", "model": "UP1", "state": "ONLINE", "features": [], "name": "", } managed = managed_from_integration(device, "default") assert managed["role"] == "other" assert managed["ip"] is None assert managed["mac"] == "f4:92:bf:00:00:01" assert "firmware" not in managed and "id" not in managed def test_a_pending_device(self) -> None: device = load("integration/v10.6.106-pending-devices.json")["data"][0] assert managed_from_pending(device, "default") == { "mac": "f4:92:bf:00:00:aa", "name": "", "model": "U6LR", "role": "access_point", "state": "pending", "site": "default", "ip": "192.168.0.77", "firmware": "6.6.55", } class TestClassic: def test_a_switch(self, us16: dict[str, Any]) -> None: managed = managed_from_classic(us16, "default") assert managed == { "mac": "fc:ec:da:11:22:33", "name": "Switch 16", "model": "US16P150", "role": "switch", "state": "online", "site": "default", "ip": "192.168.0.57", "id": us16["_id"], "serial": "FCECDA112233", "firmware": "4.0.66.10832", "firmware_available": "4.0.80.10875", } def test_no_offer_when_it_is_not_upgradable(self, us16: dict[str, Any]) -> None: assert "firmware_available" not in managed_from_classic( dict(us16, upgradable=False), "default" ) def test_the_offered_firmware_and_the_product_name(self, us16: dict[str, Any]) -> None: record = dict( us16, upgradable=True, upgrade_to_firmware="7.1.26.15869", model_display="USW 16 PoE" ) managed = managed_from_classic(record, "default") assert managed["firmware_available"] == "7.1.26.15869" assert managed["model_name"] == "USW 16 PoE" @pytest.mark.parametrize( ("kind", "role"), [ ("usw", "switch"), ("uap", "access_point"), ("ugw", "gateway"), ("udm", "gateway"), ("uxg", "gateway"), ("uph", "other"), ], ) def test_roles(self, kind: str, role: str) -> None: assert managed_from_classic({"mac": "x", "type": kind, "state": 1}, "s")["role"] == role @pytest.mark.parametrize( ("state", "word"), [ (1, "online"), (0, "offline"), (6, "offline"), (11, "offline"), (2, "pending"), (4, "busy"), (5, "busy"), (7, "busy"), (99, "offline"), ], ) def test_states(self, state: int, word: str) -> None: assert ( managed_from_classic({"mac": "x", "type": "usw", "state": state}, "s")["state"] == word )