From c898d7ebdb53085f5dd05eec52a80606e07faeeb Mon Sep 17 00:00:00 2001 From: David Bonnes Date: Sat, 21 Dec 2024 22:57:21 +0000 Subject: [PATCH] extend tests --- tests/tests/__snapshots__/test_installs.ambr | 187 +++++++++++++++++++ tests/tests/test_v2_auth.py | 56 +++++- 2 files changed, 242 insertions(+), 1 deletion(-) diff --git a/tests/tests/__snapshots__/test_installs.ambr b/tests/tests/__snapshots__/test_installs.ambr index 588bf8b..2e25f9b 100644 --- a/tests/tests/__snapshots__/test_installs.ambr +++ b/tests/tests/__snapshots__/test_installs.ambr @@ -84,6 +84,11 @@ 'id': ''' '3432522' + ''', + 'is_permanent': ''' + true + ... + ''', 'mode': ''' AutoWithEco @@ -203,12 +208,21 @@ temperature: 19.5 zone_id: '3450733' + ''', + 'systemId': ''' + '3432522' + ''', 'system_mode_status': ''' is_permanent: true mode: AutoWithEco ''', + 'until': ''' + null + ... + + ''', }) # --- # name: test_system_snapshot[default][gateway] @@ -223,6 +237,10 @@ is_wi_fi: false mac: 00D02DEE4E56 + ''', + 'gatewayId': ''' + '2499896' + ''', 'id': ''' '2499896' @@ -359,6 +377,10 @@ min_switchpoints_per_day: 1 timing_resolution: 00:10:00 + ''', + 'dhwId': ''' + '3933910' + ''', 'id': ''' '3933910' @@ -479,6 +501,10 @@ 'id': ''' '2738909' + ''', + 'locationId': ''' + '2738909' + ''', 'name': ''' My Home @@ -711,6 +737,9 @@ ... + ' + zoneId: '''3432521'' + ' '3432576': active_faults: '!!python/tuple [] @@ -819,6 +848,9 @@ ... + ' + zoneId: '''3432576'' + ' '3432577': active_faults: '!!python/tuple [] @@ -927,6 +959,9 @@ ... + ' + zoneId: '''3432577'' + ' '3432578': active_faults: '!!python/tuple [] @@ -1035,6 +1070,9 @@ ... + ' + zoneId: '''3432578'' + ' '3432579': active_faults: '!!python/tuple [] @@ -1143,6 +1181,9 @@ ... + ' + zoneId: '''3432579'' + ' '3432580': active_faults: '!!python/tuple [] @@ -1251,6 +1292,9 @@ ... + ' + zoneId: '''3432580'' + ' '3449703': active_faults: '!!python/tuple [] @@ -1359,6 +1403,9 @@ ... + ' + zoneId: '''3449703'' + ' '3449740': active_faults: '!!python/tuple [] @@ -1467,6 +1514,9 @@ ... + ' + zoneId: '''3449740'' + ' '3450733': active_faults: '!!python/tuple [] @@ -1575,6 +1625,9 @@ ... + ' + zoneId: '''3450733'' + ' ''' @@ -1664,6 +1717,11 @@ 'id': ''' '3432522' + ''', + 'is_permanent': ''' + true + ... + ''', 'mode': ''' AutoWithEco @@ -1783,12 +1841,21 @@ temperature: 19.5 zone_id: '3450733' + ''', + 'systemId': ''' + '3432522' + ''', 'system_mode_status': ''' is_permanent: true mode: AutoWithEco ''', + 'until': ''' + null + ... + + ''', }) # --- # name: test_system_snapshot[hass_118169][gateway] @@ -1803,6 +1870,10 @@ is_wi_fi: false mac: 00D02DEE4E56 + ''', + 'gatewayId': ''' + '2499896' + ''', 'id': ''' '2499896' @@ -1939,6 +2010,10 @@ min_switchpoints_per_day: 1 timing_resolution: 00:10:00 + ''', + 'dhwId': ''' + '3933910' + ''', 'id': ''' '3933910' @@ -2059,6 +2134,10 @@ 'id': ''' '2738909' + ''', + 'locationId': ''' + '2738909' + ''', 'name': ''' My Home @@ -2291,6 +2370,9 @@ ... + ' + zoneId: '''3432521'' + ' '3432576': active_faults: '!!python/tuple [] @@ -2399,6 +2481,9 @@ ... + ' + zoneId: '''3432576'' + ' '3432577': active_faults: '!!python/tuple [] @@ -2507,6 +2592,9 @@ ... + ' + zoneId: '''3432577'' + ' '3432578': active_faults: '!!python/tuple [] @@ -2615,6 +2703,9 @@ ... + ' + zoneId: '''3432578'' + ' '3432579': active_faults: '!!python/tuple [] @@ -2723,6 +2814,9 @@ ... + ' + zoneId: '''3432579'' + ' '3432580': active_faults: '!!python/tuple [] @@ -2831,6 +2925,9 @@ ... + ' + zoneId: '''3432580'' + ' '3449703': active_faults: '!!python/tuple [] @@ -2939,6 +3036,9 @@ ... + ' + zoneId: '''3449703'' + ' '3449740': active_faults: '!!python/tuple [] @@ -3047,6 +3147,9 @@ ... + ' + zoneId: '''3449740'' + ' '3450733': active_faults: '!!python/tuple [] @@ -3155,6 +3258,9 @@ ... + ' + zoneId: '''3450733'' + ' ''' @@ -3244,6 +3350,11 @@ 'id': ''' '3432522' + ''', + 'is_permanent': ''' + true + ... + ''', 'mode': ''' Auto @@ -3365,12 +3476,21 @@ temperature: 15.0 zone_id: '3450733' + ''', + 'systemId': ''' + '3432522' + ''', 'system_mode_status': ''' is_permanent: true mode: Auto ''', + 'until': ''' + null + ... + + ''', }) # --- # name: test_system_snapshot[system_002][gateway] @@ -3385,6 +3505,10 @@ is_wi_fi: false mac: 00D02DEE4E56 + ''', + 'gatewayId': ''' + '2499896' + ''', 'id': ''' '2499896' @@ -3525,6 +3649,10 @@ min_switchpoints_per_day: 1 timing_resolution: 00:10:00 + ''', + 'dhwId': ''' + '3933910' + ''', 'id': ''' '3933910' @@ -3645,6 +3773,10 @@ 'id': ''' '2738909' + ''', + 'locationId': ''' + '2738909' + ''', 'name': ''' My Home @@ -3879,6 +4011,9 @@ ... + ' + zoneId: '''3432521'' + ' '3432576': active_faults: '!!python/tuple [] @@ -3987,6 +4122,9 @@ ... + ' + zoneId: '''3432576'' + ' '3432577': active_faults: '!!python/tuple [] @@ -4095,6 +4233,9 @@ ... + ' + zoneId: '''3432577'' + ' '3432578': active_faults: '!!python/tuple [] @@ -4203,6 +4344,9 @@ ... + ' + zoneId: '''3432578'' + ' '3432579': active_faults: '!!python/tuple [] @@ -4311,6 +4455,9 @@ ... + ' + zoneId: '''3432579'' + ' '3432580': active_faults: '!!python/tuple [] @@ -4421,6 +4568,9 @@ ... + ' + zoneId: '''3432580'' + ' '3449703': active_faults: '!!python/tuple [] @@ -4529,6 +4679,9 @@ ... + ' + zoneId: '''3449703'' + ' '3449740': active_faults: '!!python/tuple [] @@ -4637,6 +4790,9 @@ ... + ' + zoneId: '''3449740'' + ' '3450733': active_faults: '!!python/tuple [] @@ -4745,6 +4901,9 @@ ... + ' + zoneId: '''3450733'' + ' ''' @@ -4804,6 +4963,11 @@ 'id': ''' '3935923' + ''', + 'is_permanent': ''' + true + ... + ''', 'mode': ''' Auto @@ -4849,12 +5013,21 @@ temperature: 23.0 zone_id: '4368406' + ''', + 'systemId': ''' + '3935923' + ''', 'system_mode_status': ''' is_permanent: true mode: Auto ''', + 'until': ''' + null + ... + + ''', }) # --- # name: test_system_snapshot[system_004][gateway] @@ -4869,6 +5042,10 @@ is_wi_fi: false mac: 00D02DF114FD + ''', + 'gatewayId': ''' + '2820628' + ''', 'id': ''' '2820628' @@ -4949,6 +5126,10 @@ 'id': ''' '2664492' + ''', + 'locationId': ''' + '2664492' + ''', 'name': ''' Summer House @@ -5112,6 +5293,9 @@ ... + ' + zoneId: '''3935922'' + ' '4368406': active_faults: '!!python/tuple [] @@ -5220,6 +5404,9 @@ ... + ' + zoneId: '''4368406'' + ' ''' diff --git a/tests/tests/test_v2_auth.py b/tests/tests/test_v2_auth.py index ba2b483..d51b358 100644 --- a/tests/tests/test_v2_auth.py +++ b/tests/tests/test_v2_auth.py @@ -3,6 +3,7 @@ from __future__ import annotations import json +import logging import uuid from http import HTTPMethod, HTTPStatus from typing import TYPE_CHECKING @@ -135,6 +136,7 @@ async def test_token_manager( cache_file: Path, client_session: aiohttp.ClientSession, credentials: tuple[str, str], + caplog: pytest.LogCaptureFixture, freezer: FrozenDateTimeFactory, tmp_path_factory: pytest.TempPathFactory, ) -> None: @@ -190,9 +192,61 @@ async def test_token_manager( "access_token": "new_access_token...", "expires_in": 1800, "refresh_token": "new_refresh_token...", + "token_type": "bearer", + } + + with caplog.at_level(logging.DEBUG): + assert await cache_manager.get_access_token() == "new_access_token..." + + assert caplog.records[0].message == ( + "Null/Expired/Invalid access_token, re-authenticating." + ) + assert caplog.records[1].message == ( + "Authenticating with the refresh_token..." + ) + assert caplog.records[2].message.startswith( + "POST https://tccna.resideo.com/Auth/OAuth/Token" + ) + assert caplog.records[3].message == ( + " - access_token = new_access_token..." + ) + assert caplog.records[4].message.startswith( + " - access_token_expires = ", + ) + assert caplog.records[5].message == ( + " - refresh_token = new_refresh_token..." + ) + + req.assert_called_once() + wrt.assert_called_once() + + assert cache_manager.is_access_token_valid() is True + + # + # TEST 5: look for warning message + freezer.tick(2400) # advance time by another 20 minutes + assert cache_manager.is_access_token_valid() is False + + with ( + patch( + "evohomeasync2.auth.AbstractTokenManager._post_access_token_request", + new_callable=AsyncMock, + ) as req, + patch("cli.auth.TokenManager.save_access_token", new_callable=AsyncMock) as wrt, + ): + req.return_value = { + "access_token": "newer_access_token...", + "expires_in": 1800, + "refresh_token": "newer_refresh_token...", + # "token_type": "bearer", # should throw a warning } - assert await cache_manager.get_access_token() == "new_access_token..." + caplog.clear() + with caplog.at_level(logging.DEBUG): + assert await cache_manager.get_access_token() == "newer_access_token..." + + # required key not provided @ data['token_type'] + assert "payload may be invalid" in caplog.records[2].message req.assert_called_once() wrt.assert_called_once()