From c7dfd7ca41555b326a3e297f3cce9eb9183b09f2 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 14 Oct 2023 11:34:09 +0200 Subject: [PATCH] refactor dovecot tests, move online tests one level up --- README.md | 9 ++++-- chatmail-pyinfra/tests/test_online_login.py | 28 ----------------- online-tests/conftest.py | 33 +++++++++++++++++++++ online-tests/test_login.py | 18 +++++++++++ scripts/init.sh | 4 +++ scripts/test.sh | 7 +++-- 6 files changed, 65 insertions(+), 34 deletions(-) delete mode 100644 chatmail-pyinfra/tests/test_online_login.py create mode 100644 online-tests/conftest.py create mode 100644 online-tests/test_login.py diff --git a/README.md b/README.md index 6ca6d3f3..4bb9ab92 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,6 @@ chatmail-pyinfra pyproject.toml chatmail/__init__ ... - # tests against the deployed system - tests/test_online_test.py - # doveauth tool used by dovecot's auth mechanism on the host system doveauth README.md @@ -44,12 +41,18 @@ filtermail pyproject.toml .... +# online tests (after deploy) + +online-tests # runnable via pytest + + # scripts for setup/development/deployment scripts/ init.sh # create venv/other perequires deploy.sh # run pyinfra based deploy of everything + test.sh # run all local and online tests ``` diff --git a/chatmail-pyinfra/tests/test_online_login.py b/chatmail-pyinfra/tests/test_online_login.py deleted file mode 100644 index b84a7a64..00000000 --- a/chatmail-pyinfra/tests/test_online_login.py +++ /dev/null @@ -1,28 +0,0 @@ -import pytest -import imaplib - - -@pytest.fixture -def conn(): - return connect("c1.testrun.org") - - -def login(conn, user, password): - print("trying to login", user, password) - conn.login(user, password) - - -def connect(host): - print(f"connecting to {host}") - conn = imaplib.IMAP4_SSL(host) - return conn - - -def test_login_ok(conn): - login(conn, "link2xt@c1.testrun.org", "Ahyei6ie") - - -def test_login_fail(conn): - with pytest.raises(imaplib.IMAP4.error) as excinfo: - login(conn, "link2xt@c1.testrun.org", "qweqwe") - assert "AUTHENTICATIONFAILED" in str(excinfo) diff --git a/online-tests/conftest.py b/online-tests/conftest.py new file mode 100644 index 00000000..319bea38 --- /dev/null +++ b/online-tests/conftest.py @@ -0,0 +1,33 @@ +import imaplib +import itertools +import pytest + + +@pytest.fixture +def imap(): + return ImapConn("c1.testrun.org") + + +class ImapConn: + def __init__(self, host): + self.host = host + + def connect(self): + print(f"imap-connect {self.host}") + self.conn = imaplib.IMAP4_SSL(self.host) + + def login(self, user, password): + print(f"imap-login {user!r} {password!r}") + self.conn.login(user, password) + + +@pytest.fixture +def gencreds(): + count = itertools.count() + + def gen(): + while 1: + num = next(count) + yield f"user{num}", f"password{num}" + + return lambda: next(gen()) diff --git a/online-tests/test_login.py b/online-tests/test_login.py new file mode 100644 index 00000000..0bdba134 --- /dev/null +++ b/online-tests/test_login.py @@ -0,0 +1,18 @@ +import pytest +import imaplib + + +class TestDovecot: + def test_login_ok(self, imap, gencreds): + user, password = gencreds() + imap.connect() + imap.login(user, password) + + 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) diff --git a/scripts/init.sh b/scripts/init.sh index 466a9806..4f7dc33c 100755 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -3,6 +3,10 @@ python3 -m venv chatmail-pyinfra/venv chatmail-pyinfra/venv/bin/pip install pyinfra pytest chatmail-pyinfra/venv/bin/pip install -e chatmail-pyinfra chatmail-pyinfra/venv/bin/pip install -e doveauth + python3 -m venv doveauth/venv doveauth/venv/bin/pip install pytest build doveauth/venv/bin/pip install -e doveauth + +python3 -m venv online-tests/venv +online-tests/venv/bin/pip install pytest pytest-timeout diff --git a/scripts/test.sh b/scripts/test.sh index 928f2c3f..4e5ab986 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -1,4 +1,5 @@ -#!/bin/sh -chatmail-pyinfra/venv/bin/pytest chatmail-pyinfra/tests -cd doveauth/src/doveauth +#!/bin/bash +pushd doveauth/src/doveauth ../../venv/bin/pytest +popd +online-tests/venv/bin/pytest online-tests/