From 84501c663ea9c30ff3d354915e5df5f0fd4ff1e0 Mon Sep 17 00:00:00 2001 From: kamaljohnson Date: Tue, 8 Feb 2022 08:33:09 +0530 Subject: [PATCH] fix: use new mocked_inbound_mail in test_email - remove in_test block from production code --- .../doctype/email_account/email_account.py | 3 --- .../email_account/test_email_account.py | 6 ++--- frappe/tests/test_email.py | 26 ++++++++++++++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index feef9dc234..f20a25f88f 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -471,9 +471,6 @@ class EmailAccount(Document): # only append the emails with status != 'SEEN' if sync option is set to 'UNSEEN' mails.append(InboundMail(message, self, uid, seen_status, append_to)) - if frappe.local.flags.in_test: - return [InboundMail(msg, self) for msg in test_mails or []] - if not self.enable_incoming: return [] diff --git a/frappe/email/doctype/email_account/test_email_account.py b/frappe/email/doctype/email_account/test_email_account.py index fc8b678fde..06c4323f4e 100644 --- a/frappe/email/doctype/email_account/test_email_account.py +++ b/frappe/email/doctype/email_account/test_email_account.py @@ -386,10 +386,9 @@ class TestEmailAccount(unittest.TestCase): self.assertEqual(inbox_mails, 2) self.assertEqual(test_folder_mails, 1) - @patch("frappe.local.flags.in_test", False) @patch("frappe.email.receive.EmailServer.select_imap_folder", return_value=True) @patch("frappe.email.receive.EmailServer.logout", side_effect=lambda: None) - def mocked_get_inbound_mails(email_account, messages={}, mocked_logout=None, mocked_select_imap_folder=None, mocked_in_test=None): + def mocked_get_inbound_mails(email_account, messages={}, mocked_logout=None, mocked_select_imap_folder=None): from frappe.email.receive import EmailServer def get_mocked_messages(**kwargs): @@ -400,10 +399,9 @@ class TestEmailAccount(unittest.TestCase): return mails - @patch("frappe.local.flags.in_test", False) @patch("frappe.email.receive.EmailServer.select_imap_folder", return_value=True) @patch("frappe.email.receive.EmailServer.logout", side_effect=lambda: None) - def mocked_email_receive(email_account, messages={}, mocked_logout=None, mocked_select_imap_folder=None, mocked_in_test=None): + def mocked_email_receive(email_account, messages={}, mocked_logout=None, mocked_select_imap_folder=None): def get_mocked_messages(**kwargs): return messages[kwargs["folder"]] diff --git a/frappe/tests/test_email.py b/frappe/tests/test_email.py index ef9515f5ba..34c401b7b0 100644 --- a/frappe/tests/test_email.py +++ b/frappe/tests/test_email.py @@ -3,6 +3,8 @@ import unittest, frappe, re, email +from frappe.email.doctype.email_account.test_email_account import TestEmailAccount + test_dependencies = ['Email Account'] class TestEmail(unittest.TestCase): @@ -173,12 +175,34 @@ class TestEmail(unittest.TestCase): frappe.db.delete("Communication", {"sender": "sukh@yyy.com"}) with open(frappe.get_app_path('frappe', 'tests', 'data', 'email_with_image.txt'), 'r') as raw: - mails = email_account.get_inbound_mails(test_mails=[raw.read()]) + messages = { + '"INBOX"': { # append_to = ToDo + 'latest_messages': [ + raw.read() + ], + 'seen_status': { + 2: 'UNSEEN' + }, + 'uid_list': [2] + } + } + + email_account = frappe.get_doc("Email Account", "_Test Email Account 1") + changed_flag = False + if not email_account.enable_incoming: + email_account.enable_incoming = True + changed_flag = True + mails = TestEmailAccount.mocked_get_inbound_mails(email_account, messages) + + # mails = email_account.get_inbound_mails(test_mails=[raw.read()]) communication = mails[0].process() self.assertTrue(re.search(''']*src=["']/private/files/rtco1.png[^>]*>''', communication.content)) self.assertTrue(re.search(''']*src=["']/private/files/rtco2.png[^>]*>''', communication.content)) + if changed_flag: + email_account.enable_incoming = False + if __name__ == '__main__': frappe.connect()