diff --git a/care/facility/api/viewsets/bed.py b/care/facility/api/viewsets/bed.py index d282cae2ad..aedec583a5 100644 --- a/care/facility/api/viewsets/bed.py +++ b/care/facility/api/viewsets/bed.py @@ -241,10 +241,14 @@ def get_queryset(self): tags=["consultationbed"], ) @action(detail=True, methods=["PATCH"]) - def toggle_patient_privacy(self, request, *args, **kwargs): + def toggle_patient_privacy(self, request, external_id): try: user: User = request.user - if ( + consultation_bed: ConsultationBed = ( + self.get_queryset().filter(external_id=external_id).first() + ) + + if consultation_bed and ( user.user_type == User.TYPE_VALUE_MAP["WardAdmin"] or user.user_type == User.TYPE_VALUE_MAP["LocalBodyAdmin"] or user.user_type == User.TYPE_VALUE_MAP["DistrictAdmin"] @@ -252,15 +256,14 @@ def toggle_patient_privacy(self, request, *args, **kwargs): or ( user.user_type == User.TYPE_VALUE_MAP["Doctor"] and user.home_facility.external_id - == self.get_object().bed.facility.external_id + == consultation_bed.bed.facility.external_id ) or ( user.user_type == User.TYPE_VALUE_MAP["Staff"] and user.home_facility.external_id - == self.get_object().bed.facility.external_id + == consultation_bed.bed.facility.external_id ) ): - consultation_bed: ConsultationBed = self.get_object() consultation_bed.privacy = not consultation_bed.privacy consultation_bed.save() return Response({"status": "success"}, status=status.HTTP_200_OK) @@ -270,4 +273,4 @@ def toggle_patient_privacy(self, request, *args, **kwargs): except PermissionDenied as e: return Response({"message": e.detail}, status=status.HTTP_403_FORBIDDEN) except Exception as e: - return Response({"message": e}, status=status.HTTP_400_BAD_REQUEST) + return Response({"message": str(e)}, status=status.HTTP_400_BAD_REQUEST) diff --git a/care/facility/migrations/0396_merge_20231122_1620.py b/care/facility/migrations/0396_merge_20231122_1620.py new file mode 100644 index 0000000000..95c6996e18 --- /dev/null +++ b/care/facility/migrations/0396_merge_20231122_1620.py @@ -0,0 +1,14 @@ +# Generated by Django 4.2.2 on 2023-11-22 10:50 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0379_consultationbed_privacy"), + ("facility", "0393_alter_notification_event"), + ("facility", "0394_auto_20231114_2219"), + ("facility", "0395_alter_patientconsultation_route_to_facility"), + ] + + operations = [] diff --git a/care/facility/tests/test_asset_operate_api.py b/care/facility/tests/test_asset_operate_api.py index ab9fe0410a..68c977e7f2 100644 --- a/care/facility/tests/test_asset_operate_api.py +++ b/care/facility/tests/test_asset_operate_api.py @@ -1,46 +1,35 @@ from rest_framework import status -from rest_framework.test import APIRequestFactory, APITestCase +from rest_framework.test import APITestCase -from care.facility.api.viewsets.asset import AssetViewSet -from care.facility.models import Asset, AssetBed, AssetLocation, Bed -from care.facility.tests.mixins import TestClassMixin -from care.utils.tests.test_base import TestBase +from care.facility.models import AssetBed +from care.utils.tests.test_utils import TestUtils -class AssetViewSetTestCase(TestBase, TestClassMixin, APITestCase): - asset_id = None - - def setUp(self): - self.factory = APIRequestFactory() - state = self.create_state() - district = self.create_district(state=state) - self.user = self.create_user(district=district, username="test user") - facility = self.create_facility(district=district, user=self.user) - self.asset1_location = AssetLocation.objects.create( - name="asset1 location", location_type=1, facility=facility +class AssetViewSetTestCase(TestUtils, APITestCase): + @classmethod + def setUpTestData(cls): + cls.state = cls.create_state() + cls.district = cls.create_district(state=cls.state) + cls.local_body = cls.create_local_body(cls.district) + cls.user = cls.create_user(district=cls.district, username="test user") + cls.facility = cls.create_facility( + district=cls.district, local_body=cls.local_body, user=cls.user ) + cls.asset1_location = cls.create_asset_location(facility=cls.facility) # depends upon the operational dev camera config - self.onvif_meta = { + cls.onvif_meta = { "asset_type": "CAMERA", "local_ip_address": "192.168.1.64", "camera_access_key": "remote_user:2jCkrCRSeahzKEU:d5694af2-21e2-4a39-9bad-2fb98d9818bd", "middleware_hostname": "dev_middleware.coronasafe.live", } - self.hl7monitor_meta = {} - self.ventilator_meta = {} - self.bed = Bed.objects.create( - name="Test Bed", - facility=facility, - location=self.asset1_location, - meta={}, - bed_type=1, - ) - self.asset: Asset = Asset.objects.create( - name="Test Asset", - current_location=self.asset1_location, - asset_type=50, + cls.hl7monitor_meta = {} + cls.ventilator_meta = {} + cls.bed = cls.create_bed( + facility=cls.facility, location=cls.asset1_location, meta={} ) + cls.asset = cls.create_asset(location=cls.asset1_location) def test_onvif_relative_move(self): self.asset.asset_class = "ONVIF" @@ -70,16 +59,10 @@ def test_onvif_relative_move(self): }, } } - response = self.new_request( - ( - f"/api/v1/asset/{self.asset.external_id}/operate_assets/", - sample_data, - "json", - ), - {"post": "operate_assets"}, - AssetViewSet, - self.user, - {"external_id": self.asset.external_id}, + response = self.client.post( + f"/api/v1/asset/{self.asset.external_id}/operate_assets/", + sample_data, + format="json", ) self.assertEqual(response.status_code, status.HTTP_200_OK) @@ -96,16 +79,10 @@ def test_onvif_relative_move(self): }, } } - response_invalid = self.new_request( - ( - f"/api/v1/asset/{self.asset.external_id}/operate_assets/", - sample_data_invald, - "json", - ), - {"post": "operate_assets"}, - AssetViewSet, - self.user, - {"external_id": self.asset.external_id}, + response_invalid = self.client.post( + f"/api/v1/asset/{self.asset.external_id}/operate_assets/", + sample_data_invald, + "json", ) self.assertEqual(response_invalid.status_code, status.HTTP_400_BAD_REQUEST) diff --git a/care/facility/tests/test_patient_consultationbed.py b/care/facility/tests/test_patient_consultationbed.py index 6fedc397a3..d6fa223699 100644 --- a/care/facility/tests/test_patient_consultationbed.py +++ b/care/facility/tests/test_patient_consultationbed.py @@ -2,29 +2,32 @@ from django.utils.timezone import make_aware from rest_framework import status -from rest_framework.test import APIRequestFactory, APITestCase +from rest_framework.test import APITestCase -from care.facility.api.viewsets.bed import ConsultationBedViewSet -from care.facility.models import FacilityUser -from care.facility.models.asset import AssetLocation -from care.facility.models.bed import Bed, ConsultationBed -from care.facility.tests.mixins import TestClassMixin from care.users.models import User -from care.utils.tests.test_base import TestBase +from care.utils.tests.test_utils import TestUtils -class TestPatientConsultationbed(TestBase, TestClassMixin, APITestCase): - def setUp(self): - self.factory = APIRequestFactory() - self.location = AssetLocation.objects.create( - name="asset location", location_type=1, facility=self.facility +class TestPatientConsultationbed(TestUtils, APITestCase): + @classmethod + def setUpTestData(cls): + cls.state = cls.create_state() + cls.district = cls.create_district(state=cls.state) + cls.local_body = cls.create_local_body(cls.district) + cls.user = cls.create_user(district=cls.district, username="test user") + cls.facility = cls.create_facility( + district=cls.district, local_body=cls.local_body, user=cls.user ) - self.bed: Bed = Bed.objects.create( + cls.patient = cls.create_patient(cls.district, cls.facility) + cls.location = cls.create_asset_location(facility=cls.facility) + cls.bed = cls.create_bed( name="Test Bed", - facility=self.facility, - location=self.location, + facility=cls.facility, + location=cls.location, + ) + cls.consultation = cls.create_consultation( + facility=cls.facility, patient=cls.patient ) - self.consultation = self.create_consultation() def test_patient_privacy_toggle_success(self): allowed_user_types = [ @@ -42,10 +45,7 @@ def test_patient_privacy_toggle_success(self): district=self.district, home_facility=self.facility, ) - self.facility_user = FacilityUser.objects.create( - created_by=self.user, facility=self.facility, user=self.user - ) - consultation_bed: ConsultationBed = ConsultationBed.objects.create( + consultation_bed = self.create_consultation_bed( consultation=self.consultation, bed=self.bed, start_date=make_aware(datetime.datetime.now()), @@ -53,23 +53,13 @@ def test_patient_privacy_toggle_success(self): privacy=True, ) - response = self.new_request( - ( - f"/api/v1/consultationbed/{consultation_bed.external_id}/toggle_patient_privacy/", - {}, - "json", - ), - {"patch": "toggle_patient_privacy"}, - ConsultationBedViewSet, - self.user, + self.client.force_authenticate(user=self.user) + response = self.client.patch( + f"/api/v1/consultationbed/{consultation_bed.external_id}/toggle_patient_privacy/", {"external_id": consultation_bed.external_id}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) consultation_bed.delete() - self.consultation.delete() - self.bed.delete() - self.location.delete() - self.facility.delete() self.user.delete() def test_patient_privacy_toggle_failure(self): @@ -87,18 +77,17 @@ def test_patient_privacy_toggle_failure(self): "Staff", ] for user_type in non_allowed_user_types: - self.facility2 = self.create_facility(self.district, name="Test Facility 2") + facility2 = self.create_facility( + district=self.district, local_body=self.local_body, user=self.user + ) self.user = self.create_user( username=f"{user_type} test user", user_type=User.TYPE_VALUE_MAP[user_type], district=self.district, - home_facility=self.facility2, - ) - self.facility_user = FacilityUser.objects.create( - created_by=self.user, facility=self.facility, user=self.user + home_facility=facility2, ) - consultation_bed: ConsultationBed = ConsultationBed.objects.create( + consultation_bed = self.create_consultation_bed( consultation=self.consultation, bed=self.bed, start_date=make_aware(datetime.datetime.now()), @@ -106,21 +95,11 @@ def test_patient_privacy_toggle_failure(self): privacy=True, ) - response = self.new_request( - ( - f"/api/v1/consultationbed/{consultation_bed.external_id}/toggle_patient_privacy/", - {}, - "json", - ), - {"patch": "toggle_patient_privacy"}, - ConsultationBedViewSet, - self.user, + self.client.force_authenticate(user=self.user) + response = self.client.patch( + f"/api/v1/consultationbed/{consultation_bed.external_id}/toggle_patient_privacy/", {"external_id": consultation_bed.external_id}, ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) consultation_bed.delete() - self.consultation.delete() - self.bed.delete() - self.location.delete() - self.facility.delete() self.user.delete()