core/tests/components/hangouts/test_config_flow.py

117 lines
3.8 KiB
Python
Raw Normal View History

"""Tests for the Google Hangouts config flow."""
from unittest.mock import patch
from homeassistant import data_entry_flow
from homeassistant.components.hangouts import config_flow
async def test_flow_works(hass, aioclient_mock):
"""Test config flow without 2fa."""
flow = config_flow.HangoutsFlowHandler()
flow.hass = hass
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth"):
result = await flow.async_step_user(
2019-07-31 19:25:30 +00:00
{"email": "test@test.com", "password": "1232456"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test@test.com"
async def test_flow_works_with_authcode(hass, aioclient_mock):
"""Test config flow without 2fa."""
flow = config_flow.HangoutsFlowHandler()
flow.hass = hass
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth"):
result = await flow.async_step_user(
2019-07-31 19:25:30 +00:00
{
"email": "test@test.com",
"password": "1232456",
"authorization_code": "c29tZXJhbmRvbXN0cmluZw==",
}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test@test.com"
async def test_flow_works_with_2fa(hass, aioclient_mock):
"""Test config flow with 2fa."""
from homeassistant.components.hangouts.hangups_utils import Google2FAError
flow = config_flow.HangoutsFlowHandler()
flow.hass = hass
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth", side_effect=Google2FAError):
result = await flow.async_step_user(
2019-07-31 19:25:30 +00:00
{"email": "test@test.com", "password": "1232456"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "2fa"
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth"):
result = await flow.async_step_2fa({"2fa": 123456})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test@test.com"
async def test_flow_with_unknown_2fa(hass, aioclient_mock):
"""Test config flow with invalid 2fa method."""
from homeassistant.components.hangouts.hangups_utils import GoogleAuthError
flow = config_flow.HangoutsFlowHandler()
flow.hass = hass
2019-07-31 19:25:30 +00:00
with patch(
"hangups.get_auth",
side_effect=GoogleAuthError("Unknown verification code input"),
):
result = await flow.async_step_user(
2019-07-31 19:25:30 +00:00
{"email": "test@test.com", "password": "1232456"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"]["base"] == "invalid_2fa_method"
async def test_flow_invalid_login(hass, aioclient_mock):
"""Test config flow with invalid 2fa method."""
from homeassistant.components.hangouts.hangups_utils import GoogleAuthError
flow = config_flow.HangoutsFlowHandler()
flow.hass = hass
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth", side_effect=GoogleAuthError):
result = await flow.async_step_user(
2019-07-31 19:25:30 +00:00
{"email": "test@test.com", "password": "1232456"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"]["base"] == "invalid_login"
async def test_flow_invalid_2fa(hass, aioclient_mock):
"""Test config flow with 2fa."""
from homeassistant.components.hangouts.hangups_utils import Google2FAError
flow = config_flow.HangoutsFlowHandler()
flow.hass = hass
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth", side_effect=Google2FAError):
result = await flow.async_step_user(
2019-07-31 19:25:30 +00:00
{"email": "test@test.com", "password": "1232456"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "2fa"
2019-07-31 19:25:30 +00:00
with patch("hangups.get_auth", side_effect=Google2FAError):
result = await flow.async_step_2fa({"2fa": 123456})
2019-07-31 19:25:30 +00:00
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"]["base"] == "invalid_2fa"