diff --git a/python/conftest.py b/python/conftest.py index 9539890c..20c50e66 100644 --- a/python/conftest.py +++ b/python/conftest.py @@ -23,6 +23,13 @@ def acfactory(pytestconfig, tmpdir, request): def __init__(self): self.live_count = 0 self.offline_count = 0 + self._finalizers = [] + request.addfinalizer(self.finalize) + + def finalize(self): + while self._finalizers: + fin = self._finalizers.pop() + fin() @cached_property def configlist (self): @@ -44,19 +51,18 @@ def acfactory(pytestconfig, tmpdir, request): return ac def get_configured_offline_account(self): - self.offline_count += 1 - tmpdb = tmpdir.join("offlinedb%d" % self.offline_count) - ac = Account(tmpdb.strpath, logid="ac{}".format(self.offline_count)) + ac = self.get_unconfigured_account() # do a pseudo-configured account addr = "addr{}@offline.org".format(self.offline_count) ac.set_config("addr", addr) lib.dc_set_config(ac._dc_context, b"configured_addr", addr.encode("ascii")) + ac.set_config("mail_pw", "123") + lib.dc_set_config(ac._dc_context, b"configured_mail_pw", b"123") lib.dc_set_config_int(ac._dc_context, b"configured", 1); - ac._evlogger.set_timeout(2) return ac - def get_live_account(self, started=True): + def get_online_configuring_account(self): if not fn: pytest.skip("specify a --liveconfig file to run tests with real accounts") self.live_count += 1 @@ -65,9 +71,8 @@ def acfactory(pytestconfig, tmpdir, request): ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count)) ac._evlogger.set_timeout(30) ac.configure(**configdict) - if started: - ac.start() - request.addfinalizer(ac.shutdown) + ac.start_threads() + self._finalizers.append(ac.stop_threads) return ac return AccountMaker() @@ -87,3 +92,24 @@ def lp(): def step(self, msg): print("-" * 5, "step " + msg, "-" * 5) return Printer() + + +def wait_configuration_progress(account, target): + while 1: + evt_name, data1, data2 = \ + account._evlogger.get_matching("DC_EVENT_CONFIGURE_PROGRESS") + if data1 >= target: + print("** CONFIG PROGRESS {}".format(target), account) + break + + +def wait_successful_IMAP_SMTP_connection(account): + imap_ok = smtp_ok = False + while not imap_ok or not smtp_ok: + evt_name, data1, data2 = \ + account._evlogger.get_matching("DC_EVENT_(IMAP|SMTP)_CONNECTED") + if evt_name == "DC_EVENT_IMAP_CONNECTED": + imap_ok = True + if evt_name == "DC_EVENT_SMTP_CONNECTED": + smtp_ok = True + print("** IMAP and SMTP logins successful", account) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 263d8577..c6017494 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -114,6 +114,7 @@ class Account(object): name = as_dc_charpointer(name) email = as_dc_charpointer(email) contact_id = lib.dc_create_contact(self._dc_context, name, email) + assert contact_id > lib.DC_CHAT_ID_LAST_SPECIAL return Contact(self._dc_context, contact_id) def get_contacts(self, query=None, with_self=False, only_verified=False): @@ -228,15 +229,18 @@ class Account(object): msg_ids = [msg.id for msg in messages] lib.dc_delete_msgs(self._dc_context, msg_ids, len(msg_ids)) - def start(self): - """ configure this account object, start receiving events, - start IMAP/SMTP threads. """ + def start_threads(self): + """ start IMAP/SMTP threads (and configure account if it hasn't happened). + + :raises: ValueError if 'addr' or 'mail_pw' are not configured. + :returns: None + """ if not self.is_configured(): self.configure() self._threads.start() - def shutdown(self): - """ shutdown IMAP/SMTP threads and stop receiving events""" + def stop_threads(self): + """ stop IMAP/SMTP threads. """ self._threads.stop(wait=True) def _process_event(self, ctx, evt_name, data1, data2): diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 475e584d..3276da34 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -1,6 +1,7 @@ from __future__ import print_function import pytest from deltachat.capi import lib +from conftest import wait_configuration_progress, wait_successful_IMAP_SMTP_connection class TestOfflineAccount: @@ -95,31 +96,8 @@ class TestOfflineAccount: assert not msg_state.is_out_delivered() assert not msg_state.is_out_mdn_received() - -class TestOnlineAccount: - def wait_successful_IMAP_SMTP_connection(self, account): - imap_ok = smtp_ok = False - while not imap_ok or not smtp_ok: - evt_name, data1, data2 = \ - account._evlogger.get_matching("DC_EVENT_(IMAP|SMTP)_CONNECTED") - if evt_name == "DC_EVENT_IMAP_CONNECTED": - imap_ok = True - if evt_name == "DC_EVENT_SMTP_CONNECTED": - smtp_ok = True - print("** IMAP and SMTP logins successful", account) - - def wait_configuration_progress(self, account, target): - while 1: - evt_name, data1, data2 = \ - account._evlogger.get_matching("DC_EVENT_CONFIGURE_PROGRESS") - if data1 >= target: - print("** CONFIG PROGRESS {}".format(target), account) - break - - def test_basic_configure_login_ok(self, acfactory): - ac1 = acfactory.get_live_account() - self.wait_successful_IMAP_SMTP_connection(ac1) - self.wait_configuration_progress(ac1, 1000) + def test_basic_configure_ok_addr_setting_forbidden(self, acfactory): + ac1 = acfactory.get_configured_offline_account() assert ac1.get_config("mail_pw") assert ac1.is_configured() with pytest.raises(ValueError): @@ -127,17 +105,19 @@ class TestOnlineAccount: with pytest.raises(ValueError): ac1.configure(addr="123@example.org") + +class TestOnlineAccount: def test_forward_messages(self, acfactory): - ac1 = acfactory.get_live_account() - ac2 = acfactory.get_live_account() + ac1 = acfactory.get_online_configuring_account() + ac2 = acfactory.get_online_configuring_account() c2 = ac1.create_contact(email=ac2.get_config("addr")) chat = ac1.create_chat_by_contact(c2) assert chat.id >= lib.DC_CHAT_ID_LAST_SPECIAL + wait_successful_IMAP_SMTP_connection(ac1) + wait_configuration_progress(ac1, 1000) + wait_successful_IMAP_SMTP_connection(ac2) + wait_configuration_progress(ac2, 1000) - self.wait_successful_IMAP_SMTP_connection(ac1) - self.wait_successful_IMAP_SMTP_connection(ac2) - self.wait_configuration_progress(ac1, 1000) - self.wait_configuration_progress(ac2, 1000) msg_out = chat.send_text_message("message2") # wait for other account to receive @@ -161,16 +141,14 @@ class TestOnlineAccount: def test_send_and_receive_message(self, acfactory, lp): lp.sec("starting accounts, waiting for configuration") - ac1 = acfactory.get_live_account() - ac2 = acfactory.get_live_account() + ac1 = acfactory.get_online_configuring_account() + ac2 = acfactory.get_online_configuring_account() c2 = ac1.create_contact(email=ac2.get_config("addr")) chat = ac1.create_chat_by_contact(c2) assert chat.id >= lib.DC_CHAT_ID_LAST_SPECIAL - self.wait_successful_IMAP_SMTP_connection(ac1) - self.wait_successful_IMAP_SMTP_connection(ac2) - self.wait_configuration_progress(ac1, 1000) - self.wait_configuration_progress(ac2, 1000) + wait_configuration_progress(ac1, 1000) + wait_configuration_progress(ac2, 1000) lp.sec("sending text message from ac1 to ac2") msg_out = chat.send_text_message("message1") diff --git a/python/tox.ini b/python/tox.ini index 8fab1f18..4864835e 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -9,6 +9,7 @@ envlist = [testenv] commands = pytest -rsXx {posargs:tests} usedevelop = True +passenv = TRAVIS deps = pytest pytest-faulthandler @@ -43,7 +44,7 @@ commands = [pytest] python_files = tests/test_*.py norecursedirs = .tox -# xfail_strict=true +xfail_strict=true [flake8] max-line-length = 120