From 41d524c9a0a4c9faee507e11064175740d698cef Mon Sep 17 00:00:00 2001 From: Akhil Narang Date: Fri, 13 Oct 2023 17:54:57 +0530 Subject: [PATCH] chore: add in a test for failed email notification Signed-off-by: Akhil Narang --- .../doctype/email_queue/test_email_queue.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/frappe/email/doctype/email_queue/test_email_queue.py b/frappe/email/doctype/email_queue/test_email_queue.py index 5a608b1b23..dbd01019b9 100644 --- a/frappe/email/doctype/email_queue/test_email_queue.py +++ b/frappe/email/doctype/email_queue/test_email_queue.py @@ -1,7 +1,9 @@ # Copyright (c) 2015, Frappe Technologies and Contributors # License: MIT. See LICENSE +import textwrap import frappe +from frappe.email.doctype.email_queue.email_queue import SendMailContext, get_email_retry_limit from frappe.tests.utils import FrappeTestCase @@ -39,3 +41,40 @@ class TestEmailQueue(FrappeTestCase): self.assertTrue(frappe.db.exists("Email Queue", new_record.name)) self.assertTrue(frappe.db.exists("Email Queue Recipient", {"parent": new_record.name})) + + def test_failed_email_notification(self): + subject = frappe.generate_hash() + email_record = frappe.new_doc("Email Queue") + email_record.sender = "Test " + email_record.message = textwrap.dedent( + f"""\ + MIME-Version: 1.0 + Message-Id: {frappe.generate_hash()} + X-Original-From: Test + Subject: {subject} + From: Test + To: + Date: {frappe.utils.now_datetime().strftime('%a, %d %b %Y %H:%M:%S %z')} + Reply-To: test@example.com + X-Frappe-Site: {frappe.local.site} + """ + ) + email_record.status = "Error" + email_record.retry = get_email_retry_limit() + email_record.priority = 1 + email_record.reference_doctype = "User" + email_record.reference_name = "Administrator" + email_record.insert() + + # Simulate an exception so that we get a notification + try: + with SendMailContext(queue_doc=email_record): + raise Exception("Test Exception") + except Exception: + pass + + notification_log = frappe.db.get_value( + "Notification Log", + {"subject": f"Failed to send email with subject: {subject}"}, + ) + self.assertTrue(notification_log)