|
2 | 2 |
|
3 | 3 | # pylint: disable=protected-access |
4 | 4 |
|
5 | | -import pytest |
| 5 | +from unittest.mock import AsyncMock, MagicMock |
6 | 6 |
|
| 7 | +from aiohttp import ClientSession |
| 8 | +import pytest |
7 | 9 | from sagemcom_api.client import SagemcomClient |
8 | | -from sagemcom_api.enums import EncryptionMethod |
| 10 | +from sagemcom_api.enums import ApiMode, EncryptionMethod |
9 | 11 | from sagemcom_api.exceptions import AuthenticationException |
10 | 12 |
|
11 | 13 |
|
@@ -131,3 +133,104 @@ async def test_login_with_preconfigured_fixture(mock_client_sha512): |
131 | 133 | assert client.authentication_method == EncryptionMethod.SHA512 |
132 | 134 | assert client._session_id == 12345 |
133 | 135 | assert client._server_nonce == "abcdef1234567890" |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.asyncio |
| 139 | +async def test_login_auto_fallbacks_to_rest_when_legacy_503(): |
| 140 | + """Auto mode should switch to REST when legacy endpoint is unavailable.""" |
| 141 | + mock_session = MagicMock(spec=ClientSession) |
| 142 | + mock_session.close = AsyncMock() |
| 143 | + |
| 144 | + legacy_response = AsyncMock() |
| 145 | + legacy_response.status = 503 |
| 146 | + legacy_response.text = AsyncMock(return_value="<html>503 Service Unavailable</html>") |
| 147 | + legacy_response.__aenter__ = AsyncMock(return_value=legacy_response) |
| 148 | + legacy_response.__aexit__ = AsyncMock(return_value=None) |
| 149 | + mock_session.post.return_value = legacy_response |
| 150 | + |
| 151 | + rest_response = AsyncMock() |
| 152 | + rest_response.status = 204 |
| 153 | + rest_response.text = AsyncMock(return_value="") |
| 154 | + rest_response.__aenter__ = AsyncMock(return_value=rest_response) |
| 155 | + rest_response.__aexit__ = AsyncMock(return_value=None) |
| 156 | + mock_session.request.return_value = rest_response |
| 157 | + |
| 158 | + client = SagemcomClient( |
| 159 | + host="192.168.1.1", |
| 160 | + username="admin", |
| 161 | + password="admin", |
| 162 | + authentication_method=EncryptionMethod.MD5, |
| 163 | + session=mock_session, |
| 164 | + api_mode=ApiMode.AUTO, |
| 165 | + ) |
| 166 | + |
| 167 | + result = await client.login() |
| 168 | + |
| 169 | + assert result is True |
| 170 | + assert client.active_api_mode == ApiMode.REST |
| 171 | + assert mock_session.post.call_count == 1 |
| 172 | + assert mock_session.request.call_count == 1 |
| 173 | + |
| 174 | + |
| 175 | +@pytest.mark.asyncio |
| 176 | +async def test_get_hosts_rest_mode(): |
| 177 | + """get_hosts should parse wifi and ethernet devices on REST firmware.""" |
| 178 | + mock_session = MagicMock(spec=ClientSession) |
| 179 | + mock_session.close = AsyncMock() |
| 180 | + |
| 181 | + login_response = AsyncMock() |
| 182 | + login_response.status = 204 |
| 183 | + login_response.text = AsyncMock(return_value="") |
| 184 | + login_response.__aenter__ = AsyncMock(return_value=login_response) |
| 185 | + login_response.__aexit__ = AsyncMock(return_value=None) |
| 186 | + |
| 187 | + home_payload = [ |
| 188 | + { |
| 189 | + "wirelessListDevice": [ |
| 190 | + { |
| 191 | + "id": 1, |
| 192 | + "hostname": "wifi-device", |
| 193 | + "friendlyname": "wifi-device", |
| 194 | + "macAddress": "aa:bb:cc:dd:ee:ff", |
| 195 | + "ipAddress": "192.168.1.2", |
| 196 | + "active": True, |
| 197 | + "devicetype": "MISCELLANEOUS", |
| 198 | + } |
| 199 | + ], |
| 200 | + "ethernetListDevice": [ |
| 201 | + { |
| 202 | + "id": 2, |
| 203 | + "hostname": "lan-device", |
| 204 | + "friendlyname": "lan-device", |
| 205 | + "macAddress": "11:22:33:44:55:66", |
| 206 | + "ipAddress": "192.168.1.3", |
| 207 | + "active": True, |
| 208 | + "devicetype": "MISCELLANEOUS", |
| 209 | + } |
| 210 | + ], |
| 211 | + } |
| 212 | + ] |
| 213 | + hosts_response = AsyncMock() |
| 214 | + hosts_response.status = 200 |
| 215 | + hosts_response.json = AsyncMock(return_value=home_payload) |
| 216 | + hosts_response.__aenter__ = AsyncMock(return_value=hosts_response) |
| 217 | + hosts_response.__aexit__ = AsyncMock(return_value=None) |
| 218 | + |
| 219 | + mock_session.request.side_effect = [login_response, hosts_response] |
| 220 | + |
| 221 | + client = SagemcomClient( |
| 222 | + host="192.168.1.1", |
| 223 | + username="admin", |
| 224 | + password="admin", |
| 225 | + session=mock_session, |
| 226 | + api_mode=ApiMode.REST, |
| 227 | + ) |
| 228 | + |
| 229 | + await client.login() |
| 230 | + devices = await client.get_hosts() |
| 231 | + |
| 232 | + assert len(devices) == 2 |
| 233 | + assert devices[0].host_name == "wifi-device" |
| 234 | + assert devices[0].interface_type == "wifi" |
| 235 | + assert devices[1].host_name == "lan-device" |
| 236 | + assert devices[1].interface_type == "ethernet" |
0 commit comments