Skip to content

Commit

Permalink
Fixes state and district admin not able to see users of same user typ…
Browse files Browse the repository at this point in the history
…e level (#2200)

* Fixes state and district admin not able to see users of same user type level

* correct test

---------

Co-authored-by: Vignesh Hari <[email protected]>
  • Loading branch information
rithviknishad and vigneshhari authored May 28, 2024
1 parent 0e947b8 commit 28bedb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ def get_queryset(self):
if self.request.user.user_type >= User.TYPE_VALUE_MAP["StateReadOnlyAdmin"]:
query |= Q(
state=self.request.user.state,
user_type__lt=User.TYPE_VALUE_MAP["StateAdmin"],
user_type__lte=User.TYPE_VALUE_MAP["StateAdmin"],
is_superuser=False,
)
elif (
self.request.user.user_type >= User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"]
):
query |= Q(
district=self.request.user.district,
user_type__lt=User.TYPE_VALUE_MAP["DistrictAdmin"],
user_type__lte=User.TYPE_VALUE_MAP["DistrictAdmin"],
is_superuser=False,
)
else:
Expand Down
32 changes: 24 additions & 8 deletions care/users/tests/test_facility_user_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def setUpTestData(cls) -> None:
cls.super_user = cls.create_super_user("su", cls.district)
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
cls.user = cls.create_user("staff1", cls.district, home_facility=cls.facility)
cls.state_admin = cls.create_user(
"stateadmin1",
cls.district,
home_facility=cls.facility,
user_type=User.TYPE_VALUE_MAP["StateAdmin"],
)

def get_base_url(self):
return "/api/v1/users/add_user/"
Expand Down Expand Up @@ -46,8 +52,8 @@ def get_detail_representation(self, obj: User = None) -> dict:
"ward": getattr(obj.ward, "id", None),
}

def get_new_user_data(self):
return {
def get_user_data(self, **kwargs):
data = {
"username": "roopak",
"user_type": "Staff",
"phone_number": "+917795937091",
Expand All @@ -60,18 +66,28 @@ def get_new_user_data(self):
"verified": True,
"facilities": [self.facility.external_id],
}
data.update(kwargs)
return data.copy()

def test_create_facility_user__should_fail__when_higher_level(self):
data = self.get_new_user_data().copy()
data.update({"user_type": "DistrictAdmin"})

data = self.get_user_data(user_type="DistrictAdmin")
response = self.client.post(self.get_base_url(), data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_create_facility_user__should_fail__when_different_location(self):
new_district = self.clone_object(self.district)
data = self.get_new_user_data().copy()
data.update({"district": new_district.id})

data = self.get_user_data(district=new_district.id)
response = self.client.post(self.get_base_url(), data=data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_create_user_of_same_type(self):
self.client.force_authenticate(self.state_admin)

data = self.get_user_data(
username="stateadmin2", user_type=User.TYPE_VALUE_MAP["StateAdmin"]
)
res = self.client.post(self.get_base_url(), data=data, format="json")
self.assertEqual(res.status_code, status.HTTP_201_CREATED)

res = self.client.get("/api/v1/users/", {"username": "stateadmin2"})
self.assertContains(res, "stateadmin2")

0 comments on commit 28bedb1

Please sign in to comment.