From 0130c063960dd6bd25bc5a9d405baf7dda93516c Mon Sep 17 00:00:00 2001 From: Baptiste BEAUPLAT Date: Thu, 2 Apr 2020 18:50:54 +0200 Subject: [PATCH 1/3] Test Email class to expose uncatched exceptions --- debexpo/tests/test_email.py | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 debexpo/tests/test_email.py diff --git a/debexpo/tests/test_email.py b/debexpo/tests/test_email.py new file mode 100644 index 00000000..db68abe1 --- /dev/null +++ b/debexpo/tests/test_email.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# +# test_email.py - unit tests for Email +# +# This file is part of debexpo +# https://salsa.debian.org/mentors.debian.net-team/debexpo +# +# Copyright © 2020 Baptiste BEAUPLAT +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following +# conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. + +__author__ = 'Baptiste BEAUPLAT' +__copyright__ = 'Copyright © 2020 Baptiste BEAUPLAT' +__license__ = 'MIT' + +import pylons.test + +from debexpo.lib.email import Email +from debexpo.tests import TestController + + +class TestEmail(TestController): + def setUp(self): + self._setup_models() + + app_config = pylons.test.pylonsapp.config + self.testsmtp = app_config['debexpo.testsmtp'] + + def tearDown(self): + pylons.test.pylonsapp.config['debexpo.testsmtp'] = self.testsmtp + + def test_unreachable_smtp(self): + pylons.test.pylonsapp.config.pop('debexpo.testsmtp') + + email = Email('importer_fail_admin') + self.assertEquals(email.send(['user@example.org'], message=''), False) -- GitLab From 0df14c44aa5799ce13c5baf2d950f3a17eb71e9a Mon Sep 17 00:00:00 2001 From: Baptiste BEAUPLAT Date: Thu, 2 Apr 2020 18:51:25 +0200 Subject: [PATCH 2/3] Catch socket/SMTP exception when sending emails --- debexpo/lib/email.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/debexpo/lib/email.py b/debexpo/lib/email.py index 6f5238f3..25236162 100644 --- a/debexpo/lib/email.py +++ b/debexpo/lib/email.py @@ -170,15 +170,21 @@ class Email(object): pylons.url._pop_object() if 'debexpo.testsmtp' in pylons.config: - self._save_as_file(recipients, message) + return self._save_as_file(recipients, message) else: - self._send_as_mail(recipients, message) + try: + return self._send_as_mail(recipients, message) + except (IOError, smtplib.SMTPException) as e: + log.critical('Failed to send email: {}'.format(e)) + return False def _save_as_file(self, recipients, message): log.debug('Save email as file to %s' % self.server) with open(pylons.config['debexpo.testsmtp'], 'a') as email: email.write(message) + return True + def _send_as_mail(self, recipients, message): log.debug('Starting SMTP session to %s:%s' % (self.server, self.port)) session = smtplib.SMTP(self.server, self.port) @@ -197,8 +203,10 @@ class Email(object): log.critical('Failed sending to %s: %s, %s' % (recipient, result[recipient][0], result[recipient][1])) - else: - log.debug('Successfully sent') + return False + + log.debug('Successfully sent') + return True def _check_error(self, msg, err, data=None): if err != 'OK': -- GitLab From a510cf6daa0766c26821d973a2d62e690333a208 Mon Sep 17 00:00:00 2001 From: Baptiste BEAUPLAT Date: Fri, 3 Apr 2020 22:55:40 +0200 Subject: [PATCH 3/3] Prevent the importer of sending email with empty email address --- debexpo/importer/importer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debexpo/importer/importer.py b/debexpo/importer/importer.py index 637be130..84e0253f 100644 --- a/debexpo/importer/importer.py +++ b/debexpo/importer/importer.py @@ -264,7 +264,7 @@ class Importer(object): self._remove_files() - if self.user is not None: + if self.user is not None and self.user.email: email = Email('importer_fail_maintainer') package = self.changes.get('Source', '') @@ -287,7 +287,7 @@ class Importer(object): self._remove_files() - if self.user is not None: + if self.user is not None and self.user.email: email = Email('importer_reject_maintainer') package = self.changes.get('Source', '') -- GitLab