1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 02:09:17 +02:00

refine test setup wrt to helper methods, also xfail a test on travis

This commit is contained in:
holger krekel 2018-09-24 19:34:43 +02:00
parent 366da3e9fa
commit d392324ec6
4 changed files with 60 additions and 51 deletions

View file

@ -23,6 +23,13 @@ def acfactory(pytestconfig, tmpdir, request):
def __init__(self): def __init__(self):
self.live_count = 0 self.live_count = 0
self.offline_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 @cached_property
def configlist (self): def configlist (self):
@ -44,19 +51,18 @@ def acfactory(pytestconfig, tmpdir, request):
return ac return ac
def get_configured_offline_account(self): def get_configured_offline_account(self):
self.offline_count += 1 ac = self.get_unconfigured_account()
tmpdb = tmpdir.join("offlinedb%d" % self.offline_count)
ac = Account(tmpdb.strpath, logid="ac{}".format(self.offline_count))
# do a pseudo-configured account # do a pseudo-configured account
addr = "addr{}@offline.org".format(self.offline_count) addr = "addr{}@offline.org".format(self.offline_count)
ac.set_config("addr", addr) ac.set_config("addr", addr)
lib.dc_set_config(ac._dc_context, b"configured_addr", addr.encode("ascii")) 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); lib.dc_set_config_int(ac._dc_context, b"configured", 1);
ac._evlogger.set_timeout(2)
return ac return ac
def get_live_account(self, started=True): def get_online_configuring_account(self):
if not fn: if not fn:
pytest.skip("specify a --liveconfig file to run tests with real accounts") pytest.skip("specify a --liveconfig file to run tests with real accounts")
self.live_count += 1 self.live_count += 1
@ -65,9 +71,8 @@ def acfactory(pytestconfig, tmpdir, request):
ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count)) ac = Account(tmpdb.strpath, logid="ac{}".format(self.live_count))
ac._evlogger.set_timeout(30) ac._evlogger.set_timeout(30)
ac.configure(**configdict) ac.configure(**configdict)
if started: ac.start_threads()
ac.start() self._finalizers.append(ac.stop_threads)
request.addfinalizer(ac.shutdown)
return ac return ac
return AccountMaker() return AccountMaker()
@ -87,3 +92,24 @@ def lp():
def step(self, msg): def step(self, msg):
print("-" * 5, "step " + msg, "-" * 5) print("-" * 5, "step " + msg, "-" * 5)
return Printer() 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)

View file

@ -114,6 +114,7 @@ class Account(object):
name = as_dc_charpointer(name) name = as_dc_charpointer(name)
email = as_dc_charpointer(email) email = as_dc_charpointer(email)
contact_id = lib.dc_create_contact(self._dc_context, name, 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) return Contact(self._dc_context, contact_id)
def get_contacts(self, query=None, with_self=False, only_verified=False): 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] msg_ids = [msg.id for msg in messages]
lib.dc_delete_msgs(self._dc_context, msg_ids, len(msg_ids)) lib.dc_delete_msgs(self._dc_context, msg_ids, len(msg_ids))
def start(self): def start_threads(self):
""" configure this account object, start receiving events, """ start IMAP/SMTP threads (and configure account if it hasn't happened).
start IMAP/SMTP threads. """
:raises: ValueError if 'addr' or 'mail_pw' are not configured.
:returns: None
"""
if not self.is_configured(): if not self.is_configured():
self.configure() self.configure()
self._threads.start() self._threads.start()
def shutdown(self): def stop_threads(self):
""" shutdown IMAP/SMTP threads and stop receiving events""" """ stop IMAP/SMTP threads. """
self._threads.stop(wait=True) self._threads.stop(wait=True)
def _process_event(self, ctx, evt_name, data1, data2): def _process_event(self, ctx, evt_name, data1, data2):

View file

@ -1,6 +1,7 @@
from __future__ import print_function from __future__ import print_function
import pytest import pytest
from deltachat.capi import lib from deltachat.capi import lib
from conftest import wait_configuration_progress, wait_successful_IMAP_SMTP_connection
class TestOfflineAccount: class TestOfflineAccount:
@ -95,31 +96,8 @@ class TestOfflineAccount:
assert not msg_state.is_out_delivered() assert not msg_state.is_out_delivered()
assert not msg_state.is_out_mdn_received() assert not msg_state.is_out_mdn_received()
def test_basic_configure_ok_addr_setting_forbidden(self, acfactory):
class TestOnlineAccount: ac1 = acfactory.get_configured_offline_account()
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)
assert ac1.get_config("mail_pw") assert ac1.get_config("mail_pw")
assert ac1.is_configured() assert ac1.is_configured()
with pytest.raises(ValueError): with pytest.raises(ValueError):
@ -127,17 +105,19 @@ class TestOnlineAccount:
with pytest.raises(ValueError): with pytest.raises(ValueError):
ac1.configure(addr="123@example.org") ac1.configure(addr="123@example.org")
class TestOnlineAccount:
def test_forward_messages(self, acfactory): def test_forward_messages(self, acfactory):
ac1 = acfactory.get_live_account() ac1 = acfactory.get_online_configuring_account()
ac2 = acfactory.get_live_account() ac2 = acfactory.get_online_configuring_account()
c2 = ac1.create_contact(email=ac2.get_config("addr")) c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2) chat = ac1.create_chat_by_contact(c2)
assert chat.id >= lib.DC_CHAT_ID_LAST_SPECIAL 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") msg_out = chat.send_text_message("message2")
# wait for other account to receive # wait for other account to receive
@ -161,16 +141,14 @@ class TestOnlineAccount:
def test_send_and_receive_message(self, acfactory, lp): def test_send_and_receive_message(self, acfactory, lp):
lp.sec("starting accounts, waiting for configuration") lp.sec("starting accounts, waiting for configuration")
ac1 = acfactory.get_live_account() ac1 = acfactory.get_online_configuring_account()
ac2 = acfactory.get_live_account() ac2 = acfactory.get_online_configuring_account()
c2 = ac1.create_contact(email=ac2.get_config("addr")) c2 = ac1.create_contact(email=ac2.get_config("addr"))
chat = ac1.create_chat_by_contact(c2) chat = ac1.create_chat_by_contact(c2)
assert chat.id >= lib.DC_CHAT_ID_LAST_SPECIAL assert chat.id >= lib.DC_CHAT_ID_LAST_SPECIAL
self.wait_successful_IMAP_SMTP_connection(ac1) wait_configuration_progress(ac1, 1000)
self.wait_successful_IMAP_SMTP_connection(ac2) wait_configuration_progress(ac2, 1000)
self.wait_configuration_progress(ac1, 1000)
self.wait_configuration_progress(ac2, 1000)
lp.sec("sending text message from ac1 to ac2") lp.sec("sending text message from ac1 to ac2")
msg_out = chat.send_text_message("message1") msg_out = chat.send_text_message("message1")

View file

@ -9,6 +9,7 @@ envlist =
[testenv] [testenv]
commands = pytest -rsXx {posargs:tests} commands = pytest -rsXx {posargs:tests}
usedevelop = True usedevelop = True
passenv = TRAVIS
deps = deps =
pytest pytest
pytest-faulthandler pytest-faulthandler
@ -43,7 +44,7 @@ commands =
[pytest] [pytest]
python_files = tests/test_*.py python_files = tests/test_*.py
norecursedirs = .tox norecursedirs = .tox
# xfail_strict=true xfail_strict=true
[flake8] [flake8]
max-line-length = 120 max-line-length = 120