From 77e91427225704dbfa4336bc24bb9b5429ff908a Mon Sep 17 00:00:00 2001 From: Manu <4445816+tr4nt0r@users.noreply.github.com> Date: Mon, 12 May 2025 07:25:43 +0200 Subject: [PATCH] Increase test coverage for ntfy integration (#144701) Increase test coverage --- tests/components/ntfy/test_notify.py | 54 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/tests/components/ntfy/test_notify.py b/tests/components/ntfy/test_notify.py index 76bf1049ae8..ec947ba5a1f 100644 --- a/tests/components/ntfy/test_notify.py +++ b/tests/components/ntfy/test_notify.py @@ -4,7 +4,11 @@ from collections.abc import AsyncGenerator from unittest.mock import patch from aiontfy import Message -from aiontfy.exceptions import NtfyException, NtfyHTTPError +from aiontfy.exceptions import ( + NtfyException, + NtfyHTTPError, + NtfyUnauthorizedAuthenticationError, +) from freezegun.api import freeze_time import pytest from syrupy.assertion import SnapshotAssertion @@ -15,7 +19,8 @@ from homeassistant.components.notify import ( DOMAIN as NOTIFY_DOMAIN, SERVICE_SEND_MESSAGE, ) -from homeassistant.config_entries import ConfigEntryState +from homeassistant.components.ntfy.const import DOMAIN +from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -101,6 +106,10 @@ async def test_send_message( NtfyException, "Failed to publish notification due to a connection error", ), + ( + NtfyUnauthorizedAuthenticationError(40101, 401, "unauthorized"), + "Failed to authenticate with ntfy service. Please verify your credentials", + ), ], ) async def test_send_message_exception( @@ -135,3 +144,44 @@ async def test_send_message_exception( mock_aiontfy.publish.assert_called_once_with( Message(topic="mytopic", message="triggered", title="test") ) + + +async def test_send_message_reauth_flow( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_aiontfy: AsyncMock, +) -> None: + """Test unauthorized exception initiates reauth flow.""" + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + mock_aiontfy.publish.side_effect = ( + NtfyUnauthorizedAuthenticationError(40101, 401, "unauthorized"), + ) + + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + NOTIFY_DOMAIN, + SERVICE_SEND_MESSAGE, + { + ATTR_ENTITY_ID: "notify.mytopic", + ATTR_MESSAGE: "triggered", + ATTR_TITLE: "test", + }, + blocking=True, + ) + + 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") == config_entry.entry_id