this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

added pending testcases (#3532)

Co-authored-by: Nandkishor R <96693626+nandkishorr@users.noreply.github.com>

authored by

Prafful Sharma
Nandkishor R
and committed by
GitHub
76bd0c9f 28451821

+913 -2
+238
care/emr/tests/test_account_api.py
··· 1 + from django.urls import reverse 2 + from rest_framework import status 3 + 4 + from care.emr.resources.account.spec import ( 5 + AccountBillingStatusOptions, 6 + AccountStatusOptions, 7 + ) 8 + from care.utils.tests.base import CareAPITestBase 9 + 10 + 11 + class TestAccountViewSet(CareAPITestBase): 12 + def setUp(self): 13 + super().setUp() 14 + self.superuser = self.create_super_user() 15 + self.user = self.create_user() 16 + self.facility = self.create_facility(user=self.superuser) 17 + self.facility_organization = self.create_facility_organization( 18 + facility=self.facility 19 + ) 20 + self.patient = self.create_patient() 21 + self.client.force_authenticate(user=self.superuser) 22 + self.base_url = reverse( 23 + "account-list", 24 + kwargs={"facility_external_id": self.facility.external_id}, 25 + ) 26 + 27 + def _get_detail_url(self, account_id): 28 + return reverse( 29 + "account-detail", 30 + kwargs={ 31 + "facility_external_id": self.facility.external_id, 32 + "external_id": account_id, 33 + }, 34 + ) 35 + 36 + def _get_account_data(self, **overrides): 37 + data = { 38 + "status": AccountStatusOptions.active.value, 39 + "billing_status": AccountBillingStatusOptions.open.value, 40 + "name": "Test Account", 41 + "service_period": { 42 + "start": "2025-01-01T00:00:00Z", 43 + "end": "2025-12-31T23:59:59Z", 44 + }, 45 + "patient": str(self.patient.external_id), 46 + } 47 + data.update(overrides) 48 + return data 49 + 50 + def test_create_account(self): 51 + data = self._get_account_data() 52 + response = self.client.post(self.base_url, data, format="json") 53 + self.assertEqual(response.status_code, status.HTTP_200_OK) 54 + self.assertEqual(response.data["name"], "Test Account") 55 + 56 + def test_list_accounts(self): 57 + self.client.post(self.base_url, self._get_account_data(), format="json") 58 + response = self.client.get(self.base_url) 59 + self.assertEqual(response.status_code, status.HTTP_200_OK) 60 + self.assertGreaterEqual(len(response.data["results"]), 1) 61 + 62 + def test_retrieve_account(self): 63 + create_res = self.client.post( 64 + self.base_url, self._get_account_data(), format="json" 65 + ) 66 + self.assertEqual(create_res.status_code, status.HTTP_200_OK) 67 + url = self._get_detail_url(create_res.data["id"]) 68 + response = self.client.get(url) 69 + self.assertEqual(response.status_code, status.HTTP_200_OK) 70 + self.assertEqual(response.data["id"], create_res.data["id"]) 71 + 72 + def test_update_account(self): 73 + create_res = self.client.post( 74 + self.base_url, self._get_account_data(), format="json" 75 + ) 76 + self.assertEqual(create_res.status_code, status.HTTP_200_OK) 77 + url = self._get_detail_url(create_res.data["id"]) 78 + update_data = self._get_account_data( 79 + name="Updated Account", 80 + status=AccountStatusOptions.inactive.value, 81 + ) 82 + del update_data["patient"] 83 + response = self.client.put(url, update_data, format="json") 84 + self.assertEqual(response.status_code, status.HTTP_200_OK) 85 + self.assertEqual(response.data["name"], "Updated Account") 86 + self.assertEqual(response.data["status"], AccountStatusOptions.inactive.value) 87 + 88 + def test_duplicate_active_open_account_for_patient(self): 89 + data = self._get_account_data() 90 + response = self.client.post(self.base_url, data, format="json") 91 + self.assertEqual(response.status_code, status.HTTP_200_OK) 92 + response2 = self.client.post(self.base_url, data, format="json") 93 + self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST) 94 + self.assertIn("Active account already exists", str(response2.data)) 95 + 96 + def test_allow_second_account_when_first_inactive(self): 97 + data = self._get_account_data(status=AccountStatusOptions.inactive.value) 98 + response = self.client.post(self.base_url, data, format="json") 99 + self.assertEqual(response.status_code, status.HTTP_200_OK) 100 + data2 = self._get_account_data() 101 + response2 = self.client.post(self.base_url, data2, format="json") 102 + self.assertEqual(response2.status_code, status.HTTP_200_OK) 103 + 104 + def test_filter_by_status(self): 105 + self.client.post(self.base_url, self._get_account_data(), format="json") 106 + patient2 = self.create_patient() 107 + self.client.post( 108 + self.base_url, 109 + self._get_account_data( 110 + patient=str(patient2.external_id), 111 + status=AccountStatusOptions.inactive.value, 112 + ), 113 + format="json", 114 + ) 115 + response = self.client.get( 116 + self.base_url, {"status": AccountStatusOptions.active.value} 117 + ) 118 + self.assertEqual(response.status_code, status.HTTP_200_OK) 119 + for r in response.data["results"]: 120 + self.assertEqual(r["status"], AccountStatusOptions.active.value) 121 + 122 + def test_filter_by_patient(self): 123 + self.client.post(self.base_url, self._get_account_data(), format="json") 124 + patient2 = self.create_patient() 125 + self.client.post( 126 + self.base_url, 127 + self._get_account_data(patient=str(patient2.external_id)), 128 + format="json", 129 + ) 130 + response = self.client.get( 131 + self.base_url, {"patient": str(self.patient.external_id)} 132 + ) 133 + self.assertEqual(response.status_code, status.HTTP_200_OK) 134 + self.assertEqual(len(response.data["results"]), 1) 135 + 136 + def test_filter_by_billing_status(self): 137 + self.client.post(self.base_url, self._get_account_data(), format="json") 138 + response = self.client.get( 139 + self.base_url, 140 + {"billing_status": AccountBillingStatusOptions.open.value}, 141 + ) 142 + self.assertEqual(response.status_code, status.HTTP_200_OK) 143 + for r in response.data["results"]: 144 + self.assertEqual( 145 + r["billing_status"], AccountBillingStatusOptions.open.value 146 + ) 147 + 148 + def test_update_with_primary_encounter_different_facility(self): 149 + create_res = self.client.post( 150 + self.base_url, self._get_account_data(), format="json" 151 + ) 152 + self.assertEqual(create_res.status_code, status.HTTP_200_OK) 153 + other_facility = self.create_facility(user=self.superuser) 154 + other_org = self.create_facility_organization(facility=other_facility) 155 + encounter = self.create_encounter( 156 + patient=self.patient, facility=other_facility, organization=other_org 157 + ) 158 + url = self._get_detail_url(create_res.data["id"]) 159 + update_data = self._get_account_data( 160 + primary_encounter=str(encounter.external_id) 161 + ) 162 + del update_data["patient"] 163 + response = self.client.put(url, update_data, format="json") 164 + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 165 + self.assertIn( 166 + "Primary encounter is not associated with the facility", 167 + response.data["detail"], 168 + ) 169 + 170 + def test_update_with_primary_encounter_different_patient(self): 171 + create_res = self.client.post( 172 + self.base_url, self._get_account_data(), format="json" 173 + ) 174 + self.assertEqual(create_res.status_code, status.HTTP_200_OK) 175 + other_patient = self.create_patient() 176 + encounter = self.create_encounter( 177 + patient=other_patient, 178 + facility=self.facility, 179 + organization=self.facility_organization, 180 + ) 181 + url = self._get_detail_url(create_res.data["id"]) 182 + update_data = self._get_account_data( 183 + primary_encounter=str(encounter.external_id) 184 + ) 185 + del update_data["patient"] 186 + response = self.client.put(url, update_data, format="json") 187 + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 188 + self.assertIn( 189 + "Primary encounter is not associated with the patient", 190 + response.data["detail"], 191 + ) 192 + 193 + def test_default_account_action(self): 194 + create_res = self.client.post( 195 + self.base_url, self._get_account_data(), format="json" 196 + ) 197 + self.assertEqual(create_res.status_code, status.HTTP_200_OK) 198 + encounter = self.create_encounter( 199 + patient=self.patient, 200 + facility=self.facility, 201 + organization=self.facility_organization, 202 + ) 203 + url = reverse( 204 + "account-default-account", 205 + kwargs={"facility_external_id": self.facility.external_id}, 206 + ) 207 + response = self.client.post( 208 + url, 209 + { 210 + "patient": str(self.patient.external_id), 211 + "facility": str(self.facility.external_id), 212 + "encounter": str(encounter.external_id), 213 + }, 214 + format="json", 215 + ) 216 + self.assertEqual(response.status_code, status.HTTP_200_OK) 217 + 218 + def test_default_account_no_account_found(self): 219 + encounter = self.create_encounter( 220 + patient=self.patient, 221 + facility=self.facility, 222 + organization=self.facility_organization, 223 + ) 224 + url = reverse( 225 + "account-default-account", 226 + kwargs={"facility_external_id": self.facility.external_id}, 227 + ) 228 + response = self.client.post( 229 + url, 230 + { 231 + "patient": str(self.patient.external_id), 232 + "facility": str(self.facility.external_id), 233 + "encounter": str(encounter.external_id), 234 + }, 235 + format="json", 236 + ) 237 + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) 238 + self.assertIn("No account found", str(response.data))
+51
care/emr/tests/test_encounter_api.py
··· 458 458 self.assertEqual(get_response.status_code, 200) 459 459 self.assertEqual(get_response.data["status"], StatusChoices.completed.value) 460 460 461 + def test_update_encounter_without_permissions(self): 462 + update_data = self.encounter_data.copy() 463 + update_data["status"] = StatusChoices.completed.value 464 + response = self.client.put( 465 + self._get_detail_url(self.facility.external_id, self.patient.external_id), 466 + update_data, 467 + format="json", 468 + ) 469 + self.assertEqual(response.status_code, 403) 470 + self.assertIn( 471 + "You do not have permission to update encounter", response.data["detail"] 472 + ) 473 + 474 + def test_restart_completed_encounter(self): 475 + self.client.force_authenticate(user=self.superuser) 476 + self.encounter.status = StatusChoices.completed.value 477 + self.encounter.save() 478 + url = reverse( 479 + "encounter-restart", 480 + kwargs={"external_id": self.encounter.external_id}, 481 + ) 482 + with self.settings(ENCOUNTER_RESTART_TIME_LIMIT_HOURS=24): 483 + response = self.client.post(url, format="json") 484 + self.assertEqual(response.status_code, 204) 485 + self.encounter.refresh_from_db() 486 + self.assertEqual(self.encounter.status, StatusChoices.in_progress.value) 487 + 488 + def test_restart_non_completed_encounter(self): 489 + self.client.force_authenticate(user=self.superuser) 490 + url = reverse( 491 + "encounter-restart", 492 + kwargs={"external_id": self.encounter.external_id}, 493 + ) 494 + response = self.client.post(url, format="json") 495 + self.assertEqual(response.status_code, 403) 496 + 497 + def test_restart_expired_encounter(self): 498 + self.client.force_authenticate(user=self.superuser) 499 + self.encounter.status = StatusChoices.completed.value 500 + self.encounter.modified_date = timezone.now() - timedelta( 501 + hours=settings.ENCOUNTER_RESTART_TIME_LIMIT_HOURS + 1 502 + ) 503 + self.encounter.save(update_fields=["status", "modified_date"]) 504 + url = reverse( 505 + "encounter-restart", 506 + kwargs={"external_id": self.encounter.external_id}, 507 + ) 508 + response = self.client.post(url, format="json") 509 + self.assertEqual(response.status_code, 400) 510 + self.assertIn("cannot be restarted after", str(response.data)) 511 + 461 512 462 513 class EncounterOrganizationAPITests(CareAPITestBase): 463 514 def setUp(self):
+139
care/emr/tests/test_facility_api.py
··· 1 + from django.urls import reverse 2 + from rest_framework import status 3 + 4 + from care.facility.models.facility import REVERSE_FACILITY_TYPES 5 + from care.utils.tests.base import CareAPITestBase 6 + 7 + 8 + class TestFacilityViewSet(CareAPITestBase): 9 + def setUp(self): 10 + super().setUp() 11 + self.superuser = self.create_super_user() 12 + self.user = self.create_user() 13 + self.geo_organization = self.create_organization(org_type="govt") 14 + self.facility = self.create_facility( 15 + user=self.superuser, 16 + name="Test Facility", 17 + facility_type=2, 18 + pincode=123456, 19 + ) 20 + self.client.force_authenticate(user=self.superuser) 21 + self.base_url = reverse("facility-list") 22 + 23 + def _get_detail_url(self, facility_id): 24 + return reverse( 25 + "facility-detail", 26 + kwargs={"external_id": facility_id}, 27 + ) 28 + 29 + def _get_facility_data(self, **overrides): 30 + data = { 31 + "name": "New Facility", 32 + "description": "Test desc", 33 + "facility_type": REVERSE_FACILITY_TYPES[2], 34 + "address": "Test Address", 35 + "pincode": 123456, 36 + "phone_number": "+911234567890", 37 + "is_public": True, 38 + "geo_organization": str(self.geo_organization.external_id), 39 + "features": [], 40 + } 41 + data.update(overrides) 42 + return data 43 + 44 + def test_create_facility(self): 45 + data = self._get_facility_data() 46 + response = self.client.post(self.base_url, data, format="json") 47 + self.assertEqual(response.status_code, status.HTTP_200_OK) 48 + self.assertEqual(response.data["name"], "New Facility") 49 + 50 + def test_create_facility_duplicate_name(self): 51 + data = self._get_facility_data(name="Test Facility") 52 + response = self.client.post(self.base_url, data, format="json") 53 + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) 54 + 55 + def test_list_facilities(self): 56 + response = self.client.get(self.base_url) 57 + self.assertEqual(response.status_code, status.HTTP_200_OK) 58 + self.assertGreaterEqual(len(response.data["results"]), 1) 59 + 60 + def test_retrieve_facility(self): 61 + url = self._get_detail_url(self.facility.external_id) 62 + response = self.client.get(url) 63 + self.assertEqual(response.status_code, status.HTTP_200_OK) 64 + self.assertEqual(response.data["id"], str(self.facility.external_id)) 65 + 66 + def test_update_facility(self): 67 + url = self._get_detail_url(self.facility.external_id) 68 + data = self._get_facility_data(name="Updated Facility") 69 + response = self.client.put(url, data, format="json") 70 + self.assertEqual(response.status_code, status.HTTP_200_OK) 71 + self.assertEqual(response.data["name"], "Updated Facility") 72 + 73 + def test_delete_facility_as_superuser(self): 74 + facility = self.create_facility( 75 + user=self.superuser, name="To Delete", facility_type=2 76 + ) 77 + url = self._get_detail_url(facility.external_id) 78 + response = self.client.delete(url) 79 + self.assertIn(response.status_code, [204, 200]) 80 + 81 + def test_delete_facility_as_non_superuser(self): 82 + self.client.force_authenticate(user=self.user) 83 + url = self._get_detail_url(self.facility.external_id) 84 + response = self.client.delete(url) 85 + self.assertIn(response.status_code, [403, 404]) 86 + 87 + def test_create_facility_without_permission(self): 88 + self.client.force_authenticate(user=self.user) 89 + data = self._get_facility_data(name="Unauth Facility") 90 + response = self.client.post(self.base_url, data, format="json") 91 + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 92 + 93 + def test_filter_by_name(self): 94 + response = self.client.get(self.base_url, {"name": "Test"}) 95 + self.assertEqual(response.status_code, status.HTTP_200_OK) 96 + for r in response.data["results"]: 97 + self.assertIn("Test", r["name"]) 98 + 99 + def test_filter_by_phone_number(self): 100 + facility = self.create_facility( 101 + user=self.superuser, phone_number="+919876543210" 102 + ) 103 + response = self.client.get(self.base_url, {"phone_number": "+919876543210"}) 104 + self.assertEqual(response.status_code, status.HTTP_200_OK) 105 + self.assertTrue( 106 + any(r["id"] == str(facility.external_id) for r in response.data["results"]) 107 + ) 108 + 109 + def test_delete_cover_image_when_none(self): 110 + url = reverse("facility-cover-image", args=[self.facility.external_id]) 111 + response = self.client.delete(url) 112 + self.assertEqual(response.status_code, 404) 113 + self.assertIn("No cover image to delete", str(response.data)) 114 + 115 + 116 + class TestAllFacilityViewSet(CareAPITestBase): 117 + def setUp(self): 118 + super().setUp() 119 + self.superuser = self.create_super_user() 120 + self.facility = self.create_facility( 121 + user=self.superuser, name="Public Facility", is_public=True 122 + ) 123 + self.private_facility = self.create_facility( 124 + user=self.superuser, name="Private Facility", is_public=False 125 + ) 126 + self.base_url = reverse("getallfacilities-list") 127 + 128 + def test_list_public_facilities_unauthenticated(self): 129 + response = self.client.get(self.base_url) 130 + self.assertEqual(response.status_code, status.HTTP_200_OK) 131 + ids = [r["id"] for r in response.data["results"]] 132 + self.assertIn(str(self.facility.external_id), ids) 133 + self.assertNotIn(str(self.private_facility.external_id), ids) 134 + 135 + def test_search_public_facilities(self): 136 + response = self.client.get(self.base_url, {"search": "Public"}) 137 + self.assertEqual(response.status_code, status.HTTP_200_OK) 138 + for r in response.data["results"]: 139 + self.assertIn("Public", r["name"])
+124
care/emr/tests/test_file_upload_api.py
··· 182 182 183 183 with self.assertRaises(FileUpload.DoesNotExist): 184 184 file_obj.refresh_from_db() 185 + 186 + def test_archive_file(self): 187 + url = reverse("files-list") 188 + response = self.client.post( 189 + url, 190 + { 191 + "name": "file", 192 + "original_name": "file.jpg", 193 + "file_type": "patient", 194 + "file_category": "unspecified", 195 + "associating_id": str(self.patient.external_id), 196 + "mime_type": self.file_mime_type, 197 + }, 198 + format="json", 199 + ) 200 + self.assertEqual(response.status_code, 200, response.data) 201 + file_id = response.data["id"] 202 + 203 + self.client.post( 204 + reverse("files-mark-upload-completed", args=[file_id]), format="json" 205 + ) 206 + 207 + archive_url = reverse("files-archive", args=[file_id]) 208 + response = self.client.post( 209 + archive_url, {"archive_reason": "No longer needed"}, format="json" 210 + ) 211 + self.assertEqual(response.status_code, 200, response.data) 212 + self.assertTrue(response.data["is_archived"]) 213 + self.assertEqual(response.data["archive_reason"], "No longer needed") 214 + 215 + def test_list_files_without_required_params(self): 216 + response = self.client.get(reverse("files-list")) 217 + self.assertEqual(response.status_code, 403) 218 + 219 + def test_list_files_with_params(self): 220 + url = reverse("files-list") 221 + self.client.post( 222 + url, 223 + { 224 + "name": "file", 225 + "original_name": "file.jpg", 226 + "file_type": "patient", 227 + "file_category": "unspecified", 228 + "associating_id": str(self.patient.external_id), 229 + "mime_type": self.file_mime_type, 230 + }, 231 + format="json", 232 + ) 233 + response = self.client.get( 234 + url, 235 + { 236 + "file_type": "patient", 237 + "associating_id": str(self.patient.external_id), 238 + }, 239 + ) 240 + self.assertEqual(response.status_code, 200) 241 + 242 + def test_create_file_with_invalid_mime_type(self): 243 + url = reverse("files-list") 244 + response = self.client.post( 245 + url, 246 + { 247 + "name": "file", 248 + "original_name": "file.exe", 249 + "file_type": "patient", 250 + "file_category": "unspecified", 251 + "associating_id": str(self.patient.external_id), 252 + "mime_type": "application/x-msdownload", 253 + }, 254 + format="json", 255 + ) 256 + self.assertEqual(response.status_code, 400) 257 + 258 + def test_create_file_with_empty_original_name(self): 259 + url = reverse("files-list") 260 + response = self.client.post( 261 + url, 262 + { 263 + "name": "file", 264 + "original_name": "", 265 + "file_type": "patient", 266 + "file_category": "unspecified", 267 + "associating_id": str(self.patient.external_id), 268 + "mime_type": self.file_mime_type, 269 + }, 270 + format="json", 271 + ) 272 + self.assertEqual(response.status_code, 400) 273 + 274 + def test_direct_upload_missing_fields(self): 275 + url = reverse("files-upload-file") 276 + response = self.client.post( 277 + url, 278 + { 279 + "name": "file", 280 + "file_type": "patient", 281 + "file_category": "unspecified", 282 + "associating_id": str(self.patient.external_id), 283 + "mime_type": self.file_mime_type, 284 + }, 285 + format="multipart", 286 + ) 287 + self.assertEqual(response.status_code, 400) 288 + 289 + def test_update_file_name(self): 290 + url = reverse("files-list") 291 + response = self.client.post( 292 + url, 293 + { 294 + "name": "original_name", 295 + "original_name": "file.jpg", 296 + "file_type": "patient", 297 + "file_category": "unspecified", 298 + "associating_id": str(self.patient.external_id), 299 + "mime_type": self.file_mime_type, 300 + }, 301 + format="json", 302 + ) 303 + self.assertEqual(response.status_code, 200, response.data) 304 + file_id = response.data["id"] 305 + detail_url = reverse("files-detail", args=[file_id]) 306 + response = self.client.put(detail_url, {"name": "updated_name"}, format="json") 307 + self.assertEqual(response.status_code, 200) 308 + self.assertEqual(response.data["name"], "updated_name")
+153
care/emr/tests/test_medication_request_prescription_api.py
··· 1 + from django.urls import reverse 2 + from model_bakery import baker 3 + from rest_framework import status 4 + 5 + from care.emr.models.medication_request import MedicationRequestPrescription 6 + from care.emr.resources.medication.request_prescription.spec import ( 7 + MedicationRequestPrescriptionStatus, 8 + ) 9 + from care.utils.tests.base import CareAPITestBase 10 + 11 + 12 + class TestMedicationRequestPrescriptionViewSet(CareAPITestBase): 13 + def setUp(self): 14 + super().setUp() 15 + self.superuser = self.create_super_user() 16 + self.user = self.create_user() 17 + self.facility = self.create_facility(user=self.superuser) 18 + self.facility_organization = self.create_facility_organization( 19 + facility=self.facility 20 + ) 21 + self.patient = self.create_patient() 22 + self.encounter = self.create_encounter( 23 + patient=self.patient, 24 + facility=self.facility, 25 + organization=self.facility_organization, 26 + ) 27 + self.client.force_authenticate(user=self.superuser) 28 + self.base_url = reverse( 29 + "medication-request-prescription-list", 30 + kwargs={"patient_external_id": self.patient.external_id}, 31 + ) 32 + 33 + def _get_detail_url(self, prescription_id): 34 + return reverse( 35 + "medication-request-prescription-detail", 36 + kwargs={ 37 + "patient_external_id": self.patient.external_id, 38 + "external_id": prescription_id, 39 + }, 40 + ) 41 + 42 + def _get_prescription_data(self, **overrides): 43 + data = { 44 + "status": MedicationRequestPrescriptionStatus.active.value, 45 + "encounter": str(self.encounter.external_id), 46 + "prescribed_by": str(self.superuser.external_id), 47 + "name": "Test Prescription", 48 + "note": "Test note", 49 + } 50 + data.update(overrides) 51 + return data 52 + 53 + def _create_prescription_obj(self, **kwargs): 54 + data = { 55 + "encounter": self.encounter, 56 + "patient": self.patient, 57 + "status": MedicationRequestPrescriptionStatus.active.value, 58 + "name": "Test Prescription", 59 + "prescribed_by": self.superuser, 60 + } 61 + data.update(kwargs) 62 + return baker.make(MedicationRequestPrescription, **data) 63 + 64 + def test_create_prescription(self): 65 + data = self._get_prescription_data() 66 + response = self.client.post(self.base_url, data, format="json") 67 + self.assertEqual(response.status_code, status.HTTP_200_OK) 68 + self.assertEqual(response.data["name"], "Test Prescription") 69 + self.assertEqual( 70 + response.data["status"], 71 + MedicationRequestPrescriptionStatus.active.value, 72 + ) 73 + 74 + def test_list_prescriptions(self): 75 + self._create_prescription_obj() 76 + self._create_prescription_obj(name="Second Prescription") 77 + response = self.client.get( 78 + self.base_url, {"encounter": str(self.encounter.external_id)} 79 + ) 80 + self.assertEqual(response.status_code, status.HTTP_200_OK) 81 + self.assertEqual(len(response.data["results"]), 2) 82 + 83 + def test_retrieve_prescription(self): 84 + prescription = self._create_prescription_obj() 85 + url = self._get_detail_url(prescription.external_id) 86 + response = self.client.get(url) 87 + self.assertEqual(response.status_code, status.HTTP_200_OK) 88 + self.assertEqual(response.data["id"], str(prescription.external_id)) 89 + 90 + def test_update_prescription(self): 91 + prescription = self._create_prescription_obj() 92 + url = self._get_detail_url(prescription.external_id) 93 + update_data = { 94 + "status": MedicationRequestPrescriptionStatus.completed.value, 95 + "name": "Updated Prescription", 96 + } 97 + response = self.client.put(url, update_data, format="json") 98 + self.assertEqual(response.status_code, status.HTTP_200_OK) 99 + self.assertEqual( 100 + response.data["status"], 101 + MedicationRequestPrescriptionStatus.completed.value, 102 + ) 103 + 104 + def test_filter_by_status(self): 105 + self._create_prescription_obj( 106 + status=MedicationRequestPrescriptionStatus.active.value 107 + ) 108 + self._create_prescription_obj( 109 + status=MedicationRequestPrescriptionStatus.completed.value 110 + ) 111 + response = self.client.get( 112 + self.base_url, 113 + { 114 + "status": MedicationRequestPrescriptionStatus.active.value, 115 + "encounter": str(self.encounter.external_id), 116 + }, 117 + ) 118 + self.assertEqual(response.status_code, status.HTTP_200_OK) 119 + for r in response.data["results"]: 120 + self.assertEqual( 121 + r["status"], MedicationRequestPrescriptionStatus.active.value 122 + ) 123 + 124 + def test_filter_by_encounter(self): 125 + self._create_prescription_obj() 126 + other_encounter = self.create_encounter( 127 + patient=self.patient, 128 + facility=self.facility, 129 + organization=self.facility_organization, 130 + ) 131 + self._create_prescription_obj(encounter=other_encounter, patient=self.patient) 132 + response = self.client.get( 133 + self.base_url, {"encounter": str(self.encounter.external_id)} 134 + ) 135 + self.assertEqual(response.status_code, status.HTTP_200_OK) 136 + self.assertEqual(len(response.data["results"]), 1) 137 + 138 + def test_create_prescription_without_permissions(self): 139 + self.client.force_authenticate(user=self.user) 140 + data = self._get_prescription_data() 141 + response = self.client.post(self.base_url, data, format="json") 142 + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 143 + 144 + def test_update_prescription_without_permissions(self): 145 + prescription = self._create_prescription_obj() 146 + self.client.force_authenticate(user=self.user) 147 + url = self._get_detail_url(prescription.external_id) 148 + update_data = { 149 + "status": MedicationRequestPrescriptionStatus.completed.value, 150 + "name": "Updated", 151 + } 152 + response = self.client.put(url, update_data, format="json") 153 + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
+54 -1
care/emr/tests/test_patient_api.py
··· 206 206 self.assertEqual(error["type"], "validation_error") 207 207 self.assertIn("Date of birth cannot be after the date of death", error["msg"]) 208 208 209 - def test_invalid_age_and_death_date(self): 209 + def test_create_patient_with_future_deceased_datetime(self): 210 + user = self.create_user() 211 + geo_organization = self.create_organization(org_type="govt") 212 + role = self.create_role_with_permissions( 213 + permissions=[PatientPermissions.can_create_patient.name] 214 + ) 215 + self.attach_role_organization_user(geo_organization, user, role) 216 + self.client.force_authenticate(user=user) 217 + patient_data = self.generate_patient_data( 218 + geo_organization=geo_organization.external_id, 219 + deceased_datetime=care_now() + datetime.timedelta(days=10), 220 + ) 221 + response = self.client.post(self.base_url, patient_data, format="json") 222 + self.assertEqual(response.status_code, 400) 223 + 224 + def test_delete_patient_as_superuser(self): 225 + superuser = self.create_super_user() 226 + geo_organization = self.create_organization(org_type="govt") 227 + role = self.create_role_with_permissions( 228 + permissions=[PatientPermissions.can_create_patient.name] 229 + ) 230 + self.attach_role_organization_user(geo_organization, superuser, role) 231 + self.client.force_authenticate(user=superuser) 232 + patient_data = self.generate_patient_data( 233 + geo_organization=geo_organization.external_id 234 + ) 235 + response = self.client.post(self.base_url, patient_data, format="json") 236 + self.assertEqual(response.status_code, status.HTTP_200_OK) 237 + patient_id = response.data["id"] 238 + delete_url = reverse("patient-detail", kwargs={"external_id": patient_id}) 239 + response = self.client.delete(delete_url) 240 + self.assertIn(response.status_code, [204, 200]) 241 + 242 + def test_delete_patient_as_non_superuser(self): 243 + user = self.create_user() 244 + geo_organization = self.create_organization(org_type="govt") 245 + role = self.create_role_with_permissions( 246 + permissions=[ 247 + PatientPermissions.can_create_patient.name, 248 + PatientPermissions.can_write_patient.name, 249 + PatientPermissions.can_list_patients.name, 250 + ] 251 + ) 252 + self.attach_role_organization_user(geo_organization, user, role) 253 + self.client.force_authenticate(user=user) 254 + patient_data = self.generate_patient_data( 255 + geo_organization=geo_organization.external_id 256 + ) 257 + response = self.client.post(self.base_url, patient_data, format="json") 258 + self.assertEqual(response.status_code, status.HTTP_200_OK) 259 + patient_id = response.data["id"] 260 + delete_url = reverse("patient-detail", kwargs={"external_id": patient_id}) 261 + response = self.client.delete(delete_url) 262 + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 210 263 user = self.create_user() 211 264 geo_organization = self.create_organization(org_type="govt") 212 265 role = self.create_role_with_permissions(
+154 -1
care/emr/tests/test_resource_request_api.py
··· 1 1 from django.urls import reverse 2 2 3 + from care.emr.models.resource_request import ResourceRequestComment 3 4 from care.utils.tests.base import CareAPITestBase 4 5 5 6 ··· 14 15 self.base_url = reverse("resource-request-list") 15 16 16 17 def _get_resource_request_url(self, resource_request_id): 17 - """Helper to get the detail URL for a specific resource request.""" 18 18 return reverse( 19 19 "resource-request-detail", 20 20 kwargs={"external_id": resource_request_id}, 21 21 ) 22 22 23 + def _get_create_data(self, **overrides): 24 + data = { 25 + "origin_facility": str(self.facility.external_id), 26 + "related_patient": str(self.patient.external_id), 27 + "title": "Resource Request", 28 + "status": "pending", 29 + "category": "other", 30 + "priority": 1, 31 + "emergency": False, 32 + "reason": "Test reason", 33 + "referring_facility_contact_name": "Contact", 34 + "referring_facility_contact_number": "", 35 + } 36 + data.update(overrides) 37 + return data 38 + 23 39 def create_resource_request(self, **kwargs): 24 40 from care.emr.models.resource_request import ResourceRequest 25 41 ··· 34 50 data.update(kwargs) 35 51 return ResourceRequest.objects.create(**data) 36 52 53 + def test_create_resource_request(self): 54 + data = self._get_create_data() 55 + res = self.client.post(self.base_url, data, format="json") 56 + self.assertEqual(res.status_code, 200) 57 + self.assertEqual(res.data["title"], "Resource Request") 58 + 59 + def test_list_resource_requests(self): 60 + self.create_resource_request() 61 + res = self.client.get(self.base_url) 62 + self.assertEqual(res.status_code, 200) 63 + self.assertGreaterEqual(len(res.data["results"]), 1) 64 + 65 + def test_retrieve_resource_request(self): 66 + instance = self.create_resource_request() 67 + url = self._get_resource_request_url(instance.external_id) 68 + res = self.client.get(url) 69 + self.assertEqual(res.status_code, 200) 70 + self.assertEqual(res.data["id"], str(instance.external_id)) 71 + 72 + def test_update_resource_request(self): 73 + instance = self.create_resource_request() 74 + url = self._get_resource_request_url(instance.external_id) 75 + data = self._get_create_data(title="Updated Title", status="approved") 76 + res = self.client.put(url, data, format="json") 77 + self.assertEqual(res.status_code, 200) 78 + self.assertEqual(res.data["title"], "Updated Title") 79 + self.assertEqual(res.data["status"], "approved") 80 + 81 + def test_delete_resource_request(self): 82 + instance = self.create_resource_request() 83 + url = self._get_resource_request_url(instance.external_id) 84 + res = self.client.delete(url) 85 + self.assertIn(res.status_code, [204, 200]) 86 + 87 + def test_filter_by_status(self): 88 + self.create_resource_request(status="pending") 89 + self.create_resource_request(status="approved") 90 + res = self.client.get(self.base_url, {"status": "pending"}) 91 + self.assertEqual(res.status_code, 200) 92 + for r in res.data["results"]: 93 + self.assertEqual(r["status"], "pending") 94 + 95 + def test_filter_by_origin_facility(self): 96 + other_facility = self.create_facility(user=self.user) 97 + self.create_resource_request() 98 + self.create_resource_request(origin_facility=other_facility) 99 + res = self.client.get( 100 + self.base_url, {"origin_facility": self.facility.external_id} 101 + ) 102 + self.assertEqual(res.status_code, 200) 103 + for r in res.data["results"]: 104 + self.assertEqual(r["origin_facility"]["id"], str(self.facility.external_id)) 105 + 106 + def test_filter_by_category(self): 107 + self.create_resource_request(category="other") 108 + self.create_resource_request(category="medicines") 109 + res = self.client.get(self.base_url, {"category": "other"}) 110 + self.assertEqual(res.status_code, 200) 111 + for r in res.data["results"]: 112 + self.assertEqual(r["category"], "other") 113 + 114 + def test_filter_by_title(self): 115 + self.create_resource_request(title="Unique Title") 116 + self.create_resource_request(title="Other Request") 117 + res = self.client.get(self.base_url, {"title": "Unique"}) 118 + self.assertEqual(res.status_code, 200) 119 + self.assertEqual(len(res.data["results"]), 1) 120 + 121 + def test_assigned_to_without_assigned_facility(self): 122 + user2 = self.create_user() 123 + data = self._get_create_data(assigned_to=str(user2.external_id)) 124 + res = self.client.post(self.base_url, data, format="json") 125 + self.assertContains( 126 + res, 127 + "Assigned facility is required for assigning the request to a user", 128 + status_code=400, 129 + ) 130 + 37 131 def test_resource_request_assigned_to_user_outside_assigned_facility(self): 38 132 assigned_to_user = self.create_user() 39 133 assigned_facility = self.create_facility(user=assigned_to_user) ··· 76 170 } 77 171 res = self.client.put(url, data, "json") 78 172 self.assertEqual(res.status_code, 200) 173 + 174 + 175 + class TestResourceRequestCommentViewSet(CareAPITestBase): 176 + def setUp(self): 177 + super().setUp() 178 + self.user = self.create_user() 179 + self.facility = self.create_facility(user=self.user) 180 + self.organization = self.create_facility_organization(facility=self.facility) 181 + self.patient = self.create_patient() 182 + self.client.force_authenticate(user=self.user) 183 + self.resource_request = self._create_resource_request() 184 + self.base_url = reverse( 185 + "resource-request-comment-list", 186 + kwargs={"resource_external_id": self.resource_request.external_id}, 187 + ) 188 + 189 + def _create_resource_request(self): 190 + from care.emr.models.resource_request import ResourceRequest 191 + 192 + return ResourceRequest.objects.create( 193 + origin_facility=self.facility, 194 + related_patient=self.patient, 195 + title="Test", 196 + status="pending", 197 + category="other", 198 + priority=1, 199 + ) 200 + 201 + def _get_comment_url(self, comment_id): 202 + return reverse( 203 + "resource-request-comment-detail", 204 + kwargs={ 205 + "resource_external_id": self.resource_request.external_id, 206 + "external_id": comment_id, 207 + }, 208 + ) 209 + 210 + def test_create_comment(self): 211 + res = self.client.post( 212 + self.base_url, {"comment": "Test comment"}, format="json" 213 + ) 214 + self.assertEqual(res.status_code, 200) 215 + 216 + def test_list_comments(self): 217 + self.client.post(self.base_url, {"comment": "Comment 1"}, format="json") 218 + self.client.post(self.base_url, {"comment": "Comment 2"}, format="json") 219 + res = self.client.get(self.base_url) 220 + self.assertEqual(res.status_code, 200) 221 + self.assertEqual(len(res.data["results"]), 2) 222 + 223 + def test_delete_comment(self): 224 + comment = ResourceRequestComment.objects.create( 225 + request=self.resource_request, 226 + comment="To delete", 227 + created_by=self.user, 228 + ) 229 + url = self._get_comment_url(comment.external_id) 230 + res = self.client.delete(url) 231 + self.assertIn(res.status_code, [204, 200])