2017-08-29 20:40:08 +00:00
|
|
|
"""Tests for the HTTP API for the cloud component."""
|
|
|
|
import asyncio
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
|
|
|
|
import pytest
|
2017-11-15 07:16:19 +00:00
|
|
|
from jose import jwt
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
from homeassistant.bootstrap import async_setup_component
|
2017-11-15 07:16:19 +00:00
|
|
|
from homeassistant.components.cloud import DOMAIN, auth_api, iot
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-10-15 02:43:14 +00:00
|
|
|
from tests.common import mock_coro
|
|
|
|
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2018-03-23 19:13:52 +00:00
|
|
|
GOOGLE_ACTIONS_SYNC_URL = 'https://api-test.hass.io/google_actions_sync'
|
|
|
|
|
|
|
|
|
2017-08-29 20:40:08 +00:00
|
|
|
@pytest.fixture
|
2018-03-15 20:49:49 +00:00
|
|
|
def cloud_client(hass, aiohttp_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Fixture that can fetch from the cloud client."""
|
2018-02-10 10:40:24 +00:00
|
|
|
with patch('homeassistant.components.cloud.Cloud.async_start',
|
|
|
|
return_value=mock_coro()):
|
2017-10-15 02:43:14 +00:00
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, 'cloud', {
|
|
|
|
'cloud': {
|
|
|
|
'mode': 'development',
|
|
|
|
'cognito_client_id': 'cognito_client_id',
|
|
|
|
'user_pool_id': 'user_pool_id',
|
|
|
|
'region': 'region',
|
|
|
|
'relayer': 'relayer',
|
2018-03-23 19:13:52 +00:00
|
|
|
'google_actions_sync_url': GOOGLE_ACTIONS_SYNC_URL,
|
2017-10-15 02:43:14 +00:00
|
|
|
}
|
|
|
|
}))
|
2018-01-03 18:16:59 +00:00
|
|
|
hass.data['cloud']._decode_claims = \
|
|
|
|
lambda token: jwt.get_unverified_claims(token)
|
2017-11-15 07:16:19 +00:00
|
|
|
with patch('homeassistant.components.cloud.Cloud.write_user_info'):
|
2018-03-15 20:49:49 +00:00
|
|
|
yield hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_cognito():
|
|
|
|
"""Mock warrant."""
|
|
|
|
with patch('homeassistant.components.cloud.auth_api._cognito') as mock_cog:
|
|
|
|
yield mock_cog()
|
|
|
|
|
|
|
|
|
2018-03-23 19:13:52 +00:00
|
|
|
async def test_google_actions_sync(mock_cognito, 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):
|
|
|
|
"""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')
|
|
|
|
assert req.status == 403
|
|
|
|
|
|
|
|
|
2017-08-29 20:40:08 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_account_view_no_account(cloud_client):
|
|
|
|
"""Test fetching account if no account available."""
|
|
|
|
req = yield from cloud_client.get('/api/cloud/account')
|
|
|
|
assert req.status == 400
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_account_view(hass, cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test fetching account if no account available."""
|
2017-11-15 07:16:19 +00:00
|
|
|
hass.data[DOMAIN].id_token = jwt.encode({
|
|
|
|
'email': 'hello@home-assistant.io',
|
|
|
|
'custom:sub-exp': '2018-01-03'
|
|
|
|
}, 'test')
|
|
|
|
hass.data[DOMAIN].iot.state = iot.STATE_CONNECTED
|
2017-08-29 20:40:08 +00:00
|
|
|
req = yield from cloud_client.get('/api/cloud/account')
|
|
|
|
assert req.status == 200
|
|
|
|
result = yield from req.json()
|
2017-11-15 07:16:19 +00:00
|
|
|
assert result == {
|
|
|
|
'email': 'hello@home-assistant.io',
|
|
|
|
'sub_exp': '2018-01-03',
|
|
|
|
'cloud': iot.STATE_CONNECTED,
|
|
|
|
}
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-11-15 07:16:19 +00:00
|
|
|
def test_login_view(hass, cloud_client, mock_cognito):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test logging in."""
|
2017-11-15 07:16:19 +00:00
|
|
|
mock_cognito.id_token = jwt.encode({
|
|
|
|
'email': 'hello@home-assistant.io',
|
|
|
|
'custom:sub-exp': '2018-01-03'
|
|
|
|
}, 'test')
|
|
|
|
mock_cognito.access_token = 'access_token'
|
|
|
|
mock_cognito.refresh_token = 'refresh_token'
|
|
|
|
|
|
|
|
with patch('homeassistant.components.cloud.iot.CloudIoT.'
|
|
|
|
'connect') as mock_connect, \
|
|
|
|
patch('homeassistant.components.cloud.auth_api._authenticate',
|
|
|
|
return_value=mock_cognito) as mock_auth:
|
2017-10-15 02:43:14 +00:00
|
|
|
req = yield from cloud_client.post('/api/cloud/login', json={
|
|
|
|
'email': 'my_username',
|
|
|
|
'password': 'my_password'
|
|
|
|
})
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
assert req.status == 200
|
|
|
|
result = yield from req.json()
|
2017-11-15 07:16:19 +00:00
|
|
|
assert result['email'] == 'hello@home-assistant.io'
|
|
|
|
assert result['sub_exp'] == '2018-01-03'
|
|
|
|
|
|
|
|
assert len(mock_connect.mock_calls) == 1
|
|
|
|
|
|
|
|
assert len(mock_auth.mock_calls) == 1
|
|
|
|
cloud, result_user, result_pass = mock_auth.mock_calls[0][1]
|
2017-09-12 16:47:04 +00:00
|
|
|
assert result_user == 'my_username'
|
|
|
|
assert result_pass == 'my_password'
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_login_view_invalid_json(cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Try logging in with invalid JSON."""
|
2017-10-15 02:43:14 +00:00
|
|
|
with patch('homeassistant.components.cloud.auth_api.login') as mock_login:
|
|
|
|
req = yield from cloud_client.post('/api/cloud/login', data='Not JSON')
|
2017-08-29 20:40:08 +00:00
|
|
|
assert req.status == 400
|
2017-10-15 02:43:14 +00:00
|
|
|
assert len(mock_login.mock_calls) == 0
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_login_view_invalid_schema(cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Try logging in with invalid schema."""
|
2017-10-15 02:43:14 +00:00
|
|
|
with patch('homeassistant.components.cloud.auth_api.login') as mock_login:
|
|
|
|
req = yield from cloud_client.post('/api/cloud/login', json={
|
|
|
|
'invalid': 'schema'
|
|
|
|
})
|
2017-08-29 20:40:08 +00:00
|
|
|
assert req.status == 400
|
2017-10-15 02:43:14 +00:00
|
|
|
assert len(mock_login.mock_calls) == 0
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_login_view_request_timeout(cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test request timeout while trying to log in."""
|
2017-10-15 02:43:14 +00:00
|
|
|
with patch('homeassistant.components.cloud.auth_api.login',
|
|
|
|
side_effect=asyncio.TimeoutError):
|
|
|
|
req = yield from cloud_client.post('/api/cloud/login', json={
|
|
|
|
'email': 'my_username',
|
|
|
|
'password': 'my_password'
|
|
|
|
})
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
assert req.status == 502
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_login_view_invalid_credentials(cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test logging in with invalid credentials."""
|
2017-10-15 02:43:14 +00:00
|
|
|
with patch('homeassistant.components.cloud.auth_api.login',
|
|
|
|
side_effect=auth_api.Unauthenticated):
|
|
|
|
req = yield from cloud_client.post('/api/cloud/login', json={
|
|
|
|
'email': 'my_username',
|
|
|
|
'password': 'my_password'
|
|
|
|
})
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
assert req.status == 401
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_login_view_unknown_error(cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test unknown error while logging in."""
|
2017-10-15 02:43:14 +00:00
|
|
|
with patch('homeassistant.components.cloud.auth_api.login',
|
|
|
|
side_effect=auth_api.UnknownError):
|
|
|
|
req = yield from cloud_client.post('/api/cloud/login', json={
|
|
|
|
'email': 'my_username',
|
|
|
|
'password': 'my_password'
|
|
|
|
})
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
assert req.status == 502
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_logout_view(hass, cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test logging out."""
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data['cloud'] = MagicMock()
|
|
|
|
cloud.logout.return_value = mock_coro()
|
2017-08-29 20:40:08 +00:00
|
|
|
req = yield from cloud_client.post('/api/cloud/logout')
|
|
|
|
assert req.status == 200
|
|
|
|
data = yield from req.json()
|
2017-09-12 16:47:04 +00:00
|
|
|
assert data == {'message': 'ok'}
|
2017-10-15 02:43:14 +00:00
|
|
|
assert len(cloud.logout.mock_calls) == 1
|
2017-08-29 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_logout_view_request_timeout(hass, cloud_client):
|
2017-08-29 20:40:08 +00:00
|
|
|
"""Test timeout while logging out."""
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data['cloud'] = MagicMock()
|
|
|
|
cloud.logout.side_effect = asyncio.TimeoutError
|
2017-09-12 16:47:04 +00:00
|
|
|
req = yield from cloud_client.post('/api/cloud/logout')
|
|
|
|
assert req.status == 502
|
|
|
|
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
@asyncio.coroutine
|
2017-10-15 02:43:14 +00:00
|
|
|
def test_logout_view_unknown_error(hass, cloud_client):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Test unknown error while logging out."""
|
2017-10-15 02:43:14 +00:00
|
|
|
cloud = hass.data['cloud'] = MagicMock()
|
|
|
|
cloud.logout.side_effect = auth_api.UnknownError
|
2017-08-29 20:40:08 +00:00
|
|
|
req = yield from cloud_client.post('/api/cloud/logout')
|
|
|
|
assert req.status == 502
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2017-09-12 16:47:04 +00:00
|
|
|
def test_register_view(mock_cognito, cloud_client):
|
|
|
|
"""Test logging out."""
|
|
|
|
req = yield from cloud_client.post('/api/cloud/register', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
'password': 'falcon42'
|
|
|
|
})
|
|
|
|
assert req.status == 200
|
|
|
|
assert len(mock_cognito.register.mock_calls) == 1
|
|
|
|
result_email, result_pass = mock_cognito.register.mock_calls[0][1]
|
2017-11-27 09:09:17 +00:00
|
|
|
assert result_email == 'hello@bla.com'
|
2017-09-12 16:47:04 +00:00
|
|
|
assert result_pass == 'falcon42'
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_register_view_bad_data(mock_cognito, cloud_client):
|
|
|
|
"""Test logging out."""
|
|
|
|
req = yield from cloud_client.post('/api/cloud/register', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
'not_password': 'falcon'
|
|
|
|
})
|
|
|
|
assert req.status == 400
|
|
|
|
assert len(mock_cognito.logout.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_register_view_request_timeout(mock_cognito, cloud_client):
|
|
|
|
"""Test timeout while logging out."""
|
|
|
|
mock_cognito.register.side_effect = asyncio.TimeoutError
|
|
|
|
req = yield from cloud_client.post('/api/cloud/register', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
'password': 'falcon42'
|
|
|
|
})
|
|
|
|
assert req.status == 502
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_register_view_unknown_error(mock_cognito, cloud_client):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Test unknown error while logging out."""
|
2017-09-12 16:47:04 +00:00
|
|
|
mock_cognito.register.side_effect = auth_api.UnknownError
|
|
|
|
req = yield from cloud_client.post('/api/cloud/register', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
'password': 'falcon42'
|
|
|
|
})
|
|
|
|
assert req.status == 502
|
2017-08-29 20:40:08 +00:00
|
|
|
|
2017-09-12 16:47:04 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_forgot_password_view(mock_cognito, cloud_client):
|
|
|
|
"""Test logging out."""
|
|
|
|
req = yield from cloud_client.post('/api/cloud/forgot_password', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 200
|
|
|
|
assert len(mock_cognito.initiate_forgot_password.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_forgot_password_view_bad_data(mock_cognito, cloud_client):
|
|
|
|
"""Test logging out."""
|
|
|
|
req = yield from cloud_client.post('/api/cloud/forgot_password', json={
|
|
|
|
'not_email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 400
|
|
|
|
assert len(mock_cognito.initiate_forgot_password.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_forgot_password_view_request_timeout(mock_cognito, cloud_client):
|
|
|
|
"""Test timeout while logging out."""
|
|
|
|
mock_cognito.initiate_forgot_password.side_effect = asyncio.TimeoutError
|
|
|
|
req = yield from cloud_client.post('/api/cloud/forgot_password', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 502
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_forgot_password_view_unknown_error(mock_cognito, cloud_client):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Test unknown error while logging out."""
|
2017-09-12 16:47:04 +00:00
|
|
|
mock_cognito.initiate_forgot_password.side_effect = auth_api.UnknownError
|
|
|
|
req = yield from cloud_client.post('/api/cloud/forgot_password', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 502
|
|
|
|
|
|
|
|
|
2017-12-29 13:46:10 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_resend_confirm_view(mock_cognito, cloud_client):
|
|
|
|
"""Test logging out."""
|
|
|
|
req = yield from cloud_client.post('/api/cloud/resend_confirm', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 200
|
|
|
|
assert len(mock_cognito.client.resend_confirmation_code.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_resend_confirm_view_bad_data(mock_cognito, cloud_client):
|
|
|
|
"""Test logging out."""
|
|
|
|
req = yield from cloud_client.post('/api/cloud/resend_confirm', json={
|
|
|
|
'not_email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 400
|
|
|
|
assert len(mock_cognito.client.resend_confirmation_code.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_resend_confirm_view_request_timeout(mock_cognito, cloud_client):
|
|
|
|
"""Test timeout while logging out."""
|
|
|
|
mock_cognito.client.resend_confirmation_code.side_effect = \
|
|
|
|
asyncio.TimeoutError
|
|
|
|
req = yield from cloud_client.post('/api/cloud/resend_confirm', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 502
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_resend_confirm_view_unknown_error(mock_cognito, cloud_client):
|
|
|
|
"""Test unknown error while logging out."""
|
|
|
|
mock_cognito.client.resend_confirmation_code.side_effect = \
|
|
|
|
auth_api.UnknownError
|
|
|
|
req = yield from cloud_client.post('/api/cloud/resend_confirm', json={
|
|
|
|
'email': 'hello@bla.com',
|
|
|
|
})
|
|
|
|
assert req.status == 502
|