From cf3f69199a06017bf84788236ab67841a84a2a6f Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Mon, 6 Sep 2021 12:34:38 +0100 Subject: [PATCH] Support new broadcasts (without area IDs) Previously we relied on the API defaulting this field to an empty array [1], but that conflicts with using it to decide whether a broadcast is custom or created in this app. [1]: https://github.com/alphagov/notifications-api/blob/3779146cc51385327cef90390af1f62db983919f/app/models.py#L2342 --- app/models/broadcast_message.py | 2 +- tests/app/models/test_broadcast_message.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/broadcast_message.py b/app/models/broadcast_message.py index 3a180dba2..b7e6c218e 100644 --- a/app/models/broadcast_message.py +++ b/app/models/broadcast_message.py @@ -111,7 +111,7 @@ class BroadcastMessage(JSONModel): @property def area_ids(self): - return self._dict['areas']['ids'] + return self._dict['areas'].get('ids', []) @area_ids.setter def area_ids(self, value): diff --git a/tests/app/models/test_broadcast_message.py b/tests/app/models/test_broadcast_message.py index 025cd24f1..4e2705b90 100644 --- a/tests/app/models/test_broadcast_message.py +++ b/tests/app/models/test_broadcast_message.py @@ -4,6 +4,21 @@ from app.models.broadcast_message import BroadcastMessage from tests import broadcast_message_json +@pytest.mark.parametrize('areas, expected_area_ids', [ + ({'simple_polygons': []}, []), + ({'ids': ['123'], 'simple_polygons': []}, ['123']) +]) +def test_area_ids( + areas, + expected_area_ids, +): + broadcast_message = BroadcastMessage(broadcast_message_json( + areas=areas + )) + + assert broadcast_message.area_ids == expected_area_ids + + def test_simple_polygons(): broadcast_message = BroadcastMessage(broadcast_message_json( area_ids=[