mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 01:16:31 +03:00
streamline session_live_config implementation and usage
This commit is contained in:
@@ -9,7 +9,7 @@ import fnmatch
|
|||||||
import time
|
import time
|
||||||
import weakref
|
import weakref
|
||||||
import tempfile
|
import tempfile
|
||||||
from typing import List, Dict, Callable
|
from typing import List, Callable
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import requests
|
import requests
|
||||||
@@ -126,57 +126,48 @@ def pytest_report_header(config, startdir):
|
|||||||
return summary
|
return summary
|
||||||
|
|
||||||
|
|
||||||
class SessionLiveConfigFromFile:
|
@pytest.fixture(scope="session")
|
||||||
def __init__(self, fn) -> None:
|
def session_liveconfig_producer(request):
|
||||||
self.fn = fn
|
""" provide live account configs, cached on a per-test-process scope so that test functions
|
||||||
self.configlist = []
|
can re-use already known live configs. Depending on the --liveconfig option this comes from
|
||||||
for line in open(fn):
|
a HTTP provider or a file with a line specifying each accounts config.
|
||||||
|
"""
|
||||||
|
liveconfig_opt = request.config.option.liveconfig
|
||||||
|
if not liveconfig_opt:
|
||||||
|
def skip_producer():
|
||||||
|
pytest.skip("specify DCC_NEW_TMP_EMAIL or --liveconfig to provide live accounts for testing")
|
||||||
|
return skip_producer
|
||||||
|
|
||||||
|
configlist = []
|
||||||
|
if not liveconfig_opt.startswith("http"):
|
||||||
|
for line in open(liveconfig_opt):
|
||||||
if line.strip() and not line.strip().startswith('#'):
|
if line.strip() and not line.strip().startswith('#'):
|
||||||
d = {}
|
d = {}
|
||||||
for part in line.split():
|
for part in line.split():
|
||||||
name, value = part.split("=")
|
name, value = part.split("=")
|
||||||
d[name] = value
|
d[name] = value
|
||||||
self.configlist.append(d)
|
configlist.append(d)
|
||||||
|
|
||||||
def get(self, index: int):
|
def from_file_producer():
|
||||||
return self.configlist[index]
|
return iter(configlist)
|
||||||
|
|
||||||
def exists(self) -> bool:
|
return from_file_producer
|
||||||
return bool(self.configlist)
|
|
||||||
|
|
||||||
|
def caching_producer_from_url():
|
||||||
|
for index in range(10):
|
||||||
|
try:
|
||||||
|
yield configlist[index]
|
||||||
|
except IndexError:
|
||||||
|
res = requests.post(liveconfig_opt)
|
||||||
|
if res.status_code != 200:
|
||||||
|
pytest.fail("creating newtmpuser failed with code {}: '{}'".format(res.status_code, res.text))
|
||||||
|
d = res.json()
|
||||||
|
config = dict(addr=d["email"], mail_pw=d["password"])
|
||||||
|
configlist.append(config)
|
||||||
|
yield config
|
||||||
|
pytest.fail("more than 10 live accounts requested. Is a test running wild?")
|
||||||
|
|
||||||
class SessionLiveConfigFromURL:
|
return caching_producer_from_url
|
||||||
configlist: List[Dict[str, str]]
|
|
||||||
|
|
||||||
def __init__(self, url: str) -> None:
|
|
||||||
self.configlist = []
|
|
||||||
self.url = url
|
|
||||||
|
|
||||||
def get(self, index: int):
|
|
||||||
try:
|
|
||||||
return self.configlist[index]
|
|
||||||
except IndexError:
|
|
||||||
assert index == len(self.configlist), index
|
|
||||||
res = requests.post(self.url)
|
|
||||||
if res.status_code != 200:
|
|
||||||
pytest.skip("creating newtmpuser failed with code {}: '{}'".format(res.status_code, res.text))
|
|
||||||
d = res.json()
|
|
||||||
config = dict(addr=d["email"], mail_pw=d["password"])
|
|
||||||
self.configlist.append(config)
|
|
||||||
return config
|
|
||||||
|
|
||||||
def exists(self) -> bool:
|
|
||||||
return bool(self.configlist)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
|
||||||
def session_liveconfig(request):
|
|
||||||
liveconfig_opt = request.config.option.liveconfig
|
|
||||||
if liveconfig_opt:
|
|
||||||
if liveconfig_opt.startswith("http"):
|
|
||||||
return SessionLiveConfigFromURL(liveconfig_opt)
|
|
||||||
else:
|
|
||||||
return SessionLiveConfigFromFile(liveconfig_opt)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -213,10 +204,10 @@ class ACFactory:
|
|||||||
_finalizers: List[Callable[[], None]]
|
_finalizers: List[Callable[[], None]]
|
||||||
_accounts: List[Account]
|
_accounts: List[Account]
|
||||||
|
|
||||||
def __init__(self, strict_tls, tmpdir, session_liveconfig, data) -> None:
|
def __init__(self, strict_tls, tmpdir, session_liveconfig_producer, data) -> None:
|
||||||
self.strict_tls = strict_tls
|
self.strict_tls = strict_tls
|
||||||
self.tmpdir = tmpdir
|
self.tmpdir = tmpdir
|
||||||
self.session_liveconfig = session_liveconfig
|
self._liveconfig_producer = session_liveconfig_producer()
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
self.live_count = 0
|
self.live_count = 0
|
||||||
@@ -240,6 +231,9 @@ class ACFactory:
|
|||||||
acc.disable_logging()
|
acc.disable_logging()
|
||||||
deltachat.unregister_global_plugin(direct_imap)
|
deltachat.unregister_global_plugin(direct_imap)
|
||||||
|
|
||||||
|
def get_next_liveconfig(self):
|
||||||
|
return next(self._liveconfig_producer)
|
||||||
|
|
||||||
def make_account(self, which):
|
def make_account(self, which):
|
||||||
if which == "offline":
|
if which == "offline":
|
||||||
self.offline_count += 1
|
self.offline_count += 1
|
||||||
@@ -305,9 +299,7 @@ class ACFactory:
|
|||||||
""" Base function to get functional online accounts where we can make
|
""" Base function to get functional online accounts where we can make
|
||||||
valid SMTP and IMAP connections with.
|
valid SMTP and IMAP connections with.
|
||||||
"""
|
"""
|
||||||
if not self.session_liveconfig:
|
configdict = self.get_next_liveconfig()
|
||||||
pytest.skip("specify DCC_NEW_TMP_EMAIL or --liveconfig")
|
|
||||||
configdict = self.session_liveconfig.get(self.live_count)
|
|
||||||
if "e2ee_enabled" not in configdict:
|
if "e2ee_enabled" not in configdict:
|
||||||
configdict["e2ee_enabled"] = "1"
|
configdict["e2ee_enabled"] = "1"
|
||||||
|
|
||||||
@@ -453,9 +445,9 @@ class ACFactory:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data):
|
def acfactory(pytestconfig, tmpdir, request, session_liveconfig_producer, data):
|
||||||
strict_tls = pytestconfig.getoption("--strict-tls")
|
strict_tls = pytestconfig.getoption("--strict-tls")
|
||||||
am = ACFactory(strict_tls, tmpdir, session_liveconfig, data)
|
am = ACFactory(strict_tls, tmpdir, session_liveconfig_producer, data)
|
||||||
request.addfinalizer(am.finalize)
|
request.addfinalizer(am.finalize)
|
||||||
yield am
|
yield am
|
||||||
if hasattr(request.node, "rep_call") and request.node.rep_call.failed:
|
if hasattr(request.node, "rep_call") and request.node.rep_call.failed:
|
||||||
|
|||||||
Reference in New Issue
Block a user