Skip to content

Commit 741a204

Browse files
authored
Add a system test for running the filtermail module
1 parent b7fadcd commit 741a204

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

chatmaild/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ commands =
7171
[testenv]
7272
deps = pytest
7373
pdbpp
74+
pytest-localserver
7475
commands = pytest -v -rsXx {posargs}
7576
"""
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import smtplib
2+
import subprocess
3+
import sys
4+
5+
import pytest
6+
7+
8+
@pytest.fixture
9+
def smtpserver():
10+
from pytest_localserver import smtp
11+
12+
server = smtp.Server("127.0.0.1")
13+
server.start()
14+
yield server
15+
server.stop()
16+
17+
18+
@pytest.fixture
19+
def make_popen(request):
20+
def popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kw):
21+
p = subprocess.Popen(
22+
cmdargs,
23+
stdout=subprocess.PIPE,
24+
stderr=subprocess.PIPE,
25+
)
26+
27+
def fin():
28+
p.terminate()
29+
out, err = p.communicate()
30+
print(out.decode("ascii"))
31+
print(err.decode("ascii"), file=sys.stderr)
32+
33+
request.addfinalizer(fin)
34+
return p
35+
36+
return popen
37+
38+
39+
@pytest.mark.parametrize("filtermail_mode", ["outgoing", "incoming"])
40+
def test_one_mail(
41+
make_config, make_popen, smtpserver, maildata, filtermail_mode, monkeypatch
42+
):
43+
monkeypatch.setenv("PYTHONUNBUFFERED", "1")
44+
smtp_inject_port = 20025
45+
if filtermail_mode == "outgoing":
46+
settings = dict(
47+
postfix_reinject_port=smtpserver.port,
48+
filtermail_smtp_port=smtp_inject_port,
49+
)
50+
else:
51+
settings = dict(
52+
postfix_reinject_port_incoming=smtpserver.port,
53+
filtermail_smtp_port_incoming=smtp_inject_port,
54+
)
55+
56+
config = make_config("example.org", settings=settings)
57+
path = str(config._inipath)
58+
59+
popen = make_popen(["filtermail", path, filtermail_mode])
60+
line = popen.stderr.readline().strip()
61+
if b"loop" not in line:
62+
print(line.decode("ascii"), file=sys.stderr)
63+
pytest.fail("starting filtermail failed")
64+
65+
addr = f"user1@{config.mail_domain}"
66+
config.get_user(addr).set_password("l1k2j3l1k2j3l")
67+
68+
# send encrypted mail
69+
data = str(maildata("encrypted.eml", from_addr=addr, to_addr=addr))
70+
client = smtplib.SMTP("localhost", smtp_inject_port)
71+
client.sendmail(addr, [addr], data)
72+
assert len(smtpserver.outbox) == 1
73+
74+
# send un-encrypted mail that errors
75+
data = str(maildata("fake-encrypted.eml", from_addr=addr, to_addr=addr))
76+
with pytest.raises(smtplib.SMTPDataError) as e:
77+
client.sendmail(addr, [addr], data)
78+
assert e.value.smtp_code == 523

0 commit comments

Comments
 (0)