-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict Admin from unlinking users from other district (#1157)
* prevent unlink for other district * use id for comparision * Update care/users/api/viewsets/users.py Co-authored-by: Aakash Singh <mail@singhaakash.dev> * add tests * fix lint * fix tests and restrict in clear_home_Facility function * add unlink facility tests * assert error message in tests * Update care/users/api/viewsets/users.py Co-authored-by: Aakash Singh <mail@singhaakash.dev> * check if user is district admin and above --------- Co-authored-by: Aakash Singh <mail@singhaakash.dev>
- Loading branch information
1 parent
940e0e7
commit 7663481
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from django.conf import settings | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from care.utils.tests.test_utils import TestUtils | ||
|
||
|
||
class UnlinkDistrictAdmin(TestUtils, APITestCase): | ||
def setUp(self): | ||
settings.DISABLE_RATELIMIT = True | ||
self.state = self.create_state() | ||
|
||
self.district1 = self.create_district(self.state) | ||
self.admin1 = self.create_user("user12345678", self.district1, user_type=30) | ||
|
||
self.district2 = self.create_district(self.state) | ||
self.admin2 = self.create_user("user12345679", self.district2, user_type=30) | ||
|
||
self.local_body1 = self.create_local_body(self.district1) | ||
self.local_body2 = self.create_local_body(self.district2) | ||
|
||
self.facility1 = self.create_facility( | ||
district=self.district1, user=self.admin1, local_body=self.local_body1 | ||
) | ||
self.facility2 = self.create_facility( | ||
district=self.district2, user=self.admin2, local_body=self.local_body2 | ||
) | ||
|
||
self.staff1 = self.create_user( | ||
"staff1234", self.district1, home_facility=self.facility1 | ||
) | ||
self.staff2 = self.create_user( | ||
"staff1235", self.district2, home_facility=self.facility2 | ||
) | ||
|
||
def test_unlink_home_facility_admin_same_district(self): | ||
self.client.force_login(self.admin1) | ||
|
||
username = self.staff1.username | ||
response = self.client.delete( | ||
"/api/v1/users/" + username + "/clear_home_facility/" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
|
||
def test_unlink_home_facility_admin_different_district(self): | ||
self.client.force_login(self.admin1) | ||
|
||
username = self.staff2.username | ||
response = self.client.delete( | ||
"/api/v1/users/" + username + "/clear_home_facility/" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual( | ||
response.json()["facility"], | ||
"Cannot unlink User's Home Facility from other district", | ||
) | ||
|
||
def test_unlink_faciltity_admin_same_district(self): | ||
self.client.force_login(self.admin1) | ||
|
||
username = self.staff1.username | ||
|
||
# clear from home facility to linked facility | ||
response = self.client.delete( | ||
"/api/v1/users/" + username + "/clear_home_facility/" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
|
||
response = self.client.delete( | ||
"/api/v1/users/" + username + "/delete_facility/", | ||
{"facility": self.facility1.external_id}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
|
||
def test_unlink_faciltity_admin_different_district(self): | ||
self.client.force_login(self.admin1) | ||
|
||
username = self.staff2.username | ||
response = self.client.delete( | ||
"/api/v1/users/" + username + "/delete_facility/", | ||
{"facility": self.facility2.external_id}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual(response.json()["facility"], "Facility Access not Present") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters