""" Tests for the email reminder template consistency fixes: - Part D: NotificationService.send_email auto-wraps plain-text in branded template - Part A: dept-champion notification uses email= (not recipient=) kwarg - Part B/C: branded reminder templates render with the expected context """ from unittest.mock import patch from django.template.loader import render_to_string from django.test import TestCase from apps.notifications.services import NotificationService class AutoWrapBrandedTemplateTests(TestCase): """Part D — plain-text messages are auto-wrapped in the branded template.""" @patch("apps.notifications.services.send_mail") def test_plain_text_gets_branded_html(self, mock_send_mail): NotificationService.send_email( email="patient@example.com", subject="Test Subject", message="Line one.\n\nLine two.\nLine three.", ) # send_mail was called with an html_message _, kwargs = mock_send_mail.call_args self.assertIsNotNone(kwargs.get("html_message")) html = kwargs["html_message"] # The branded template contributes these structural markers self.assertIn("email-container", html) # the 600px card from base_email_template self.assertIn("", html) # single newline ->
self.assertIn("

", html) # double newline -> new paragraph @patch("apps.notifications.services.send_mail") def test_explicit_html_message_is_not_overwritten(self, mock_send_mail): """Callers that already provide html_message keep it as-is (no double-wrapping).""" custom_html = "

My custom layout
" NotificationService.send_email( email="patient@example.com", subject="Test", message="Plain text fallback", html_message=custom_html, ) _, kwargs = mock_send_mail.call_args self.assertEqual(kwargs["html_message"], custom_html) class DeptChampionNotificationKwargTests(TestCase): """Part A — the recipient= bug is fixed (send_email uses email= kwarg). We verify indirectly: send_email no longer receives an unexpected 'recipient' kwarg, which used to cause a TypeError swallowed by try/except. """ @patch("apps.notifications.services.send_mail") def test_send_email_signature_rejects_recipient_kwarg(self, mock_mail): """The send_email method must not accept 'recipient' as a kwarg (the bug). Python validates kwargs at call time, so TypeError fires before the body runs — we must NOT mock send_email itself. """ with self.assertRaises(TypeError): NotificationService.send_email( recipient="someone@example.com", # wrong kwarg — should TypeError subject="Test", message="Test", ) class BrandedReminderTemplateTests(TestCase): """Parts B & C — the reminder templates extend the base and render branded HTML.""" def _assert_branded(self, html): """Structural markers contributed by base_email_template.html.""" self.assertIn("