From 32fb29c299ccf717e3527d892d8f2ca816fc6000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 1 Aug 2015 16:06:04 +0300 Subject: [PATCH] Webhook for Mandrill inbound email notifications, WIP --- hc/api/tests/test_email_webhook.py | 9 ++++++++- hc/api/views.py | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/hc/api/tests/test_email_webhook.py b/hc/api/tests/test_email_webhook.py index 431d17c8..8f7ae9bc 100644 --- a/hc/api/tests/test_email_webhook.py +++ b/hc/api/tests/test_email_webhook.py @@ -15,7 +15,10 @@ class EmailTestCase(TestCase): "event": "inbound", "msg": { "raw_msg": "This is raw message", - "to": ["somewhere@example.com", "%s@example.com" % check.code] + "to": [ + ["somewhere@example.com", "Somebody"], + ["%s@example.com" % check.code, "Healthchecks"] + ] } }] @@ -29,3 +32,7 @@ class EmailTestCase(TestCase): pings = list(Ping.objects.all()) assert pings[0].scheme == "email" assert pings[0].body == "This is raw message" + + def test_it_rejects_get(self): + r = self.client.get("/handle_email/") + assert r.status_code == 400 diff --git a/hc/api/views.py b/hc/api/views.py index 9b6b4aef..8cde1dec 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -40,10 +40,13 @@ def ping(request, code): @csrf_exempt def handle_email(request): + if request.method != "POST": + return HttpResponseBadRequest() + events = json.loads(request.POST["mandrill_events"]) for event in events: - for to_address in event["msg"]["to"]: - code, domain = to_address.split("@") + for recipient_address, recipient_name in event["msg"]["to"]: + code, domain = recipient_address.split("@") try: check = Check.objects.get(code=code) except ValueError: