| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- """Unit tests for wifi helpers."""
- from unittest.mock import AsyncMock, MagicMock, patch
- import aiounifi
- import pytest
- from homeassistant.exceptions import HomeAssistantError
- from custom_components.room_cal_ha.wifi import async_set_ssid_enabled, async_set_wifi_key
- def _wlan(name: str) -> MagicMock:
- w = MagicMock()
- w.name = name
- return w
- def _hub(available: bool = True, wlans: dict | None = None) -> MagicMock:
- hub = MagicMock()
- hub.available = available
- hub.api.wlans = wlans or {}
- hub.api.request = AsyncMock()
- return hub
- def _hass(*hubs: MagicMock) -> MagicMock:
- hass = MagicMock()
- entries = [MagicMock(runtime_data=hub) for hub in hubs]
- hass.config_entries.async_loaded_entries.return_value = entries
- return hass
- # ---------------------------------------------------------------------------
- # Not found cases
- # ---------------------------------------------------------------------------
- async def test_no_entries_raises():
- with pytest.raises(HomeAssistantError, match="not found"):
- await async_set_wifi_key(_hass(), "GuestNet", "password1")
- async def test_hub_unavailable_raises():
- hub = _hub(available=False, wlans={"w1": _wlan("GuestNet")})
- with pytest.raises(HomeAssistantError, match="not found"):
- await async_set_wifi_key(_hass(hub), "GuestNet", "password1")
- async def test_ssid_not_matched_raises():
- hub = _hub(wlans={"w1": _wlan("OtherNet")})
- with pytest.raises(HomeAssistantError, match="not found"):
- await async_set_wifi_key(_hass(hub), "GuestNet", "password1")
- # ---------------------------------------------------------------------------
- # Happy path
- # ---------------------------------------------------------------------------
- async def test_ssid_found_calls_request():
- hub = _hub(wlans={"w1": _wlan("GuestNet")})
- with patch("custom_components.room_cal_ha.wifi.WlanChangePasswordRequest.create") as mock_create:
- mock_create.return_value = "fake_req"
- await async_set_wifi_key(_hass(hub), "GuestNet", "password123")
- mock_create.assert_called_once_with("w1", "password123")
- hub.api.request.assert_awaited_once_with("fake_req")
- async def test_second_hub_used_when_first_lacks_ssid():
- hub1 = _hub(wlans={"w1": _wlan("OtherNet")})
- hub2 = _hub(wlans={"w2": _wlan("GuestNet")})
- with patch("custom_components.room_cal_ha.wifi.WlanChangePasswordRequest.create") as mock_create:
- mock_create.return_value = "req"
- await async_set_wifi_key(_hass(hub1, hub2), "GuestNet", "password123")
- mock_create.assert_called_once_with("w2", "password123")
- hub1.api.request.assert_not_called()
- hub2.api.request.assert_awaited_once()
- async def test_first_hub_used_when_both_have_ssid():
- hub1 = _hub(wlans={"w1": _wlan("GuestNet")})
- hub2 = _hub(wlans={"w2": _wlan("GuestNet")})
- with patch("custom_components.room_cal_ha.wifi.WlanChangePasswordRequest.create"):
- await async_set_wifi_key(_hass(hub1, hub2), "GuestNet", "password123")
- hub1.api.request.assert_awaited_once()
- hub2.api.request.assert_not_called()
- # ---------------------------------------------------------------------------
- # API error
- # ---------------------------------------------------------------------------
- async def test_api_error_raises_ha_error():
- hub = _hub(wlans={"w1": _wlan("GuestNet")})
- hub.api.request = AsyncMock(side_effect=aiounifi.AiounifiException("bad"))
- with pytest.raises(HomeAssistantError, match="Failed to set WiFi key"):
- await async_set_wifi_key(_hass(hub), "GuestNet", "password123")
- # ---------------------------------------------------------------------------
- # async_set_ssid_enabled
- # ---------------------------------------------------------------------------
- async def test_ssid_enable_calls_request():
- hub = _hub(wlans={"w1": _wlan("GuestNet")})
- with patch("custom_components.room_cal_ha.wifi.WlanEnableRequest.create") as mock_create:
- mock_create.return_value = "enable_req"
- await async_set_ssid_enabled(_hass(hub), "GuestNet", True)
- mock_create.assert_called_once_with("w1", True)
- hub.api.request.assert_awaited_once_with("enable_req")
- async def test_ssid_disable_calls_request():
- hub = _hub(wlans={"w1": _wlan("GuestNet")})
- with patch("custom_components.room_cal_ha.wifi.WlanEnableRequest.create") as mock_create:
- mock_create.return_value = "disable_req"
- await async_set_ssid_enabled(_hass(hub), "GuestNet", False)
- mock_create.assert_called_once_with("w1", False)
- hub.api.request.assert_awaited_once_with("disable_req")
- async def test_ssid_not_found_raises():
- with pytest.raises(HomeAssistantError, match="not found"):
- await async_set_ssid_enabled(_hass(), "GuestNet", True)
- async def test_ssid_enable_api_error_raises():
- hub = _hub(wlans={"w1": _wlan("GuestNet")})
- hub.api.request = AsyncMock(side_effect=aiounifi.AiounifiException("bad"))
- with pytest.raises(HomeAssistantError, match="enable"):
- await async_set_ssid_enabled(_hass(hub), "GuestNet", True)
- async def test_ssid_disable_api_error_raises():
- hub = _hub(wlans={"w1": _wlan("GuestNet")})
- hub.api.request = AsyncMock(side_effect=aiounifi.AiounifiException("bad"))
- with pytest.raises(HomeAssistantError, match="disable"):
- await async_set_ssid_enabled(_hass(hub), "GuestNet", False)
|