"""Comprehensive unit tests for app/api/diagnostic.py module.""" from unittest.mock import patch import pytest @pytest.mark.unit class TestTestNotification: """Tests for POST /diagnostic/test-notification endpoint.""" @patch("app.utils.notification.send_notification") def test_test_notification_success(self, mock_send): """Test successful notification send.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): with patch.object(settings, "external_hostname", "test-host"): # Should return success status # mock_send should be called with correct parameters pass @patch("app.utils.notification.send_notification") def test_test_notification_no_services_configured(self, mock_send): """Test notification when no services are configured.""" from app.config import settings with patch.object(settings, "notification_urls", []): # Should return warning status # mock_send should not be called pass @patch("app.utils.notification.send_notification") def test_test_notification_send_failure(self, mock_send): """Test notification when send fails.""" from app.config import settings mock_send.return_value = False with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # Should return error status pass @patch("app.utils.notification.send_notification") def test_test_notification_exception(self, mock_send): """Test notification when exception occurs.""" from app.config import settings mock_send.side_effect = Exception("Notification error") with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # Should return error status with exception message pass @patch("app.utils.notification.send_notification") def test_test_notification_includes_timestamp(self, mock_send): """Test that notification includes timestamp.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): with patch.object(settings, "external_hostname", "test-host"): # Notification message should include request_time pass @patch("app.utils.notification.send_notification") def test_test_notification_uses_external_hostname(self, mock_send): """Test that notification uses external_hostname in title.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): with patch.object(settings, "external_hostname", "my-custom-host"): # Title should include "my-custom-host" pass @patch("app.utils.notification.send_notification") def test_test_notification_fallback_hostname(self, mock_send): """Test notification when external_hostname is not set.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): with patch.object(settings, "external_hostname", None): # Should use fallback "Document Processor" pass @patch("app.utils.notification.send_notification") def test_test_notification_correct_tags(self, mock_send): """Test that notification includes correct tags.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # Notification should have tags: ["test", "notification", "diagnostic"] pass @patch("app.utils.notification.send_notification") def test_test_notification_success_type(self, mock_send): """Test that notification type is 'success'.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # notification_type should be "success" pass @patch("app.utils.notification.send_notification") def test_test_notification_multiple_services(self, mock_send): """Test notification with multiple configured services.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test1", "https://ntfy.sh/test2"]): # Response should indicate 2 services pass @patch("app.utils.notification.send_notification") def test_test_notification_logs_success(self, mock_send): """Test that successful notification is logged.""" from app.config import settings mock_send.return_value = True with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # Should log at INFO level pass @patch("app.utils.notification.send_notification") def test_test_notification_logs_failure(self, mock_send): """Test that failed notification is logged.""" from app.config import settings mock_send.return_value = False with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # Should log at WARNING level pass @patch("app.utils.notification.send_notification") def test_test_notification_logs_exception(self, mock_send): """Test that exceptions are logged.""" from app.config import settings mock_send.side_effect = Exception("Test error") with patch.object(settings, "notification_urls", ["https://ntfy.sh/test"]): # Should log exception pass