From 7e1470ea4613b3880b2b185e83fe1176f3450d36 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 27 Mar 2020 09:27:43 +0100 Subject: [PATCH] refactor preconfigure handling to not break deltabot's usage of deltachat's test fixtures and relax timestamp comparisons --- python/src/deltachat/account.py | 6 ++-- python/src/deltachat/testplugin.py | 44 ++++++++++++++++++++++++++---- python/tests/conftest.py | 28 ------------------- python/tests/data/key | 1 + python/tests/test_account.py | 11 ++++---- 5 files changed, 48 insertions(+), 42 deletions(-) create mode 120000 python/tests/data/key diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index c70c54deb..f9b71ed0a 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -46,7 +46,6 @@ class Account(object): ) hook = hookspec.Global._get_plugin_manager().hook - hook.account_init(account=self) self._threads = iothreads.IOThreads(self) self._hook_event_queue = queue.Queue() @@ -61,6 +60,7 @@ class Account(object): raise ValueError("Could not dc_open: {}".format(db_path)) self._configkeys = self.get_config("sys.config_keys").split() atexit.register(self.shutdown) + hook.account_init(account=self) @hookspec.account_hookimpl def process_ffi_event(self, ffi_event): @@ -519,11 +519,11 @@ class Account(object): # meta API for start/stop and event based processing # - def add_account_plugin(self, plugin): + def add_account_plugin(self, plugin, name=None): """ add an account plugin which implements one or more of the :class:`deltachat.hookspec.PerAccount` hooks. """ - self._pm.register(plugin) + self._pm.register(plugin, name=name) self._pm.check_pending() return plugin diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index 6f1b3437e..c3e92f0eb 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -127,7 +127,37 @@ def session_liveconfig(request): @pytest.fixture -def acfactory(pytestconfig, tmpdir, request, session_liveconfig, datadir): +def data(request): + class Data: + def __init__(self): + # trying to find test data heuristically + # because we are run from a dev-setup with pytest direct, + # through tox, and then maybe also from deltachat-binding + # users like "deltabot". + self.paths = [os.path.normpath(x) for x in [ + os.path.join(os.path.dirname(request.fspath.strpath), "data"), + os.path.join(os.path.dirname(__file__), "..", "..", "..", "test-data") + ]] + + def get_path(self, bn): + """ return path of file or None if it doesn't exist. """ + for path in self.paths: + fn = os.path.join(path, *bn.split("/")) + if os.path.exists(fn): + return fn + print("WARNING: path does not exist: {!r}".format(fn)) + + def read_path(self, bn, mode="r"): + fn = self.get_path(bn) + if fn is not None: + with open(fn, mode) as f: + return f.read() + + return Data() + + +@pytest.fixture +def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data): class AccountMaker: def __init__(self): @@ -164,11 +194,13 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, datadir): # Only set a key if we haven't used it yet for another account. if self._generated_keys: keyname = self._generated_keys.pop(0) - fname_pub = "key/{name}-public.asc".format(name=keyname) - fname_sec = "key/{name}-secret.asc".format(name=keyname) - account._preconfigure_keypair(addr, - datadir.join(fname_pub).read(), - datadir.join(fname_sec).read()) + fname_pub = data.read_path("key/{name}-public.asc".format(name=keyname)) + fname_sec = data.read_path("key/{name}-secret.asc".format(name=keyname)) + if fname_pub and fname_sec: + account._preconfigure_keypair(addr, fname_pub, fname_sec) + return True + else: + print("WARN: could not use preconfigured keys for {!r}".format(addr)) def get_configured_offline_account(self): ac = self.get_unconfigured_account() diff --git a/python/tests/conftest.py b/python/tests/conftest.py index 2db21033e..b32e2fd0d 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -1,33 +1,5 @@ from __future__ import print_function -import os -import pytest -import py - - -@pytest.fixture(scope="session") -def data(): - class Data: - def __init__(self): - self.path = os.path.join(os.path.dirname(__file__), "data") - - def get_path(self, bn): - fn = os.path.join(self.path, bn) - assert os.path.exists(fn) - return fn - return Data() - - -@pytest.fixture(scope='session') -def datadir(): - """The py.path.local object of the test-data/ directory.""" - for path in reversed(py.path.local(__file__).parts()): - datadir = path.join('test-data') - if datadir.isdir(): - return datadir - else: - pytest.skip('test-data directory not found') - def wait_configuration_progress(account, min_target, max_target=1001): min_target = min(min_target, max_target) diff --git a/python/tests/data/key b/python/tests/data/key new file mode 120000 index 000000000..0351f725d --- /dev/null +++ b/python/tests/data/key @@ -0,0 +1 @@ +../../../test-data/key \ No newline at end of file diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 69b3f3b0b..a86170f8b 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -26,11 +26,12 @@ class TestOfflineAccountBasic: ac1 = Account(p.strpath, os_name="solarpunk") ac1.get_info() - def test_preconfigure_keypair(self, acfactory, datadir): + def test_preconfigure_keypair(self, acfactory, data): ac = acfactory.get_unconfigured_account() - ac._preconfigure_keypair("alice@example.com", - datadir.join("key/alice-public.asc").read(), - datadir.join("key/alice-secret.asc").read()) + alice_public = data.read_path("key/alice-public.asc") + alice_secret = data.read_path("key/alice-secret.asc") + assert alice_public and alice_secret + ac._preconfigure_keypair("alice@example.com", alice_public, alice_secret) def test_getinfo(self, acfactory): ac1 = acfactory.get_unconfigured_account() @@ -827,7 +828,7 @@ class TestOnlineAccount: assert msg_in in chat2.get_messages() assert chat2.is_deaddrop() assert chat2.count_fresh_messages() == 0 - assert msg_in.time_received > msg_in.time_sent + assert msg_in.time_received >= msg_out.time_sent lp.sec("create new chat with contact and verify it's proper") chat2b = ac2.create_chat_by_message(msg_in)