hass-core/tests/components/fivem/test_config_flow.py
Sander Jochems 0ea82bdbfb
Fivem integration (#65089)
* Initial fivem integration setup

* Use licenseKey for unique ID

* Create FiveMServer class

* Create FiveMStatusBinarySensor

* Fix platform loading

* Create sensor platform

* Remove config flow tests

* Update manifest.json

* Use attr_ instead or properties in sensors.py

* Use entry_id as unique_id

* Move device info to _attr instead of property

* Register callback in FiveMEntity

* Create config flow tests

* Add loggin to fivem

* Use FiveM in config_flow

* Use update_coordinator instead of dispatcher

* Bump fivem-api to 0.1.2

* Remove leftovers

* More tests for config flow

* Add component files to .coveragerc

* Fix simple comments

* Add gamename check to config flow

* Use entity descriptions for sensors

* Move extra attributes to init

* Use [] instead of get() for server info

* Fix error in gamename test
2022-02-08 11:27:11 +02:00

146 lines
4.2 KiB
Python

"""Test the FiveM config flow."""
from unittest.mock import patch
from fivem import FiveMServerOfflineError
from homeassistant import config_entries
from homeassistant.components.fivem.config_flow import DEFAULT_PORT
from homeassistant.components.fivem.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
USER_INPUT = {
CONF_NAME: "Dummy Server",
CONF_HOST: "fivem.dummyserver.com",
CONF_PORT: DEFAULT_PORT,
}
def __mock_fivem_info_success():
return {
"resources": [
"fivem",
"monitor",
],
"server": "FXServer-dummy v0.0.0.DUMMY linux",
"vars": {
"gamename": "gta5",
},
"version": 123456789,
}
def __mock_fivem_info_invalid():
return {
"plugins": [
"sample",
],
"data": {
"gamename": "gta5",
},
}
def __mock_fivem_info_invalid_gamename():
info = __mock_fivem_info_success()
info["vars"]["gamename"] = "redm"
return info
async def test_show_config_form(hass: HomeAssistant) -> None:
"""Test if initial configuration form is shown."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["errors"] is None
with patch(
"fivem.fivem.FiveM.get_info_raw",
return_value=__mock_fivem_info_success(),
), patch(
"homeassistant.components.fivem.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == USER_INPUT[CONF_NAME]
assert result2["data"] == USER_INPUT
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"fivem.fivem.FiveM.get_info_raw",
side_effect=FiveMServerOfflineError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_invalid(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"fivem.fivem.FiveM.get_info_raw",
return_value=__mock_fivem_info_invalid(),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "unknown"}
async def test_form_invalid_gamename(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"fivem.fivem.FiveM.get_info_raw",
return_value=__mock_fivem_info_invalid_gamename(),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "invalid_gamename"}