-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_smtp_backend.py
More file actions
54 lines (42 loc) · 1.77 KB
/
Copy pathtest_smtp_backend.py
File metadata and controls
54 lines (42 loc) · 1.77 KB
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
import socket
import pytest
import emails
from emails.backend.smtp import SMTPBackend
from emails.testsuite.smtp_servers import get_servers
SAMPLE_MESSAGE = {'html': '<p>Test from python-emails',
'text': 'Test from python-emails',
'mail_from': 's@lavr.me',
'mail_to': 'sergei-nko@yandex.r'}
def test_send_to_unknown_host():
server = SMTPBackend(host='invalid-server.invalid-domain-42.com', port=2525)
response = server.sendmail(to_addrs='s@lavr.me', from_addr='s@lavr.me', msg=emails.html(**SAMPLE_MESSAGE))
server.close()
assert response.status_code is None
assert isinstance(response.error, IOError)
assert not response.success
# IOError: [Errno 8] nodename nor servname provided, or not known
assert response.error.errno == socket.EAI_NONAME
@pytest.mark.e2e
def test_smtp_send_with_reconnect():
"""
Check SMTPBackend.sendmail reconnect
"""
for tag, server in get_servers():
print("-- test_smtp_reconnect: %s" % server)
params = server.params
params['fail_silently'] = True
message = server.patch_message(emails.html(subject='reconnect test', **SAMPLE_MESSAGE))
backend = message.smtp_pool[params]
backend.get_client().sock.close() # simulate disconnect
response = message.send(smtp=params)
assert response.success or response.status_code in (421, 451) # gmail don't like test emails
server.sleep()
def test_smtp_init_error():
"""
Test error when ssl and tls arguments both set
"""
with pytest.raises(ValueError):
SMTPBackend(host='X', port=25, ssl=True, tls=True)
def test_smtp_empty_sendmail():
response = SMTPBackend().sendmail(to_addrs=[], from_addr='a@b.com', msg='')
assert not response