@@ -146,6 +146,73 @@ async def test_safe_dict_post_and_list_post(monkeypatch, make_client) -> None:
146146 assert result_empty_list == []
147147
148148
149+ @pytest .mark .asyncio
150+ async def test_get_check (make_client ) -> None :
151+ """Test _get_check method returns True for ok responses, False otherwise."""
152+ session = MagicMock (spec = aiohttp .ClientSession )
153+
154+ # Fake response class for testing
155+ class FakeResp :
156+ def __init__ (self , status = 500 , ok = False ):
157+ self .status = status
158+ self .reason = "Test"
159+ self .ok = ok
160+ self .request_info = MagicMock ()
161+ self .history = []
162+ self .headers = {}
163+
164+ async def __aenter__ (self ):
165+ return self
166+
167+ async def __aexit__ (self , exc_type , exc , tb ):
168+ return False
169+
170+ # Test successful response (ok=True)
171+ session .get = lambda * a , ** k : FakeResp (status = 200 , ok = True )
172+ client = make_client (session = session )
173+ result = await client ._get_check ("/api/test" )
174+ assert result is True
175+
176+ # Test failed response (ok=False)
177+ session .get = lambda * a , ** k : FakeResp (status = 404 , ok = False )
178+ client = make_client (session = session )
179+ result = await client ._get_check ("/api/test" )
180+ assert result is False
181+
182+ # Test 403 response specifically
183+ session .get = lambda * a , ** k : FakeResp (status = 403 , ok = False )
184+ client = make_client (session = session )
185+ result = await client ._get_check ("/api/test" )
186+ assert result is False
187+
188+
189+ @pytest .mark .asyncio
190+ @pytest .mark .parametrize ("initial,should_raise" , [(False , False ), (True , True )])
191+ async def test_get_check_handles_client_error (make_client , initial , should_raise ) -> None :
192+ """Ensure _get_check handles aiohttp.ClientError correctly.
193+
194+ When client is not in initialization mode, the method should swallow the
195+ ClientError and return False. When the client is in initialization mode
196+ (used during setup), it should re-raise the exception.
197+ """
198+ session = MagicMock (spec = aiohttp .ClientSession )
199+
200+ def _raise (* a , ** k ):
201+ raise aiohttp .ClientError ("boom" )
202+
203+ session .get = _raise
204+ client = make_client (session = session )
205+ # simulate the initialization flag behavior
206+ client ._initial = initial
207+
208+ if should_raise :
209+ with pytest .raises (aiohttp .ClientError ):
210+ await client ._get_check ("/api/test" )
211+ else :
212+ result = await client ._get_check ("/api/test" )
213+ assert result is False
214+
215+
149216@pytest .mark .asyncio
150217async def test_get_ip_key_sorting (make_client ) -> None :
151218 """Sort IP-like items using get_ip_key ordering."""
@@ -358,6 +425,26 @@ async def test_dhcp_leases_and_keep_latest_and_dnsmasq(make_client) -> None:
358425 await client .async_close ()
359426
360427
428+ @pytest .mark .asyncio
429+ async def test_isc_dhcp_service_not_running (make_client ) -> None :
430+ """Test ISC DHCP lease methods return empty list when service is not running."""
431+ session = MagicMock (spec = aiohttp .ClientSession )
432+ client = make_client (session = session )
433+ try :
434+ # Mock _get_check to return False (service not running)
435+ client ._get_check = AsyncMock (return_value = False )
436+
437+ # Test DHCPv4
438+ leases_v4 = await client ._get_isc_dhcpv4_leases ()
439+ assert leases_v4 == []
440+
441+ # Test DHCPv6
442+ leases_v6 = await client ._get_isc_dhcpv6_leases ()
443+ assert leases_v6 == []
444+ finally :
445+ await client .async_close ()
446+
447+
361448@pytest .mark .asyncio
362449async def test_carp_and_reboot_and_wol (make_client ) -> None :
363450 """Verify CARP interface discovery and system control endpoints (reboot/halt/WOL)."""
0 commit comments