Skip to content

Commit

Permalink
Merge pull request #411 from dougiteixeira:tests-init-p1
Browse files Browse the repository at this point in the history
Add tests to __init__.py
  • Loading branch information
dougiteixeira authored Dec 17, 2024
2 parents 7fa8ac5 + c2292fa commit 20c307e
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 43 deletions.
1 change: 0 additions & 1 deletion custom_components/proxmoxve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_TOKEN_NAME, default=""): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_REALM, default=DEFAULT_REALM): cv.string,
Expand Down
71 changes: 45 additions & 26 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,53 @@
CONF_LXC: ["100"],
}
YAML_INPUT_OK = {
CONF_HOST: "192.168.10.101",
CONF_PORT: 8006,
CONF_USERNAME: "root",
CONF_PASSWORD: "secret",
CONF_REALM: "pam",
CONF_VERIFY_SSL: True,
CONF_NODES: [
{
CONF_NODE: "pve",
CONF_VMS: ["100", "101", "102"],
CONF_CONTAINERS: ["201", "202", "203"],
}
],
"proxmoxve": {
CONF_HOST: "192.168.10.101",
CONF_PORT: 8006,
CONF_USERNAME: "root",
CONF_PASSWORD: "secret",
CONF_REALM: "pam",
CONF_VERIFY_SSL: True,
CONF_NODES: [
{
CONF_NODE: "pve",
CONF_VMS: ["100", "101", "102"],
CONF_CONTAINERS: ["201", "202", "203"],
}
],
}
}
YAML_INPUT_INVALID = {
"proxmoxve": {
CONF_HOST: "test_host",
CONF_USERNAME: "user_test",
CONF_PASSWORD: "passw_test",
"error": "error",
CONF_NODES: [
{
CONF_NODE: "pve",
CONF_VMS: ["100", "101", "102"],
CONF_CONTAINERS: ["201", "202", "203"],
}
],
}
}
YAML_INPUT_NOT_EXIST = {
CONF_HOST: "192.168.10.152",
CONF_PORT: 8006,
CONF_USERNAME: "root",
CONF_PASSWORD: "secret",
CONF_REALM: "pam",
CONF_VERIFY_SSL: True,
CONF_NODES: [
{
CONF_NODE: "not_exist",
CONF_VMS: ["100", "101", "102"],
CONF_CONTAINERS: ["201", "202", "203"],
}
],
"proxmoxve": {
CONF_HOST: "192.168.10.152",
CONF_PORT: 8006,
CONF_USERNAME: "root",
CONF_PASSWORD: "secret",
CONF_REALM: "pam",
CONF_VERIFY_SSL: True,
CONF_NODES: [
{
CONF_NODE: "not_exist",
CONF_VMS: ["100", "101", "102"],
CONF_CONTAINERS: ["201", "202", "203"],
}
],
}
}
USER_INPUT_USER_HOST = {
CONF_HOST: "192.168.10.101",
Expand Down
39 changes: 23 additions & 16 deletions tests/test_config_flow_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

async def test_flow_import_ok(hass: HomeAssistant) -> None:
"""Test import flow ok."""
conf = YAML_INPUT_OK[DOMAIN]
with (
patch("proxmoxer.ProxmoxResource.get", return_value=MOCK_GET_RESPONSE),
patch(
Expand All @@ -43,25 +44,26 @@ async def test_flow_import_ok(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=YAML_INPUT_OK,
data=conf,
)

assert result["type"] == FlowResultType.CREATE_ENTRY
assert "data" in result
assert result["data"][CONF_HOST] == YAML_INPUT_OK[CONF_HOST]
assert result["data"][CONF_HOST] == conf[CONF_HOST]

issue_registry = ir.async_get(hass)
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_OK.get(CONF_HOST)}_{YAML_INPUT_OK.get(CONF_PORT)}_import_success",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_import_success",
)
is not None
)


