feat(piaf): prévient par email d'un retour anormal de tipi; fix #37
Parent
5ac69fc9b5
révision
13ba8e34ce
|
@ -106,6 +106,17 @@ class TipiPaiement(models.Model):
|
|||
r = Request('GET', url, params=params)
|
||||
return r.prepare().url
|
||||
|
||||
@staticmethod
|
||||
def send_by_email(request, title, message):
|
||||
fr = settings.DEFAULT_FROM_EMAIL
|
||||
subject = title
|
||||
body = render_to_string(
|
||||
'emails/warn_ignored_tipi_post.txt',
|
||||
{'message': message, 'request': request}
|
||||
)
|
||||
email = EmailMessage(subject, body, fr, [a[1] for a in settings.ADMINS])
|
||||
email.send(fail_silently=False)
|
||||
|
||||
@classmethod
|
||||
def set_from_post(cls, data):
|
||||
paiement = cls.objects.filter(~Q(result='P')).get(
|
||||
|
|
|
@ -419,8 +419,25 @@ class TrustedPaidInvoice(TestCase):
|
|||
response = self.client.post(reverse('paid_invoice'), follow=False)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'KO')
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
self.assertTrue('Retour TIPI invalide' in mail.outbox[0].subject)
|
||||
|
||||
def test_paid_invoice_unauthorized(self):
|
||||
with self.settings(TIPI_TRUSTED_NETWORK='192.0.2.0/24'):
|
||||
response = self.client.post(reverse('paid_invoice'), follow=False)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_paid_invoice_authorized___but_unknown_data(self):
|
||||
response = self.client.post(
|
||||
reverse('retrieve_invoice'),
|
||||
{'numero': '54', 'nom': 'Isabelle Merignac'},
|
||||
follow=True,
|
||||
)
|
||||
data = get_paiement_post_data(response.content.decode())
|
||||
data['objet'] = 'bling blang bling'
|
||||
|
||||
response = self.client.post(reverse('paid_invoice'), data, follow=False)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'KO')
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
self.assertTrue('TIPI valide mais incongru' in mail.outbox[0].subject)
|
||||
|
|
|
@ -204,6 +204,12 @@ class PaidInvoice(mixins.RestrictToTipiHost, generic.edit.FormView):
|
|||
form_class = forms.PaidInvoiceForm
|
||||
|
||||
def form_invalid(self, form):
|
||||
title = "[IMPORTANT] Retour TIPI invalide"
|
||||
message = (
|
||||
"Le retour de TIPI ne valide pas le formulaire. "
|
||||
"L'erreur est :\n\n{}"
|
||||
).format(form.errors.as_text())
|
||||
models.TipiPaiement.send_by_email(self.request, title, message)
|
||||
return HttpResponse("KO.", content_type="text/plain")
|
||||
|
||||
def form_valid(self, form):
|
||||
|
@ -216,6 +222,19 @@ class PaidInvoice(mixins.RestrictToTipiHost, generic.edit.FormView):
|
|||
except Exception as e:
|
||||
if settings.DEBUG:
|
||||
raise e
|
||||
title = "[IMPORTANT] Retour TIPI valide mais incongru"
|
||||
message = (
|
||||
"Le retour de TIPI valide le formulaire mais nous ne "
|
||||
"savons pas quoi faire des données ; comme si la requête "
|
||||
"avait été forgée. Le formulaire contenait "
|
||||
":\n\n{}"
|
||||
).format('\n'.join(
|
||||
[
|
||||
'{}: {}'.format(k, v)
|
||||
for k, v in form.cleaned_data.items()
|
||||
]
|
||||
))
|
||||
models.TipiPaiement.send_by_email(self.request, title, message)
|
||||
return HttpResponse("KO.", content_type="text/plain")
|
||||
|
||||
# Only the next HackyMessages will be relevant for this session
|
||||
|
|
Chargement…
Référencer dans un nouveau ticket