from typing import Any from unittest import mock from anydi import Container from app.handlers import SendgridWebhooksReplyHandler, SendgridWebhooksUnsubHandler from app.main import handle from app.models import ReplyRequest, UnsubRequest def test_handle_unsub_path(container: Container, test_event: dict[str, Any]) -> None: """Test that unsub_handler is called for /inbound path""" handler_mock = mock.MagicMock(spec=SendgridWebhooksUnsubHandler) with container.override(SendgridWebhooksUnsubHandler, handler_mock): handle(test_event, None) assert handler_mock.handle.called call_args = handler_mock.handle.call_args assert isinstance(call_args.args[0], UnsubRequest) def test_handle_reply_path( container: Container, test_reply_event: dict[str, Any] ) -> None: """Test that reply_handler is called for /reply-inbound path""" handler_mock = mock.MagicMock(spec=SendgridWebhooksReplyHandler) with container.override(SendgridWebhooksReplyHandler, handler_mock): handle(test_reply_event, None) assert handler_mock.handle.called call_args = handler_mock.handle.call_args assert isinstance(call_args.args[0], ReplyRequest) def test_handle_reply_validation_error_returns_200( container: Container, test_reply_event_invalid_signature: dict[str, Any], ) -> None: unsub_handler_mock = mock.MagicMock(spec=SendgridWebhooksUnsubHandler) reply_handler_mock = mock.MagicMock(spec=SendgridWebhooksReplyHandler) reply_handler_mock.handle.return_value = {"statusCode": 200} with ( container.override(SendgridWebhooksUnsubHandler, unsub_handler_mock), container.override(SendgridWebhooksReplyHandler, reply_handler_mock), ): result = handle(test_reply_event_invalid_signature, None) assert result == {"statusCode": 200} unsub_handler_mock.handle.assert_not_called() reply_handler_mock.handle.assert_called_once() call_args = reply_handler_mock.handle.call_args assert isinstance(call_args.args[0], ReplyRequest)