Add client metadata to cloud register (#63794)

This commit is contained in:
Joakim Sørensen 2022-01-10 14:49:25 +01:00 committed by GitHub
parent ca4cd4fbda
commit d1bb916070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 11 deletions

View File

@ -21,6 +21,7 @@ from homeassistant.components.google_assistant import helpers as google_helpers
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.components.websocket_api import const as ws_const
from homeassistant.util.location import async_detect_location_info
from .const import (
DOMAIN,
@ -220,8 +221,23 @@ class CloudRegisterView(HomeAssistantView):
hass = request.app["hass"]
cloud = hass.data[DOMAIN]
client_metadata = None
if location_info := await async_detect_location_info(
hass.helpers.aiohttp_client.async_get_clientsession()
):
client_metadata = {
"NC_COUNTRY_CODE": location_info.country_code,
"NC_REGION_CODE": location_info.region_code,
"NC_ZIP_CODE": location_info.zip_code,
}
async with async_timeout.timeout(REQUEST_TIMEOUT):
await cloud.auth.async_register(data["email"], data["password"])
await cloud.auth.async_register(
data["email"],
data["password"],
client_metadata=client_metadata,
)
return self.json_message("ok")

View File

@ -2,7 +2,7 @@
"domain": "cloud",
"name": "Home Assistant Cloud",
"documentation": "https://www.home-assistant.io/integrations/cloud",
"requirements": ["hass-nabucasa==0.50.0"],
"requirements": ["hass-nabucasa==0.51.0"],
"dependencies": ["http", "webhook"],
"after_dependencies": ["google_assistant", "alexa"],
"codeowners": ["@home-assistant/cloud"],

View File

@ -15,7 +15,7 @@ certifi>=2021.5.30
ciso8601==2.2.0
cryptography==35.0.0
emoji==1.5.0
hass-nabucasa==0.50.0
hass-nabucasa==0.51.0
home-assistant-frontend==20211229.0
httpx==0.21.0
ifaddr==0.1.7

View File

@ -806,7 +806,7 @@ habitipy==0.2.0
hangups==0.4.16
# homeassistant.components.cloud
hass-nabucasa==0.50.0
hass-nabucasa==0.51.0
# homeassistant.components.splunk
hass_splunk==0.1.1

View File

@ -516,7 +516,7 @@ habitipy==0.2.0
hangups==0.4.16
# homeassistant.components.cloud
hass-nabucasa==0.50.0
hass-nabucasa==0.51.0
# homeassistant.components.tasmota
hatasmota==0.3.1

View File

@ -15,6 +15,7 @@ from homeassistant.components.alexa.entities import LightCapabilities
from homeassistant.components.cloud.const import DOMAIN, RequireRelink
from homeassistant.components.google_assistant.helpers import GoogleEntity
from homeassistant.core import State
from homeassistant.util.location import LocationInfo
from . import mock_cloud, mock_cloud_prefs
@ -203,16 +204,60 @@ async def test_logout_view_unknown_error(hass, cloud_client):
assert req.status == HTTPStatus.BAD_GATEWAY
async def test_register_view(mock_cognito, cloud_client):
"""Test logging out."""
req = await cloud_client.post(
"/api/cloud/register", json={"email": "hello@bla.com", "password": "falcon42"}
)
async def test_register_view_no_location(mock_cognito, cloud_client):
"""Test register without location."""
with patch(
"homeassistant.components.cloud.http_api.async_detect_location_info",
return_value=None,
):
req = await cloud_client.post(
"/api/cloud/register",
json={"email": "hello@bla.com", "password": "falcon42"},
)
assert req.status == HTTPStatus.OK
assert len(mock_cognito.register.mock_calls) == 1
result_email, result_pass = mock_cognito.register.mock_calls[0][1]
call = mock_cognito.register.mock_calls[0]
result_email, result_pass = call.args
assert result_email == "hello@bla.com"
assert result_pass == "falcon42"
assert call.kwargs["client_metadata"] is None
async def test_register_view_with_location(mock_cognito, cloud_client):
"""Test register with location."""
with patch(
"homeassistant.components.cloud.http_api.async_detect_location_info",
return_value=LocationInfo(
**{
"country_code": "XX",
"zip_code": "12345",
"region_code": "GH",
"ip": "1.2.3.4",
"city": "Gotham",
"region_name": "Gotham",
"time_zone": "Earth/Gotham",
"currency": "XXX",
"latitude": "12.34567",
"longitude": "12.34567",
"use_metric": True,
}
),
):
req = await cloud_client.post(
"/api/cloud/register",
json={"email": "hello@bla.com", "password": "falcon42"},
)
assert req.status == HTTPStatus.OK
assert len(mock_cognito.register.mock_calls) == 1
call = mock_cognito.register.mock_calls[0]
result_email, result_pass = call.args
assert result_email == "hello@bla.com"
assert result_pass == "falcon42"
assert call.kwargs["client_metadata"] == {
"NC_COUNTRY_CODE": "XX",
"NC_REGION_CODE": "GH",
"NC_ZIP_CODE": "12345",
}
async def test_register_view_bad_data(mock_cognito, cloud_client):