Skip to content

Commit

Permalink
clean up test handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdavb committed Nov 2, 2024
1 parent 3f80284 commit 1fb1594
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 16 deletions.
6 changes: 3 additions & 3 deletions tests/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@


# used to construct the default token cache
DEFAULT_USERNAME: Final[str] = "[email protected]"
DEFAULT_PASSWORD: Final[str] = "P@ssw0rd!!" # noqa: S105
TEST_USERNAME: Final[str] = "[email protected]"
TEST_PASSWORD: Final[str] = "P@ssw0rd!!" # noqa: S105


class ClientStub:
Expand Down Expand Up @@ -136,7 +136,7 @@ async def client_session() -> AsyncGenerator[aiohttp.ClientSession, None]:
def credentials() -> tuple[str, str]:
"""Return a username and a password."""

return DEFAULT_USERNAME, DEFAULT_PASSWORD
return TEST_USERNAME, TEST_PASSWORD


@pytest.fixture # @pytest_asyncio.fixture(scope="session", loop_scope="session")
Expand Down
50 changes: 45 additions & 5 deletions tests/tests_rf/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations

import functools
import json
import os
from collections.abc import AsyncGenerator
Expand All @@ -20,15 +21,46 @@

#
# normally, we want debug flags to be False
_DBG_USE_REAL_AIOHTTP = True
_DBG_USE_REAL_AIOHTTP = False
_DBG_DISABLE_STRICT_ASSERTS = False # of response content-type, schema

if TYPE_CHECKING:
import aiohttp

# used to construct the default token cache
DEFAULT_USERNAME: Final[str] = "[email protected]"
DEFAULT_PASSWORD: Final[str] = "P@ssw0rd!!" # noqa: S105
TEST_USERNAME: Final[str] = "[email protected]"
TEST_PASSWORD: Final[str] = "P@ssw0rd!!" # noqa: S105


# Global flag to indicate if AuthenticationFailedError has been encountered
global_auth_failed = False


def skipif_auth_failed(fnc):
"""Decorator to skip tests if AuthenticationFailedError is encountered."""

@functools.wraps(fnc)
async def wrapper(*args, **kwargs):
global global_auth_failed

if global_auth_failed:
pytest.skip("Unable to authenticate")

try:
result = await fnc(*args, **kwargs)
return result

except (
evo1.AuthenticationFailedError,
evo2.AuthenticationFailedError,
) as err:
if not _DBG_USE_REAL_AIOHTTP:
raise

global_auth_failed = True
pytest.fail(f"Unable to authenticate: {err}")

return wrapper


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -69,8 +101,8 @@ async def client_session() -> AsyncGenerator[aiohttp.ClientSession, None]:
def credentials() -> tuple[str, str]:
"""Return a username and a password."""

username: str = os.getenv("TEST_USERNAME") or DEFAULT_USERNAME
password: str = os.getenv("TEST_PASSWORD") or DEFAULT_PASSWORD
username: str = os.getenv("TEST_USERNAME") or TEST_USERNAME
password: str = os.getenv("TEST_PASSWORD") or TEST_PASSWORD

return username, password

Expand Down Expand Up @@ -134,6 +166,8 @@ async def evohome_v1(
) -> AsyncGenerator[evo1.EvohomeClient, None]:
"""Yield an instance of a v1 EvohomeClient."""

global skipif_auth_failed

evo = evo1.EvohomeClient(*credentials, websession=client_session)

