Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar committed Nov 22, 2023
1 parent 9610e8b commit ecc2cbc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 108 deletions.
15 changes: 9 additions & 6 deletions care/facility/api/viewsets/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,29 @@ 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"]
or user.user_type == User.TYPE_VALUE_MAP["StateAdmin"]
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)
Expand All @@ -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)
14 changes: 14 additions & 0 deletions care/facility/migrations/0396_merge_20231122_1620.py
Original file line number Diff line number Diff line change
@@ -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 = []
77 changes: 27 additions & 50 deletions care/facility/tests/test_asset_operate_api.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
83 changes: 31 additions & 52 deletions care/facility/tests/test_patient_consultationbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -42,34 +45,21 @@ 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()),
end_date=make_aware(datetime.datetime.now()),
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):
Expand All @@ -87,40 +77,29 @@ 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()),
end_date=make_aware(datetime.datetime.now()),
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()

0 comments on commit ecc2cbc

Please sign in to comment.