Skip to content

Commit

Permalink
Add validation checks in ResourceRequestSerializer (#1928)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rash-27 and sainak authored Mar 17, 2024
1 parent 114c643 commit 7f3bb93
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
56 changes: 9 additions & 47 deletions care/facility/api/serializers/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
36 changes: 36 additions & 0 deletions care/facility/models/tests/test_resource.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7f3bb93

Please sign in to comment.