Fix and test error when sending email

This commit is contained in:
Claude Paroz 2017-12-06 12:23:12 +01:00
parent 8a5c88583f
commit a09a5159a7
3 changed files with 19 additions and 2 deletions

View file

@ -67,7 +67,7 @@ def send_confirmation_mail(modeladmin, request, queryset):
try:
send_mail(subject, body, from_email, to, fail_silently=False)
except Exception as err:
self.message_user(request, "Échec d'envoi pour le candidat {0} ({1})".format(candidate, err))
modeladmin.message_user(request, "Échec denvoi pour le candidat {0} ({1})".format(candidate, err))
else:
candidate.date_confirmation_mail = date.today()
candidate.save()

View file

@ -1,4 +1,5 @@
from datetime import date
from unittest import mock
from django.contrib.auth.models import User
from django.core import mail
@ -77,3 +78,19 @@ me@example.org
)
# One was already set, 2 new.
self.assertEqual(Candidate.objects.filter(date_confirmation_mail__isnull=False).count(), 3)
def test_send_confirmation_error(self):
ede = Section.objects.create(name='EDE')
Candidate.objects.create(
first_name='Henri', last_name='Dupond', gender='M', section=ede,
email='henri@example.org', deposite_date=date.today()
)
change_url = reverse('admin:candidats_candidate_changelist')
self.client.login(username='me', password='mepassword')
with mock.patch('candidats.admin.send_mail') as mocked:
mocked.side_effect = Exception("Error sending mail")
response = self.client.post(change_url, {
'action': 'send_confirmation_mail',
'_selected_action': Candidate.objects.values_list('pk', flat=True)
}, follow=True)
self.assertContains(response, "Échec denvoi pour le candidat Dupond Henri (Error sending mail)")

View file

@ -1,4 +1,4 @@
Django==1.11.2
Django==1.11.8
tabimport>=0.4.0
openpyxl==2.4.9
xlrd