"""Tests for the Overseerr integration.""" from typing import Any from unittest.mock import AsyncMock import pytest from python_overseerr.models import WebhookNotificationOptions from syrupy import SnapshotAssertion from homeassistant.components.overseerr import JSON_PAYLOAD, REGISTERED_NOTIFICATIONS from homeassistant.components.overseerr.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from . import setup_integration from tests.common import MockConfigEntry async def test_device_info( hass: HomeAssistant, snapshot: SnapshotAssertion, mock_overseerr_client: AsyncMock, mock_config_entry: MockConfigEntry, device_registry: dr.DeviceRegistry, ) -> None: """Test device registry integration.""" await setup_integration(hass, mock_config_entry) device_entry = device_registry.async_get_device( identifiers={(DOMAIN, mock_config_entry.entry_id)} ) assert device_entry is not None assert device_entry == snapshot async def test_proper_webhook_configuration( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_overseerr_client: AsyncMock, ) -> None: """Test the webhook configuration.""" await setup_integration(hass, mock_config_entry) assert REGISTERED_NOTIFICATIONS == 222 mock_overseerr_client.test_webhook_notification_config.assert_not_called() mock_overseerr_client.set_webhook_notification_config.assert_not_called() @pytest.mark.parametrize( "update_mock", [ {"return_value.enabled": False}, {"return_value.types": 4}, {"return_value.types": 4062}, { "return_value.options": WebhookNotificationOptions( webhook_url="http://example.com", json_payload=JSON_PAYLOAD ) }, { "return_value.options": WebhookNotificationOptions( webhook_url="http://10.10.10.10:8123/api/webhook/test-webhook-id", json_payload='"{\\"message\\": \\"{{title}}\\"}"', ) }, ], ids=[ "Disabled", "Smaller scope", "Bigger scope", "Webhook URL", "JSON Payload", ], ) async def test_webhook_configuration_need_update( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_overseerr_client: AsyncMock, update_mock: dict[str, Any], ) -> None: """Test the webhook configuration.""" mock_overseerr_client.get_webhook_notification_config.configure_mock(**update_mock) await setup_integration(hass, mock_config_entry) mock_overseerr_client.test_webhook_notification_config.assert_called_once() mock_overseerr_client.set_webhook_notification_config.assert_called_once() @pytest.mark.parametrize( "update_mock", [ {"return_value.enabled": False}, {"return_value.types": 4}, {"return_value.types": 4062}, { "return_value.options": WebhookNotificationOptions( webhook_url="http://example.com", json_payload=JSON_PAYLOAD ) }, { "return_value.options": WebhookNotificationOptions( webhook_url="http://10.10.10.10:8123/api/webhook/test-webhook-id", json_payload='"{\\"message\\": \\"{{title}}\\"}"', ) }, ], ids=[ "Disabled", "Smaller scope", "Bigger scope", "Webhook URL", "JSON Payload", ], ) async def test_webhook_failing_test( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_overseerr_client: AsyncMock, update_mock: dict[str, Any], ) -> None: """Test the webhook configuration.""" mock_overseerr_client.test_webhook_notification_config.return_value = False mock_overseerr_client.get_webhook_notification_config.configure_mock(**update_mock) await setup_integration(hass, mock_config_entry) mock_overseerr_client.test_webhook_notification_config.assert_called_once() mock_overseerr_client.set_webhook_notification_config.assert_not_called()