async def test_flow_import_error_node_not_exist(hass: HomeAssistant) -> None:
"""Test import error in case node not exist in Proxmox."""
conf = YAML_INPUT_NOT_EXIST[DOMAIN]
with (
patch("proxmoxer.ProxmoxResource.get", return_value=MOCK_GET_RESPONSE),
patch(
Expand All @@ -71,33 +73,34 @@ async def test_flow_import_error_node_not_exist(hass: HomeAssistant) -> None:
):
# imported config is identical to the one generated from config flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=YAML_INPUT_NOT_EXIST
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)

assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "import_failed"

issue_registry = ir.async_get(hass)
for nodes in YAML_INPUT_NOT_EXIST.get(CONF_NODES):
for nodes in conf.get(CONF_NODES):
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_NOT_EXIST.get(CONF_HOST)}_{YAML_INPUT_NOT_EXIST.get(CONF_PORT)}_{nodes[CONF_NODE]}_import_node_not_exist",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_{nodes[CONF_NODE]}_import_node_not_exist",
)
is not None
)


async def test_flow_import_error_auth_error(hass: HomeAssistant) -> None:
"""Test import errors in case username or password are incorrect."""
conf = YAML_INPUT_OK[DOMAIN]
with patch(
"custom_components.proxmoxve.ProxmoxClient.build_client",
side_effect=proxmoxer.backends.https.AuthenticationError("mock msg"),
return_value=None,
):
# imported config is identical to the one generated from config flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=YAML_INPUT_OK
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)

assert result["type"] == FlowResultType.ABORT
Expand All @@ -107,14 +110,15 @@ async def test_flow_import_error_auth_error(hass: HomeAssistant) -> None:
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_OK.get(CONF_HOST)}_{YAML_INPUT_OK.get(CONF_PORT)}_import_auth_error",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_import_auth_error",
)
is not None
)


async def test_flow_import_error_ssl_rejection(hass: HomeAssistant) -> None:
"""Test import errors in case the SSL certificare is not present or is not valid or is expired."""
conf = YAML_INPUT_OK[DOMAIN]
with patch(
"custom_components.proxmoxve.ProxmoxClient.build_client",
side_effect=SSLError,
Expand All @@ -132,22 +136,23 @@ async def test_flow_import_error_ssl_rejection(hass: HomeAssistant) -> None:
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_OK.get(CONF_HOST)}_{YAML_INPUT_OK.get(CONF_PORT)}_import_ssl_rejection",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_import_ssl_rejection",
)
is not None
)


async def test_flow_import_error_cant_connect(hass: HomeAssistant) -> None:
"""Test import errors in case the connection fails."""
conf = YAML_INPUT_OK[DOMAIN]
with patch(
"custom_components.proxmoxve.ProxmoxClient.build_client",
side_effect=ConnectTimeout,
return_value=None,
):
# imported config is identical to the one generated from config flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=YAML_INPUT_OK
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)

assert result["type"] == FlowResultType.ABORT
Expand All @@ -157,22 +162,23 @@ async def test_flow_import_error_cant_connect(hass: HomeAssistant) -> None:
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_OK.get(CONF_HOST)}_{YAML_INPUT_OK.get(CONF_PORT)}_import_cant_connect",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_import_cant_connect",
)
is not None
)


async def test_flow_import_error_general_error(hass: HomeAssistant) -> None:
"""Test import errors in case of an unknown exception occurs."""
conf = YAML_INPUT_OK[DOMAIN]
with patch(
"custom_components.proxmoxve.ProxmoxClient.build_client",
side_effect=Exception,
return_value=None,
):
# imported config is identical to the one generated from config flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=YAML_INPUT_OK
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)

assert result["type"] == FlowResultType.ABORT
Expand All @@ -182,17 +188,18 @@ async def test_flow_import_error_general_error(hass: HomeAssistant) -> None:
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_OK.get(CONF_HOST)}_{YAML_INPUT_OK.get(CONF_PORT)}_import_general_error",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_import_general_error",
)
is not None
)


