smtp tests refactoring

This commit is contained in:
Sergey Lavrinenko 2015-03-31 13:00:08 +03:00
parent e698f6ee71
commit ce787c125e
2 changed files with 24 additions and 47 deletions

View File

@ -8,35 +8,28 @@ import emails.loader
from .helpers import common_email_data
def test_send_attachment(smtp_servers):
"""
Test email with attachment
"""
def get_letters():
# Test email with attachment
URL = 'http://lavr.github.io/python-emails/tests/campaignmonitor-samples/sample-template/images/gallery.png'
data = common_email_data(subject='Single attachment', attachments=[emails.store.LazyHTTPFile(uri=URL), ])
m = emails.html(**data)
for tag, server in smtp_servers.items():
server.patch_message(m)
r = m.send(smtp=server.params)
server.sleep()
yield emails.html(**data), None
# Email with render
yield emails.html(**common_email_data(subject='Render with name=John')), {'name': u'John'}
def test_send_with_render(smtp_servers):
for tag, server in smtp_servers.items():
m = emails.html(**common_email_data(subject='Render with name=John'))
server.patch_message(m)
r = m.send(render={'name': u'John'}, smtp=server.params)
server.sleep()
def test_send_with_inline_images(smtp_servers):
# Email with several inline images
url = 'http://lavr.github.io/python-emails/tests/campaignmonitor-samples/sample-template/template-widgets.html'
data = common_email_data(subject='Sample html with inline images')
del data['html']
m = emails.loader.from_url(url=url, message_params=data, images_inline=True)
yield emails.loader.from_url(url=url, message_params=data, images_inline=True), None
for tag, server in smtp_servers.items():
server.patch_message(m)
r = m.send(smtp=server.params)
server.sleep()
def test_send_letters(smtp_servers):
for m, render in get_letters():
for tag, server in smtp_servers.items():
server.patch_message(m)
response = m.send(smtp=server.params, render=render)
assert response.success or response.status_code in (421, 451) # gmail not always like test emails
server.sleep()

View File

@ -6,14 +6,12 @@ import emails
from emails.backend.smtp import SMTPBackend
TRAVIS_CI = os.environ.get('TRAVIS')
SAMPLE_MESSAGE = {'html': '<p>Test from python-emails',
'text': 'Test from python-emails',
'mail_from': 's@lavr.me',
'mail_to': 'sergei-nko@yandex.ru',
'subject': 'Sample message'}
'mail_to': 'sergei-nko@yandex.ru'}
def test_send_to_unknown_host():
@ -29,35 +27,21 @@ def test_send_to_unknown_host():
assert response.error.errno == 8
def test_smtp_send(smtp_servers):
"""
Check SMTPBackend.sendmail
"""
for tag, server in smtp_servers.items():
print("-- test_smtp_send: %s" % server)
smtp = server.params
smtp['fail_silently'] = True
response = server.patch_message(emails.html(**SAMPLE_MESSAGE)).send(smtp=server.params)
assert response.success or response.status_code == 421 # gmail sometimes fail sending
#message.smtp_pool[smtp].get_client().quit()
server.sleep()
def test_smtp_send_with_reconnect(smtp_servers):
"""
Check SMTPBackend.sendmail reconnect
"""
for tag, server in smtp_servers.items():
print("-- test_smtp_reconnect: %s" % server)
params = server.params
params['fail_silently'] = True
backend = SMTPBackend(**params)
message = server.patch_message(emails.html(subject='reconnect test', **SAMPLE_MESSAGE))
message.mail_from = server.from_email
message.mail_to = server.to_email
backend = message.smtp_pool[params]
backend.get_client().sock.close() # simulate disconnect
response = backend.sendmail(to_addrs=server.to_email,
from_addr=server.from_email,
msg=server.patch_message(emails.html(**SAMPLE_MESSAGE)))
assert response.success or response.status_code == 421 # gmail sometimes fail sending
response = message.send(smtp=params)
assert response.success or response.status_code in (421, 451) # gmail don't like test emails
server.sleep()