Adjust tests for removing github devices (#64448)

This commit is contained in:
Joakim Sørensen 2022-01-19 20:30:57 +01:00 committed by GitHub
parent 9facd3962c
commit 554e51017e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 41 deletions

View File

@ -1,3 +1,40 @@
"""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"
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

View File

@ -10,12 +10,10 @@ from homeassistant.components.github.const import (
DEFAULT_REPOSITORIES,
DOMAIN,
)
from homeassistant.core import HomeAssistant
from .common import MOCK_ACCESS_TOKEN
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker
@pytest.fixture
@ -34,28 +32,3 @@ def mock_setup_entry() -> Generator[None, None, None]:
"""Mock setting up a config entry."""
with patch("homeassistant.components.github.async_setup_entry", return_value=True):
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()

View File

@ -1,11 +1,13 @@
"""Test GitHub diagnostics."""
from collections.abc import Generator
from aiogithubapi import GitHubException
from aiohttp import ClientSession
from homeassistant.components.github.const import CONF_REPOSITORIES
from homeassistant.core import HomeAssistant
from .common import setup_github_integration
from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.test_util.aiohttp import AiohttpClientMocker
@ -16,9 +18,10 @@ async def test_entry_diagnostics(
hass_client: ClientSession,
mock_config_entry: MockConfigEntry,
aioclient_mock: AiohttpClientMocker,
setup_github_integration: Generator[None, None, None],
) -> None:
"""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(
"https://api.github.com/rate_limit",
json={"resources": {"core": {"remaining": 100, "limit": 100}}},
@ -35,7 +38,10 @@ async def test_entry_diagnostics(
assert result["rate_limit"] == {
"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(
@ -43,9 +49,9 @@ async def test_entry_diagnostics_exception(
hass_client: ClientSession,
mock_config_entry: MockConfigEntry,
aioclient_mock: AiohttpClientMocker,
setup_github_integration: Generator[None, None, None],
) -> None:
"""Test config entry diagnostics with exception for ratelimit."""
await setup_github_integration(hass, mock_config_entry, aioclient_mock)
aioclient_mock.get(
"https://api.github.com/rate_limit",
exc=GitHubException("error"),

View File

@ -1,31 +1,46 @@
"""Test the GitHub init file."""
from pytest import LogCaptureFixture
from homeassistant.components.github import async_cleanup_device_registry
from homeassistant.components.github.const import DOMAIN
from homeassistant.components.github.const import CONF_REPOSITORIES
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(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
aioclient_mock: AiohttpClientMocker,
caplog: LogCaptureFixture,
) -> None:
"""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(
identifiers={(DOMAIN, "test/repository")},
device_registry = dr.async_get(hass)
devices = dr.async_entries_for_config_entry(
registry=device_registry,
config_entry_id=mock_config_entry.entry_id,
)
assert registry.async_get_device({(DOMAIN, "test/repository")}) == device
await async_cleanup_device_registry(hass, mock_config_entry)
assert len(devices) == 1
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 (
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
)
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