From 9d37bcd06fae5e8cbebf68aaf8670551fe6e5db1 Mon Sep 17 00:00:00 2001 From: Jonathan Sandoval Date: Fri, 8 Apr 2016 13:07:06 -0500 Subject: [PATCH 1/4] Mail tests with no mail server configured. --- mediagoblin/tests/test_util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index 8193233f..014c5eb8 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -181,3 +181,28 @@ def test_html_cleaner(): '

innocent link!

') assert result == ( '

innocent link!

') +class TestMail(object): + """ Test mediagoblin's mail tool """ + def test_no_mail_server(self): + """ Tests that no smtp server is available """ + with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock: + smtp_mock.side_effect = socket.error + mg_globals.app_config = { + "email_debug_mode": False, + "email_smtp_use_ssl": False, + "email_smtp_host": "127.0.0.1", + "email_smtp_port": 0} + common.TESTS_ENABLED = False + mail.send_email("", "", "", "") + + def test_no_smtp_host(self): + """ Empty email_smtp_host """ + with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock: + smtp_mock.return_value.connect.side_effect = socket.error + mg_globals.app_config = { + "email_debug_mode": False, + "email_smtp_use_ssl": False, + "email_smtp_host": "", + "email_smtp_port": 0} + common.TESTS_ENABLED = False + mail.send_email("", "", "", "") From d4eadc946190b40a7244320ba46e0fd2370e4009 Mon Sep 17 00:00:00 2001 From: Jonathan Sandoval Date: Fri, 8 Apr 2016 13:12:21 -0500 Subject: [PATCH 2/4] Custom exception in mail. --- mediagoblin/tools/mail.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py index c11e392b..8161a324 100644 --- a/mediagoblin/tools/mail.py +++ b/mediagoblin/tools/mail.py @@ -16,6 +16,8 @@ from __future__ import print_function, unicode_literals +import socket +import logging import six import smtplib import sys @@ -54,6 +56,14 @@ EMAIL_TEST_INBOX = [] EMAIL_TEST_MBOX_INBOX = [] +class MailError(Exception): + """ General exception for mail errors """ + + +class NoSMTPServerError(MailError): + pass + + class FakeMhost(object): """ Just a fake mail host so we can capture and test messages @@ -101,13 +111,27 @@ def send_email(from_addr, to_addrs, subject, message_body): else: smtp_init = smtplib.SMTP - mhost = smtp_init( - mg_globals.app_config['email_smtp_host'], - mg_globals.app_config['email_smtp_port']) + try: + mhost = smtp_init( + mg_globals.app_config['email_smtp_host'], + mg_globals.app_config['email_smtp_port']) + except socket.error: + error_message = "Couldn't contact mail server on <{}>:<{}>".format( + mg_globals.app_config['email_smtp_host'], + mg_globals.app_config['email_smtp_port']) + logging.debug(error_message) + raise NoSMTPServerError(error_message) # SMTP.__init__ Issues SMTP.connect implicitly if host if not mg_globals.app_config['email_smtp_host']: # e.g. host = '' - mhost.connect() # We SMTP.connect explicitly + try: + mhost.connect() # We SMTP.connect explicitly + except socket.error: + error_message = "Couldn't contact mail server on <{}>:<{}>".format( + mg_globals.app_config['email_smtp_host'], + mg_globals.app_config['email_smtp_port']) + logging.debug(error_message) + raise NoSMTPServerError(error_message) try: mhost.starttls() From caee9aa2e3a9d12e924f56149c29063dad8025e5 Mon Sep 17 00:00:00 2001 From: Jonathan Sandoval Date: Fri, 8 Apr 2016 13:17:26 -0500 Subject: [PATCH 3/4] squash! Custom exception in mail. --- mediagoblin/tests/test_util.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index 014c5eb8..02976405 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -19,6 +19,7 @@ try: except ImportError: import unittest.mock as mock import email +import socket import pytest import smtplib import pkg_resources @@ -26,6 +27,7 @@ import pkg_resources import six from mediagoblin.tests.tools import get_app +from mediagoblin import mg_globals from mediagoblin.tools import common, url, translate, mail, text, testing testing._activate_testing() @@ -181,6 +183,8 @@ def test_html_cleaner(): '

innocent link!

') assert result == ( '

innocent link!

') + + class TestMail(object): """ Test mediagoblin's mail tool """ def test_no_mail_server(self): From b2bc8e654efae0b94185960a65273ed410cf8874 Mon Sep 17 00:00:00 2001 From: Jonathan Sandoval Date: Fri, 8 Apr 2016 13:21:31 -0500 Subject: [PATCH 4/4] Include original error in debug log --- mediagoblin/tools/mail.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py index 8161a324..3dc180d8 100644 --- a/mediagoblin/tools/mail.py +++ b/mediagoblin/tools/mail.py @@ -115,22 +115,22 @@ def send_email(from_addr, to_addrs, subject, message_body): mhost = smtp_init( mg_globals.app_config['email_smtp_host'], mg_globals.app_config['email_smtp_port']) - except socket.error: + except socket.error as original_error: error_message = "Couldn't contact mail server on <{}>:<{}>".format( mg_globals.app_config['email_smtp_host'], mg_globals.app_config['email_smtp_port']) - logging.debug(error_message) + logging.debug(original_error) raise NoSMTPServerError(error_message) # SMTP.__init__ Issues SMTP.connect implicitly if host if not mg_globals.app_config['email_smtp_host']: # e.g. host = '' try: mhost.connect() # We SMTP.connect explicitly - except socket.error: + except socket.error as original_error: error_message = "Couldn't contact mail server on <{}>:<{}>".format( mg_globals.app_config['email_smtp_host'], mg_globals.app_config['email_smtp_port']) - logging.debug(error_message) + logging.debug(original_error) raise NoSMTPServerError(error_message) try: