From 7f3bb9379edfee43cd282792ea987fe8ee2abca5 Mon Sep 17 00:00:00 2001 From: Rashmik <146672184+rash-27@users.noreply.github.com> Date: Sun, 17 Mar 2024 11:13:46 +0530 Subject: [PATCH] Add validation checks in ResourceRequestSerializer (#1928) * Add validation checks in ResourceRequestSerializer * add test for resource * Fix lint errors in Contributing.md * Discard changes to CONTRIBUTING.md * lint fix --------- Co-authored-by: Aakash Singh --- care/facility/api/serializers/resources.py | 56 ++++----------------- care/facility/models/tests/test_resource.py | 36 +++++++++++++ 2 files changed, 45 insertions(+), 47 deletions(-) create mode 100644 care/facility/models/tests/test_resource.py diff --git a/care/facility/api/serializers/resources.py b/care/facility/api/serializers/resources.py index e94cc8f05f..efc5df3294 100644 --- a/care/facility/api/serializers/resources.py +++ b/care/facility/api/serializers/resources.py @@ -13,6 +13,7 @@ ) from care.facility.models.resources import RESOURCE_SUB_CATEGORY_CHOICES from care.users.api.serializers.user import UserBaseMinimumSerializer +from care.utils.serializer.external_id_field import ExternalIdSerializerField from config.serializers import ChoiceField @@ -61,14 +62,16 @@ class ResourceRequestSerializer(serializers.ModelSerializer): category = ChoiceField(choices=RESOURCE_CATEGORY_CHOICES) sub_category = ChoiceField(choices=RESOURCE_SUB_CATEGORY_CHOICES) - origin_facility = serializers.UUIDField( - source="origin_facility.external_id", allow_null=False, required=True + origin_facility = ExternalIdSerializerField( + queryset=Facility.objects.all(), required=True ) - approving_facility = serializers.UUIDField( - source="approving_facility.external_id", allow_null=False, required=True + + approving_facility = ExternalIdSerializerField( + queryset=Facility.objects.all(), required=True ) - assigned_facility = serializers.UUIDField( - source="assigned_facility.external_id", allow_null=True, required=False + + assigned_facility = ExternalIdSerializerField( + queryset=Facility.objects.all(), required=False ) assigned_to_object = UserBaseMinimumSerializer(source="assigned_to", read_only=True) @@ -115,24 +118,6 @@ def update(self, instance, validated_data): if "origin_facility" in validated_data: validated_data.pop("origin_facility") - if "approving_facility" in validated_data: - approving_facility_external_id = validated_data.pop("approving_facility")[ - "external_id" - ] - if approving_facility_external_id: - validated_data["approving_facility_id"] = Facility.objects.get( - external_id=approving_facility_external_id - ).id - - if "assigned_facility" in validated_data: - assigned_facility_external_id = validated_data.pop("assigned_facility")[ - "external_id" - ] - if assigned_facility_external_id: - validated_data["assigned_facility_id"] = Facility.objects.get( - external_id=assigned_facility_external_id - ).id - instance.last_edited_by = self.context["request"].user new_instance = super().update(instance, validated_data) @@ -144,29 +129,6 @@ def create(self, validated_data): if "status" in validated_data: validated_data.pop("status") - origin_facility_external_id = validated_data.pop("origin_facility")[ - "external_id" - ] - validated_data["origin_facility_id"] = Facility.objects.get( - external_id=origin_facility_external_id - ).id - - request_approving_facility_external_id = validated_data.pop( - "approving_facility" - )["external_id"] - validated_data["approving_facility_id"] = Facility.objects.get( - external_id=request_approving_facility_external_id - ).id - - if "assigned_facility" in validated_data: - assigned_facility_external_id = validated_data.pop("assigned_facility")[ - "external_id" - ] - if assigned_facility_external_id: - validated_data["assigned_facility_id"] = Facility.objects.get( - external_id=assigned_facility_external_id - ).id - validated_data["created_by"] = self.context["request"].user validated_data["last_edited_by"] = self.context["request"].user diff --git a/care/facility/models/tests/test_resource.py b/care/facility/models/tests/test_resource.py new file mode 100644 index 0000000000..51e349a75b --- /dev/null +++ b/care/facility/models/tests/test_resource.py @@ -0,0 +1,36 @@ +from rest_framework import status +from rest_framework.test import APITestCase + +from care.utils.tests.test_utils import TestUtils + + +class ResourceTransferTest(TestUtils, APITestCase): + @classmethod + def setUpTestData(cls): + cls.state = cls.create_state() + cls.district = cls.create_district(cls.state) + cls.local_body = cls.create_local_body(cls.district) + cls.super_user = cls.create_super_user("su", cls.district) + cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body) + cls.user = cls.create_user("staff1", cls.district, home_facility=cls.facility) + cls.patient = cls.create_patient(cls.district, cls.facility) + + def test_with_invalid_facilityid_input(self): + dist_admin = self.create_user("dist_admin", self.district, user_type=30) + sample_data = { + "approving_facility": self.facility.external_id, + "category": "OXYGEN", + "emergency": "false", + "origin_facility": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", # invalid facility id + "reason": "adadasa", + "refering_facility_contact_name": "rash", + "refering_facility_contact_number": "+918888888889", + "requested_quantity": "10", + "status": "PENDING", + "sub_category": 110, + "title": "a", + } + self.client.force_authenticate(user=dist_admin) + response = self.client.post("/api/v1/resource/", sample_data, format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertIn("origin_facility", response.data)