1- import pytest
1+ import json
22
3+ import pytest
4+ from django .urls import reverse
5+ from scheduler .factories import (
6+ CoordinatorFactory ,
7+ CourseFactory ,
8+ MentorFactory ,
9+ SectionFactory ,
10+ StudentFactory ,
11+ UserFactory ,
12+ )
313from scheduler .models import User
414
515
616@pytest .mark .django_db
717def test_create_user ():
18+ """
19+ Test that a user can be created.
20+ """
821 email = "test@berkeley.edu"
922 username = "test"
1023 user , created = User .objects .get_or_create (email = email , username = username )
@@ -13,3 +26,194 @@ def test_create_user():
1326 assert user .username == username
1427 assert User .objects .count () == 1
1528 assert User .objects .get (email = email ).username == username
29+
30+
31+ # avoid pylint warning redefining name in outer scope
32+ @pytest .fixture (name = "setup_permissions" )
33+ def fixture_setup_permissions ():
34+ """
35+ Setup users, courses, and sections for testing permissions
36+ """
37+ student_user = UserFactory (username = "student_user" )
38+ other_student_user = UserFactory (username = "other_student_user" )
39+ mentor_user = UserFactory (username = "mentor_user" )
40+ other_mentor_user = UserFactory (username = "other_mentor_user" )
41+ coordinator_user = UserFactory (username = "coordinator_user" )
42+
43+ # Create courses
44+ course_a = CourseFactory (name = "course_a" )
45+ course_b = CourseFactory (name = "course_b" )
46+
47+ # Assign mentors to courses
48+ mentor_a = MentorFactory (user = mentor_user , course = course_a )
49+ mentor_b = MentorFactory (user = other_mentor_user , course = course_b )
50+ coordinator = CoordinatorFactory (user = coordinator_user , course = course_a )
51+
52+ # Create sections associated with the correct course via the mentor
53+ section_a1 = SectionFactory (mentor = mentor_a )
54+ section_b1 = SectionFactory (mentor = mentor_b )
55+
56+ # Ensure students are enrolled in sections that match their course
57+ student_a1 = StudentFactory (user = student_user , section = section_a1 , course = course_a )
58+ other_student_a1 = StudentFactory (
59+ user = other_student_user , section = section_a1 , course = course_a
60+ )
61+
62+ return {
63+ "student_user" : student_user ,
64+ "other_student_user" : other_student_user ,
65+ "mentor_user" : mentor_user ,
66+ "other_mentor_user" : other_mentor_user ,
67+ "coordinator_user" : coordinator ,
68+ "course_a" : course_a ,
69+ "course_b" : course_b ,
70+ "section_a1" : section_a1 ,
71+ "section_b1" : section_b1 ,
72+ "student_a1" : student_a1 ,
73+ "other_student_a1" : other_student_a1 ,
74+ }
75+
76+
77+ ###############
78+ # Student tests
79+ ###############
80+
81+
82+ @pytest .mark .django_db
83+ def test_student_view_own_profile (client , setup_permissions ):
84+ """
85+ Test that a student can view their own profile.
86+ """
87+ student_user = setup_permissions ["student_user" ]
88+ client .force_login (student_user )
89+
90+ response = client .get (reverse ("user_retrieve" , kwargs = {"pk" : student_user .pk }))
91+ assert response .status_code == 200
92+ assert response .data ["email" ] == student_user .email
93+
94+
95+ @pytest .mark .django_db
96+ def test_student_view_other_student_in_same_section (client , setup_permissions ):
97+ """
98+ Test that a student can view another student in the same section.
99+ """
100+ student = setup_permissions ["student_user" ]
101+ other_student = setup_permissions ["other_student_user" ]
102+ client .force_login (student )
103+ response = client .get (reverse ("user_retrieve" , kwargs = {"pk" : other_student .pk }))
104+ assert response .status_code == 200
105+ assert response .data ["email" ] == other_student .email
106+
107+
108+ @pytest .mark .django_db
109+ def test_student_view_mentors (client , setup_permissions ):
110+ """
111+ Test that a student can view a mentor's profile.
112+ """
113+ student = setup_permissions ["student_user" ]
114+ mentor = setup_permissions ["mentor_user" ]
115+ client .force_login (student )
116+ response = client .get (reverse ("user_retrieve" , kwargs = {"pk" : mentor .pk }))
117+ assert response .status_code == 200
118+ assert response .data ["email" ] == mentor .email
119+
120+
121+ @pytest .mark .django_db
122+ def test_student_edit_own_profile (client , setup_permissions ):
123+ """
124+ Test that a student can edit their own profile.
125+ """
126+ student = setup_permissions ["student_user" ]
127+ client .force_login (student )
128+ edit_url = reverse ("user_update" , kwargs = {"pk" : student .pk })
129+ response = client .put (
130+ edit_url ,
131+ data = json .dumps ({"first_name" : "NewName" }),
132+ content_type = "application/json" ,
133+ )
134+ assert response .status_code == 200
135+ student .refresh_from_db ()
136+ assert student .first_name == "NewName"
137+
138+
139+ ##############
140+ # Mentor tests
141+ ##############
142+ @pytest .mark .django_db
143+ def test_mentor_view_own_profile (client , setup_permissions ):
144+ """
145+ Test that a mentor can view their own profile.
146+ """
147+ mentor_user = setup_permissions ["mentor_user" ]
148+ client .force_login (mentor_user )
149+
150+ response = client .get (reverse ("user_retrieve" , kwargs = {"pk" : mentor_user .pk }))
151+ assert response .status_code == 200
152+ assert response .data ["email" ] == mentor_user .email
153+
154+
155+ @pytest .mark .django_db
156+ def test_mentor_view_students_in_course (client , setup_permissions ):
157+ """
158+ Test that a mentor can view student profiles in the course they teach.
159+ """
160+ mentor_user = setup_permissions ["mentor_user" ]
161+ student_user = setup_permissions ["student_user" ]
162+ client .force_login (mentor_user )
163+
164+ response = client .get (reverse ("user_retrieve" , kwargs = {"pk" : student_user .pk }))
165+ assert response .status_code == 200
166+ assert response .data ["email" ] == student_user .email
167+
168+
169+ @pytest .mark .django_db
170+ def test_mentor_cannot_edit_other_profiles (client , setup_permissions ):
171+ """
172+ Test that a mentor cannot edit another student's or mentor's profile.
173+ """
174+ mentor_user = setup_permissions ["mentor_user" ]
175+ other_student_user = setup_permissions ["other_student_user" ]
176+ client .force_login (mentor_user )
177+ response = client .put (
178+ reverse ("user_update" , kwargs = {"pk" : other_student_user .pk }),
179+ data = json .dumps ({"first_name" : "new_username" }),
180+ content_type = "application/json" ,
181+ )
182+ assert response .status_code == 403
183+
184+
185+ ###################
186+ # Coordinator tests
187+ ###################
188+
189+
190+ @pytest .mark .django_db
191+ def test_coordinator_view_all_profiles_in_course (client , setup_permissions ):
192+ """
193+ Test that a coordinator can view all profiles in the course they coordinate.
194+ """
195+ coordinator_user = setup_permissions ["coordinator_user" ]
196+ student_user = setup_permissions ["student_user" ]
197+ client .force_login (coordinator_user )
198+
199+ response = client .get (reverse ("user_retrieve" , kwargs = {"pk" : student_user .pk }))
200+ assert response .status_code == 200
201+
202+
203+ @pytest .mark .django_db
204+ def test_coordinator_edit_all_profiles_in_course (client , setup_permissions ):
205+ """
206+ Test that a coordinator can edit all profiles in the course they coordinate.
207+ """
208+ coordinator_user = setup_permissions ["coordinator_user" ]
209+ student_user = setup_permissions ["student_user" ]
210+ client .force_login (coordinator_user )
211+
212+ response = client .put (
213+ reverse ("user_update" , kwargs = {"pk" : student_user .pk }),
214+ data = json .dumps ({"first_name" : "new_student_name" }),
215+ content_type = "application/json" ,
216+ )
217+ assert response .status_code == 200
218+ student_user .refresh_from_db ()
219+ assert student_user .first_name == "new_student_name"
0 commit comments