Skip to content

Commit

Permalink
Add basic patient tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari committed Jan 6, 2025
1 parent 9009c7f commit 7040be4
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 236 deletions.
12 changes: 0 additions & 12 deletions aws/backend.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,6 @@
"valueFrom": "/care/backend/ABDM_CLIENT_SECRET",
"name": "ABDM_CLIENT_SECRET"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_HOST",
"name": "PLAUSIBLE_HOST"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_SITE_ID",
"name": "PLAUSIBLE_SITE_ID"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_AUTH_TOKEN",
"name": "PLAUSIBLE_AUTH_TOKEN"
},
{
"valueFrom": "/care/backend/JWKS_BASE64",
"name": "JWKS_BASE64"
Expand Down
24 changes: 0 additions & 24 deletions aws/celery.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,6 @@
"valueFrom": "/care/backend/HCX_CERT_URL",
"name": "HCX_CERT_URL"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_HOST",
"name": "PLAUSIBLE_HOST"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_SITE_ID",
"name": "PLAUSIBLE_SITE_ID"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_AUTH_TOKEN",
"name": "PLAUSIBLE_AUTH_TOKEN"
},
{
"valueFrom": "/care/backend/ABDM_CLIENT_ID",
"name": "ABDM_CLIENT_ID"
Expand Down Expand Up @@ -525,18 +513,6 @@
"valueFrom": "/care/backend/HCX_CERT_URL",
"name": "HCX_CERT_URL"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_HOST",
"name": "PLAUSIBLE_HOST"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_SITE_ID",
"name": "PLAUSIBLE_SITE_ID"
},
{
"valueFrom": "/care/backend/PLAUSIBLE_AUTH_TOKEN",
"name": "PLAUSIBLE_AUTH_TOKEN"
},
{
"valueFrom": "/care/backend/ABDM_CLIENT_ID",
"name": "ABDM_CLIENT_ID"
Expand Down
6 changes: 3 additions & 3 deletions care/emr/resources/patient/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import Enum

from django.utils import timezone
from pydantic import UUID4, field_validator, model_validator
from pydantic import UUID4, Field, field_validator, model_validator

from care.emr.models import Organization
from care.emr.models.patient import Patient
Expand Down Expand Up @@ -36,8 +36,8 @@ class PatientBaseSpec(EMRResource):
id: UUID4 | None = None
name: str
gender: GenderChoices
phone_number: str
emergency_phone_number: str | None = None
phone_number: str = Field(max_length=14)
emergency_phone_number: str | None = Field(None, max_length=14)
address: str
permanent_address: str
pincode: int
Expand Down
Empty file added care/emr/tests/__init__.py
Empty file.
81 changes: 81 additions & 0 deletions care/emr/tests/test_patient_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from django.urls import reverse
from polyfactory.factories.pydantic_factory import ModelFactory
from rest_framework import status

from care.emr.resources.patient.spec import PatientCreateSpec
from care.security.permissions.patient import PatientPermissions
from care.utils.tests.base import CareAPITestBase


class PatientFactory(ModelFactory[PatientCreateSpec]):
__model__ = PatientCreateSpec


class TestPatientViewSet(CareAPITestBase):
"""
Test cases for checking Patient CRUD operations
Tests check if:
1. Permissions are enforced for all operations
2. Data validations work
3. Proper responses are returned
4. Filters work as expected
"""

def setUp(self):
"""Set up test data that's needed for all tests"""
super().setUp() # Call parent's setUp to ensure proper initialization
self.base_url = reverse("patient-list")

def generate_patient_data(self, **kwargs):
if "age" not in kwargs and "date_of_birth" not in kwargs:
kwargs["age"] = self.fake.random_int(min=1, max=100)
return PatientFactory.build(meta={}, **kwargs)

def test_create_patient_unauthenticated(self):
"""Test that unauthenticated users cannot create patients"""
response = self.client.post(self.base_url, {}, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_create_empty_patient_validation(self):
"""Test validation when creating patient with empty data"""
user = self.create_user()
self.client.force_authenticate(user=user)
response = self.client.post(self.base_url, {}, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_create_patient_authorization(self):
"""Test patient creation with proper authorization"""
user = self.create_user()
geo_organization = self.create_organization(org_type="govt")
patient_data = self.generate_patient_data(
geo_organization=geo_organization.external_id
)
organization = self.create_organization(org_type="govt")
role = self.create_role_with_permissions(
permissions=[PatientPermissions.can_create_patient.name]
)
self.attach_role_organization_user(organization, user, role)
self.client.force_authenticate(user=user)
response = self.client.post(
self.base_url, patient_data.model_dump(mode="json"), format="json"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_create_patient_unauthorization(self):
"""Test patient creation with proper authorization"""
user = self.create_user()
geo_organization = self.create_organization(org_type="govt")
patient_data = self.generate_patient_data(
geo_organization=geo_organization.external_id
)
organization = self.create_organization(org_type="govt")
role = self.create_role_with_permissions(
permissions=[PatientPermissions.can_list_patients.name]
)
self.attach_role_organization_user(organization, user, role)
self.client.force_authenticate(user=user)
response = self.client.post(
self.base_url, patient_data.model_dump(mode="json"), format="json"
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
51 changes: 22 additions & 29 deletions care/facility/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
from celery import current_app
from celery.schedules import crontab
# from celery import current_app
# from celery.schedules import crontab

from care.facility.tasks.asset_monitor import check_asset_status
from care.facility.tasks.cleanup import delete_old_notifications
from care.facility.tasks.location_monitor import check_location_status
from care.facility.tasks.plausible_stats import capture_goals
# from care.facility.tasks.asset_monitor import check_asset_status
# from care.facility.tasks.cleanup import delete_old_notifications
# from care.facility.tasks.location_monitor import check_location_status

# @current_app.on_after_finalize.connect
# def setup_periodic_tasks(sender, **kwargs):
# sender.add_periodic_task(
# crontab(hour="0", minute="0"),
# delete_old_notifications.s(),
# name="delete_old_notifications",
# )

@current_app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(
crontab(hour="0", minute="0"),
delete_old_notifications.s(),
name="delete_old_notifications",
)

sender.add_periodic_task(
crontab(minute="*/30"),
check_asset_status.s(),
name="check_asset_status",
)
sender.add_periodic_task(
crontab(hour="0", minute="0"),
capture_goals.s(),
name="capture_goals",
)
sender.add_periodic_task(
crontab(minute="*/30"),
check_location_status.s(),
name="check_location_status",
)
# sender.add_periodic_task(
# crontab(minute="*/30"),
# check_asset_status.s(),
# name="check_asset_status",
# )
# sender.add_periodic_task(
# crontab(minute="*/30"),
# check_location_status.s(),
# name="check_location_status",
# )
155 changes: 0 additions & 155 deletions care/facility/tasks/plausible_stats.py

This file was deleted.

2 changes: 2 additions & 0 deletions care/security/authorization/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def can_submit_questionnaire_patient_obj(self, user, patient):
def can_create_patient(self, user):
return self.check_permission_in_facility_organization(
[PatientPermissions.can_create_patient.name], user
) or self.check_permission_in_organization(
[PatientPermissions.can_create_patient.name], user
)

def can_view_clinical_data(self, user, patient):
Expand Down
5 changes: 1 addition & 4 deletions care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from care.emr.models import Organization
from care.emr.models.organization import FacilityOrganizationUser, OrganizationUser
from care.emr.resources.organization.spec import OrganizationReadSpec
from care.emr.resources.role.spec import PermissionSpec
from care.facility.api.serializers.facility import FacilityBareMinimumSerializer
from care.facility.models import Facility, FacilityUser
from care.security.models import RolePermission
Expand Down Expand Up @@ -306,9 +305,7 @@ def get_permissions(self, user):
"role_id", flat=True
)
).select_related("permission")
return [
PermissionSpec.serialize(obj.permission).to_json() for obj in permissions
]
return [obj.permission.slug for obj in permissions]

def get_facilities(self, user):
unique_ids = []
Expand Down
Loading

0 comments on commit 7040be4

Please sign in to comment.