Ticket #5081: issue_5081_rev1.patch

File issue_5081_rev1.patch, 6.9 KB (added by jsandoval, 9 years ago)
  • mediagoblin/tools/mail.py

    From 8c5349ec56d877de74d7aa8c62565bddcaa593b2 Mon Sep 17 00:00:00 2001
    From: jsandoval <jsandoval@utp.edu.co>
    Date: Tue, 22 Mar 2016 20:05:26 -0400
    Subject: [PATCH 1/2] Fix #5081 - Unhelpful SMTP error
    
    ---
     mediagoblin/tools/mail.py | 23 +++++++++++++++++------
     1 file changed, 17 insertions(+), 6 deletions(-)
    
    diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py
    index 74e59fb..a290879 100644
    a b def send_email(from_addr, to_addrs, subject, message_body):  
    101101        else:
    102102            smtp_init = smtplib.SMTP
    103103
    104         mhost = smtp_init(
    105             mg_globals.app_config['email_smtp_host'],
    106             mg_globals.app_config['email_smtp_port'])
    107 
     104        try:
     105            mhost = smtp_init(
     106                mg_globals.app_config['email_smtp_host'],
     107                mg_globals.app_config['email_smtp_port'])
     108        except socket.error:
     109            print("Couldn't contact mail server on <{host}>:<{port}>".format(
     110                host=mg_globals.app_config['email_smtp_host'],
     111                port=mg_globals.app_config['email_smtp_port']))
     112            return
     113           
    108114        # SMTP.__init__ Issues SMTP.connect implicitly if host
    109115        if not mg_globals.app_config['email_smtp_host']:  # e.g. host = ''
    110             mhost.connect()  # We SMTP.connect explicitly
    111 
     116            try:
     117                mhost.connect()  # We SMTP.connect explicitly
     118            except socket.error:
     119                print("Couldn't contact mail server on <{host}>:<{port}>".format(
     120                    host=mg_globals.app_config['email_smtp_host'],
     121                    port=mg_globals.app_config['email_smtp_port']))
     122                return
    112123        try:
    113124            mhost.starttls()
    114125        except smtplib.SMTPException:
  • new file mediagoblin/tests/test_mail.py

    -- 
    2.1.4
    
    
    From 58bae4a0ff4cdc65b95bc497de80fb23de3d4eca Mon Sep 17 00:00:00 2001
    From: jsandoval <jsandoval@utp.edu.co>
    Date: Wed, 23 Mar 2016 16:39:39 -0400
    Subject: [PATCH 2/2] Fix issue 5081
    
    - Custom exception class defined.
    - Tests.
    - Logs in debug level.
    ---
     mediagoblin/tests/test_mail.py | 57 ++++++++++++++++++++++++++++++++++++++++++
     mediagoblin/tools/mail.py      | 29 +++++++++++++++------
     2 files changed, 78 insertions(+), 8 deletions(-)
     create mode 100644 mediagoblin/tests/test_mail.py
    
    diff --git a/mediagoblin/tests/test_mail.py b/mediagoblin/tests/test_mail.py
    new file mode 100644
    index 0000000..59f3034
    - +  
     1# GNU MediaGoblin -- federated, autonomous media hosting
     2# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
     3#
     4# This program is free software: you can redistribute it and/or modify
     5# it under the terms of the GNU Affero General Public License as published by
     6# the Free Software Foundation, either version 3 of the License, or
     7# (at your option) any later version.
     8#
     9# This program is distributed in the hope that it will be useful,
     10# but WITHOUT ANY WARRANTY; without even the implied warranty of
     11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12# GNU Affero General Public License for more details.
     13#
     14# You should have received a copy of the GNU Affero General Public License
     15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16
     17from __future__ import print_function, unicode_literals
     18
     19import socket
     20import pytest
     21
     22try:
     23    import mock
     24except ImportError:
     25    import unittest.mock as mock
     26
     27from mediagoblin import mg_globals
     28from mediagoblin.tools import mail, common
     29
     30
     31class TestMail(object):
     32    """ Test mediagoblin's mail tool """
     33
     34    def test_no_mail_server(self):
     35        """ Tests that no smtp server is available """
     36        with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock:
     37                smtp_mock.side_effect = socket.error
     38                mg_globals.app_config = {
     39                    "email_debug_mode": False,
     40                    "email_smtp_use_ssl": False,
     41                    "email_smtp_host": "127.0.0.1",
     42                    "email_smtp_port": 0}
     43                common.TESTS_ENABLED = False
     44                mail.send_email("", "", "", "")
     45
     46    def test_no_smtp_host(self):
     47        """ Empty email_smtp_host """
     48        with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock:
     49                smtp_mock.return_value.connect.side_effect = socket.error
     50                mg_globals.app_config = {
     51                    "email_debug_mode": False,
     52                    "email_smtp_use_ssl": False,
     53                    "email_smtp_host": "",
     54                    "email_smtp_port": 0}
     55                common.TESTS_ENABLED = False
     56                mail.send_email("", "", "", "")
     57
  • mediagoblin/tools/mail.py

    diff --git a/mediagoblin/tools/mail.py b/mediagoblin/tools/mail.py
    index a290879..06a30d5 100644
    a b  
    1616
    1717from __future__ import print_function, unicode_literals
    1818
     19import socket
     20import logging
    1921import six
    2022import smtplib
    2123import sys
    EMAIL_TEST_INBOX = []  
    5456EMAIL_TEST_MBOX_INBOX = []
    5557
    5658
     59class MailError(Exception):
     60    """ General exception for mail errors """
     61
     62
     63class NoSMTPServerError(MailError):
     64    pass
     65
     66
    5767class FakeMhost(object):
    5868    """
    5969    Just a fake mail host so we can capture and test messages
    def send_email(from_addr, to_addrs, subject, message_body):  
    106116                mg_globals.app_config['email_smtp_host'],
    107117                mg_globals.app_config['email_smtp_port'])
    108118        except socket.error:
    109             print("Couldn't contact mail server on <{host}>:<{port}>".format(
    110                 host=mg_globals.app_config['email_smtp_host'],
    111                 port=mg_globals.app_config['email_smtp_port']))
    112             return
     119            error_message = "Couldn't contact mail server on <{}>:<{}>".format(
     120                mg_globals.app_config['email_smtp_host'],
     121                mg_globals.app_config['email_smtp_port'])
     122            logging.debug(error_message)
     123            raise NoSMTPServerError(error_message)
    113124           
    114125        # SMTP.__init__ Issues SMTP.connect implicitly if host
    115126        if not mg_globals.app_config['email_smtp_host']:  # e.g. host = ''
    116127            try:
    117128                mhost.connect()  # We SMTP.connect explicitly
    118129            except socket.error:
    119                 print("Couldn't contact mail server on <{host}>:<{port}>".format(
    120                     host=mg_globals.app_config['email_smtp_host'],
    121                     port=mg_globals.app_config['email_smtp_port']))
    122                 return
     130                error_message = "Couldn't contact mail server on <{}>:<{}>".format(
     131                    mg_globals.app_config['email_smtp_host'],
     132                    mg_globals.app_config['email_smtp_port'])
     133                logging.debug(error_message)
     134                raise NoSMTPServerError(error_message)
     135
    123136        try:
    124137            mhost.starttls()
    125138        except smtplib.SMTPException: