From dc137d705e2d3ddc73ecdf7266220fe884ede03d Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Tue, 18 Jul 2023 20:29:04 +0100 Subject: [PATCH] Fix public API integration default route (#2573) Fix bug related to `order` and default route introduced in https://github.com/grafana/oncall/pull/2572 --- .../public_api/tests/test_integrations.py | 42 +++++++++++++++++++ engine/common/ordered_model/serializer.py | 4 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/engine/apps/public_api/tests/test_integrations.py b/engine/apps/public_api/tests/test_integrations.py index c154c00f..ab71e147 100644 --- a/engine/apps/public_api/tests/test_integrations.py +++ b/engine/apps/public_api/tests/test_integrations.py @@ -775,3 +775,45 @@ def test_get_list_integrations_link_and_inbound_email( else: assert integration_link == f"https://test.com/integrations/v1/{integration_type}/test123/" assert integration_inbound_email is None + + +@pytest.mark.django_db +def test_create_integration_default_route( + make_organization_and_user_with_token, + make_escalation_chain, +): + organization, _, token = make_organization_and_user_with_token() + escalation_chain = make_escalation_chain(organization) + + client = APIClient() + data_for_create = { + "type": "grafana", + "name": "grafana_created", + "team_id": None, + "default_route": {"escalation_chain_id": escalation_chain.public_primary_key}, + } + url = reverse("api-public:integrations-list") + response = client.post(url, data=data_for_create, format="json", HTTP_AUTHORIZATION=f"{token}") + assert response.status_code == status.HTTP_201_CREATED + assert response.data["default_route"]["escalation_chain_id"] == escalation_chain.public_primary_key + + +@pytest.mark.django_db +def test_update_integration_default_route( + make_organization_and_user_with_token, make_escalation_chain, make_alert_receive_channel, make_channel_filter +): + organization, _, token = make_organization_and_user_with_token() + integration = make_alert_receive_channel(organization) + make_channel_filter(integration, is_default=True) + escalation_chain = make_escalation_chain(organization) + + client = APIClient() + data_for_update = { + "default_route": {"escalation_chain_id": escalation_chain.public_primary_key}, + } + + url = reverse("api-public:integrations-detail", args=[integration.public_primary_key]) + response = client.put(url, data=data_for_update, format="json", HTTP_AUTHORIZATION=f"{token}") + + assert response.status_code == status.HTTP_200_OK + assert response.data["default_route"]["escalation_chain_id"] == escalation_chain.public_primary_key diff --git a/engine/common/ordered_model/serializer.py b/engine/common/ordered_model/serializer.py index 3e1022c7..d5c36ed1 100644 --- a/engine/common/ordered_model/serializer.py +++ b/engine/common/ordered_model/serializer.py @@ -15,7 +15,7 @@ class OrderedModelSerializer(serializers.ModelSerializer): def create(self, validated_data): # Remove "manual_order" and "order" fields from validated_data, so they are not passed to create method. - manual_order = validated_data.pop("manual_order") + manual_order = validated_data.pop("manual_order", False) order = validated_data.pop("order", None) # Create the instance. @@ -30,7 +30,7 @@ class OrderedModelSerializer(serializers.ModelSerializer): def update(self, instance, validated_data): # Remove "manual_order" and "order" fields from validated_data, so they are not passed to update method. - manual_order = validated_data.pop("manual_order") + manual_order = validated_data.pop("manual_order", False) order = validated_data.pop("order", None) # Adjust order of the instance if necessary.