Adjust tests for removing github devices (#64448)
This commit is contained in:
parent
9facd3962c
commit
554e51017e
4 changed files with 72 additions and 41 deletions
|
@ -1,3 +1,40 @@
|
||||||
"""Common helpers for GitHub integration tests."""
|
"""Common helpers for GitHub integration tests."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.components.github.const import CONF_REPOSITORIES
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
|
||||||
MOCK_ACCESS_TOKEN = "gho_16C7e42F292c6912E7710c838347Ae178B4a"
|
MOCK_ACCESS_TOKEN = "gho_16C7e42F292c6912E7710c838347Ae178B4a"
|
||||||
|
|
||||||
|
|
||||||
|
async def setup_github_integration(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
aioclient_mock: AiohttpClientMocker,
|
||||||
|
) -> None:
|
||||||
|
"""Mock setting up the integration."""
|
||||||
|
repository_id = 1
|
||||||
|
for repository in mock_config_entry.options[CONF_REPOSITORIES]:
|
||||||
|
aioclient_mock.get(
|
||||||
|
f"https://api.github.com/repos/{repository}",
|
||||||
|
json={"full_name": repository, "id": repository_id},
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
repository_id += 1
|
||||||
|
for endpoint in ("issues", "pulls", "releases", "commits"):
|
||||||
|
aioclient_mock.get(
|
||||||
|
f"https://api.github.com/repos/{repository}/{endpoint}",
|
||||||
|
json=[],
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
setup_result = await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert setup_result
|
||||||
|
assert mock_config_entry.state == config_entries.ConfigEntryState.LOADED
|
||||||
|
|
|
@ -10,12 +10,10 @@ from homeassistant.components.github.const import (
|
||||||
DEFAULT_REPOSITORIES,
|
DEFAULT_REPOSITORIES,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
|
|
||||||
from .common import MOCK_ACCESS_TOKEN
|
from .common import MOCK_ACCESS_TOKEN
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -34,28 +32,3 @@ def mock_setup_entry() -> Generator[None, None, None]:
|
||||||
"""Mock setting up a config entry."""
|
"""Mock setting up a config entry."""
|
||||||
with patch("homeassistant.components.github.async_setup_entry", return_value=True):
|
with patch("homeassistant.components.github.async_setup_entry", return_value=True):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
async def setup_github_integration(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
mock_config_entry: MockConfigEntry,
|
|
||||||
aioclient_mock: AiohttpClientMocker,
|
|
||||||
) -> Generator[None, None, None]:
|
|
||||||
"""Mock setting up the integration."""
|
|
||||||
aioclient_mock.get(
|
|
||||||
"https://api.github.com/repos/home-assistant/core",
|
|
||||||
json={},
|
|
||||||
headers={"Content-Type": "application/json"},
|
|
||||||
)
|
|
||||||
for endpoint in ("issues", "pulls", "releases", "commits"):
|
|
||||||
aioclient_mock.get(
|
|
||||||
f"https://api.github.com/repos/home-assistant/core/{endpoint}",
|
|
||||||
json=[],
|
|
||||||
headers={"Content-Type": "application/json"},
|
|
||||||
)
|
|
||||||
mock_config_entry.options = {CONF_REPOSITORIES: ["home-assistant/core"]}
|
|
||||||
mock_config_entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
"""Test GitHub diagnostics."""
|
"""Test GitHub diagnostics."""
|
||||||
from collections.abc import Generator
|
|
||||||
|
|
||||||
from aiogithubapi import GitHubException
|
from aiogithubapi import GitHubException
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
|
|
||||||
|
from homeassistant.components.github.const import CONF_REPOSITORIES
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .common import setup_github_integration
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
@ -16,9 +18,10 @@ async def test_entry_diagnostics(
|
||||||
hass_client: ClientSession,
|
hass_client: ClientSession,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
aioclient_mock: AiohttpClientMocker,
|
aioclient_mock: AiohttpClientMocker,
|
||||||
setup_github_integration: Generator[None, None, None],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test config entry diagnostics."""
|
"""Test config entry diagnostics."""
|
||||||
|
mock_config_entry.options = {CONF_REPOSITORIES: ["home-assistant/core"]}
|
||||||
|
await setup_github_integration(hass, mock_config_entry, aioclient_mock)
|
||||||
aioclient_mock.get(
|
aioclient_mock.get(
|
||||||
"https://api.github.com/rate_limit",
|
"https://api.github.com/rate_limit",
|
||||||
json={"resources": {"core": {"remaining": 100, "limit": 100}}},
|
json={"resources": {"core": {"remaining": 100, "limit": 100}}},
|
||||||
|
@ -35,7 +38,10 @@ async def test_entry_diagnostics(
|
||||||
assert result["rate_limit"] == {
|
assert result["rate_limit"] == {
|
||||||
"resources": {"core": {"remaining": 100, "limit": 100}}
|
"resources": {"core": {"remaining": 100, "limit": 100}}
|
||||||
}
|
}
|
||||||
assert result["repositories"]["home-assistant/core"] == {}
|
assert (
|
||||||
|
result["repositories"]["home-assistant/core"]["full_name"]
|
||||||
|
== "home-assistant/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_entry_diagnostics_exception(
|
async def test_entry_diagnostics_exception(
|
||||||
|
@ -43,9 +49,9 @@ async def test_entry_diagnostics_exception(
|
||||||
hass_client: ClientSession,
|
hass_client: ClientSession,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
aioclient_mock: AiohttpClientMocker,
|
aioclient_mock: AiohttpClientMocker,
|
||||||
setup_github_integration: Generator[None, None, None],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test config entry diagnostics with exception for ratelimit."""
|
"""Test config entry diagnostics with exception for ratelimit."""
|
||||||
|
await setup_github_integration(hass, mock_config_entry, aioclient_mock)
|
||||||
aioclient_mock.get(
|
aioclient_mock.get(
|
||||||
"https://api.github.com/rate_limit",
|
"https://api.github.com/rate_limit",
|
||||||
exc=GitHubException("error"),
|
exc=GitHubException("error"),
|
||||||
|
|
|
@ -1,31 +1,46 @@
|
||||||
"""Test the GitHub init file."""
|
"""Test the GitHub init file."""
|
||||||
from pytest import LogCaptureFixture
|
from pytest import LogCaptureFixture
|
||||||
|
|
||||||
from homeassistant.components.github import async_cleanup_device_registry
|
from homeassistant.components.github.const import CONF_REPOSITORIES
|
||||||
from homeassistant.components.github.const import DOMAIN
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, mock_device_registry
|
from .common import setup_github_integration
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
|
||||||
|
|
||||||
async def test_device_registry_cleanup(
|
async def test_device_registry_cleanup(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
|
aioclient_mock: AiohttpClientMocker,
|
||||||
caplog: LogCaptureFixture,
|
caplog: LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that we remove untracked repositories from the decvice registry."""
|
"""Test that we remove untracked repositories from the decvice registry."""
|
||||||
registry = mock_device_registry(hass)
|
mock_config_entry.options = {CONF_REPOSITORIES: ["home-assistant/core"]}
|
||||||
|
await setup_github_integration(hass, mock_config_entry, aioclient_mock)
|
||||||
|
|
||||||
device = registry.async_get_or_create(
|
device_registry = dr.async_get(hass)
|
||||||
identifiers={(DOMAIN, "test/repository")},
|
devices = dr.async_entries_for_config_entry(
|
||||||
|
registry=device_registry,
|
||||||
config_entry_id=mock_config_entry.entry_id,
|
config_entry_id=mock_config_entry.entry_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert registry.async_get_device({(DOMAIN, "test/repository")}) == device
|
assert len(devices) == 1
|
||||||
await async_cleanup_device_registry(hass, mock_config_entry)
|
|
||||||
|
mock_config_entry.options = {CONF_REPOSITORIES: []}
|
||||||
|
assert await hass.config_entries.async_reload(mock_config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
f"Unlinking device {device.id} for untracked repository test/repository from config entry {mock_config_entry.entry_id}"
|
f"Unlinking device {devices[0].id} for untracked repository home-assistant/core from config entry {mock_config_entry.entry_id}"
|
||||||
in caplog.text
|
in caplog.text
|
||||||
)
|
)
|
||||||
assert registry.async_get_device({(DOMAIN, "test/repository")}) is None
|
|
||||||
|
devices = dr.async_entries_for_config_entry(
|
||||||
|
registry=device_registry,
|
||||||
|
config_entry_id=mock_config_entry.entry_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(devices) == 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue