1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
import smtplib
import pytest
try: # python 3
from email.mime.text import MIMEText
except ImportError: # python 2?
from email.MIMEText import MIMEText
from pytest_localserver import plugin
smtp = pytest.importorskip("pytest_localserver.smtp")
def send_plain_email(to, from_, subject, txt, server=("localhost", 25)):
"""
Sends a simple plain text message via SMTP.
"""
if type(to) in (tuple, list):
to = ", ".join(to)
# Create a text/plain message
msg = MIMEText(txt)
msg["Subject"] = subject
msg["From"] = from_
msg["To"] = to
host, port = server[:2]
server = smtplib.SMTP(host, port)
server.set_debuglevel(1)
server.sendmail(from_, to, msg.as_string())
server.quit()
# define test fixture here again in order to run tests without having to
# install the plugin anew every single time
smtpserver = plugin.smtpserver
def test_smtpserver_funcarg(smtpserver):
assert isinstance(smtpserver, smtp.Server)
assert smtpserver.is_alive()
assert smtpserver.accepting and smtpserver.addr
def test_smtpserver_addr(smtpserver):
host, port = smtpserver.addr
assert isinstance(host, str)
assert isinstance(port, int)
assert port > 0
def test_server_is_killed(smtpserver):
assert smtpserver.is_alive()
smtpserver.stop()
assert not smtpserver.is_alive()
def test_server_is_deleted(smtpserver):
assert smtpserver.is_alive()
smtpserver.__del__() # need to call magic method here!
assert not smtpserver.is_alive()
def test_smtpserver_has_empty_outbox_at_startup(smtpserver):
assert len(smtpserver.outbox) == 0
def test_send_email(smtpserver):
# send one e-mail
send_plain_email(
"alice@example.com",
"webmaster@example.com",
"Your e-mail is getting there",
"Seems like this test actually works!",
smtpserver.addr,
)
msg = smtpserver.outbox[-1]
assert msg["To"] == "alice@example.com"
assert msg["From"] == "webmaster@example.com"
assert msg["Subject"] == "Your e-mail is getting there"
assert msg.details.rcpttos == ["alice@example.com"]
assert msg.details.peer
assert msg.details.mailfrom
# send another e-mail
send_plain_email(
"bob@example.com",
"webmaster@example.com",
"His e-mail too",
"Seems like this test actually works!",
smtpserver.addr,
)
msg = smtpserver.outbox[-1]
assert msg["To"] == "bob@example.com"
assert msg["From"] == "webmaster@example.com"
assert msg["Subject"] == "His e-mail too"
# two mails are now in outbox
assert len(smtpserver.outbox) == 2
|