Skip to content

Commit

Permalink
[create-pull-request] automated change (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: MatthewFlamm <MatthewFlamm@users.noreply.github.com>
  • Loading branch information
MatthewFlamm and MatthewFlamm authored Feb 1, 2021
1 parent 508443d commit 9440f5e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pytest_homeassistant_custom_component/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
52 changes: 47 additions & 5 deletions pytest_homeassistant_custom_component/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions pytest_homeassistant_custom_component/test_util/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

-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
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
Expand Down

0 comments on commit 9440f5e

Please sign in to comment.