Skip to content

Commit 104bb32

Browse files
author
Dahye Kim
committed
Assigned approvers as staff to let them view staff navigation bar for allocation request review + added tests for load_approver_schools.py
1 parent 04b9123 commit 104bb32

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

coldfront/core/user/management/commands/load_approver_schools.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010

1111

1212
def load_approver_schools(json_data):
13+
"""
14+
Grant is_staff, approver_profile, "can_review_allocation_requests" permission associated with schools
15+
"""
1316
for approver_username, school_descriptions in json_data.items():
1417
try:
1518
# Check if the user exists
1619
user = User.objects.get(username=approver_username)
20+
# Make as a staff to let an approver view admin navigation bar
21+
user.is_staff = True
22+
user.save()
1723
except User.DoesNotExist:
18-
print(f"User {approver_username} not found. Skipping.")
24+
# print(f"User {approver_username} not found. Skipping.")
1925
continue # Skip to the next user
2026

21-
# Check if a UserProfile exists for the user
27+
# Get UserProfile for the user
2228
user_profile = UserProfile.objects.filter(user=user).first()
23-
if not user_profile:
24-
print(f"Skipping {approver_username}: UserProfile does not exist.")
25-
continue
2629

2730
# Grant 'can_review_allocation_requests' permission if not already granted
2831
perm_codename = "can_review_allocation_requests"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)