|
11 | 11 | from sagemcom_api.exceptions import AuthenticationException |
12 | 12 |
|
13 | 13 |
|
| 14 | +@pytest.mark.asyncio |
| 15 | +async def test_default_session_accepts_ip_cookies(): |
| 16 | + """Default aiohttp session should accept cookies from IP hosts.""" |
| 17 | + client = SagemcomClient( |
| 18 | + host="192.168.1.1", |
| 19 | + username="admin", |
| 20 | + password="admin", |
| 21 | + authentication_method=EncryptionMethod.MD5, |
| 22 | + ) |
| 23 | + try: |
| 24 | + assert getattr(client.session.cookie_jar, "_unsafe", False) is True |
| 25 | + finally: |
| 26 | + await client.close() |
| 27 | + |
| 28 | + |
14 | 29 | @pytest.mark.asyncio |
15 | 30 | async def test_login_success(mock_session_factory, login_success_response): |
16 | 31 | """ |
@@ -291,6 +306,60 @@ async def test_get_hosts_rest_fallbacks_to_hosts_endpoint(): |
291 | 306 | assert devices[0].active is True |
292 | 307 |
|
293 | 308 |
|
| 309 | +@pytest.mark.asyncio |
| 310 | +async def test_get_hosts_rest_fallbacks_on_home_400(): |
| 311 | + """/api/v1/hosts should be tried when /api/v1/home returns HTTP 400.""" |
| 312 | + mock_session = MagicMock(spec=ClientSession) |
| 313 | + mock_session.close = AsyncMock() |
| 314 | + |
| 315 | + login_response = AsyncMock() |
| 316 | + login_response.status = 204 |
| 317 | + login_response.text = AsyncMock(return_value="") |
| 318 | + login_response.__aenter__ = AsyncMock(return_value=login_response) |
| 319 | + login_response.__aexit__ = AsyncMock(return_value=None) |
| 320 | + |
| 321 | + home_response = AsyncMock() |
| 322 | + home_response.status = 400 |
| 323 | + home_response.text = AsyncMock(return_value='{"exception":{"domain":"/api/v1/home"}}') |
| 324 | + home_response.__aenter__ = AsyncMock(return_value=home_response) |
| 325 | + home_response.__aexit__ = AsyncMock(return_value=None) |
| 326 | + |
| 327 | + hosts_payload = [ |
| 328 | + { |
| 329 | + "id": 3, |
| 330 | + "hostname": "phone", |
| 331 | + "friendlyname": "phone", |
| 332 | + "macAddress": "de:ad:be:ef:00:01", |
| 333 | + "ipAddress": "192.168.1.25", |
| 334 | + "active": True, |
| 335 | + "interfaceType": "wireless", |
| 336 | + "devicetype": "SMARTPHONE", |
| 337 | + } |
| 338 | + ] |
| 339 | + hosts_response = AsyncMock() |
| 340 | + hosts_response.status = 200 |
| 341 | + hosts_response.json = AsyncMock(return_value=hosts_payload) |
| 342 | + hosts_response.__aenter__ = AsyncMock(return_value=hosts_response) |
| 343 | + hosts_response.__aexit__ = AsyncMock(return_value=None) |
| 344 | + |
| 345 | + mock_session.request.side_effect = [login_response, home_response, hosts_response] |
| 346 | + |
| 347 | + client = SagemcomClient( |
| 348 | + host="192.168.1.1", |
| 349 | + username="admin", |
| 350 | + password="admin", |
| 351 | + session=mock_session, |
| 352 | + api_mode=ApiMode.REST, |
| 353 | + ) |
| 354 | + |
| 355 | + await client.login() |
| 356 | + devices = await client.get_hosts() |
| 357 | + |
| 358 | + assert len(devices) == 1 |
| 359 | + assert devices[0].host_name == "phone" |
| 360 | + assert devices[0].interface_type == "wifi" |
| 361 | + |
| 362 | + |
294 | 363 | @pytest.mark.asyncio |
295 | 364 | async def test_reboot_rest_mode(): |
296 | 365 | """reboot should call REST endpoint on REST firmware.""" |
|
0 commit comments