Files
core/tests/components/portainer/test_init.py
T
2025-09-27 14:32:25 +02:00

63 lines
1.9 KiB
Python

"""Test the Portainer initial specific behavior."""
from unittest.mock import AsyncMock
from pyportainer.exceptions import (
PortainerAuthenticationError,
PortainerConnectionError,
PortainerTimeoutError,
)
import pytest
from homeassistant.components.portainer.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN, CONF_HOST, CONF_URL
from homeassistant.core import HomeAssistant
from . import setup_integration
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
("exception", "expected_state"),
[
(PortainerAuthenticationError("bad creds"), ConfigEntryState.SETUP_ERROR),
(PortainerConnectionError("cannot connect"), ConfigEntryState.SETUP_RETRY),
(PortainerTimeoutError("timeout"), ConfigEntryState.SETUP_RETRY),
],
)
async def test_setup_exceptions(
hass: HomeAssistant,
mock_portainer_client: AsyncMock,
mock_config_entry: MockConfigEntry,
exception: Exception,
expected_state: ConfigEntryState,
) -> None:
"""Test the _async_setup."""
mock_portainer_client.get_endpoints.side_effect = exception
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state == expected_state
async def test_v1_migration(hass: HomeAssistant) -> None:
"""Test migration from v1 to v2 config entry."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "http://test_host",
CONF_API_KEY: "test_key",
},
unique_id="1",
version=1,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.version == 2
assert CONF_HOST not in entry.data
assert CONF_API_KEY not in entry.data
assert entry.data[CONF_URL] == "http://test_host"
assert entry.data[CONF_API_TOKEN] == "test_key"