|
5 | 5 | import os |
6 | 6 | from unittest.mock import AsyncMock, MagicMock, patch |
7 | 7 |
|
| 8 | +import airos.exceptions |
8 | 9 | import pytest |
9 | 10 |
|
10 | 11 | import aiofiles |
| 12 | +import aiohttp |
11 | 13 |
|
12 | 14 |
|
13 | 15 | async def _read_fixture(fixture: str = "ap-ptp"): |
@@ -56,3 +58,88 @@ async def test_ap(airos_device, base_url, mode): |
56 | 58 |
|
57 | 59 | # Verify the fixture returns the correct mode |
58 | 60 | assert status.get("wireless", {}).get("mode") == mode |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_reconnect(airos_device, base_url): |
| 65 | + """Test reconnect client.""" |
| 66 | + # --- Prepare fake POST /api/stakick response --- |
| 67 | + mock_stakick_response = MagicMock() |
| 68 | + mock_stakick_response.__aenter__.return_value = mock_stakick_response |
| 69 | + mock_stakick_response.status = 200 |
| 70 | + |
| 71 | + with ( |
| 72 | + patch.object(airos_device.session, "post", return_value=mock_stakick_response), |
| 73 | + patch.object(airos_device, "connected", True), |
| 74 | + ): |
| 75 | + assert await airos_device.stakick("01:23:45:67:89:aB") |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_ap_corners(airos_device, base_url, mode="ap-ptp"): |
| 80 | + """Test device operation.""" |
| 81 | + cookie = SimpleCookie() |
| 82 | + cookie["session_id"] = "test-cookie" |
| 83 | + cookie["AIROS_TOKEN"] = "abc123" |
| 84 | + |
| 85 | + # --- Prepare fake POST /api/auth response with cookies --- |
| 86 | + mock_login_response = MagicMock() |
| 87 | + mock_login_response.__aenter__.return_value = mock_login_response |
| 88 | + mock_login_response.text = AsyncMock(return_value="{}") |
| 89 | + mock_login_response.status = 200 |
| 90 | + mock_login_response.cookies = cookie |
| 91 | + mock_login_response.headers = {"X-CSRF-ID": "test-csrf-token"} |
| 92 | + |
| 93 | + with ( |
| 94 | + patch.object(airos_device.session, "post", return_value=mock_login_response), |
| 95 | + patch.object(airos_device, "_use_json_for_login_post", return_value=True), |
| 96 | + ): |
| 97 | + assert await airos_device.login() |
| 98 | + |
| 99 | + mock_login_response.cookies = {} |
| 100 | + with ( |
| 101 | + patch.object(airos_device.session, "post", return_value=mock_login_response), |
| 102 | + ): |
| 103 | + try: |
| 104 | + assert await airos_device.login() |
| 105 | + assert False |
| 106 | + except airos.exceptions.ConnectionSetupError: |
| 107 | + assert True |
| 108 | + |
| 109 | + mock_login_response.cookies = cookie |
| 110 | + mock_login_response.headers = {} |
| 111 | + with ( |
| 112 | + patch.object(airos_device.session, "post", return_value=mock_login_response), |
| 113 | + ): |
| 114 | + result = await airos_device.login() |
| 115 | + assert result is None |
| 116 | + |
| 117 | + mock_login_response.headers = {"X-CSRF-ID": "test-csrf-token"} |
| 118 | + mock_login_response.text = AsyncMock(return_value="abc123") |
| 119 | + with ( |
| 120 | + patch.object(airos_device.session, "post", return_value=mock_login_response), |
| 121 | + ): |
| 122 | + try: |
| 123 | + assert await airos_device.login() |
| 124 | + assert False |
| 125 | + except airos.exceptions.DataMissingError: |
| 126 | + assert True |
| 127 | + |
| 128 | + mock_login_response.text = AsyncMock(return_value="{}") |
| 129 | + mock_login_response.status = 400 |
| 130 | + with ( |
| 131 | + patch.object(airos_device.session, "post", return_value=mock_login_response), |
| 132 | + ): |
| 133 | + try: |
| 134 | + assert await airos_device.login() |
| 135 | + assert False |
| 136 | + except airos.exceptions.ConnectionAuthenticationError: |
| 137 | + assert True |
| 138 | + |
| 139 | + mock_login_response.status = 200 |
| 140 | + with patch.object(airos_device.session, "post", side_effect=aiohttp.ClientError): |
| 141 | + try: |
| 142 | + assert await airos_device.login() |
| 143 | + assert False |
| 144 | + except airos.exceptions.DeviceConnectionError: |
| 145 | + assert True |
0 commit comments