Verified Commit 81f83af7 authored by Mattia Rizzolo's avatar Mattia Rizzolo
Browse files

Merge branch 'fix/crash-smtp-error' of salsa.debian.org:lyknode-guest/debexpo into live

MR: !118


Signed-off-by: Mattia Rizzolo's avatarMattia Rizzolo <mattia@debian.org>
parents c898c19c a510cf6d
Pipeline #123248 passed with stage
in 4 minutes and 55 seconds
......@@ -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', '')
......
......@@ -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:
return False
log.debug('Successfully sent')
return True
def _check_error(self, msg, err, data=None):
if err != 'OK':
......
# -*- 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 <lyknode@cilg.org>
#
# 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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment