Allow unloading mobile app (#30995)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-01-22 09:57:47 -08:00
1 parent 3c44a1353a
commit e5365779fe
6 files changed
+86 -206

No files matched your search

+8 -4
View File
@@ -19,6 +19,8 @@ def registry(hass):
@pytest.fixture
async def create_registrations(hass, authed_api_client):
"""Return two new registrations."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
enc_reg = await authed_api_client.post(
"/api/mobile_app/registrations", json=REGISTER
)
@@ -39,11 +41,13 @@ async def create_registrations(hass, authed_api_client):
@pytest.fixture
async def webhook_client(hass, aiohttp_client):
async def webhook_client(hass, authed_api_client, aiohttp_client):
"""mobile_app mock client."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
return await aiohttp_client(hass.http.app)
# We pass in the authed_api_client server instance because
# it is used inside create_registrations and just passing in
# the app instance would cause the server to start twice,
# which caused deprecation warnings to be printed.
return await aiohttp_client(authed_api_client.server)
@pytest.fixture
+37
View File
@@ -0,0 +1,37 @@
"""Tests for the mobile app integration."""
from homeassistant.components.mobile_app.const import DATA_DELETED_IDS, DOMAIN
from .const import CALL_SERVICE
from tests.common import async_mock_service
async def test_unload_unloads(hass, create_registrations, webhook_client):
"""Test we clean up when we unload."""
# Second config entry is the one without encryption
config_entry = hass.config_entries.async_entries("mobile_app")[1]
webhook_id = config_entry.data["webhook_id"]
calls = async_mock_service(hass, "test", "mobile_app")
# Test it works
await webhook_client.post(f"/api/webhook/{webhook_id}", json=CALL_SERVICE)
assert len(calls) == 1
await hass.config_entries.async_unload(config_entry.entry_id)
# Test it no longer works
await webhook_client.post(f"/api/webhook/{webhook_id}", json=CALL_SERVICE)
assert len(calls) == 1
async def test_remove_entry(hass, create_registrations):
"""Test we clean up when we remove entry."""
for config_entry in hass.config_entries.async_entries("mobile_app"):
await hass.config_entries.async_remove(config_entry.entry_id)
assert config_entry.data["webhook_id"] in hass.data[DOMAIN][DATA_DELETED_IDS]
dev_reg = await hass.helpers.device_registry.async_get_registry()
assert len(dev_reg.devices) == 0
ent_reg = await hass.helpers.entity_registry.async_get_registry()
assert len(ent_reg.entities) == 0
+1 -2
View File
@@ -67,9 +67,8 @@ async def test_webhook_handle_fire_event(hass, create_registrations, webhook_cli
assert events[0].data["hello"] == "yo world"
async def test_webhook_update_registration(webhook_client, hass_client):
async def test_webhook_update_registration(webhook_client, authed_api_client):
"""Test that a we can update an existing registration via webhook."""
authed_api_client = await hass_client()
register_resp = await authed_api_client.post(
"/api/mobile_app/registrations", json=REGISTER_CLEARTEXT
)
@@ -1,76 +0,0 @@
"""Test the mobile_app websocket API."""
# pylint: disable=redefined-outer-name,unused-import
from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN
from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.setup import async_setup_component
from .const import CALL_SERVICE, REGISTER
async def test_webocket_get_user_registrations(
hass, aiohttp_client, hass_ws_client, hass_read_only_access_token
):
"""Test get_user_registrations websocket command from admin perspective."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
user_api_client = await aiohttp_client(
hass.http.app,
headers={"Authorization": "Bearer {}".format(hass_read_only_access_token)},
)
# First a read only user registers.
register_resp = await user_api_client.post(
"/api/mobile_app/registrations", json=REGISTER
)
assert register_resp.status == 201
register_json = await register_resp.json()
assert CONF_WEBHOOK_ID in register_json
assert CONF_SECRET in register_json
# Then the admin user attempts to access it.
client = await hass_ws_client(hass)
await client.send_json({"id": 5, "type": "mobile_app/get_user_registrations"})
msg = await client.receive_json()
assert msg["id"] == 5
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert len(msg["result"]) == 1
async def test_webocket_delete_registration(
hass, hass_client, hass_ws_client, webhook_client
):
"""Test delete_registration websocket command."""
authed_api_client = await hass_client() # noqa: F811
register_resp = await authed_api_client.post(
"/api/mobile_app/registrations", json=REGISTER
)
assert register_resp.status == 201
register_json = await register_resp.json()
assert CONF_WEBHOOK_ID in register_json
assert CONF_SECRET in register_json
webhook_id = register_json[CONF_WEBHOOK_ID]
client = await hass_ws_client(hass)
await client.send_json(
{"id": 5, "type": "mobile_app/delete_registration", CONF_WEBHOOK_ID: webhook_id}
)
msg = await client.receive_json()
assert msg["id"] == 5
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert msg["result"] == "ok"
ensure_four_ten_gone = await webhook_client.post(
"/api/webhook/{}".format(webhook_id), json=CALL_SERVICE
)
assert ensure_four_ten_gone.status == 410