diff --git a/README.md b/README.md index 1811c3b..7a0a0be 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pytest-homeassistant-custom-component -![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=2021.2.0.dev0&labelColor=blue) +![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=2021.3.0.dev0&labelColor=blue) Package to automatically extract testing plugins from Home Assistant for custom component testing. The goal is to provide the same functionality as the tests in home-assistant/core. diff --git a/pytest_homeassistant_custom_component/const.py b/pytest_homeassistant_custom_component/const.py index e125a94..b48674e 100644 --- a/pytest_homeassistant_custom_component/const.py +++ b/pytest_homeassistant_custom_component/const.py @@ -4,7 +4,7 @@ This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component. """ MAJOR_VERSION = 2021 -MINOR_VERSION = 2 +MINOR_VERSION = 3 PATCH_VERSION = "0.dev0" __short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}" __version__ = f"{__short_version__}.{PATCH_VERSION}" diff --git a/pytest_homeassistant_custom_component/plugins.py b/pytest_homeassistant_custom_component/plugins.py index 239d0c1..36513e9 100644 --- a/pytest_homeassistant_custom_component/plugins.py +++ b/pytest_homeassistant_custom_component/plugins.py @@ -18,6 +18,7 @@ from homeassistant import core as ha, loader, runner, util from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY +from homeassistant.auth.models import Credentials from homeassistant.auth.providers import homeassistant, legacy_api_password from homeassistant.components import mqtt from homeassistant.components.websocket_api.auth import ( @@ -101,6 +102,21 @@ def verify_cleanup(): assert not threads +@pytest.fixture(autouse=True) +def bcrypt_cost(): + """Run with reduced rounds during tests, to speed up uses.""" + import bcrypt + + gensalt_orig = bcrypt.gensalt + + def gensalt_mock(rounds=12, prefix=b"2b"): + return gensalt_orig(4, prefix) + + bcrypt.gensalt = gensalt_mock + yield + bcrypt.gensalt = gensalt_orig + + @pytest.fixture def hass_storage(): """Fixture to mock storage.""" @@ -205,10 +221,24 @@ async def mock_update_config(path, id, entity): @pytest.fixture -def hass_access_token(hass, hass_admin_user): +async def hass_admin_credential(hass, local_auth): + """Provide credentials for admin user.""" + return Credentials( + id="mock-credential-id", + auth_provider_type="homeassistant", + auth_provider_id=None, + data={"username": "admin"}, + is_new=False, + ) + + +@pytest.fixture +async def hass_access_token(hass, hass_admin_user, hass_admin_credential): """Return an access token to access Home Assistant.""" - refresh_token = hass.loop.run_until_complete( - hass.auth.async_create_refresh_token(hass_admin_user, CLIENT_ID) + await hass.auth.async_link_user(hass_admin_user, hass_admin_credential) + + refresh_token = await hass.auth.async_create_refresh_token( + hass_admin_user, CLIENT_ID, credential=hass_admin_credential ) return hass.auth.async_create_access_token(refresh_token) @@ -238,10 +268,21 @@ def hass_read_only_user(hass, local_auth): @pytest.fixture -def hass_read_only_access_token(hass, hass_read_only_user): +def hass_read_only_access_token(hass, hass_read_only_user, local_auth): """Return a Home Assistant read only user.""" + credential = Credentials( + id="mock-readonly-credential-id", + auth_provider_type="homeassistant", + auth_provider_id=None, + data={"username": "readonly"}, + is_new=False, + ) + hass_read_only_user.credentials.append(credential) + refresh_token = hass.loop.run_until_complete( - hass.auth.async_create_refresh_token(hass_read_only_user, CLIENT_ID) + hass.auth.async_create_refresh_token( + hass_read_only_user, CLIENT_ID, credential=credential + ) ) return hass.auth.async_create_access_token(refresh_token) @@ -264,6 +305,7 @@ def local_auth(hass): prv = homeassistant.HassAuthProvider( hass, hass.auth._store, {"type": "homeassistant"} ) + hass.loop.run_until_complete(prv.async_initialize()) hass.auth._providers[(prv.type, prv.id)] = prv return prv diff --git a/pytest_homeassistant_custom_component/test_util/aiohttp.py b/pytest_homeassistant_custom_component/test_util/aiohttp.py index 60d22e1..f18c95e 100644 --- a/pytest_homeassistant_custom_component/test_util/aiohttp.py +++ b/pytest_homeassistant_custom_component/test_util/aiohttp.py @@ -249,9 +249,9 @@ async def read(self): """Return mock response.""" return self.response - async def text(self, encoding="utf-8"): + async def text(self, encoding="utf-8", errors="strict"): """Return mock response as a string.""" - return self.response.decode(encoding) + return self.response.decode(encoding, errors=errors) async def json(self, encoding="utf-8", content_type=None): """Return mock response as a json.""" @@ -280,7 +280,7 @@ def mock_aiohttp_client(): """Context manager to mock aiohttp client.""" mocker = AiohttpClientMocker() - def create_session(hass, *args): + def create_session(hass, *args, **kwargs): session = mocker.create_session(hass.loop) async def close_session(event): diff --git a/requirements_dev.txt b/requirements_dev.txt index 249297c..b01432a 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,6 +1,6 @@ # This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component. codecov==2.1.10 -mypy==0.790 -pre-commit==2.9.3 +mypy==0.800 +pre-commit==2.10.0 pylint==2.6.0 astroid==2.4.2 diff --git a/requirements_test.txt b/requirements_test.txt index aa4abc5..567c0d7 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -5,10 +5,11 @@ -c homeassistant/package_constraints.txt -r requirements_test_pre_commit.txt -coverage==5.3 +coverage==5.4 jsonpickle==1.4.1 mock-open==1.4.0 pipdeptree==1.0.0 +awesomeversion==21.1.6 pylint-strict-informational==0.1 pytest-aiohttp==0.3.0 pytest-cov==2.10.1 @@ -16,7 +17,7 @@ pytest-test-groups==1.0.3 pytest-sugar==0.9.4 pytest-timeout==1.4.2 pytest-xdist==2.1.0 -pytest==6.1.2 +pytest==6.2.2 requests_mock==1.8.0 responses==0.12.0 respx==0.16.2