From e91441d8146e97bbabaf11a2797bc921ac764712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 21 May 2021 13:05:37 +0300 Subject: [PATCH] Add fallback for legacy sms values --- hc/api/models.py | 10 ++++++++-- hc/api/tests/test_channel_model.py | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 7311b44c..a20674dd 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -742,14 +742,20 @@ class Channel(models.Model): @property def sms_notify_up(self): assert self.kind == "sms" + if not self.value.startswith("{"): + return False + doc = json.loads(self.value) - return doc["up"] + return doc.get("up", False) @property def sms_notify_down(self): assert self.kind == "sms" + if not self.value.startswith("{"): + return True + doc = json.loads(self.value) - return doc["down"] + return doc.get("down", True) @property def opsgenie_key(self): diff --git a/hc/api/tests/test_channel_model.py b/hc/api/tests/test_channel_model.py index 9c106de7..ab3f9e61 100644 --- a/hc/api/tests/test_channel_model.py +++ b/hc/api/tests/test_channel_model.py @@ -50,3 +50,13 @@ class ChannelModelTestCase(BaseTestCase): c.value = json.dumps({"key": "abc", "region": "eu"}) self.assertEqual(c.opsgenie_key, "abc") self.assertEqual(c.opsgenie_region, "eu") + + def test_it_handles_legacy_sms_value(self): + c = Channel(kind="sms", value="+123123123") + self.assertTrue(c.sms_notify_down) + self.assertFalse(c.sms_notify_up) + + def test_it_handles_legacy_sms_json_value(self): + c = Channel(kind="sms", value=json.dumps({"value": "+123123123"})) + self.assertTrue(c.sms_notify_down) + self.assertFalse(c.sms_notify_up)