From caab903edb79fac0420656416cf39584c33a87e6 Mon Sep 17 00:00:00 2001 From: kamaljohnson Date: Mon, 7 Feb 2022 15:49:59 +0530 Subject: [PATCH] fix: add back `in_test` override for `imap_folders` test Since a lot of tests still uses the old way i.e, use `in_test` and execute custome (non-production) code to get results. Kept the same code for now else as a major test refactor needs to be done. --- .../doctype/email_account/email_account.py | 12 ++------ .../email_account/test_email_account.py | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 424690e797..feef9dc234 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -457,7 +457,7 @@ class EmailAccount(Document): if exceptions: raise Exception(frappe.as_json(exceptions)) - def get_inbound_mails(self, test_mails=None, messages=None) -> List[InboundMail]: + def get_inbound_mails(self, test_mails=None) -> List[InboundMail]: """retrive and return inbound mails. """ @@ -471,14 +471,8 @@ 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: - # if self.enable_incoming and not test_mails and messages: - # for folder in self.imap_folder: - # _messages = messages[folder.folder_name] if folder.folder_name in messages and (messages[folder.folder_name] is not None) else {} - # process_mail(_messages, folder.append_to) - # return mails - # else: - # return [InboundMail(msg, self) for msg in test_mails or []] + 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 4c1acf091b..c53f8fa1ab 100644 --- a/frappe/email/doctype/email_account/test_email_account.py +++ b/frappe/email/doctype/email_account/test_email_account.py @@ -257,10 +257,7 @@ class TestEmailAccount(unittest.TestCase): self.assertTrue(communication.reference_name) self.assertTrue(frappe.db.exists(communication.reference_doctype, communication.reference_name)) - @patch("frappe.email.receive.EmailServer.select_imap_folder", return_value=True) - @patch("frappe.email.receive.EmailServer.logout", side_effect=lambda: None) - # @patch("frappe.email.receive.EmailServer.get_messages", side_effect=get_mocked_messages) - def test_append_to_with_imap_folders(self, mocked_logout, mocked_select_imap_folder): + def test_append_to_with_imap_folders(self): mail_content_1 = self.get_test_mail(fname="incoming-1.raw") mail_content_2 = self.get_test_mail(fname="incoming-2.raw") mail_content_3 = self.get_test_mail(fname="incoming-3.raw") @@ -287,14 +284,10 @@ class TestEmailAccount(unittest.TestCase): 'uid_list': [2] } } - from frappe.email.receive import EmailServer - def get_mocked_messages(**args): - return messages[args["folder"]] - with patch.object(EmailServer, "get_messages", side_effect=get_mocked_messages): - email_account = frappe.get_doc("Email Account", "_Test Email Account 1") - mails = email_account.get_inbound_mails(messages=messages) - self.assertEqual(len(mails), 3) + email_account = frappe.get_doc("Email Account", "_Test Email Account 1") + mails = TestEmailAccount.mocked_get_inbound_mails(email_account, messages) + self.assertEqual(len(mails), 3) inbox_mails = 0 test_folder_mails = 0 @@ -313,6 +306,20 @@ 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): + from frappe.email.receive import EmailServer + + def get_mocked_messages(**kwargs): + return messages[kwargs["folder"]] + + with patch.object(EmailServer, "get_messages", side_effect=get_mocked_messages): + mails = email_account.get_inbound_mails() + + return mails + class TestInboundMail(unittest.TestCase): @classmethod def setUpClass(cls):