async def test_flow_import_error_already_configured(hass: HomeAssistant) -> None:
"""Test import error in case entry already configured."""
conf = YAML_INPUT_OK[DOMAIN]
entry = MockConfigEntry(
domain=DOMAIN,
data=YAML_INPUT_OK,
data=conf,
)

entry.add_to_hass(hass)
Expand All @@ -207,7 +214,7 @@ async def test_flow_import_error_already_configured(hass: HomeAssistant) -> None
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=YAML_INPUT_OK,
data=conf,
)

assert result["type"] == FlowResultType.ABORT
Expand All @@ -217,7 +224,7 @@ async def test_flow_import_error_already_configured(hass: HomeAssistant) -> None
assert (
issue_registry.async_get_issue(
DOMAIN,
f"{YAML_INPUT_OK.get(CONF_HOST)}_{YAML_INPUT_OK.get(CONF_PORT)}_import_already_configured",
f"{conf.get(CONF_HOST)}_{conf.get(CONF_PORT)}_import_already_configured",
)
is not None
)
117 changes: 117 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1 +1,118 @@
"""Tests for Proxmox VE."""

from unittest.mock import patch

from homeassistant.config_entries import (
ConfigEntryState,
)
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.proxmoxve import DOMAIN
from custom_components.proxmoxve.const import (
CONF_LXC,
CONF_NODES,
CONF_QEMU,
CONF_REALM,
)

from . import async_init_integration
from .const import (
MOCK_GET_RESPONSE,
YAML_INPUT_INVALID,
YAML_INPUT_OK,
)


async def test_setup_entry(hass: HomeAssistant) -> None:
"""Test setup entry."""
mock_config_entry = MockConfigEntry(
domain=DOMAIN,
title="Test",
data={
CONF_HOST: "192.168.10.107",
CONF_PORT: 8006,
CONF_USERNAME: "root",
CONF_PASSWORD: "secret",
CONF_REALM: "pam",
CONF_VERIFY_SSL: True,
CONF_NODES: ["pve"],
CONF_QEMU: ["101"],
CONF_LXC: ["100"],
},
)
with (
patch("proxmoxer.ProxmoxResource.get", return_value=MOCK_GET_RESPONSE),
patch(
"proxmoxer.backends.https.ProxmoxHTTPAuth._get_new_tokens",
return_value=None,
),
):
await async_init_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED


async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test unload an entry."""
mock_config_entry = MockConfigEntry(
domain=DOMAIN,
title="Test",
data={
CONF_HOST: "192.168.10.108",
CONF_PORT: 8006,
CONF_USERNAME: "root",
CONF_PASSWORD: "secret",
CONF_REALM: "pam",
CONF_VERIFY_SSL: True,
CONF_NODES: ["pve"],
CONF_QEMU: ["101"],
CONF_LXC: ["100"],
},
)
with (
patch("proxmoxer.ProxmoxResource.get", return_value=MOCK_GET_RESPONSE),
patch(
"proxmoxer.backends.https.ProxmoxHTTPAuth._get_new_tokens",
return_value=None,
),
):
await async_init_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED

assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED


async def test_setup_config(hass: HomeAssistant) -> None:
"""Test setup from yaml config."""
with (
patch("proxmoxer.ProxmoxResource.get", return_value=MOCK_GET_RESPONSE),
patch(
"proxmoxer.backends.https.ProxmoxHTTPAuth._get_new_tokens",
return_value=None,
),
):
assert await async_setup_component(hass, DOMAIN, YAML_INPUT_OK)
await hass.async_block_till_done()


async def test_setup_invalid_config(hass: HomeAssistant) -> None:
"""Test setup from yaml with invalid config."""
with (
patch("proxmoxer.ProxmoxResource.get", return_value=MOCK_GET_RESPONSE),
patch(
"proxmoxer.backends.https.ProxmoxHTTPAuth._get_new_tokens",
return_value=None,
),
):
assert not await async_setup_component(hass, DOMAIN, YAML_INPUT_INVALID)
await hass.async_block_till_done()

0 comments on commit 20c307e

Please sign in to comment.