|
1 | 1 | import datetime |
2 | 2 |
|
3 | 3 | import factory |
| 4 | +from django.contrib.auth.models import Group, Permission |
4 | 5 | from django.contrib.gis.geos import Point |
| 6 | +from django.core import management |
5 | 7 | from factory import fuzzy |
6 | 8 |
|
7 | 9 | from api.models import Country, Region |
@@ -308,6 +310,29 @@ def test_detail(self): |
308 | 310 |
|
309 | 311 | class TestLocalUnitCreate(APITestCase): |
310 | 312 |
|
| 313 | + def setUp(self): |
| 314 | + super().setUp() |
| 315 | + self.region = Region.objects.create(name=2) |
| 316 | + self.country = Country.objects.create(name="Nepal", iso3="NLP", region=self.region) |
| 317 | + management.call_command("make_permissions") |
| 318 | + management.call_command("make_global_validator_permission") |
| 319 | + |
| 320 | + # Permissions and different validators |
| 321 | + self.global_validator_user = UserFactory.create() |
| 322 | + self.local_unit_admin_user = UserFactory.create() |
| 323 | + self.regional_validator_user = UserFactory.create() |
| 324 | + |
| 325 | + # Adding permissions to the users |
| 326 | + global_validator_permission = Permission.objects.filter(codename="local_unit_global_validator").first() |
| 327 | + |
| 328 | + country_group = Group.objects.filter(name="%s Admins" % self.country.name).first() |
| 329 | + region_group = Group.objects.filter(name="%s Regional Admins" % self.region.name).first() |
| 330 | + |
| 331 | + self.local_unit_admin_user.groups.add(country_group) |
| 332 | + self.regional_validator_user.groups.add(region_group) |
| 333 | + |
| 334 | + self.global_validator_user.user_permissions.add(global_validator_permission) |
| 335 | + |
311 | 336 | def test_create_local_unit_administrative(self): |
312 | 337 | region = Region.objects.create(name=2) |
313 | 338 | country = Country.objects.create( |
@@ -624,3 +649,64 @@ def test_latest_changes(self): |
624 | 649 | self.assert_200(response) |
625 | 650 | self.assertEqual(response.data["previous_data_details"]["local_branch_name"], previous_data["local_branch_name"]) |
626 | 651 | self.assertEqual(response.data["previous_data_details"]["english_branch_name"], previous_data["english_branch_name"]) |
| 652 | + |
| 653 | + def test_validate_local_unit(self): |
| 654 | + type = LocalUnitType.objects.create(code=0, name="Code 0") |
| 655 | + data = { |
| 656 | + "local_branch_name": "Silele Red Cross Clinic, Sigombeni Red Cross Clinic & Mahwalala Red Cross Clinic", |
| 657 | + "english_branch_name": None, |
| 658 | + "type": type.id, |
| 659 | + "country": self.country.id, |
| 660 | + "date_of_data": "2024-05-13", |
| 661 | + "location_json": { |
| 662 | + "lat": 42.066667, |
| 663 | + "lng": 19.983333, |
| 664 | + }, |
| 665 | + } |
| 666 | + self.authenticate() |
| 667 | + response = self.client.post("/api/v2/local-units/", data=data, format="json") |
| 668 | + self.assert_201(response) |
| 669 | + |
| 670 | + local_unit_id = response.data["id"] |
| 671 | + # Testing For the local unit Global validator |
| 672 | + self.authenticate(self.global_validator_user) |
| 673 | + # validating the local unit by the Global validator |
| 674 | + response = self.client.post(f"/api/v2/local-units/{local_unit_id}/validate/") |
| 675 | + self.assert_200(response) |
| 676 | + local_unit_request = LocalUnitChangeRequest.objects.filter( |
| 677 | + local_unit=local_unit_id, status=LocalUnitChangeRequest.Status.APPROVED |
| 678 | + ).last() |
| 679 | + self.assertEqual(local_unit_request.current_validator, LocalUnitChangeRequest.Validator.GLOBAL) |
| 680 | + |
| 681 | + # Testing For the local unit admin/Local validator |
| 682 | + self.authenticate(self.local_unit_admin_user) |
| 683 | + response = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json") |
| 684 | + self.assert_200(response) |
| 685 | + # validating the local unit by the local unit admin |
| 686 | + response = self.client.post(f"/api/v2/local-units/{local_unit_id}/validate/") |
| 687 | + local_unit_request = LocalUnitChangeRequest.objects.filter( |
| 688 | + local_unit=local_unit_id, status=LocalUnitChangeRequest.Status.APPROVED |
| 689 | + ).last() |
| 690 | + self.assertEqual(local_unit_request.current_validator, LocalUnitChangeRequest.Validator.LOCAL) |
| 691 | + |
| 692 | + # Testing For the regional validator |
| 693 | + self.authenticate(self.regional_validator_user) |
| 694 | + response = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json") |
| 695 | + self.assert_200(response) |
| 696 | + # validating the local unit by the regional validator |
| 697 | + response = self.client.post(f"/api/v2/local-units/{local_unit_id}/validate/") |
| 698 | + local_unit_request = LocalUnitChangeRequest.objects.filter( |
| 699 | + local_unit=local_unit_id, status=LocalUnitChangeRequest.Status.APPROVED |
| 700 | + ).last() |
| 701 | + self.assertEqual(local_unit_request.current_validator, LocalUnitChangeRequest.Validator.REGIONAL) |
| 702 | + |
| 703 | + # Testing for Root User/Global validator |
| 704 | + self.authenticate(self.root_user) |
| 705 | + response = self.client.put(f"/api/v2/local-units/{local_unit_id}/", data=data, format="json") |
| 706 | + self.assert_200(response) |
| 707 | + # validating the local unit by the global validator |
| 708 | + response = self.client.post(f"/api/v2/local-units/{local_unit_id}/validate/") |
| 709 | + local_unit_request = LocalUnitChangeRequest.objects.filter( |
| 710 | + local_unit=local_unit_id, status=LocalUnitChangeRequest.Status.APPROVED |
| 711 | + ).last() |
| 712 | + self.assertEqual(local_unit_request.current_validator, LocalUnitChangeRequest.Validator.GLOBAL) |
0 commit comments