From c6e35c9b391f670060abe12e41e3f9438676792d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 18 Jan 2018 00:39:44 +0200 Subject: [PATCH] Handle Telegram callbacks that are not text messages. Fixes #147 --- hc/front/schemas.py | 2 +- hc/front/tests/test_add_telegram.py | 22 +++++++++++++++++++--- hc/front/views.py | 3 ++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/hc/front/schemas.py b/hc/front/schemas.py index 7bbd976b..f940bf08 100644 --- a/hc/front/schemas.py +++ b/hc/front/schemas.py @@ -16,7 +16,7 @@ telegram_callback = { }, "text": {"type": "string"} }, - "required": ["chat", "text"] + "required": ["chat"] } }, "required": ["message"] diff --git a/hc/front/tests/test_add_telegram.py b/hc/front/tests/test_add_telegram.py index 35b138f1..b72ffd47 100644 --- a/hc/front/tests/test_add_telegram.py +++ b/hc/front/tests/test_add_telegram.py @@ -56,12 +56,15 @@ class AddTelegramTestCase(BaseTestCase): def test_bot_handles_bad_message(self, mock_get): samples = ["", "{}"] - # text is missing + # text is not string samples.append(json.dumps({ - "message": {"chat": {"id": 123, "type": "group"}} + "message": { + "chat": {"id": 123, "type": "invalid"}, + "text": 123 + } })) - # bad type + # bad message type samples.append(json.dumps({ "message": { "chat": {"id": 123, "type": "invalid"}, @@ -74,3 +77,16 @@ class AddTelegramTestCase(BaseTestCase): content_type="application/json") self.assertEqual(r.status_code, 400) + + @patch("hc.api.transports.requests.request") + def test_it_handles_missing_text(self, mock_get): + data = { + "message": { + "chat": {"id": 123, "title": "My Group", "type": "group"} + } + } + r = self.client.post("/integrations/telegram/bot/", json.dumps(data), + content_type="application/json") + + self.assertEqual(r.status_code, 200) + self.assertFalse(mock_get.called) diff --git a/hc/front/views.py b/hc/front/views.py index 9f8eb298..d3091f8e 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -823,7 +823,8 @@ def telegram_bot(request): except jsonschema.ValidationError: return HttpResponseBadRequest() - if "/start" not in doc["message"]["text"]: + text = doc["message"].get("text", "") + if "/start" not in text: return HttpResponse() chat = doc["message"]["chat"]