test_wifi.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """Unit tests for wifi helpers."""
  2. from unittest.mock import AsyncMock, MagicMock, patch
  3. import aiounifi
  4. import pytest
  5. from homeassistant.exceptions import HomeAssistantError
  6. from custom_components.room_cal_ha.wifi import async_set_ssid_enabled, async_set_wifi_key
  7. def _wlan(name: str) -> MagicMock:
  8. w = MagicMock()
  9. w.name = name
  10. return w
  11. def _hub(available: bool = True, wlans: dict | None = None) -> MagicMock:
  12. hub = MagicMock()
  13. hub.available = available
  14. hub.api.wlans = wlans or {}
  15. hub.api.request = AsyncMock()
  16. return hub
  17. def _hass(*hubs: MagicMock) -> MagicMock:
  18. hass = MagicMock()
  19. entries = [MagicMock(runtime_data=hub) for hub in hubs]
  20. hass.config_entries.async_loaded_entries.return_value = entries
  21. return hass
  22. # ---------------------------------------------------------------------------
  23. # Not found cases
  24. # ---------------------------------------------------------------------------
  25. async def test_no_entries_raises():
  26. with pytest.raises(HomeAssistantError, match="not found"):
  27. await async_set_wifi_key(_hass(), "GuestNet", "password1")
  28. async def test_hub_unavailable_raises():
  29. hub = _hub(available=False, wlans={"w1": _wlan("GuestNet")})
  30. with pytest.raises(HomeAssistantError, match="not found"):
  31. await async_set_wifi_key(_hass(hub), "GuestNet", "password1")
  32. async def test_ssid_not_matched_raises():
  33. hub = _hub(wlans={"w1": _wlan("OtherNet")})
  34. with pytest.raises(HomeAssistantError, match="not found"):
  35. await async_set_wifi_key(_hass(hub), "GuestNet", "password1")
  36. # ---------------------------------------------------------------------------
  37. # Happy path
  38. # ---------------------------------------------------------------------------
  39. async def test_ssid_found_calls_request():
  40. hub = _hub(wlans={"w1": _wlan("GuestNet")})
  41. with patch("custom_components.room_cal_ha.wifi.WlanChangePasswordRequest.create") as mock_create:
  42. mock_create.return_value = "fake_req"
  43. await async_set_wifi_key(_hass(hub), "GuestNet", "password123")
  44. mock_create.assert_called_once_with("w1", "password123")
  45. hub.api.request.assert_awaited_once_with("fake_req")
  46. async def test_second_hub_used_when_first_lacks_ssid():
  47. hub1 = _hub(wlans={"w1": _wlan("OtherNet")})
  48. hub2 = _hub(wlans={"w2": _wlan("GuestNet")})
  49. with patch("custom_components.room_cal_ha.wifi.WlanChangePasswordRequest.create") as mock_create:
  50. mock_create.return_value = "req"
  51. await async_set_wifi_key(_hass(hub1, hub2), "GuestNet", "password123")
  52. mock_create.assert_called_once_with("w2", "password123")
  53. hub1.api.request.assert_not_called()
  54. hub2.api.request.assert_awaited_once()
  55. async def test_first_hub_used_when_both_have_ssid():
  56. hub1 = _hub(wlans={"w1": _wlan("GuestNet")})
  57. hub2 = _hub(wlans={"w2": _wlan("GuestNet")})
  58. with patch("custom_components.room_cal_ha.wifi.WlanChangePasswordRequest.create"):
  59. await async_set_wifi_key(_hass(hub1, hub2), "GuestNet", "password123")
  60. hub1.api.request.assert_awaited_once()
  61. hub2.api.request.assert_not_called()
  62. # ---------------------------------------------------------------------------
  63. # API error
  64. # ---------------------------------------------------------------------------
  65. async def test_api_error_raises_ha_error():
  66. hub = _hub(wlans={"w1": _wlan("GuestNet")})
  67. hub.api.request = AsyncMock(side_effect=aiounifi.AiounifiException("bad"))
  68. with pytest.raises(HomeAssistantError, match="Failed to set WiFi key"):
  69. await async_set_wifi_key(_hass(hub), "GuestNet", "password123")
  70. # ---------------------------------------------------------------------------
  71. # async_set_ssid_enabled
  72. # ---------------------------------------------------------------------------
  73. async def test_ssid_enable_calls_request():
  74. hub = _hub(wlans={"w1": _wlan("GuestNet")})
  75. with patch("custom_components.room_cal_ha.wifi.WlanEnableRequest.create") as mock_create:
  76. mock_create.return_value = "enable_req"
  77. await async_set_ssid_enabled(_hass(hub), "GuestNet", True)
  78. mock_create.assert_called_once_with("w1", True)
  79. hub.api.request.assert_awaited_once_with("enable_req")
  80. async def test_ssid_disable_calls_request():
  81. hub = _hub(wlans={"w1": _wlan("GuestNet")})
  82. with patch("custom_components.room_cal_ha.wifi.WlanEnableRequest.create") as mock_create:
  83. mock_create.return_value = "disable_req"
  84. await async_set_ssid_enabled(_hass(hub), "GuestNet", False)
  85. mock_create.assert_called_once_with("w1", False)
  86. hub.api.request.assert_awaited_once_with("disable_req")
  87. async def test_ssid_not_found_raises():
  88. with pytest.raises(HomeAssistantError, match="not found"):
  89. await async_set_ssid_enabled(_hass(), "GuestNet", True)
  90. async def test_ssid_enable_api_error_raises():
  91. hub = _hub(wlans={"w1": _wlan("GuestNet")})
  92. hub.api.request = AsyncMock(side_effect=aiounifi.AiounifiException("bad"))
  93. with pytest.raises(HomeAssistantError, match="enable"):
  94. await async_set_ssid_enabled(_hass(hub), "GuestNet", True)
  95. async def test_ssid_disable_api_error_raises():
  96. hub = _hub(wlans={"w1": _wlan("GuestNet")})
  97. hub.api.request = AsyncMock(side_effect=aiounifi.AiounifiException("bad"))
  98. with pytest.raises(HomeAssistantError, match="disable"):
  99. await async_set_ssid_enabled(_hass(hub), "GuestNet", False)