From 4fc63461fbc5402bd9497fbf0d0059f4d6b06ee0 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 16 Oct 2023 16:56:22 +0200 Subject: [PATCH] - introduce pytest.mark.slow marker and "--slow" CLI option - refactor login tests to allow running them against both imap/smtp --- online-tests/conftest.py | 28 +++++++++++- online-tests/pytest.ini | 3 +- online-tests/test_0_login.py | 77 ++++++++++++-------------------- online-tests/test_1_deltachat.py | 10 ++++- 4 files changed, 66 insertions(+), 52 deletions(-) diff --git a/online-tests/conftest.py b/online-tests/conftest.py index 504e832..00f1f74 100644 --- a/online-tests/conftest.py +++ b/online-tests/conftest.py @@ -8,10 +8,25 @@ import itertools import pytest +def pytest_addoption(parser): + parser.addoption( + "--slow", action="store_true", default=False, help="also run slow tests" + ) + + +def pytest_runtest_setup(item): + markers = list(item.iter_markers(name="slow")) + if markers: + if not item.config.getoption("--slow"): + pytest.skip("skipping slow test, use --slow to run") + + @pytest.fixture def maildomain(): - return os.environ.get("CHATMAIL_DOMAIN", "c1.testrun.org") - + domain = os.environ.get("CHATMAIL_DOMAIN") + if not domain: + pytest.skip("set CHATMAIL_DOMAIN to a ssh-reachable chatmail instance") + return domain @pytest.fixture def imap(maildomain): @@ -19,6 +34,8 @@ def imap(maildomain): class ImapConn: + AuthError = imaplib.IMAP4.error + def __init__(self, host): self.host = host @@ -37,6 +54,8 @@ def smtp(maildomain): class SmtpConn: + AuthError = smtplib.SMTPAuthenticationError + def __init__(self, host): self.host = host @@ -49,6 +68,11 @@ class SmtpConn: self.conn.login(user, password) +@pytest.fixture(params=["imap", "smtp"]) +def imap_or_smtp(request): + return request.getfixturevalue(request.param) + + @pytest.fixture def gencreds(maildomain): count = itertools.count() diff --git a/online-tests/pytest.ini b/online-tests/pytest.ini index f53a739..2ac3bab 100644 --- a/online-tests/pytest.ini +++ b/online-tests/pytest.ini @@ -1,2 +1,3 @@ [pytest] -addopts = -vrsx +addopts = -vrsx --strict-markers +markers = slow: mark test as slow (requires --slow option to run) diff --git a/online-tests/test_0_login.py b/online-tests/test_0_login.py index 4d2da77..ac1a486 100644 --- a/online-tests/test_0_login.py +++ b/online-tests/test_0_login.py @@ -1,55 +1,36 @@ import pytest -import imaplib -import smtplib -class TestDovecot: - def test_login_ok(self, imap, gencreds): - user, password = gencreds() - imap.connect() - imap.login(user, password) - # verify it works on another connection - imap.connect() - imap.login(user, password) +def test_login_basic_functioning(imap_or_smtp, gencreds, lp): + """Test a) that an initial login creates a user automatically + and b) verify we can also login a second time with the same password + and c) that using a different password fails the login.""" + user, password = gencreds() + lp.sec(f"login first time with {user} {password}") + imap_or_smtp.connect() + imap_or_smtp.login(user, password) + lp.indent("success") - def test_login_same_password(self, imap, gencreds): - """Test two different users logging in with the same password. + lp.sec(f"reconnect and login second time {user} {password}") + imap_or_smtp.connect() + imap_or_smtp.login(user, password) + imap_or_smtp.connect() + lp.sec("success") - This ensures that authentication process does not confuse the users - by using only the password hash as a key. - """ - user1, password1 = gencreds() - user2, _password2 = gencreds() - imap.connect() - imap.login(user1, password1) - imap.connect() - imap.login(user2, password1) - - def test_login_fail(self, imap, gencreds): - user, password = gencreds() - imap.connect() - imap.login(user, password) - imap.connect() - with pytest.raises(imaplib.IMAP4.error) as excinfo: - imap.login(user, password + "wrong") - assert "AUTHENTICATIONFAILED" in str(excinfo) + lp.sec("reconnect and verify wrong password fails {user} ") + imap_or_smtp.connect() + with pytest.raises(imap_or_smtp.AuthError): + imap_or_smtp.login(user, password + "wrong") -class TestPostfix: - def test_login_ok(self, smtp, gencreds): - user, password = gencreds() - smtp.connect() - smtp.login(user, password) - # verify it works on another connection - smtp.connect() - smtp.login(user, password) - - def test_login_fail(self, smtp, gencreds): - user, password = gencreds() - smtp.connect() - smtp.login(user, password) - smtp.connect() - with pytest.raises(smtplib.SMTPAuthenticationError) as excinfo: - smtp.login(user, password + "wrong") - assert excinfo.value.smtp_code == 535 - assert "authentication failed" in str(excinfo) +def test_login_same_password(imap_or_smtp, gencreds): + """Test two different users logging in with the same password + to ensure that authentication process does not confuse the users + by using only the password hash as a key. + """ + user1, password1 = gencreds() + user2, _ = gencreds() + imap_or_smtp.connect() + imap_or_smtp.login(user1, password1) + imap_or_smtp.connect() + imap_or_smtp.login(user2, password1) diff --git a/online-tests/test_1_deltachat.py b/online-tests/test_1_deltachat.py index 044d1a4..588751d 100644 --- a/online-tests/test_1_deltachat.py +++ b/online-tests/test_1_deltachat.py @@ -2,8 +2,12 @@ import random import pytest -class TestMailSending: +class TestEndToEndDeltaChat: + "Tests that use Delta Chat accounts on the chat mail instance." + def test_one_on_one(self, cmfactory, lp): + """Test that a DC account can send a message to a second DC account + on the same chat-mail instance.""" ac1, ac2 = cmfactory.get_online_accounts(2) chat = cmfactory.get_accepted_chat(ac1, ac2) @@ -14,7 +18,11 @@ class TestMailSending: msg2 = ac2._evtracker.wait_next_incoming_message() assert msg2.text == "message0" + @pytest.mark.slow def test_exceed_quota(self, cmfactory, lp, tmpdir, dovelogreader): + """This is a very slow test as it needs to upload >100MB of mail data + before quota is exceeded, and thus depends on the speed of the upload. + """ ac1, ac2 = cmfactory.get_online_accounts(2) chat = cmfactory.get_accepted_chat(ac1, ac2)