Move request sync logic into GoogleConfig (#28227)

* Move request sync logic into GoogleConfig

* Return http status code for request_sync same as cloud

* agent_user_id is not optional for async_sync_entities now

* No need in checking parameter here

* Adjust some things for cloud tests

* Adjust some more stuff for cloud tests

* Drop uneccessary else

* Black required change

* Let async_schedule_google_sync take agent_user_id

* Assert return value on api call

* Test old api key method

* Update homeassistant/components/google_assistant/helpers.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Joakim PlateandPaulus Schoutsen authored and GitHub committed 2019-11-26 22:47:13 +01:00
1 parent 5f1b0fb15c
commit 06231e7ac2
8 files changed
+121 -86

No files matched your search

+13 -3
View File
@@ -12,7 +12,12 @@ from tests.common import mock_coro, async_fire_time_changed
async def test_google_update_report_state(hass, cloud_prefs):
"""Test Google config responds to updating preference."""
config = CloudGoogleConfig(hass, GACTIONS_SCHEMA({}), cloud_prefs, None)
config = CloudGoogleConfig(
hass,
GACTIONS_SCHEMA({}),
cloud_prefs,
Mock(claims={"cognito:username": "abcdefghjkl"}),
)
with patch.object(
config, "async_sync_entities", side_effect=mock_coro
@@ -39,12 +44,17 @@ async def test_sync_entities(aioclient_mock, hass, cloud_prefs):
),
)
assert await config.async_sync_entities() == 404
assert await config.async_sync_entities("user") == 404
async def test_google_update_expose_trigger_sync(hass, cloud_prefs):
"""Test Google config responds to updating exposed entities."""
config = CloudGoogleConfig(hass, GACTIONS_SCHEMA({}), cloud_prefs, None)
config = CloudGoogleConfig(
hass,
GACTIONS_SCHEMA({}),
cloud_prefs,
Mock(claims={"cognito:username": "abcdefghjkl"}),
)
with patch.object(
config, "async_sync_entities", side_effect=mock_coro
+6 -2
View File
@@ -85,14 +85,18 @@ def mock_cognito():
yield mock_cog()
async def test_google_actions_sync(mock_cognito, cloud_client, aioclient_mock):
async def test_google_actions_sync(
mock_cognito, mock_cloud_login, cloud_client, aioclient_mock
):
"""Test syncing Google Actions."""
aioclient_mock.post(GOOGLE_ACTIONS_SYNC_URL)
req = await cloud_client.post("/api/cloud/google_actions/sync")
assert req.status == 200
async def test_google_actions_sync_fails(mock_cognito, cloud_client, aioclient_mock):
async def test_google_actions_sync_fails(
mock_cognito, mock_cloud_login, cloud_client, aioclient_mock
):
"""Test syncing Google Actions gone bad."""
aioclient_mock.post(GOOGLE_ACTIONS_SYNC_URL, status=403)
req = await cloud_client.post("/api/cloud/google_actions/sync")
+30 -1
View File
@@ -106,7 +106,8 @@ async def test_call_homegraph_api(hass, aioclient_mock, hass_storage):
aioclient_mock.post(MOCK_URL, status=200, json={})
await config.async_call_homegraph_api(MOCK_URL, MOCK_JSON)
res = await config.async_call_homegraph_api(MOCK_URL, MOCK_JSON)
assert res == 200
assert mock_get_token.call_count == 1
assert aioclient_mock.call_count == 1
@@ -139,6 +140,34 @@ async def test_call_homegraph_api_retry(hass, aioclient_mock, hass_storage):
assert call[3] == MOCK_HEADER
async def test_call_homegraph_api_key(hass, aioclient_mock, hass_storage):
"""Test the function to call the homegraph api."""
config = GoogleConfig(
hass, GOOGLE_ASSISTANT_SCHEMA({"project_id": "1234", "api_key": "dummy_key"})
)
aioclient_mock.post(MOCK_URL, status=200, json={})
res = await config.async_call_homegraph_api_key(MOCK_URL, MOCK_JSON)
assert res == 200
assert aioclient_mock.call_count == 1
call = aioclient_mock.mock_calls[0]
assert call[1].query == {"key": "dummy_key"}
assert call[2] == MOCK_JSON
async def test_call_homegraph_api_key_fail(hass, aioclient_mock, hass_storage):
"""Test the function to call the homegraph api."""
config = GoogleConfig(
hass, GOOGLE_ASSISTANT_SCHEMA({"project_id": "1234", "api_key": "dummy_key"})
)
aioclient_mock.post(MOCK_URL, status=666, json={})
res = await config.async_call_homegraph_api_key(MOCK_URL, MOCK_JSON)
assert res == 666
assert aioclient_mock.call_count == 1
async def test_report_state(hass, aioclient_mock, hass_storage):
"""Test the report state function."""
config = GoogleConfig(hass, DUMMY_CONFIG)