Skip to content

Commit 19fd800

Browse files
authored
Add connection check before registering cloudhook URL (home-assistant#160284)
1 parent 8e30787 commit 19fd800

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

homeassistant/components/mobile_app/http_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def post(self, request: Request, data: dict) -> Response:
6969

7070
webhook_id = secrets.token_hex()
7171

72-
if cloud.async_active_subscription(hass):
72+
if cloud.async_active_subscription(hass) and cloud.async_is_connected(hass):
7373
data[CONF_CLOUDHOOK_URL] = await async_create_cloud_hook(
7474
hass, webhook_id, None
7575
)

tests/components/mobile_app/test_http_api.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77

88
from nacl.encoding import Base64Encoder
99
from nacl.secret import SecretBox
10+
import pytest
1011

11-
from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN
12+
from homeassistant.components.mobile_app.const import (
13+
CONF_CLOUDHOOK_URL,
14+
CONF_SECRET,
15+
DOMAIN,
16+
)
1217
from homeassistant.const import CONF_WEBHOOK_ID
1318
from homeassistant.core import HomeAssistant
1419
from homeassistant.setup import async_setup_component
@@ -101,6 +106,61 @@ async def test_registration_encryption(
101106
assert json.loads(decrypted_data) == {"one": "Hello world"}
102107

103108

109+
@pytest.mark.parametrize(
110+
"cloud_is_connected",
111+
[
112+
True,
113+
False,
114+
],
115+
)
116+
async def test_registration_with_cloud(
117+
hass: HomeAssistant,
118+
hass_client: ClientSessionGenerator,
119+
hass_admin_user: MockUser,
120+
cloud_is_connected: bool,
121+
) -> None:
122+
"""Test that cloudhook_url is only returned when cloud is connected."""
123+
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
124+
125+
api_client = await hass_client()
126+
127+
cloudhook_url = "https://hooks.nabu.casa/test123"
128+
129+
with (
130+
patch(
131+
"homeassistant.components.mobile_app.http_api.cloud.async_active_subscription",
132+
return_value=True,
133+
),
134+
patch(
135+
"homeassistant.components.mobile_app.http_api.cloud.async_is_connected",
136+
return_value=cloud_is_connected,
137+
),
138+
patch(
139+
"homeassistant.components.mobile_app.http_api.async_create_cloud_hook",
140+
return_value=cloudhook_url,
141+
),
142+
patch(
143+
"homeassistant.components.mobile_app.http_api.cloud.async_remote_ui_url",
144+
return_value="https://remote.ui",
145+
),
146+
patch(
147+
"homeassistant.components.person.async_add_user_device_tracker",
148+
spec=True,
149+
),
150+
):
151+
resp = await api_client.post(
152+
"/api/mobile_app/registrations", json=REGISTER_CLEARTEXT
153+
)
154+
155+
assert resp.status == HTTPStatus.CREATED
156+
register_json = await resp.json()
157+
assert CONF_WEBHOOK_ID in register_json
158+
159+
assert register_json.get(CONF_CLOUDHOOK_URL) == (
160+
cloudhook_url if cloud_is_connected else None
161+
)
162+
163+
104164
async def test_registration_encryption_legacy(
105165
hass: HomeAssistant, hass_client: ClientSessionGenerator
106166
) -> None:

0 commit comments

Comments
 (0)