From ab8b7e52f5c8d77ef5c72e0c65440f9d18ea6160 Mon Sep 17 00:00:00 2001 From: rup-narayan-rajbanshi Date: Tue, 9 Jul 2024 16:54:48 +0545 Subject: [PATCH] Add modified_by and modified_at in local-unit api. --- local_units/serializers.py | 6 +++++ local_units/test_views.py | 47 ++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/local_units/serializers.py b/local_units/serializers.py index 40701c6ffe..e68a1f7017 100644 --- a/local_units/serializers.py +++ b/local_units/serializers.py @@ -246,12 +246,15 @@ class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin): class Meta: model = LocalUnit fields = ( + "id", "local_branch_name", "english_branch_name", "type", "country", "created_at", + "created_by", "modified_at", + "modified_by", "draft", "validated", "postcode", @@ -405,6 +408,7 @@ class PrivateLocalUnitSerializer(serializers.ModelSerializer): type_details = LocalUnitTypeSerializer(source="type", read_only=True) health_details = MiniHealthDataSerializer(read_only=True, source="health") validated = serializers.BooleanField(read_only=True) + modified_by_details = LocalUnitMiniUserSerializer(source="modified_by", read_only=True) class Meta: model = LocalUnit @@ -426,6 +430,8 @@ class Meta: "focal_person_en", "email", "phone", + "modified_at", + "modified_by_details", ) def get_location_details(self, unit) -> dict: diff --git a/local_units/test_views.py b/local_units/test_views.py index 35f10b1e86..dab6761b25 100644 --- a/local_units/test_views.py +++ b/local_units/test_views.py @@ -4,6 +4,7 @@ from django.contrib.gis.geos import Point from api.models import Country, Region +from deployments.factories.user import UserFactory from main.test_case import APITestCase from .models import ( @@ -117,10 +118,10 @@ def test_filter(self): class TestLocalUnitsDetailView(APITestCase): def setUp(self): super().setUp() - region = Region.objects.create(name=2) - country = Country.objects.create(name="Nepal", iso3="NLP", region=region) - type = LocalUnitType.objects.create(code=0, name="Code 0") - LocalUnitFactory.create_batch(2, country=country, type=type) + self.region = Region.objects.create(name=2) + self.country = Country.objects.create(name="Nepal", iso3="NLP", region=self.region) + self.type = LocalUnitType.objects.create(code=0, name="Code 0") + LocalUnitFactory.create_batch(2, country=self.country, type=self.type) def test_detail(self): local_unit = LocalUnit.objects.all().first() @@ -133,6 +134,26 @@ def test_detail(self): self.assertEqual(response.data["type_details"]["name"], "Code 0") self.assertEqual(response.data["type_details"]["code"], 0) + def test_get_updated_at_updated_by(self): + self.authenticate() + user_1 = UserFactory.create() + user_2 = UserFactory.create() + user_1_local_units = LocalUnitFactory.create_batch( + 2, country=self.country, type=self.type, created_by=user_1, modified_by=user_1 + ) + user_2_local_units = LocalUnitFactory.create_batch( + 2, country=self.country, type=self.type, created_by=user_1, modified_by=user_2 + ) + user_1_local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in user_1_local_units]).first() + user_2_local_unit = LocalUnit.objects.filter(id__in=[unit.id for unit in user_2_local_units]).first() + response = self.client.get(f"/api/v2/local-units/{user_1_local_unit.id}/") + self.assertIsNotNone(response.data["modified_at"]) + self.assertEqual(response.data["modified_by_details"]["id"], user_1.id) + + response = self.client.get(f"/api/v2/local-units/{user_2_local_unit.id}/") + self.assertIsNotNone(response.data["modified_at"]) + self.assertEqual(response.data["modified_by_details"]["id"], user_2.id) + def test_validate_local_units(self): local_unit = LocalUnit.objects.all().first() self.authenticate() @@ -285,7 +306,7 @@ def test_create_local_unit_administrative(self): response = self.client.post("/api/v2/local-units/", data=data, format="json") self.assertEqual(response.status_code, 201) - def test_create_local_unit_health(self): + def test_create_update_local_unit_health(self): region = Region.objects.create(name=2) country = Country.objects.create(name="Philippines", iso3="PHL", iso="PH", region=region) type = LocalUnitType.objects.create(code=2, name="Code 0") @@ -366,5 +387,17 @@ def test_create_local_unit_health(self): "phone": "", } self.client.force_authenticate(self.root_user) - response = self.client.post("/api/v2/local-units/", data=data, format="json") - self.assertEqual(response.status_code, 201) + response = self.client.post("/api/v2/local-units/", data=data, format="json").json() + + # test update + local_unit_id = response["id"] + response_updated_1 = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json").json() + self.assertEqual(response_updated_1["modified_by"], response["created_by"]) + self.assertIsNotNone(response_updated_1["modified_at"]) + + # update existing local_unit with new user + user_1 = UserFactory() + self.client.force_authenticate(user_1) + response_updated_2 = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json").json() + self.assertEqual(response_updated_2["modified_by_details"]["id"], user_1.id) + assert response_updated_1["modified_at"] < response_updated_2["modified_at"]