From af33c2dea7a481016052b0953fe972064fc1b554 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 5 Jun 2020 17:51:40 +0200 Subject: [PATCH] test pass again --- python/src/deltachat/direct_imap.py | 98 ++++++++++++----------------- python/src/deltachat/testplugin.py | 8 +++ python/tests/test_account.py | 7 +-- 3 files changed, 52 insertions(+), 61 deletions(-) diff --git a/python/src/deltachat/direct_imap.py b/python/src/deltachat/direct_imap.py index 8247b08cb..d4edc56f7 100644 --- a/python/src/deltachat/direct_imap.py +++ b/python/src/deltachat/direct_imap.py @@ -2,14 +2,6 @@ import imaplib import pathlib from . import Account -INBOX = "Inbox" -SENT = "Sent" -MVBOX = "DeltaChat" -MVBOX_FALLBBACK = "INBOX/DeltaChat" -DC_CONSTANT_MSG_MOVESTATE_PENDING = 1 -DC_CONSTANT_MSG_MOVESTATE_STAY = 2 -DC_CONSTANT_MSG_MOVESTATE_MOVING = 3 - def db_folder_attr(name): def fget(s): @@ -20,21 +12,52 @@ def db_folder_attr(name): return property(fget, fset, None, None) -class ImapConn(): - def __init__(self, foldername, conn_info): - self.foldername = foldername - host, user, pw = conn_info +class ImapConn: + def __init__(self, account): + self.account = account + self.conn_info = (account.get_config("configured_mail_server"), + account.get_config("addr"), + account.get_config("mail_pw")) + host, user, pw = self.conn_info self.connection = imaplib.IMAP4_SSL(host) self.connection.login(user, pw) - messages = self.reselect_folder() + self._original_msg_count = {} + self.select_folder("INBOX") + + def shutdown(self): try: - self.original_msg_count = int(messages[0]) + self.connection.close() + except Exception: + pass + try: + self.connection.logout() + except Exception: + print("Could not logout direct_imap conn") + + def select_folder(self, foldername): + status, messages = self.connection.select(foldername) + if status != "OK": + raise ConnectionError("Could not select {}: status={} message={}".format( + foldername, status, messages)) + print("selected", foldername, messages) + self.foldername = foldername + try: + msg_count = int(messages[0]) except IndexError: - self.original_msg_count = 0 + msg_count = 0 + + # memorize initial message count on first select + self._original_msg_count.setdefault(foldername, msg_count) + return messages + + def select_config_folder(self, config_name): + if "_" not in config_name: + config_name = "configured_{}_folder".format(config_name) + foldername = self.account.get_config(config_name) + return self.select_folder(foldername) def mark_all_read(self): - self.reselect_folder() # result, data = self.connection.uid('search', None, "(UNSEEN)") result, data = self.connection.search(None, 'UnSeen') try: @@ -52,7 +75,6 @@ class ImapConn(): return False def get_unread_cnt(self): - self.reselect_folder() # result, data = self.connection.uid('search', None, "(UNSEEN)") result, data = self.connection.search(None, 'UnSeen') try: @@ -63,50 +85,12 @@ class ImapConn(): return 0 def get_new_email_cnt(self): - messages = self.reselect_folder() + messages = self.select_folder(self.foldername) try: - return int(messages[0]) - self.original_msg_count + return int(messages[0]) - self._original_msg_count[self.foldername] except IndexError: return 0 - def reselect_folder(self): - status, messages = self.connection.select(self.foldername) - if status != "OK": - print("Incorrect mail box " + status + str(messages)) - raise ConnectionError -# print("(Re-)Selected mailbox: " + status + " " + str(messages)) - return messages - - def __del__(self): - try: - self.connection.close() - except Exception: - pass - try: - self.connection.logout() - except Exception: - print("Could not logout direct_imap conn") - - -def make_direct_imap(account, folder): - conn_info = (account.get_config("configured_mail_server"), - account.get_config("addr"), account.get_config("mail_pw")) - # try: - # return ImapConn(folder, conn_info=conn_info) - # except ConnectionError as e: - # if folder == MVBOX: - # account.log("Selecting " + MVBOX_FALLBBACK + " not " + MVBOX + " because connecting to the latter failed") - # return ImapConn(MVBOX_FALLBBACK, conn_info=conn_info) - # else: - # raise e - if folder == MVBOX: - new_folder = account.get_config("configured_mvbox_folder") - else: - new_folder = folder - if new_folder != folder: - account.log("Making connection with " + new_folder + " not " + folder) - return ImapConn(new_folder, conn_info=conn_info) - def print_imap_structure(database, dir="."): print_imap_structure_ac(Account(database), dir) diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index 939e892e9..b8d4e15ac 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -16,6 +16,7 @@ from . import Account, const, direct_imap from .capi import lib from .events import FFIEventLogger, FFIEventTracker from _pytest._code import Source +from deltachat.direct_imap import ImapConn import deltachat @@ -224,6 +225,13 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data): acc.shutdown() acc.disable_logging() + def new_imap_conn(self, account, config_folder=None): + imap_conn = ImapConn(account) + self._finalizers.append(imap_conn.shutdown) + if config_folder is not None: + imap_conn.select_config_folder(config_folder) + return imap_conn + def make_account(self, path, logid, quiet=False): ac = Account(path, logging=self._logging, logid=logid) ac._evtracker = ac.add_account_plugin(FFIEventTracker(ac)) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 8eead51c2..35772fea6 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -7,8 +7,6 @@ from deltachat import const, Account from deltachat.message import Message from deltachat.hookspec import account_hookimpl from datetime import datetime, timedelta -from deltachat import direct_imap -from deltachat.direct_imap import make_direct_imap def get_chat(ac1, ac2, both_created=False): @@ -1751,7 +1749,8 @@ class TestDirectImap: ac2.wait_configure_finish() ac2.start_io() - imap2 = make_direct_imap(ac2, direct_imap.MVBOX) + imap2 = acfactory.new_imap_conn(ac2, config_folder="mvbox") + # imap2.mark_all_read() assert imap2.get_unread_cnt() == 0 @@ -1795,7 +1794,7 @@ class TestDirectImap: ac2.wait_configure_finish() ac2.start_io() - imap1 = make_direct_imap(ac1, direct_imap.MVBOX) + imap1 = acfactory.new_imap_conn(ac1, config_folder="mvbox") imap1.mark_all_read() assert imap1.get_unread_cnt() == 0