Skip to content

Commit

Permalink
Make it impossible to set event types to empty
Browse files Browse the repository at this point in the history
This would result in no notifications ever being sent
  • Loading branch information
johannaengland committed Apr 10, 2024
1 parent e74c05a commit bee6399
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,11 @@ Notification profile endpoints
}
}
.. note::
``event_types`` cannot be ``null`` or ``[]``, because all events have an event
type set, which would therefore result in no notifications ever being sent.
For not filtering by ``event_types`` the key needs to be removed.


- ``/api/v1/notificationprofiles/filters/<int:pk>/``:

Expand Down Expand Up @@ -1490,6 +1495,11 @@ Notification profile endpoints
}
}
.. note::
``event_types`` cannot be ``null`` or ``[]``, because all events have an event
type set, which would therefore result in no notifications ever being sent.
For not filtering by ``event_types`` the key needs to be removed.


- ``/api/v2/notificationprofiles/filters/<int:pk>/``:

Expand Down
5 changes: 5 additions & 0 deletions src/argus/notificationprofile/primitive_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class FilterBlobSerializer(serializers.Serializer):
required=False,
)

def validate_event_types(self, value):
if not value:
raise serializers.ValidationError("Event types cannot be empty, remove it completely from filter instead.")
return value


class FilterPreviewSerializer(serializers.Serializer):
sourceSystemIds = serializers.ListField(child=serializers.IntegerField(min_value=1), allow_empty=True)
Expand Down
11 changes: 11 additions & 0 deletions tests/notificationprofile/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ def test_should_create_filter_with_event_types(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertTrue(Filter.objects.filter(pk=response.data["pk"]).exists())

def test_should_not_create_filter_with_empty_event_types(self):
filter_name = "test-filter"
response = self.user1_rest_client.post(
path=self.ENDPOINT,
data={
"name": filter_name,
"filter": {"event_types": []},
},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_should_update_filter_name_with_valid_values(self):
filter1_pk = self.filter1.pk
filter1_path = f"{self.ENDPOINT}{filter1_pk}/"
Expand Down

0 comments on commit bee6399

Please sign in to comment.