Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #93 - cleanup error hiding exception details #101

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flask_mail.py
Original file line number Diff line number Diff line change
@@ -148,7 +148,7 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, tb):
if self.host:
if self.host and getattr(self.host, 'sock', None):
self.host.quit()

def configure_host(self):
55 changes: 55 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
import time
import re
import mock
import smtplib
import socket
import threading
from contextlib import contextmanager

from email.header import Header
@@ -701,3 +704,55 @@ def test_sendmail_with_non_ascii_body(self):
msg.mail_options,
msg.rcpt_options
)


class TestAgainstLocalServer(TestCase):
def setUp(self):
def bind_server_socket():
server_socket = socket.socket()
server_socket.bind(('localhost', 0))
server_socket.settimeout(5) # ensure thread dies
server_socket.listen(1)
return server_socket

def server_threadfunc(server_socket, errors):
try:
client_socket, client_info = server_socket.accept()
client_socket.settimeout(5) # ensure thread dies
client_socket.send(b'220 localhost ESMTP blah blah blah\r\n')
client_socket.recv(1024)
client_socket.send(b'250 ok\r\n')
client_socket.close() # unexpected disconnect
except Exception as exc:
errors.append(exc)

server_socket = bind_server_socket()
self.MAIL_SERVER, self.MAIL_PORT = server_socket.getsockname()
self._server_errors = []
thread = threading.Thread(target=server_threadfunc,
args=(server_socket, self._server_errors))
self._server_thread = thread
thread.daemon = True
thread.start()
super(TestAgainstLocalServer, self).setUp()
self._old_app_mail_instance = self.app.extensions['mail']
self.app.extensions['mail'] = self.mail
self.mail.suppress = False # do live testing

def tearDown(self):
self._server_thread.join()
assert not self._server_errors, self._server_errors
self.app.extensions['mail'] = self._old_app_mail_instance
super(TestAgainstLocalServer, self).tearDown()

def test_detect_disconnect_during_send(self):
msg = Message(sender="[email protected]",
subject="subject",
recipients=["[email protected]"],
body="some plain text")
try:
self.mail.send(msg)
assert False # exception expected
except smtplib.SMTPServerDisconnected as e:
assert e.args[0] == 'Connection unexpectedly closed', e.args[0]