|
7 | 7 |
|
8 | 8 | from nacl.encoding import Base64Encoder |
9 | 9 | from nacl.secret import SecretBox |
| 10 | +import pytest |
10 | 11 |
|
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 | +) |
12 | 17 | from homeassistant.const import CONF_WEBHOOK_ID |
13 | 18 | from homeassistant.core import HomeAssistant |
14 | 19 | from homeassistant.setup import async_setup_component |
@@ -101,6 +106,61 @@ async def test_registration_encryption( |
101 | 106 | assert json.loads(decrypted_data) == {"one": "Hello world"} |
102 | 107 |
|
103 | 108 |
|
| 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 | + |
104 | 164 | async def test_registration_encryption_legacy( |
105 | 165 | hass: HomeAssistant, hass_client: ClientSessionGenerator |
106 | 166 | ) -> None: |
|
0 commit comments