2020-08-10 12:19:38 +00:00
|
|
|
"""Test the flo config flow."""
|
2021-10-22 20:10:47 +00:00
|
|
|
from http import HTTPStatus
|
2020-08-11 12:13:40 +00:00
|
|
|
import json
|
|
|
|
import time
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2020-08-11 12:13:40 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries
|
2020-08-10 12:19:38 +00:00
|
|
|
from homeassistant.components.flo.const import DOMAIN
|
2020-09-23 18:21:55 +00:00
|
|
|
from homeassistant.const import CONTENT_TYPE_JSON
|
2020-08-10 12:19:38 +00:00
|
|
|
|
2020-08-11 12:13:40 +00:00
|
|
|
from .common import TEST_EMAIL_ADDRESS, TEST_PASSWORD, TEST_TOKEN, TEST_USER_ID
|
|
|
|
|
2020-08-10 12:19:38 +00:00
|
|
|
|
|
|
|
async def test_form(hass, aioclient_mock_fixture):
|
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-08-10 12:19:38 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.flo.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-11 12:13:40 +00:00
|
|
|
result["flow_id"], {"username": TEST_USER_ID, "password": TEST_PASSWORD}
|
2020-08-10 12:19:38 +00:00
|
|
|
)
|
|
|
|
|
2020-08-11 12:13:40 +00:00
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == "Home"
|
|
|
|
assert result2["data"] == {"username": TEST_USER_ID, "password": TEST_PASSWORD}
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2020-08-10 12:19:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_form_cannot_connect(hass, aioclient_mock):
|
|
|
|
"""Test we handle cannot connect error."""
|
2020-08-11 12:13:40 +00:00
|
|
|
now = round(time.time())
|
|
|
|
# Mocks a failed login response for flo.
|
|
|
|
aioclient_mock.post(
|
|
|
|
"https://api.meetflo.com/api/v1/users/auth",
|
|
|
|
json=json.dumps(
|
|
|
|
{
|
|
|
|
"token": TEST_TOKEN,
|
|
|
|
"tokenPayload": {
|
|
|
|
"user": {"user_id": TEST_USER_ID, "email": TEST_EMAIL_ADDRESS},
|
|
|
|
"timestamp": now,
|
|
|
|
},
|
|
|
|
"tokenExpiration": 86400,
|
|
|
|
"timeNow": now,
|
|
|
|
}
|
|
|
|
),
|
2020-09-23 18:21:55 +00:00
|
|
|
headers={"Content-Type": CONTENT_TYPE_JSON},
|
2021-10-22 20:10:47 +00:00
|
|
|
status=HTTPStatus.BAD_REQUEST,
|
2020-08-11 12:13:40 +00:00
|
|
|
)
|
2020-08-10 12:19:38 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], {"username": "test-username", "password": "test-password"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|