|
| 1 | +import json |
| 2 | +from django.test import TestCase |
| 3 | +from django.contrib.auth.models import User, Permission |
| 4 | +from coldfront.core.user.models import UserProfile, ApproverProfile |
| 5 | +from coldfront.core.school.models import School |
| 6 | +from coldfront.core.user.management.commands.load_approver_schools import load_approver_schools |
| 7 | + |
| 8 | + |
| 9 | +class LoadApproverSchoolsTest(TestCase): |
| 10 | + """Tests for load_approver_schools function.""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + """Set up test users, schools, and permissions.""" |
| 14 | + # Create users |
| 15 | + self.approver1 = User.objects.create(username="approver1", is_staff=False) |
| 16 | + self.approver2 = User.objects.create(username="approver2", is_staff=False) |
| 17 | + self.non_existent_user = "nonuser" # This user does not exist |
| 18 | + |
| 19 | + # Get UserProfiles |
| 20 | + self.approver1_profile = UserProfile.objects.get(user=self.approver1) |
| 21 | + self.approver2_profile = UserProfile.objects.get(user=self.approver2) |
| 22 | + |
| 23 | + # Create Schools |
| 24 | + self.school1 = School.objects.create(description="Tandon School of Engineering") |
| 25 | + self.school2 = School.objects.create(description="NYU IT") |
| 26 | + |
| 27 | + # Create permission |
| 28 | + self.review_permission = Permission.objects.get(codename="can_review_allocation_requests") |
| 29 | + |
| 30 | + # Sample JSON Data |
| 31 | + self.json_data = { |
| 32 | + "approver1": ["Tandon School of Engineering", "NYU IT"], |
| 33 | + "approver2": ["NYU IT"], |
| 34 | + "nonuser": ["NYU IT"] # This user does not exist |
| 35 | + } |
| 36 | + |
| 37 | + def test_users_are_assigned_as_staff(self): |
| 38 | + """Test that users are correctly marked as staff after function execution.""" |
| 39 | + load_approver_schools(self.json_data) |
| 40 | + |
| 41 | + self.approver1.refresh_from_db() |
| 42 | + self.approver2.refresh_from_db() |
| 43 | + |
| 44 | + self.assertTrue(self.approver1.is_staff) |
| 45 | + self.assertTrue(self.approver2.is_staff) |
| 46 | + |
| 47 | + def test_users_are_granted_approver_permission(self): |
| 48 | + """Test that users receive the 'can_review_allocation_requests' permission.""" |
| 49 | + load_approver_schools(self.json_data) |
| 50 | + |
| 51 | + self.assertTrue(self.approver1.has_perm("allocation.can_review_allocation_requests")) |
| 52 | + self.assertTrue(self.approver2.has_perm("allocation.can_review_allocation_requests")) |
| 53 | + |
| 54 | + def test_approver_profiles_are_created(self): |
| 55 | + """Test that an ApproverProfile is created for approvers.""" |
| 56 | + load_approver_schools(self.json_data) |
| 57 | + |
| 58 | + self.assertTrue(ApproverProfile.objects.filter(user_profile=self.approver1_profile).exists()) |
| 59 | + self.assertTrue(ApproverProfile.objects.filter(user_profile=self.approver2_profile).exists()) |
| 60 | + |
| 61 | + def test_schools_are_assigned_correctly(self): |
| 62 | + """Test that users are assigned the correct schools in their ApproverProfile.""" |
| 63 | + load_approver_schools(self.json_data) |
| 64 | + |
| 65 | + approver1_profile = ApproverProfile.objects.get(user_profile=self.approver1_profile) |
| 66 | + approver2_profile = ApproverProfile.objects.get(user_profile=self.approver2_profile) |
| 67 | + |
| 68 | + self.assertEqual(set(approver1_profile.schools.values_list("description", flat=True)), |
| 69 | + {"Tandon School of Engineering", "NYU IT"}) |
| 70 | + self.assertEqual(set(approver2_profile.schools.values_list("description", flat=True)), {"NYU IT"}) |
| 71 | + |
| 72 | + def test_nonexistent_user_is_skipped(self): |
| 73 | + """Test that non-existent users are skipped without error.""" |
| 74 | + load_approver_schools(self.json_data) |
| 75 | + |
| 76 | + # Ensure no UserProfile or ApproverProfile is created for the non-existent user |
| 77 | + self.assertFalse(UserProfile.objects.filter(user__username="jdoe").exists()) |
| 78 | + self.assertFalse(ApproverProfile.objects.filter(user_profile__user__username="jdoe").exists()) |
| 79 | + |
| 80 | + def test_function_does_not_duplicate_existing_profiles(self): |
| 81 | + """Test that the function does not create duplicate ApproverProfiles when run multiple times.""" |
| 82 | + load_approver_schools(self.json_data) # First execution |
| 83 | + load_approver_schools(self.json_data) # Second execution |
| 84 | + |
| 85 | + # Ensure only one ApproverProfile per user |
| 86 | + self.assertEqual(ApproverProfile.objects.filter(user_profile=self.approver1_profile).count(), 1) |
| 87 | + self.assertEqual(ApproverProfile.objects.filter(user_profile=self.approver2_profile).count(), 1) |
| 88 | + |
0 commit comments