Merge pull request #473 from grafana/matiasb/fix-update-channel-filter-api-multiple-backends

Fix channel filter updates when there are multiple backends
This commit is contained in:
Matias Bordese 2022-09-06 17:14:07 -03:00 committed by GitHub
commit b48aadc01c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View file

@ -96,7 +96,7 @@ class ChannelFilterSerializer(OrderedModelSerializerMixin, EagerLoadingMixin, se
organization = self.context["request"].auth.organization
if not isinstance(notification_backends, dict):
raise serializers.ValidationError(["Invalid messaging backend data"])
current = self.instance.notification_backends or {}
updated = self.instance.notification_backends or {}
for backend_id in notification_backends:
backend = get_messaging_backend_from_id(backend_id)
if backend is None:
@ -106,7 +106,8 @@ class ChannelFilterSerializer(OrderedModelSerializerMixin, EagerLoadingMixin, se
notification_backends[backend_id],
)
# update existing backend data
notification_backends[backend_id] = current.get(backend_id, {}) | updated_data
updated[backend_id] = updated.get(backend_id, {}) | updated_data
notification_backends = updated
return notification_backends

View file

@ -437,7 +437,10 @@ def test_channel_filter_update_notification_backends_updates_existing_data(
):
organization, user, token = make_organization_and_user_with_plugin_token()
alert_receive_channel = make_alert_receive_channel(organization)
existing_notification_backends = {"TESTONLY": {"enabled": True, "channel": "ABCDEF"}}
existing_notification_backends = {
"TESTONLY": {"enabled": True, "channel": "ABCDEF"},
"ANOTHERONE": {"enabled": False, "channel": "123456"},
}
channel_filter = make_channel_filter(alert_receive_channel, notification_backends=existing_notification_backends)
client = APIClient()
@ -448,7 +451,13 @@ def test_channel_filter_update_notification_backends_updates_existing_data(
"notification_backends": notification_backends_update,
}
response = client.put(url, data=data_for_update, format="json", **make_user_auth_headers(user, token))
class FakeBackend:
def validate_channel_filter_data(self, organization, data):
return data
with patch("apps.api.serializers.channel_filter.get_messaging_backend_from_id") as mock_get_backend:
mock_get_backend.return_value = FakeBackend()
response = client.put(url, data=data_for_update, format="json", **make_user_auth_headers(user, token))
channel_filter.refresh_from_db()