Use built-in test helpers on 3.8 (#34901)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-04-30 13:29:50 -07:00
1 parent 6b16c34fd0
commit ec47216388
303 files changed
+1163 -1320

No files matched your search

+2 -2
View File
@@ -3,7 +3,6 @@ import asyncio
import logging
from time import time
from asynctest import CoroutineMock, Mock, patch
import pytest
from homeassistant import config_entries, data_entry_flow
@@ -11,6 +10,7 @@ from homeassistant.components.cloud import account_link
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.util.dt import utcnow
from tests.async_mock import AsyncMock, Mock, patch
from tests.common import async_fire_time_changed, mock_platform
TEST_DOMAIN = "oauth2_test"
@@ -109,7 +109,7 @@ async def test_implementation(hass, flow_handler):
flow_finished = asyncio.Future()
helper = Mock(
async_get_authorize_url=CoroutineMock(return_value="http://example.com/auth"),
async_get_authorize_url=AsyncMock(return_value="http://example.com/auth"),
async_get_tokens=Mock(return_value=flow_finished),
)
+6 -10
View File
@@ -1,12 +1,12 @@
"""Test Alexa config."""
import contextlib
from unittest.mock import Mock, patch
from homeassistant.components.cloud import ALEXA_SCHEMA, alexa_config
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed, mock_coro
from tests.async_mock import AsyncMock, Mock, patch
from tests.common import async_fire_time_changed
async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs):
@@ -28,7 +28,7 @@ async def test_alexa_config_report_state(hass, cloud_prefs):
assert conf.should_report_state is False
assert conf.is_reporting_states is False
with patch.object(conf, "async_get_access_token", return_value=mock_coro("hello")):
with patch.object(conf, "async_get_access_token", AsyncMock(return_value="hello")):
await cloud_prefs.async_update(alexa_report_state=True)
await hass.async_block_till_done()
@@ -60,7 +60,7 @@ async def test_alexa_config_invalidate_token(hass, cloud_prefs, aioclient_mock):
cloud_prefs,
Mock(
alexa_access_token_url="http://example/alexa_token",
auth=Mock(async_check_token=Mock(side_effect=mock_coro)),
auth=Mock(async_check_token=AsyncMock()),
websession=hass.helpers.aiohttp_client.async_get_clientsession(),
),
)
@@ -189,13 +189,9 @@ async def test_alexa_update_report_state(hass, cloud_prefs):
alexa_config.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
with patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig."
"async_sync_entities",
side_effect=mock_coro,
"homeassistant.components.cloud.alexa_config.AlexaConfig.async_sync_entities",
) as mock_sync, patch(
"homeassistant.components.cloud.alexa_config."
"AlexaConfig.async_enable_proactive_mode",
side_effect=mock_coro,
"homeassistant.components.cloud.alexa_config.AlexaConfig.async_enable_proactive_mode",
):
await cloud_prefs.async_update(alexa_report_state=True)
await hass.async_block_till_done()
+2 -2
View File
@@ -1,11 +1,11 @@
"""Tests for the cloud binary sensor."""
from unittest.mock import Mock
from asynctest import patch
from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE
from homeassistant.setup import async_setup_component
from tests.async_mock import patch
async def test_remote_connection_sensor(hass):
"""Test the remote connection sensor."""
+2 -1
View File
@@ -12,6 +12,7 @@ from homeassistant.setup import async_setup_component
from . import mock_cloud, mock_cloud_prefs
from tests.async_mock import AsyncMock
from tests.common import mock_coro
from tests.components.alexa import test_smart_home as test_alexa
@@ -221,7 +222,7 @@ async def test_set_username(hass):
prefs = MagicMock(
alexa_enabled=False,
google_enabled=False,
async_set_username=MagicMock(return_value=mock_coro()),
async_set_username=AsyncMock(return_value=None),
)
client = CloudClient(hass, prefs, None, {}, {})
client.cloud = MagicMock(is_logged_in=True, username="mock-username")
+4 -5
View File
@@ -1,8 +1,6 @@
"""Test the Cloud Google Config."""
from unittest.mock import Mock
from asynctest import patch
from homeassistant.components.cloud import GACTIONS_SCHEMA
from homeassistant.components.cloud.google_config import CloudGoogleConfig
from homeassistant.components.google_assistant import helpers as ga_helpers
@@ -11,7 +9,8 @@ from homeassistant.core import CoreState
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed, mock_coro
from tests.async_mock import AsyncMock, patch
from tests.common import async_fire_time_changed
async def test_google_update_report_state(hass, cloud_prefs):
@@ -43,12 +42,12 @@ async def test_sync_entities(aioclient_mock, hass, cloud_prefs):
GACTIONS_SCHEMA({}),
"mock-user-id",
cloud_prefs,
Mock(auth=Mock(async_check_token=Mock(side_effect=mock_coro))),
Mock(auth=Mock(async_check_token=AsyncMock())),
)
with patch(
"hass_nabucasa.cloud_api.async_google_actions_request_sync",
return_value=mock_coro(Mock(status=HTTP_NOT_FOUND)),
return_value=Mock(status=HTTP_NOT_FOUND),
) as mock_request_sync:
assert await config.async_sync_entities("user") == HTTP_NOT_FOUND
assert len(mock_request_sync.mock_calls) == 1
+13 -26
View File
@@ -1,9 +1,7 @@
"""Tests for the HTTP API for the cloud component."""
import asyncio
from ipaddress import ip_network
from unittest.mock import MagicMock, Mock
from asynctest import patch
from hass_nabucasa import thingtalk
from hass_nabucasa.auth import Unauthenticated, UnknownError
from hass_nabucasa.const import STATE_CONNECTED
@@ -20,7 +18,7 @@ from homeassistant.core import State
from . import mock_cloud, mock_cloud_prefs
from tests.common import mock_coro
from tests.async_mock import AsyncMock, MagicMock, Mock, patch
from tests.components.google_assistant import MockConfig
SUBSCRIPTION_INFO_URL = "https://api-test.hass.io/subscription_info"
@@ -29,9 +27,7 @@ SUBSCRIPTION_INFO_URL = "https://api-test.hass.io/subscription_info"
@pytest.fixture()
def mock_auth():
"""Mock check token."""
with patch(
"hass_nabucasa.auth.CognitoAuth.async_check_token", side_effect=mock_coro
):
with patch("hass_nabucasa.auth.CognitoAuth.async_check_token"):
yield
@@ -89,7 +85,7 @@ async def test_google_actions_sync(mock_cognito, mock_cloud_login, cloud_client)
"""Test syncing Google Actions."""
with patch(
"hass_nabucasa.cloud_api.async_google_actions_request_sync",
return_value=mock_coro(Mock(status=200)),
return_value=Mock(status=200),
) as mock_request_sync:
req = await cloud_client.post("/api/cloud/google_actions/sync")
assert req.status == 200
@@ -100,7 +96,7 @@ async def test_google_actions_sync_fails(mock_cognito, mock_cloud_login, cloud_c
"""Test syncing Google Actions gone bad."""
with patch(
"hass_nabucasa.cloud_api.async_google_actions_request_sync",
return_value=mock_coro(Mock(status=HTTP_INTERNAL_SERVER_ERROR)),
return_value=Mock(status=HTTP_INTERNAL_SERVER_ERROR),
) as mock_request_sync:
req = await cloud_client.post("/api/cloud/google_actions/sync")
assert req.status == HTTP_INTERNAL_SERVER_ERROR
@@ -109,7 +105,7 @@ async def test_google_actions_sync_fails(mock_cognito, mock_cloud_login, cloud_c
async def test_login_view(hass, cloud_client):
"""Test logging in."""
hass.data["cloud"] = MagicMock(login=MagicMock(return_value=mock_coro()))
hass.data["cloud"] = MagicMock(login=AsyncMock())
req = await cloud_client.post(
"/api/cloud/login", json={"email": "my_username", "password": "my_password"}
@@ -184,7 +180,7 @@ async def test_login_view_unknown_error(cloud_client):
async def test_logout_view(hass, cloud_client):
"""Test logging out."""
cloud = hass.data["cloud"] = MagicMock()
cloud.logout.return_value = mock_coro()
cloud.logout = AsyncMock(return_value=None)
req = await cloud_client.post("/api/cloud/logout")
assert req.status == 200
data = await req.json()
@@ -450,8 +446,7 @@ async def test_websocket_subscription_not_logged_in(hass, hass_ws_client):
"""Test querying the status."""
client = await hass_ws_client(hass)
with patch(
"hass_nabucasa.Cloud.fetch_subscription_info",
return_value=mock_coro({"return": "value"}),
"hass_nabucasa.Cloud.fetch_subscription_info", return_value={"return": "value"},
):
await client.send_json({"id": 5, "type": "cloud/subscription"})
response = await client.receive_json()
@@ -529,7 +524,7 @@ async def test_enabling_webhook(hass, hass_ws_client, setup_api, mock_cloud_logi
"""Test we call right code to enable webhooks."""
client = await hass_ws_client(hass)
with patch(
"hass_nabucasa.cloudhooks.Cloudhooks.async_create", return_value=mock_coro()
"hass_nabucasa.cloudhooks.Cloudhooks.async_create", return_value={}
) as mock_enable:
await client.send_json(
{"id": 5, "type": "cloud/cloudhook/create", "webhook_id": "mock-webhook-id"}
@@ -544,9 +539,7 @@ async def test_enabling_webhook(hass, hass_ws_client, setup_api, mock_cloud_logi
async def test_disabling_webhook(hass, hass_ws_client, setup_api, mock_cloud_login):
"""Test we call right code to disable webhooks."""
client = await hass_ws_client(hass)
with patch(
"hass_nabucasa.cloudhooks.Cloudhooks.async_delete", return_value=mock_coro()
) as mock_disable:
with patch("hass_nabucasa.cloudhooks.Cloudhooks.async_delete") as mock_disable:
await client.send_json(
{"id": 5, "type": "cloud/cloudhook/delete", "webhook_id": "mock-webhook-id"}
)
@@ -562,9 +555,7 @@ async def test_enabling_remote(hass, hass_ws_client, setup_api, mock_cloud_login
client = await hass_ws_client(hass)
cloud = hass.data[DOMAIN]
with patch(
"hass_nabucasa.remote.RemoteUI.connect", return_value=mock_coro()
) as mock_connect:
with patch("hass_nabucasa.remote.RemoteUI.connect") as mock_connect:
await client.send_json({"id": 5, "type": "cloud/remote/connect"})
response = await client.receive_json()
assert response["success"]
@@ -578,9 +569,7 @@ async def test_disabling_remote(hass, hass_ws_client, setup_api, mock_cloud_logi
client = await hass_ws_client(hass)
cloud = hass.data[DOMAIN]
with patch(
"hass_nabucasa.remote.RemoteUI.disconnect", return_value=mock_coro()
) as mock_disconnect:
with patch("hass_nabucasa.remote.RemoteUI.disconnect") as mock_disconnect:
await client.send_json({"id": 5, "type": "cloud/remote/disconnect"})
response = await client.receive_json()
assert response["success"]
@@ -670,9 +659,7 @@ async def test_enabling_remote_trusted_networks_other(
client = await hass_ws_client(hass)
cloud = hass.data[DOMAIN]
with patch(
"hass_nabucasa.remote.RemoteUI.connect", return_value=mock_coro()
) as mock_connect:
with patch("hass_nabucasa.remote.RemoteUI.connect") as mock_connect:
await client.send_json({"id": 5, "type": "cloud/remote/connect"})
response = await client.receive_json()
@@ -885,7 +872,7 @@ async def test_thingtalk_convert(hass, hass_ws_client, setup_api):
with patch(
"homeassistant.components.cloud.http_api.thingtalk.async_convert",
return_value=mock_coro({"hello": "world"}),
return_value={"hello": "world"},
):
await client.send_json(
{"id": 5, "type": "cloud/thingtalk/convert", "query": "some-data"}