2019-02-17 20:41:55 +00:00
|
|
|
"""Define tests for the PlayStation 4 config flow."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
from pyps4_2ndscreen.errors import CredentialTimeout
|
|
|
|
|
2019-02-17 20:41:55 +00:00
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components import ps4
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.ps4.const import DEFAULT_NAME, DEFAULT_REGION
|
2019-02-17 20:41:55 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CODE,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_IP_ADDRESS,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_REGION,
|
|
|
|
CONF_TOKEN,
|
|
|
|
)
|
2019-04-23 23:32:36 +00:00
|
|
|
from homeassistant.util import location
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MOCK_TITLE = "PlayStation 4"
|
|
|
|
MOCK_CODE = "12345678"
|
|
|
|
MOCK_CREDS = "000aa000"
|
|
|
|
MOCK_HOST = "192.0.0.0"
|
|
|
|
MOCK_HOST_ADDITIONAL = "192.0.0.1"
|
2019-02-17 20:41:55 +00:00
|
|
|
MOCK_DEVICE = {
|
|
|
|
CONF_HOST: MOCK_HOST,
|
|
|
|
CONF_NAME: DEFAULT_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_REGION: DEFAULT_REGION,
|
2019-02-17 20:41:55 +00:00
|
|
|
}
|
2019-03-05 00:48:25 +00:00
|
|
|
MOCK_DEVICE_ADDITIONAL = {
|
|
|
|
CONF_HOST: MOCK_HOST_ADDITIONAL,
|
|
|
|
CONF_NAME: DEFAULT_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_REGION: DEFAULT_REGION,
|
2019-03-05 00:48:25 +00:00
|
|
|
}
|
2019-02-17 20:41:55 +00:00
|
|
|
MOCK_CONFIG = {
|
|
|
|
CONF_IP_ADDRESS: MOCK_HOST,
|
|
|
|
CONF_NAME: DEFAULT_NAME,
|
|
|
|
CONF_REGION: DEFAULT_REGION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CODE: MOCK_CODE,
|
2019-02-17 20:41:55 +00:00
|
|
|
}
|
2019-03-05 00:48:25 +00:00
|
|
|
MOCK_CONFIG_ADDITIONAL = {
|
|
|
|
CONF_IP_ADDRESS: MOCK_HOST_ADDITIONAL,
|
|
|
|
CONF_NAME: DEFAULT_NAME,
|
|
|
|
CONF_REGION: DEFAULT_REGION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CODE: MOCK_CODE,
|
2019-02-17 20:41:55 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
MOCK_DATA = {CONF_TOKEN: MOCK_CREDS, "devices": [MOCK_DEVICE]}
|
2019-02-17 20:41:55 +00:00
|
|
|
MOCK_UDP_PORT = int(987)
|
|
|
|
MOCK_TCP_PORT = int(997)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MOCK_AUTO = {"Config Mode": "Auto Discover"}
|
|
|
|
MOCK_MANUAL = {"Config Mode": "Manual Entry", CONF_IP_ADDRESS: MOCK_HOST}
|
2019-03-25 12:25:15 +00:00
|
|
|
|
2019-04-23 23:32:36 +00:00
|
|
|
MOCK_LOCATION = location.LocationInfo(
|
2019-07-31 19:25:30 +00:00
|
|
|
"0.0.0.0",
|
|
|
|
"US",
|
|
|
|
"United States",
|
|
|
|
"CA",
|
|
|
|
"California",
|
|
|
|
"San Diego",
|
|
|
|
"92122",
|
|
|
|
"America/Los_Angeles",
|
|
|
|
32.8594,
|
|
|
|
-117.2073,
|
|
|
|
True,
|
|
|
|
)
|
2019-04-23 23:32:36 +00:00
|
|
|
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
async def test_full_flow_implementation(hass):
|
|
|
|
"""Test registering an implementation and flow works."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
2019-04-23 23:32:36 +00:00
|
|
|
flow.location = MOCK_LOCATION
|
2019-03-05 00:48:25 +00:00
|
|
|
manager = hass.config_entries
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
# User Step Started, results in Step Creds
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None):
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "creds"
|
2019-02-17 20:41:55 +00:00
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
# Step Creds results with form in Step Mode.
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS):
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_creds({})
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "mode"
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
# Step Mode with User Input which is not manual, results in Step Link.
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_mode(MOCK_AUTO)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
# User Input results in created entry.
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)), patch(
|
|
|
|
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_link(MOCK_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["data"][CONF_TOKEN] == MOCK_CREDS
|
|
|
|
assert result["data"]["devices"] == [MOCK_DEVICE]
|
|
|
|
assert result["title"] == MOCK_TITLE
|
2019-03-05 00:48:25 +00:00
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-03-05 00:48:25 +00:00
|
|
|
# Add entry using result data.
|
|
|
|
mock_data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TOKEN: result["data"][CONF_TOKEN],
|
|
|
|
"devices": result["data"]["devices"],
|
|
|
|
}
|
2019-03-05 00:48:25 +00:00
|
|
|
entry = MockConfigEntry(domain=ps4.DOMAIN, data=mock_data)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
# Check if entry exists.
|
|
|
|
assert len(manager.async_entries()) == 1
|
|
|
|
# Check if there is a device config in entry.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert len(entry.data["devices"]) == 1
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_multiple_flow_implementation(hass):
|
|
|
|
"""Test multiple device flows."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
2019-04-23 23:32:36 +00:00
|
|
|
flow.location = MOCK_LOCATION
|
2019-03-05 00:48:25 +00:00
|
|
|
manager = hass.config_entries
|
|
|
|
|
|
|
|
# User Step Started, results in Step Creds
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None):
|
2019-03-05 00:48:25 +00:00
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "creds"
|
2019-03-05 00:48:25 +00:00
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
# Step Creds results with form in Step Mode.
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS):
|
2019-03-05 00:48:25 +00:00
|
|
|
result = await flow.async_step_creds({})
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "mode"
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
# Step Mode with User Input which is not manual, results in Step Link.
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}],
|
|
|
|
):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_mode(MOCK_AUTO)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
# User Input results in created entry.
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)), patch(
|
|
|
|
"pyps4_2ndscreen.Helper.has_devices",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}],
|
|
|
|
):
|
2019-03-05 00:48:25 +00:00
|
|
|
result = await flow.async_step_link(MOCK_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["data"][CONF_TOKEN] == MOCK_CREDS
|
|
|
|
assert result["data"]["devices"] == [MOCK_DEVICE]
|
|
|
|
assert result["title"] == MOCK_TITLE
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Add entry using result data.
|
|
|
|
mock_data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TOKEN: result["data"][CONF_TOKEN],
|
|
|
|
"devices": result["data"]["devices"],
|
|
|
|
}
|
2019-03-05 00:48:25 +00:00
|
|
|
entry = MockConfigEntry(domain=ps4.DOMAIN, data=mock_data)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
# Check if entry exists.
|
|
|
|
assert len(manager.async_entries()) == 1
|
|
|
|
# Check if there is a device config in entry.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert len(entry.data["devices"]) == 1
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
# Test additional flow.
|
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
# User Step Started, results in Step Mode:
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None), patch(
|
|
|
|
"pyps4_2ndscreen.Helper.has_devices",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}],
|
|
|
|
):
|
2019-03-05 00:48:25 +00:00
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "creds"
|
2019-04-23 23:32:36 +00:00
|
|
|
|
|
|
|
# Step Creds results with form in Step Mode.
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS):
|
2019-04-23 23:32:36 +00:00
|
|
|
result = await flow.async_step_creds({})
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "mode"
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
# Step Mode with User Input which is not manual, results in Step Link.
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}],
|
|
|
|
):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_mode(MOCK_AUTO)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
# Step Link
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}],
|
2019-10-06 12:43:34 +00:00
|
|
|
), patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_link(MOCK_CONFIG_ADDITIONAL)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["data"][CONF_TOKEN] == MOCK_CREDS
|
|
|
|
assert len(result["data"]["devices"]) == 1
|
|
|
|
assert result["title"] == MOCK_TITLE
|
2019-03-05 00:48:25 +00:00
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-03-05 00:48:25 +00:00
|
|
|
mock_data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TOKEN: result["data"][CONF_TOKEN],
|
|
|
|
"devices": result["data"]["devices"],
|
|
|
|
}
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
# Update config entries with result data
|
|
|
|
entry = MockConfigEntry(domain=ps4.DOMAIN, data=mock_data)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
manager.async_update_entry(entry)
|
|
|
|
|
|
|
|
# Check if there are 2 entries.
|
|
|
|
assert len(manager.async_entries()) == 2
|
|
|
|
# Check if there is device config in entry.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert len(entry.data["devices"]) == 1
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_port_bind_abort(hass):
|
|
|
|
"""Test that flow aborted when cannot bind to ports 987, 997."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.port_bind", return_value=MOCK_UDP_PORT):
|
2019-07-31 19:25:30 +00:00
|
|
|
reason = "port_987_bind_error"
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_user(user_input=None)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == reason
|
2019-02-17 20:41:55 +00:00
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.port_bind", return_value=MOCK_TCP_PORT):
|
2019-07-31 19:25:30 +00:00
|
|
|
reason = "port_997_bind_error"
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_user(user_input=None)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == reason
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_duplicate_abort(hass):
|
2019-03-05 00:48:25 +00:00
|
|
|
"""Test that Flow aborts when found devices already configured."""
|
2019-02-17 20:41:55 +00:00
|
|
|
MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA).add_to_hass(hass)
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
2019-04-23 23:32:36 +00:00
|
|
|
flow.creds = MOCK_CREDS
|
2019-02-17 20:41:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-03-05 00:48:25 +00:00
|
|
|
result = await flow.async_step_link(user_input=None)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "devices_configured"
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
|
2019-03-05 00:48:25 +00:00
|
|
|
async def test_additional_device(hass):
|
|
|
|
"""Test that Flow can configure another device."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
flow.creds = MOCK_CREDS
|
|
|
|
manager = hass.config_entries
|
|
|
|
|
|
|
|
# Mock existing entry.
|
|
|
|
entry = MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
# Check that only 1 entry exists
|
|
|
|
assert len(manager.async_entries()) == 1
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices",
|
2019-07-31 19:25:30 +00:00
|
|
|
return_value=[{"host-ip": MOCK_HOST}, {"host-ip": MOCK_HOST_ADDITIONAL}],
|
2019-10-06 12:43:34 +00:00
|
|
|
), patch("pyps4_2ndscreen.Helper.link", return_value=(True, True)):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_link(MOCK_CONFIG_ADDITIONAL)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["data"][CONF_TOKEN] == MOCK_CREDS
|
|
|
|
assert len(result["data"]["devices"]) == 1
|
|
|
|
assert result["title"] == MOCK_TITLE
|
2019-03-05 00:48:25 +00:00
|
|
|
|
|
|
|
# Add New Entry
|
|
|
|
entry = MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
# Check that there are 2 entries
|
|
|
|
assert len(manager.async_entries()) == 2
|
|
|
|
|
|
|
|
|
2019-02-17 20:41:55 +00:00
|
|
|
async def test_no_devices_found_abort(hass):
|
|
|
|
"""Test that failure to find devices aborts flow."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.has_devices", return_value=[]):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_link()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "no_devices_found"
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
async def test_manual_mode(hass):
|
|
|
|
"""Test host specified in manual mode is passed to Step Link."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
2019-04-23 23:32:36 +00:00
|
|
|
flow.location = MOCK_LOCATION
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
# Step Mode with User Input: manual, results in Step Link.
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-10-06 12:43:34 +00:00
|
|
|
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": flow.m_device}]
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-03-25 12:25:15 +00:00
|
|
|
result = await flow.async_step_mode(MOCK_MANUAL)
|
|
|
|
assert flow.m_device == MOCK_HOST
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
|
2019-02-17 20:41:55 +00:00
|
|
|
async def test_credential_abort(hass):
|
|
|
|
"""Test that failure to get credentials aborts flow."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.get_creds", return_value=None):
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_creds({})
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "credential_error"
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
|
2019-04-23 23:32:36 +00:00
|
|
|
async def test_credential_timeout(hass):
|
|
|
|
"""Test that Credential Timeout shows error."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.get_creds", side_effect=CredentialTimeout):
|
2019-04-23 23:32:36 +00:00
|
|
|
result = await flow.async_step_creds({})
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["errors"] == {"base": "credential_timeout"}
|
2019-04-23 23:32:36 +00:00
|
|
|
|
|
|
|
|
2019-03-25 12:25:15 +00:00
|
|
|
async def test_wrong_pin_error(hass):
|
|
|
|
"""Test that incorrect pin throws an error."""
|
2019-02-17 20:41:55 +00:00
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
2019-04-23 23:32:36 +00:00
|
|
|
flow.location = MOCK_LOCATION
|
2019-02-17 20:41:55 +00:00
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.link", return_value=(True, False)), patch(
|
|
|
|
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_link(MOCK_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
|
|
|
assert result["errors"] == {"base": "login_failed"}
|
2019-02-17 20:41:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_device_connection_error(hass):
|
|
|
|
"""Test that device not connected or on throws an error."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
2019-04-23 23:32:36 +00:00
|
|
|
flow.location = MOCK_LOCATION
|
2019-02-17 20:41:55 +00:00
|
|
|
|
2019-10-06 12:43:34 +00:00
|
|
|
with patch("pyps4_2ndscreen.Helper.link", return_value=(False, True)), patch(
|
|
|
|
"pyps4_2ndscreen.Helper.has_devices", return_value=[{"host-ip": MOCK_HOST}]
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2019-02-17 20:41:55 +00:00
|
|
|
result = await flow.async_step_link(MOCK_CONFIG)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
|
|
|
assert result["errors"] == {"base": "not_ready"}
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_manual_mode_no_ip_error(hass):
|
|
|
|
"""Test no IP specified in manual mode throws an error."""
|
|
|
|
flow = ps4.PlayStation4FlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_input = {"Config Mode": "Manual Entry"}
|
2019-03-25 12:25:15 +00:00
|
|
|
|
|
|
|
result = await flow.async_step_mode(mock_input)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "mode"
|
|
|
|
assert result["errors"] == {CONF_IP_ADDRESS: "no_ipaddress"}
|