Skip to content

Commit

Permalink
Add basic tests, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari committed Jan 6, 2025
1 parent dee62a9 commit 4e39243
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
12 changes: 11 additions & 1 deletion care/emr/api/viewsets/questionnaire.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django_filters import rest_framework as filters
from pydantic import UUID4, BaseModel
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.response import Response

from care.emr.api.viewsets.base import EMRModelViewSet
Expand All @@ -13,6 +13,7 @@
Patient,
Questionnaire,
QuestionnaireOrganization,
QuestionnaireResponse,
)
from care.emr.resources.organization.spec import OrganizationReadSpec
from care.emr.resources.questionnaire.spec import (
Expand Down Expand Up @@ -59,6 +60,15 @@ def perform_create(self, instance):
questionnaire=instance, organization=organization_obj
)

def validate_data(self, instance, model_obj=None):
if (
model_obj
and not QuestionnaireResponse.objects.filter(
questionnaire=model_obj
).exists()
):
raise ValidationError("Cannot edit an active questionnaire")

def authorize_create(self, instance):
for org in instance.organizations:
# Validate if the user has write permission in the organization
Expand Down
4 changes: 2 additions & 2 deletions care/emr/resources/user/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from pydantic import UUID4, field_validator
from pydantic import UUID4, Field, field_validator
from rest_framework.generics import get_object_or_404

from care.emr.models import Organization
Expand Down Expand Up @@ -35,7 +35,7 @@ class UserBaseSpec(EMRResource):

first_name: str
last_name: str
phone_number: str
phone_number: str = Field(max_length=14)


class UserUpdateSpec(UserBaseSpec):
Expand Down
18 changes: 16 additions & 2 deletions care/users/tests/test_user_create.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging

from django.urls import reverse
from polyfactory.factories.pydantic_factory import ModelFactory
from rest_framework import status

from care.emr.resources.user.spec import UserCreateSpec
from care.emr.resources.user.spec import UserCreateSpec, UserTypeRoleMapping
from care.security.permissions.user import UserPermissions
from care.utils.tests.base import CareAPITestBase


Expand Down Expand Up @@ -34,9 +37,20 @@ def test_create_empty_user_validation(self):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_create_user_authorization(self):
# Create user and assign to organization with user create role
user = self.create_user()
organization = self.create_organization(org_type="govt")
role = self.create_role_with_permissions(
permissions=[UserPermissions.can_create_user.name]
)
self.attach_role_organization_user(organization, user, role)
new_user = self.generate_user_data(geo_organization=organization.external_id)
# Create or
self.create_role(
name=UserTypeRoleMapping[new_user.user_type.value].value.name,
is_system=True,
)
logging.info(UserTypeRoleMapping[new_user.user_type.value].value.name)
self.client.force_authenticate(user=user)
response = self.client.post(self.base_url, new_user.dict(), format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.status_code, status.HTTP_200_OK)
23 changes: 20 additions & 3 deletions care/utils/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from model_bakery import baker
from rest_framework.test import APITestCase

from care.emr.models.organization import OrganizationUser


class CareAPITestBase(APITestCase):
fake = Faker()
Expand All @@ -19,11 +21,26 @@ def create_organization(self, **kwargs):

return baker.make(Organization, **kwargs)

def create_role(self, **kwargs):
from care.security.models import RoleModel

if RoleModel.objects.filter(**kwargs).exists():
return RoleModel.objects.get(**kwargs)
return baker.make(RoleModel, **kwargs)

def create_role_with_permissions(self, permissions):
pass
from care.security.models import PermissionModel, RoleModel, RolePermission

def attach_role_organization_user(self):
pass
role = baker.make(RoleModel)

for permission in permissions:
RolePermission.objects.create(
role=role, permission=baker.make(PermissionModel, slug=permission)
)
return role

def attach_role_organization_user(self, organization, user, role):
OrganizationUser.objects.create(organization=organization, user=user, role=role)

def attach_role_facility_organization_user(self):
pass
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import warnings
from datetime import datetime, timedelta
from pathlib import Path

Expand All @@ -17,6 +18,8 @@
from care.utils.csp import config as csp_config
from plug_config import manager

warnings.filterwarnings("ignore", category=UserWarning)

logger = logging.getLogger(__name__)

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
Expand Down

0 comments on commit 4e39243

Please sign in to comment.