diff --git a/debexpo/importer/importer.py b/debexpo/importer/importer.py index 637be1309231d57fbf7077c20ccb196fd6e572f5..84e0253f632317aa637530e1b5bdceccc559c55d 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', '') diff --git a/debexpo/lib/email.py b/debexpo/lib/email.py index 6f5238f377924c3e2f685d34c03f3417cbf259d5..25236162cd80b07d6ade77be5188082b6057b722 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': diff --git a/debexpo/tests/test_email.py b/debexpo/tests/test_email.py new file mode 100644 index 0000000000000000000000000000000000000000..db68abe19d8074a0705e8071bd3606074db87ac5 --- /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)