mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Add client metadata to cloud register (#63794)
This commit is contained in:
parent
ca4cd4fbda
commit
d1bb916070
@ -21,6 +21,7 @@ from homeassistant.components.google_assistant import helpers as google_helpers
|
|||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.components.http.data_validator import RequestDataValidator
|
from homeassistant.components.http.data_validator import RequestDataValidator
|
||||||
from homeassistant.components.websocket_api import const as ws_const
|
from homeassistant.components.websocket_api import const as ws_const
|
||||||
|
from homeassistant.util.location import async_detect_location_info
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -220,8 +221,23 @@ class CloudRegisterView(HomeAssistantView):
|
|||||||
hass = request.app["hass"]
|
hass = request.app["hass"]
|
||||||
cloud = hass.data[DOMAIN]
|
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):
|
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")
|
return self.json_message("ok")
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"domain": "cloud",
|
"domain": "cloud",
|
||||||
"name": "Home Assistant Cloud",
|
"name": "Home Assistant Cloud",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/cloud",
|
"documentation": "https://www.home-assistant.io/integrations/cloud",
|
||||||
"requirements": ["hass-nabucasa==0.50.0"],
|
"requirements": ["hass-nabucasa==0.51.0"],
|
||||||
"dependencies": ["http", "webhook"],
|
"dependencies": ["http", "webhook"],
|
||||||
"after_dependencies": ["google_assistant", "alexa"],
|
"after_dependencies": ["google_assistant", "alexa"],
|
||||||
"codeowners": ["@home-assistant/cloud"],
|
"codeowners": ["@home-assistant/cloud"],
|
||||||
|
@ -15,7 +15,7 @@ certifi>=2021.5.30
|
|||||||
ciso8601==2.2.0
|
ciso8601==2.2.0
|
||||||
cryptography==35.0.0
|
cryptography==35.0.0
|
||||||
emoji==1.5.0
|
emoji==1.5.0
|
||||||
hass-nabucasa==0.50.0
|
hass-nabucasa==0.51.0
|
||||||
home-assistant-frontend==20211229.0
|
home-assistant-frontend==20211229.0
|
||||||
httpx==0.21.0
|
httpx==0.21.0
|
||||||
ifaddr==0.1.7
|
ifaddr==0.1.7
|
||||||
|
@ -806,7 +806,7 @@ habitipy==0.2.0
|
|||||||
hangups==0.4.16
|
hangups==0.4.16
|
||||||
|
|
||||||
# homeassistant.components.cloud
|
# homeassistant.components.cloud
|
||||||
hass-nabucasa==0.50.0
|
hass-nabucasa==0.51.0
|
||||||
|
|
||||||
# homeassistant.components.splunk
|
# homeassistant.components.splunk
|
||||||
hass_splunk==0.1.1
|
hass_splunk==0.1.1
|
||||||
|
@ -516,7 +516,7 @@ habitipy==0.2.0
|
|||||||
hangups==0.4.16
|
hangups==0.4.16
|
||||||
|
|
||||||
# homeassistant.components.cloud
|
# homeassistant.components.cloud
|
||||||
hass-nabucasa==0.50.0
|
hass-nabucasa==0.51.0
|
||||||
|
|
||||||
# homeassistant.components.tasmota
|
# homeassistant.components.tasmota
|
||||||
hatasmota==0.3.1
|
hatasmota==0.3.1
|
||||||
|
@ -15,6 +15,7 @@ from homeassistant.components.alexa.entities import LightCapabilities
|
|||||||
from homeassistant.components.cloud.const import DOMAIN, RequireRelink
|
from homeassistant.components.cloud.const import DOMAIN, RequireRelink
|
||||||
from homeassistant.components.google_assistant.helpers import GoogleEntity
|
from homeassistant.components.google_assistant.helpers import GoogleEntity
|
||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
|
from homeassistant.util.location import LocationInfo
|
||||||
|
|
||||||
from . import mock_cloud, mock_cloud_prefs
|
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
|
assert req.status == HTTPStatus.BAD_GATEWAY
|
||||||
|
|
||||||
|
|
||||||
async def test_register_view(mock_cognito, cloud_client):
|
async def test_register_view_no_location(mock_cognito, cloud_client):
|
||||||
"""Test logging out."""
|
"""Test register without location."""
|
||||||
req = await cloud_client.post(
|
with patch(
|
||||||
"/api/cloud/register", json={"email": "hello@bla.com", "password": "falcon42"}
|
"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 req.status == HTTPStatus.OK
|
||||||
assert len(mock_cognito.register.mock_calls) == 1
|
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_email == "hello@bla.com"
|
||||||
assert result_pass == "falcon42"
|
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):
|
async def test_register_view_bad_data(mock_cognito, cloud_client):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user