mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-22 21:06:27 +01:00
Add missing tests for Nintendo parental controls code coverage (#159210)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pynintendoauth.exceptions import InvalidOAuthConfigurationException
|
||||
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
@@ -28,3 +30,28 @@ async def test_invalid_authentication(
|
||||
assert len(entries) == 0
|
||||
# Ensure the config entry is marked as error
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
async def test_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nintendo_authenticator: AsyncMock,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the reauth flow is triggered."""
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = (
|
||||
InvalidOAuthConfigurationException(
|
||||
status_code=401, message="Authentication failed"
|
||||
)
|
||||
)
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Ensure no entities are created
|
||||
entries = er.async_entries_for_config_entry(
|
||||
entity_registry, mock_config_entry.entry_id
|
||||
)
|
||||
assert len(entries) == 0
|
||||
# Ensure the config entry is marked as needing reauth
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
assert mock_config_entry.error_reason_translation_key == "auth_expired"
|
||||
|
||||
@@ -13,7 +13,7 @@ from homeassistant.components.nintendo_parental_controls.services import (
|
||||
)
|
||||
from homeassistant.const import ATTR_DEVICE_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from . import setup_integration
|
||||
@@ -51,7 +51,7 @@ async def test_add_bonus_time_invalid_device(
|
||||
) -> None:
|
||||
"""Test add bonus time service."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
with pytest.raises(ServiceValidationError) as err:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
NintendoParentalServices.ADD_BONUS_TIME,
|
||||
@@ -63,3 +63,30 @@ async def test_add_bonus_time_invalid_device(
|
||||
)
|
||||
assert err.value.translation_domain == DOMAIN
|
||||
assert err.value.translation_key == "device_not_found"
|
||||
|
||||
|
||||
async def test_add_bonus_time_device_id_not_nintendo(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_nintendo_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test add bonus time service with a device that is not a valid Nintendo device."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
# Create a device that does not have a Nintendo identifier
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=mock_config_entry.entry_id,
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, "00:11:22:33:44:55")},
|
||||
)
|
||||
with pytest.raises(ServiceValidationError) as err:
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
NintendoParentalServices.ADD_BONUS_TIME,
|
||||
{
|
||||
ATTR_DEVICE_ID: device_entry.id,
|
||||
ATTR_BONUS_TIME: 15,
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert err.value.translation_domain == DOMAIN
|
||||
assert err.value.translation_key == "invalid_device"
|
||||
|
||||
Reference in New Issue
Block a user