try:
Expand All @@ -142,6 +176,8 @@ async def evohome_v1(
except evo1.AuthenticationFailedError as err:
if not _DBG_USE_REAL_AIOHTTP:
raise

skipif_auth_failed = True
pytest.skip(f"Unable to authenticate: {err}")


Expand All @@ -151,6 +187,8 @@ async def evohome_v2(
) -> AsyncGenerator[evo2.EvohomeClientNew, None]:
"""Yield an instance of a v2 EvohomeClient."""

global skipif_auth_failed

evo = evo2.EvohomeClientNew(token_manager)

try:
Expand All @@ -159,4 +197,6 @@ async def evohome_v2(
except evo2.AuthenticationFailedError as err:
if not _DBG_USE_REAL_AIOHTTP:
raise

skipif_auth_failed = True
pytest.skip(f"Unable to authenticate: {err}")
6 changes: 3 additions & 3 deletions tests/tests_rf/test_token_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from evohomeasync2 import exceptions as exc
from evohomeasync2.client import TokenManager

from .conftest import DEFAULT_PASSWORD, DEFAULT_USERNAME
from .conftest import TEST_PASSWORD, TEST_USERNAME

if TYPE_CHECKING:
from datetime import datetime as dt
Expand Down Expand Up @@ -47,8 +47,8 @@ def post_side_effect(*args: Any, **kwargs: Any) -> Any:
# else: data["grant_type"] == "password"...

if (
data.get("Username") != DEFAULT_USERNAME
or data.get("Password") != DEFAULT_PASSWORD
data.get("Username") != TEST_USERNAME
or data.get("Password") != TEST_PASSWORD
):
raise aiohttp.ClientResponseError(None, (), status=HTTPStatus.BAD_REQUEST)

Expand Down
3 changes: 2 additions & 1 deletion tests/tests_rf/test_v1_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import evohomeasync as evo1

from .conftest import _DBG_USE_REAL_AIOHTTP
from .conftest import _DBG_USE_REAL_AIOHTTP, skipif_auth_failed


async def _test_client_apis(evo: evo1.EvohomeClient) -> None:
Expand All @@ -29,6 +29,7 @@ async def _test_client_apis(evo: evo1.EvohomeClient) -> None:
# _LOGGER.warning("get_temperatures() OK")


@skipif_auth_failed
async def test_client_apis(evohome_v1: evo1.EvohomeClient) -> None:
"""Test _populate_user_data() & _populate_full_data()"""

Expand Down
4 changes: 3 additions & 1 deletion tests/tests_rf/test_v1_xxxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import evohomeasync as evo1

from .conftest import _DBG_USE_REAL_AIOHTTP
from .conftest import _DBG_USE_REAL_AIOHTTP, skipif_auth_failed
from .helpers import should_fail_v1, should_work_v1


Expand Down Expand Up @@ -58,6 +58,7 @@ async def _test_client_apis(evo: evo1.EvohomeClient) -> None:
assert temps


@skipif_auth_failed
async def test_locations(evohome_v1: evo1.EvohomeClient) -> None:
"""Test /locations"""

Expand All @@ -67,6 +68,7 @@ async def test_locations(evohome_v1: evo1.EvohomeClient) -> None:
await _test_url_locations(evohome_v1)


@skipif_auth_failed
async def test_client_apis(evohome_v1: evo1.EvohomeClient) -> None:
"""Test _populate_user_data() & _populate_full_data()"""

Expand Down
6 changes: 5 additions & 1 deletion tests/tests_rf/test_v2_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from evohomeasync2.zone import Zone

from . import faked_server as faked
from .conftest import _DBG_USE_REAL_AIOHTTP
from .conftest import _DBG_USE_REAL_AIOHTTP, skipif_auth_failed

#######################################################################################

Expand Down Expand Up @@ -114,21 +114,25 @@ async def _test_system_apis(evo: evo2.EvohomeClientNew) -> None:
#######################################################################################


@skipif_auth_failed
async def test_basics(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test authentication, `user_account()` and `installation()`."""
await _test_basics_apis(evohome_v2)


@skipif_auth_failed
async def _test_sched_(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test `get_schedule()` and `get_schedule()`."""
await _test_sched__apis(evohome_v2)


@skipif_auth_failed
async def test_status(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test `_update()` for DHW/zone."""
await _test_update_apis(evohome_v2)


@skipif_auth_failed
async def test_system(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test `set_mode()` for TCS"""

Expand Down
3 changes: 2 additions & 1 deletion tests/tests_rf/test_v2_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from evohomeasync2.schema.helpers import pascal_case

from .conftest import _DBG_USE_REAL_AIOHTTP
from .conftest import _DBG_USE_REAL_AIOHTTP, skipif_auth_failed
from .helpers import should_fail, should_work

#######################################################################################
Expand Down Expand Up @@ -186,6 +186,7 @@ async def _test_task_id(evo: evo2.EvohomeClientNew) -> None:
#######################################################################################


@skipif_auth_failed
async def test_task_id(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /location/{loc.id}/status"""

Expand Down
8 changes: 7 additions & 1 deletion tests/tests_rf/test_v2_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from evohomeasync2.schema.schedule import convert_to_put_schedule

from . import faked_server as faked
from .conftest import _DBG_USE_REAL_AIOHTTP
from .conftest import _DBG_USE_REAL_AIOHTTP, skipif_auth_failed
from .helpers import should_fail, should_work

if TYPE_CHECKING:
Expand Down Expand Up @@ -302,6 +302,7 @@ async def _test_schedule(evo: evo2.EvohomeClientNew) -> None:
#######################################################################################


@skipif_auth_failed
async def test_usr_account(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /userAccount"""

Expand All @@ -314,18 +315,21 @@ async def test_usr_account(evohome_v2: evo2.EvohomeClientNew) -> None:
pytest.skip("Unable to authenticate")


@skipif_auth_failed
async def test_all_config(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /location/installationInfo"""

await _test_all_config(evohome_v2)


@skipif_auth_failed
async def test_loc_status(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /location/{loc.id}/status"""

await _test_loc_status(evohome_v2)


@skipif_auth_failed
async def test_tcs_mode(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /temperatureControlSystem/{tcs.id}/mode"""

Expand All @@ -338,6 +342,7 @@ async def test_tcs_mode(evohome_v2: evo2.EvohomeClientNew) -> None:
pytest.skip("Mocked server API not implemented")


@skipif_auth_failed
async def test_zone_mode(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /temperatureZone/{zone.id}/heatSetpoint"""

Expand All @@ -350,6 +355,7 @@ async def test_zone_mode(evohome_v2: evo2.EvohomeClientNew) -> None:
pytest.skip("Mocked server API not implemented")


@skipif_auth_failed
async def test_schedule(evohome_v2: evo2.EvohomeClientNew) -> None:
"""Test /{x.TYPE}/{x.id}/schedule"""

Expand Down

0 comments on commit 1fb1594

Please sign in to comment.