diff --git a/app/models/broadcast_message.py b/app/models/broadcast_message.py index fc31ec1bf..eda45fa53 100644 --- a/app/models/broadcast_message.py +++ b/app/models/broadcast_message.py @@ -78,9 +78,17 @@ class BroadcastMessage(JSONModel): @property def areas(self): - return self.get_areas( - areas=self._dict['areas'] - ) or CustomBroadcastAreas( + library_areas = self.get_areas(areas=self._dict['areas']) + + if library_areas: + if len(library_areas) != len(self._dict['areas']): + raise RuntimeError( + f'BroadcastMessage has {len(self._dict["areas"])} areas ' + f'but {len(library_areas)} found in the library' + ) + return library_areas + + return CustomBroadcastAreas( areas=self._dict['areas'], polygons=self._dict['simple_polygons'], ) diff --git a/tests/app/models/test_broadcast_message.py b/tests/app/models/test_broadcast_message.py index 1b9a46124..361c5d149 100644 --- a/tests/app/models/test_broadcast_message.py +++ b/tests/app/models/test_broadcast_message.py @@ -1,3 +1,5 @@ +import pytest + from app.models.broadcast_message import BroadcastMessage from tests import broadcast_message_json @@ -45,3 +47,24 @@ def test_content_comes_from_attribute_not_template(fake_uuid): created_by_id=fake_uuid, )) assert broadcast_message.content == 'This is a test' + + +def test_raises_for_missing_areas(fake_uuid): + broadcast_message = BroadcastMessage(broadcast_message_json( + id_=fake_uuid, + service_id=fake_uuid, + template_id=fake_uuid, + status='draft', + created_by_id=fake_uuid, + areas=[ + 'wd20-E05009372', + 'something else', + ], + )) + + with pytest.raises(RuntimeError) as exception: + broadcast_message.areas + + assert str(exception.value) == ( + 'BroadcastMessage has 2 areas but 1 found in the library' + )