Upgrade pytest-aiohttp (#82475)

* Upgrade pytest-aiohttp

* Make sure executors, tasks and timers are closed

Some test will trigger warnings on garbage collect, these warnings
spills over into next test.

Some test trigger tasks that raise errors on shutdown, these spill
over into next test.

This is to mimic older pytest-aiohttp and it's behaviour on test
cleanup.

Discussions on similar changes for pytest-aiohttp are here:
https://github.com/pytest-dev/pytest-asyncio/pull/309

* Replace loop with event_loop

* Make sure time is frozen for tests

* Make sure the ConditionType is not async

  /home-assistant/homeassistant/helpers/template.py:2082: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
    def wrapper(*args, **kwargs):
  Enable tracemalloc to get traceback where the object was allocated.
  See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.

* Increase litejet press tests with a factor 10

The times are simulated anyway, and we can't stop the normal
event from occuring.

* Use async handlers for aiohttp

tests/components/motioneye/test_camera.py::test_get_still_image_from_camera
tests/components/motioneye/test_camera.py::test_get_still_image_from_camera
tests/components/motioneye/test_camera.py::test_get_stream_from_camera
tests/components/motioneye/test_camera.py::test_get_stream_from_camera
tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template
tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template
  /Users/joakim/src/hass/home-assistant/venv/lib/python3.9/site-packages/aiohttp/web_urldispatcher.py:189: DeprecationWarning: Bare functions are deprecated, use async ones
    warnings.warn(

* Switch to freezegun in modbus tests

The tests allowed clock to tick in between steps

* Make sure skybell object are fully mocked

Old tests would trigger attempts to post to could services:

```
DEBUG:aioskybell:HTTP post https://cloud.myskybell.com/api/v3/login/ Request with headers: {'content-type': 'application/json', 'accept': '*/*', 'x-skybell-app-id': 'd2b542c7-a7e4-4e1e-b77d-2b76911c7c46', 'x-skybell-client-id': '1f36a3c0-6dee-4997-a6db-4e1c67338e57'}
```

* Fix sorting that broke after rebase
This commit is contained in:
Joakim Plate 2022-11-29 22:36:36 +01:00 committed by GitHub
parent b7652c78ee
commit c576a68d33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 263 additions and 226 deletions

View file

@ -2,6 +2,7 @@
from unittest.mock import patch
from aioskybell import exceptions
from pytest import fixture
from homeassistant import config_entries
from homeassistant.components.skybell.const import DOMAIN
@ -10,43 +11,39 @@ from homeassistant.const import CONF_PASSWORD, CONF_SOURCE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import (
CONF_CONFIG_FLOW,
PASSWORD,
USER_ID,
_patch_skybell,
_patch_skybell_devices,
)
from . import CONF_CONFIG_FLOW, PASSWORD, USER_ID
from tests.common import MockConfigEntry
def _patch_setup_entry() -> None:
return patch(
@fixture(autouse=True)
def setup_entry() -> None:
"""Make sure component doesn't initialize."""
with patch(
"homeassistant.components.skybell.async_setup_entry",
return_value=True,
)
):
yield
async def test_flow_user(hass: HomeAssistant) -> None:
"""Test that the user step works."""
with _patch_skybell(), _patch_skybell_devices(), _patch_setup_entry():
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=CONF_CONFIG_FLOW,
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=CONF_CONFIG_FLOW,
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "user"
assert result["data"] == CONF_CONFIG_FLOW
assert result["result"].unique_id == USER_ID
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "user"
assert result["data"] == CONF_CONFIG_FLOW
assert result["result"].unique_id == USER_ID
async def test_flow_user_already_configured(hass: HomeAssistant) -> None:
@ -57,50 +54,48 @@ async def test_flow_user_already_configured(hass: HomeAssistant) -> None:
)
entry.add_to_hass(hass)
with _patch_skybell(), _patch_skybell_devices():
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_flow_user_cannot_connect(hass: HomeAssistant) -> None:
async def test_flow_user_cannot_connect(hass: HomeAssistant, skybell_mock) -> None:
"""Test user initialized flow with unreachable server."""
with _patch_skybell() as skybell_mock:
skybell_mock.side_effect = exceptions.SkybellException(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
skybell_mock.async_initialize.side_effect = exceptions.SkybellException(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
async def test_invalid_credentials(hass: HomeAssistant) -> None:
async def test_invalid_credentials(hass: HomeAssistant, skybell_mock) -> None:
"""Test that invalid credentials throws an error."""
with patch("homeassistant.components.skybell.Skybell.async_login") as skybell_mock:
skybell_mock.side_effect = exceptions.SkybellAuthenticationException(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
skybell_mock.async_initialize.side_effect = (
exceptions.SkybellAuthenticationException(hass)
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_auth"}
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_auth"}
async def test_flow_user_unknown_error(hass: HomeAssistant) -> None:
async def test_flow_user_unknown_error(hass: HomeAssistant, skybell_mock) -> None:
"""Test user initialized flow with unreachable server."""
with _patch_skybell_devices() as skybell_mock:
skybell_mock.side_effect = Exception
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "unknown"}
skybell_mock.async_initialize.side_effect = Exception
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "unknown"}
async def test_step_reauth(hass: HomeAssistant) -> None:
@ -121,17 +116,15 @@ async def test_step_reauth(hass: HomeAssistant) -> None:
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
with _patch_skybell(), _patch_skybell_devices(), _patch_setup_entry():
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: PASSWORD},
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: PASSWORD},
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
async def test_step_reauth_failed(hass: HomeAssistant) -> None:
async def test_step_reauth_failed(hass: HomeAssistant, skybell_mock) -> None:
"""Test the reauth flow fails and recovers."""
entry = MockConfigEntry(domain=DOMAIN, unique_id=USER_ID, data=CONF_CONFIG_FLOW)
entry.add_to_hass(hass)
@ -149,21 +142,22 @@ async def test_step_reauth_failed(hass: HomeAssistant) -> None:
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
with patch("homeassistant.components.skybell.Skybell.async_login") as skybell_mock:
skybell_mock.side_effect = exceptions.SkybellAuthenticationException(hass)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: PASSWORD},
)
skybell_mock.async_initialize.side_effect = (
exceptions.SkybellAuthenticationException(hass)
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: PASSWORD},
)
assert result["type"] == FlowResultType.FORM
assert result["errors"] == {"base": "invalid_auth"}
assert result["type"] == FlowResultType.FORM
assert result["errors"] == {"base": "invalid_auth"}
with _patch_skybell(), _patch_skybell_devices(), _patch_setup_entry():
skybell_mock.async_initialize.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: PASSWORD},
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_PASSWORD: PASSWORD},
)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "reauth_successful"