Skip to content

Commit 58fe33f

Browse files
committed
Add tests for new endpoints + fixtures
1 parent 642e5ca commit 58fe33f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"checksum": "b1bea879a9f518f714ce638172e3a860", "version": "v8.7.19", "security": "", "date": "250811", "url": "https://dl.ubnt.com/firmwares/XC-fw/v8.7.19/WA.v8.7.19.48279.250811.0636.bin", "update": true, "changelog": "https://dl.ubnt.com/firmwares/XC-fw/v8.7.19/changelog.txt"}

fixtures/warnings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"isDefaultPasswd": false, "customScripts": false, "isWatchdogReset": 0, "label": 0, "chAvailable": false, "emergReasonCode": -1, "firmware": {"requirePasswd": false, "isThirdParty": false, "version": "", "uploaded": false}}

tests/test_airos8.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,101 @@ async def test_status_missing_required_key_in_json(airos_device: AirOS) -> None:
259259
# --- MODIFICATION START ---
260260
# Assert that the cause of our exception is the correct type from mashumaro
261261
assert isinstance(excinfo.value.__cause__, MissingField)
262+
263+
# --- Tests for warnings() and update_check() ---
264+
@pytest.mark.asyncio
265+
async def test_warnings_correctly_parses_json() -> None:
266+
"""Test that the warnings() method correctly parses a valid JSON response."""
267+
mock_session = MagicMock()
268+
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
269+
airos_device.connected = True
270+
271+
mock_response = MagicMock()
272+
mock_response.__aenter__.return_value = mock_response
273+
mock_response.status = 200
274+
with open("fixtures/warnings.json") as f:
275+
mock_response_data = json.load(f)
276+
mock_response.text = AsyncMock(return_value=json.dumps(mock_response_data))
277+
278+
with patch.object(airos_device.session, "get", return_value=mock_response):
279+
result = await airos_device.warnings()
280+
assert result["isDefaultPasswd"] is False
281+
assert result["chAvailable"] is False
282+
283+
284+
@pytest.mark.asyncio
285+
async def test_warnings_raises_exception_on_invalid_json() -> None:
286+
"""Test that warnings() raises an exception on invalid JSON response."""
287+
mock_session = MagicMock()
288+
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
289+
airos_device.connected = True
290+
291+
mock_response = MagicMock()
292+
mock_response.__aenter__.return_value = mock_response
293+
mock_response.status = 200
294+
mock_response.text = AsyncMock(return_value="This is not JSON")
295+
296+
with (patch.object(airos_device.session, "get", return_value=mock_response),
297+
pytest.raises(airos.exceptions.AirOSDataMissingError)):
298+
await airos_device.warnings()
299+
300+
301+
@pytest.mark.asyncio
302+
async def test_update_check_correctly_parses_json() -> None:
303+
"""Test that update_check() method correctly parses a valid JSON response."""
304+
mock_session = MagicMock()
305+
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
306+
airos_device.connected = True
307+
airos_device.current_csrf_token = "mock-csrf-token"
308+
309+
mock_response = MagicMock()
310+
mock_response.__aenter__.return_value = mock_response
311+
mock_response.status = 200
312+
with open("fixtures/update_check_available.json") as f:
313+
mock_response_data = json.load(f)
314+
mock_response.text = AsyncMock(return_value=json.dumps(mock_response_data))
315+
316+
with patch.object(airos_device.session, "post", return_value=mock_response):
317+
result = await airos_device.update_check()
318+
assert result["version"] == "v8.7.19"
319+
assert result["update"] is True
320+
321+
322+
@pytest.mark.asyncio
323+
async def test_update_check_raises_exception_on_invalid_json() -> None:
324+
"""Test that update_check() raises an exception on invalid JSON response."""
325+
mock_session = MagicMock()
326+
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
327+
airos_device.connected = True
328+
airos_device.current_csrf_token = "mock-csrf-token"
329+
330+
mock_response = MagicMock()
331+
mock_response.__aenter__.return_value = mock_response
332+
mock_response.status = 200
333+
mock_response.text = AsyncMock(return_value="This is not JSON")
334+
335+
with (patch.object(airos_device.session, "post", return_value=mock_response),
336+
pytest.raises(airos.exceptions.AirOSDataMissingError)):
337+
await airos_device.update_check()
338+
339+
340+
@pytest.mark.asyncio
341+
async def test_warnings_when_not_connected() -> None:
342+
"""Test calling warnings() before a successful login."""
343+
mock_session = MagicMock()
344+
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
345+
airos_device.connected = False # Explicitly set connected state to false
346+
347+
with pytest.raises(airos.exceptions.AirOSDeviceConnectionError):
348+
await airos_device.warnings()
349+
350+
351+
@pytest.mark.asyncio
352+
async def test_update_check_when_not_connected() -> None:
353+
"""Test calling update_check() before a successful login."""
354+
mock_session = MagicMock()
355+
airos_device = AirOS(host="http://192.168.1.3", username="test", password="test", session=mock_session)
356+
airos_device.connected = False # Explicitly set connected state to false
357+
358+
with pytest.raises(airos.exceptions.AirOSDeviceConnectionError):
359+
await airos_device.update_check()

0 commit comments

Comments
 (0)