init home-assistant
This commit is contained in:
1
home-assistant/tests/components/tailscale/__init__.py
Normal file
1
home-assistant/tests/components/tailscale/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the Tailscale integration."""
|
||||
77
home-assistant/tests/components/tailscale/conftest.py
Normal file
77
home-assistant/tests/components/tailscale/conftest.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""Fixtures for Tailscale integration tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from tailscale.models import Devices
|
||||
|
||||
from homeassistant.components.tailscale.const import CONF_TAILNET, DOMAIN
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Return the default mocked config entry."""
|
||||
return MockConfigEntry(
|
||||
title="homeassistant.github",
|
||||
domain=DOMAIN,
|
||||
data={CONF_TAILNET: "homeassistant.github", CONF_API_KEY: "tskey-MOCK"},
|
||||
unique_id="homeassistant.github",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Mock setting up a config entry."""
|
||||
with patch(
|
||||
"homeassistant.components.tailscale.async_setup_entry", return_value=True
|
||||
) as mock_setup:
|
||||
yield mock_setup
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_tailscale_config_flow() -> Generator[MagicMock]:
|
||||
"""Return a mocked Tailscale client."""
|
||||
with patch(
|
||||
"homeassistant.components.tailscale.config_flow.Tailscale", autospec=True
|
||||
) as tailscale_mock:
|
||||
tailscale = tailscale_mock.return_value
|
||||
tailscale.devices.return_value = Devices.from_json(
|
||||
load_fixture("tailscale/devices.json")
|
||||
).devices
|
||||
yield tailscale
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_tailscale(request: pytest.FixtureRequest) -> Generator[MagicMock]:
|
||||
"""Return a mocked Tailscale client."""
|
||||
fixture: str = "tailscale/devices.json"
|
||||
if hasattr(request, "param") and request.param:
|
||||
fixture = request.param
|
||||
|
||||
devices = Devices.from_json(load_fixture(fixture)).devices
|
||||
with patch(
|
||||
"homeassistant.components.tailscale.coordinator.Tailscale", autospec=True
|
||||
) as tailscale_mock:
|
||||
tailscale = tailscale_mock.return_value
|
||||
tailscale.devices.return_value = devices
|
||||
yield tailscale
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def init_integration(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_tailscale: MagicMock
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Tailscale integration for testing."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_config_entry
|
||||
109
home-assistant/tests/components/tailscale/fixtures/devices.json
Normal file
109
home-assistant/tests/components/tailscale/fixtures/devices.json
Normal file
@@ -0,0 +1,109 @@
|
||||
{
|
||||
"devices": [
|
||||
{
|
||||
"addresses": ["100.11.11.111"],
|
||||
"id": "123456",
|
||||
"user": "frenck",
|
||||
"name": "frencks-iphone.homeassistant.github",
|
||||
"hostname": "Frencks-iPhone",
|
||||
"clientVersion": "1.12.3-td91ea7286-ge1bbbd90c",
|
||||
"updateAvailable": true,
|
||||
"os": "iOS",
|
||||
"created": "2021-08-19T09:25:22Z",
|
||||
"lastSeen": "2021-09-16T06:11:23Z",
|
||||
"keyExpiryDisabled": false,
|
||||
"expires": "2022-02-15T09:25:22Z",
|
||||
"authorized": true,
|
||||
"isExternal": false,
|
||||
"machineKey": "mkey:mock",
|
||||
"nodeKey": "nodekey:mock",
|
||||
"blocksIncomingConnections": false,
|
||||
"enabledRoutes": [],
|
||||
"advertisedRoutes": [],
|
||||
"clientConnectivity": {
|
||||
"endpoints": ["192.0.0.1:41641", "192.168.11.154:41641"],
|
||||
"derp": "",
|
||||
"mappingVariesByDestIP": false,
|
||||
"latency": {},
|
||||
"clientSupports": {
|
||||
"hairPinning": false,
|
||||
"ipv6": false,
|
||||
"pcp": false,
|
||||
"pmp": false,
|
||||
"udp": true,
|
||||
"upnp": false
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"addresses": ["100.11.11.112"],
|
||||
"id": "123457",
|
||||
"user": "frenck",
|
||||
"name": "router.homeassistant.github",
|
||||
"hostname": "router",
|
||||
"clientVersion": "1.14.0-t5cff36945-g809e87bba",
|
||||
"updateAvailable": true,
|
||||
"os": "linux",
|
||||
"created": "2021-08-29T09:49:06Z",
|
||||
"lastSeen": "2021-11-15T20:37:03Z",
|
||||
"keyExpiryDisabled": false,
|
||||
"expires": "2022-02-25T09:49:06Z",
|
||||
"authorized": true,
|
||||
"isExternal": false,
|
||||
"machineKey": "mkey:mock",
|
||||
"nodeKey": "nodekey:mock",
|
||||
"blocksIncomingConnections": false,
|
||||
"enabledRoutes": ["0.0.0.0/0", "10.10.10.0/23", "::/0"],
|
||||
"advertisedRoutes": ["0.0.0.0/0", "10.10.10.0/23", "::/0"],
|
||||
"clientConnectivity": {
|
||||
"endpoints": ["10.10.10.1:41641", "111.111.111.111:41641"],
|
||||
"derp": "",
|
||||
"mappingVariesByDestIP": false,
|
||||
"latency": {
|
||||
"Bangalore": {
|
||||
"latencyMs": 143.42505599999998
|
||||
},
|
||||
"Chicago": {
|
||||
"latencyMs": 101.123646
|
||||
},
|
||||
"Dallas": {
|
||||
"latencyMs": 136.85886
|
||||
},
|
||||
"Frankfurt": {
|
||||
"latencyMs": 18.968314
|
||||
},
|
||||
"London": {
|
||||
"preferred": true,
|
||||
"latencyMs": 14.314574
|
||||
},
|
||||
"New York City": {
|
||||
"latencyMs": 83.078912
|
||||
},
|
||||
"San Francisco": {
|
||||
"latencyMs": 148.215522
|
||||
},
|
||||
"Seattle": {
|
||||
"latencyMs": 181.553595
|
||||
},
|
||||
"Singapore": {
|
||||
"latencyMs": 164.566539
|
||||
},
|
||||
"São Paulo": {
|
||||
"latencyMs": 207.250179
|
||||
},
|
||||
"Tokyo": {
|
||||
"latencyMs": 226.90714300000002
|
||||
}
|
||||
},
|
||||
"clientSupports": {
|
||||
"hairPinning": true,
|
||||
"ipv6": false,
|
||||
"pcp": false,
|
||||
"pmp": false,
|
||||
"udp": true,
|
||||
"upnp": false
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics
|
||||
dict({
|
||||
'devices': list([
|
||||
dict({
|
||||
'addresses': '**REDACTED**',
|
||||
'advertised_routes': list([
|
||||
]),
|
||||
'authorized': True,
|
||||
'blocks_incoming_connections': False,
|
||||
'client_connectivity': dict({
|
||||
'client_supports': dict({
|
||||
'hair_pinning': False,
|
||||
'ipv6': False,
|
||||
'pcp': False,
|
||||
'pmp': False,
|
||||
'udp': True,
|
||||
'upnp': False,
|
||||
}),
|
||||
'endpoints': '**REDACTED**',
|
||||
'mapping_varies_by_dest_ip': False,
|
||||
}),
|
||||
'client_version': '1.12.3-td91ea7286-ge1bbbd90c',
|
||||
'created': '2021-08-19T09:25:22+00:00',
|
||||
'device_id': '**REDACTED**',
|
||||
'enabled_routes': list([
|
||||
]),
|
||||
'expires': '2022-02-15T09:25:22+00:00',
|
||||
'hostname': '**REDACTED**',
|
||||
'is_external': False,
|
||||
'key_expiry_disabled': False,
|
||||
'last_seen': '2021-09-16T06:11:23+00:00',
|
||||
'machine_key': '**REDACTED**',
|
||||
'name': '**REDACTED**',
|
||||
'node_key': '**REDACTED**',
|
||||
'os': 'iOS',
|
||||
'tags': list([
|
||||
]),
|
||||
'update_available': True,
|
||||
'user': '**REDACTED**',
|
||||
}),
|
||||
dict({
|
||||
'addresses': '**REDACTED**',
|
||||
'advertised_routes': list([
|
||||
'0.0.0.0/0',
|
||||
'10.10.10.0/23',
|
||||
'::/0',
|
||||
]),
|
||||
'authorized': True,
|
||||
'blocks_incoming_connections': False,
|
||||
'client_connectivity': dict({
|
||||
'client_supports': dict({
|
||||
'hair_pinning': True,
|
||||
'ipv6': False,
|
||||
'pcp': False,
|
||||
'pmp': False,
|
||||
'udp': True,
|
||||
'upnp': False,
|
||||
}),
|
||||
'endpoints': '**REDACTED**',
|
||||
'mapping_varies_by_dest_ip': False,
|
||||
}),
|
||||
'client_version': '1.14.0-t5cff36945-g809e87bba',
|
||||
'created': '2021-08-29T09:49:06+00:00',
|
||||
'device_id': '**REDACTED**',
|
||||
'enabled_routes': list([
|
||||
'0.0.0.0/0',
|
||||
'10.10.10.0/23',
|
||||
'::/0',
|
||||
]),
|
||||
'expires': '2022-02-25T09:49:06+00:00',
|
||||
'hostname': '**REDACTED**',
|
||||
'is_external': False,
|
||||
'key_expiry_disabled': False,
|
||||
'last_seen': '2021-11-15T20:37:03+00:00',
|
||||
'machine_key': '**REDACTED**',
|
||||
'name': '**REDACTED**',
|
||||
'node_key': '**REDACTED**',
|
||||
'os': 'linux',
|
||||
'tags': list([
|
||||
]),
|
||||
'update_available': True,
|
||||
'user': '**REDACTED**',
|
||||
}),
|
||||
]),
|
||||
})
|
||||
# ---
|
||||
124
home-assistant/tests/components/tailscale/test_binary_sensor.py
Normal file
124
home-assistant/tests/components/tailscale/test_binary_sensor.py
Normal file
@@ -0,0 +1,124 @@
|
||||
"""Tests for the sensors provided by the Tailscale integration."""
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
BinarySensorDeviceClass,
|
||||
)
|
||||
from homeassistant.components.tailscale.const import DOMAIN
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_FRIENDLY_NAME, EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_tailscale_binary_sensors(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the Tailscale binary sensors."""
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_client")
|
||||
entry = entity_registry.async_get("binary_sensor.frencks_iphone_client")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_update_available"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Client"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.UPDATE
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_key_expiry_disabled")
|
||||
entry = entity_registry.async_get(
|
||||
"binary_sensor.frencks_iphone_key_expiry_disabled"
|
||||
)
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_key_expiry_disabled"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_OFF
|
||||
assert (
|
||||
state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Key expiry disabled"
|
||||
)
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_supports_hairpinning")
|
||||
entry = entity_registry.async_get(
|
||||
"binary_sensor.frencks_iphone_supports_hairpinning"
|
||||
)
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_client_supports_hair_pinning"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_OFF
|
||||
assert (
|
||||
state.attributes.get(ATTR_FRIENDLY_NAME)
|
||||
== "frencks-iphone Supports hairpinning"
|
||||
)
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_supports_ipv6")
|
||||
entry = entity_registry.async_get("binary_sensor.frencks_iphone_supports_ipv6")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_client_supports_ipv6"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Supports IPv6"
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_supports_pcp")
|
||||
entry = entity_registry.async_get("binary_sensor.frencks_iphone_supports_pcp")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_client_supports_pcp"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Supports PCP"
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_supports_nat_pmp")
|
||||
entry = entity_registry.async_get("binary_sensor.frencks_iphone_supports_nat_pmp")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_client_supports_pmp"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Supports NAT-PMP"
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_supports_udp")
|
||||
entry = entity_registry.async_get("binary_sensor.frencks_iphone_supports_udp")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_client_supports_udp"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Supports UDP"
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
state = hass.states.get("binary_sensor.frencks_iphone_supports_upnp")
|
||||
entry = entity_registry.async_get("binary_sensor.frencks_iphone_supports_upnp")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123456_client_supports_upnp"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "frencks-iphone Supports UPnP"
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
assert entry.device_id
|
||||
device_entry = device_registry.async_get(entry.device_id)
|
||||
assert device_entry
|
||||
assert device_entry.identifiers == {(DOMAIN, "123456")}
|
||||
assert device_entry.manufacturer == "Tailscale Inc."
|
||||
assert device_entry.model == "iOS"
|
||||
assert device_entry.name == "frencks-iphone"
|
||||
assert device_entry.entry_type == dr.DeviceEntryType.SERVICE
|
||||
assert device_entry.sw_version == "1.12.3-td91ea7286-ge1bbbd90c"
|
||||
assert (
|
||||
device_entry.configuration_url
|
||||
== "https://login.tailscale.com/admin/machines/100.11.11.111"
|
||||
)
|
||||
222
home-assistant/tests/components/tailscale/test_config_flow.py
Normal file
222
home-assistant/tests/components/tailscale/test_config_flow.py
Normal file
@@ -0,0 +1,222 @@
|
||||
"""Tests for the Tailscale config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from tailscale import TailscaleAuthenticationError, TailscaleConnectionError
|
||||
|
||||
from homeassistant.components.tailscale.const import CONF_TAILNET, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_full_user_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_tailscale_config_flow: MagicMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the full user configuration flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-FAKE",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result2.get("title") == "homeassistant.github"
|
||||
assert result2.get("data") == {
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-FAKE",
|
||||
}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_full_flow_with_authentication_error(
|
||||
hass: HomeAssistant,
|
||||
mock_tailscale_config_flow: MagicMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the full user configuration flow with incorrect API key.
|
||||
|
||||
This tests tests a full config flow, with a case the user enters an invalid
|
||||
Tailscale API key, but recovers by entering the correct one.
|
||||
"""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "user"
|
||||
|
||||
mock_tailscale_config_flow.devices.side_effect = TailscaleAuthenticationError
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-INVALID",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2.get("type") is FlowResultType.FORM
|
||||
assert result2.get("step_id") == "user"
|
||||
assert result2.get("errors") == {"base": "invalid_auth"}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 1
|
||||
|
||||
mock_tailscale_config_flow.devices.side_effect = None
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-VALID",
|
||||
},
|
||||
)
|
||||
|
||||
assert result3.get("type") is FlowResultType.CREATE_ENTRY
|
||||
assert result3.get("title") == "homeassistant.github"
|
||||
assert result3.get("data") == {
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-VALID",
|
||||
}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 2
|
||||
|
||||
|
||||
async def test_connection_error(
|
||||
hass: HomeAssistant, mock_tailscale_config_flow: MagicMock
|
||||
) -> None:
|
||||
"""Test API connection error."""
|
||||
mock_tailscale_config_flow.devices.side_effect = TailscaleConnectionError
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-FAKE",
|
||||
},
|
||||
)
|
||||
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("errors") == {"base": "cannot_connect"}
|
||||
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_tailscale_config_flow: MagicMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the reauthentication configuration flow."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "reauth_confirm"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "tskey-REAUTH"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2.get("type") is FlowResultType.ABORT
|
||||
assert result2.get("reason") == "reauth_successful"
|
||||
assert mock_config_entry.data == {
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-REAUTH",
|
||||
}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reauth_with_authentication_error(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_tailscale_config_flow: MagicMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test the reauthentication configuration flow with an authentication error.
|
||||
|
||||
This tests tests a reauth flow, with a case the user enters an invalid
|
||||
API key, but recover by entering the correct one.
|
||||
"""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "reauth_confirm"
|
||||
|
||||
mock_tailscale_config_flow.devices.side_effect = TailscaleAuthenticationError
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "tskey-INVALID"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2.get("type") is FlowResultType.FORM
|
||||
assert result2.get("step_id") == "reauth_confirm"
|
||||
assert result2.get("errors") == {"base": "invalid_auth"}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 1
|
||||
|
||||
mock_tailscale_config_flow.devices.side_effect = None
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={CONF_API_KEY: "tskey-VALID"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result3.get("type") is FlowResultType.ABORT
|
||||
assert result3.get("reason") == "reauth_successful"
|
||||
assert mock_config_entry.data == {
|
||||
CONF_TAILNET: "homeassistant.github",
|
||||
CONF_API_KEY: "tskey-VALID",
|
||||
}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(mock_tailscale_config_flow.devices.mock_calls) == 2
|
||||
|
||||
|
||||
async def test_reauth_api_error(
|
||||
hass: HomeAssistant,
|
||||
mock_tailscale_config_flow: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test API error during reauthentication."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
assert result.get("type") is FlowResultType.FORM
|
||||
assert result.get("step_id") == "reauth_confirm"
|
||||
|
||||
mock_tailscale_config_flow.devices.side_effect = TailscaleConnectionError
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_API_KEY: "tskey-VALID"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2.get("type") is FlowResultType.FORM
|
||||
assert result2.get("step_id") == "reauth_confirm"
|
||||
assert result2.get("errors") == {"base": "cannot_connect"}
|
||||
@@ -0,0 +1,22 @@
|
||||
"""Tests for the diagnostics data provided by the Tailscale integration."""
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
init_integration: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test diagnostics."""
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
|
||||
== snapshot
|
||||
)
|
||||
73
home-assistant/tests/components/tailscale/test_init.py
Normal file
73
home-assistant/tests/components/tailscale/test_init.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""Tests for the Tailscale integration."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from tailscale import TailscaleAuthenticationError, TailscaleConnectionError
|
||||
|
||||
from homeassistant.components.tailscale.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_load_unload_config_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_tailscale: MagicMock,
|
||||
) -> None:
|
||||
"""Test the Tailscale configuration entry loading/unloading."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not hass.data.get(DOMAIN)
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
async def test_config_entry_not_ready(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_tailscale: MagicMock,
|
||||
) -> None:
|
||||
"""Test the Tailscale configuration entry not ready."""
|
||||
mock_tailscale.devices.side_effect = TailscaleConnectionError
|
||||
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_tailscale.devices.mock_calls) == 1
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_config_entry_authentication_failed(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_tailscale: MagicMock,
|
||||
) -> None:
|
||||
"""Test trigger reauthentication flow."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
mock_tailscale.devices.side_effect = TailscaleAuthenticationError
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
|
||||
flow = flows[0]
|
||||
assert flow.get("step_id") == "reauth_confirm"
|
||||
assert flow.get("handler") == DOMAIN
|
||||
|
||||
assert "context" in flow
|
||||
assert flow["context"].get("source") == SOURCE_REAUTH
|
||||
assert flow["context"].get("entry_id") == mock_config_entry.entry_id
|
||||
61
home-assistant/tests/components/tailscale/test_sensor.py
Normal file
61
home-assistant/tests/components/tailscale/test_sensor.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""Tests for the sensors provided by the Tailscale integration."""
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass
|
||||
from homeassistant.components.tailscale.const import DOMAIN
|
||||
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_FRIENDLY_NAME, EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_tailscale_sensors(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
init_integration: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the Tailscale sensors."""
|
||||
state = hass.states.get("sensor.router_expires")
|
||||
entry = entity_registry.async_get("sensor.router_expires")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123457_expires"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == "2022-02-25T09:49:06+00:00"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "router Expires"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
|
||||
|
||||
state = hass.states.get("sensor.router_last_seen")
|
||||
entry = entity_registry.async_get("sensor.router_last_seen")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123457_last_seen"
|
||||
assert entry.entity_category is None
|
||||
assert state.state == "2021-11-15T20:37:03+00:00"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "router Last seen"
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
|
||||
|
||||
state = hass.states.get("sensor.router_ip_address")
|
||||
entry = entity_registry.async_get("sensor.router_ip_address")
|
||||
assert entry
|
||||
assert state
|
||||
assert entry.unique_id == "123457_ip"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
assert state.state == "100.11.11.112"
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "router IP address"
|
||||
assert ATTR_DEVICE_CLASS not in state.attributes
|
||||
|
||||
assert entry.device_id
|
||||
device_entry = device_registry.async_get(entry.device_id)
|
||||
assert device_entry
|
||||
assert device_entry.identifiers == {(DOMAIN, "123457")}
|
||||
assert device_entry.manufacturer == "Tailscale Inc."
|
||||
assert device_entry.model == "linux"
|
||||
assert device_entry.name == "router"
|
||||
assert device_entry.entry_type == dr.DeviceEntryType.SERVICE
|
||||
assert device_entry.sw_version == "1.14.0-t5cff36945-g809e87bba"
|
||||
assert (
|
||||
device_entry.configuration_url
|
||||
== "https://login.tailscale.com/admin/machines/100.11.11.112"
|
||||
)
|
||||
Reference in New Issue
Block a user