Skip to content

Commit 9c2d80c

Browse files
authored
Migrate UpdateInstructorActionTest (#13256)
1 parent 08256a5 commit 9c2d80c

File tree

1 file changed

+296
-0
lines changed

1 file changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
package teammates.sqlui.webapi;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.times;
5+
import static org.mockito.Mockito.verify;
6+
import static org.mockito.Mockito.when;
7+
import static teammates.common.datatransfer.InstructorPermissionRole.getEnum;
8+
import static teammates.common.util.Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_CUSTOM;
9+
10+
import org.testng.annotations.BeforeMethod;
11+
import org.testng.annotations.Test;
12+
13+
import teammates.common.datatransfer.InstructorPrivileges;
14+
import teammates.common.exception.InstructorUpdateException;
15+
import teammates.common.exception.InvalidParametersException;
16+
import teammates.common.util.Const;
17+
import teammates.common.util.TaskWrapper;
18+
import teammates.storage.sqlentity.Course;
19+
import teammates.storage.sqlentity.Instructor;
20+
import teammates.storage.sqlentity.Student;
21+
import teammates.ui.output.InstructorData;
22+
import teammates.ui.request.InstructorCreateRequest;
23+
import teammates.ui.webapi.JsonResult;
24+
import teammates.ui.webapi.UpdateInstructorAction;
25+
26+
/**
27+
* SUT: {@link UpdateInstructorAction}.
28+
*/
29+
public class UpdateInstructorActionTest extends BaseActionTest<UpdateInstructorAction> {
30+
31+
private Course typicalCourse;
32+
private Instructor typicalInstructorToUpdate;
33+
34+
@Override
35+
String getActionUri() {
36+
return Const.ResourceURIs.INSTRUCTOR;
37+
}
38+
39+
@Override
40+
String getRequestMethod() {
41+
return PUT;
42+
}
43+
44+
@BeforeMethod
45+
void setUpMethod() {
46+
typicalCourse = getTypicalCourse();
47+
typicalInstructorToUpdate = getTypicalInstructor();
48+
}
49+
50+
@Test
51+
void testExecute_typicalCase_success() throws Exception {
52+
String instructorToUpdateId = typicalInstructorToUpdate.getGoogleId();
53+
String instructorToUpdateDisplayName = typicalInstructorToUpdate.getDisplayName();
54+
55+
loginAsInstructor(instructorToUpdateId);
56+
57+
String[] params = new String[] {
58+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
59+
};
60+
61+
String updatedInstructorName = "New Instructor";
62+
String updatedInstructorEmail = "[email protected]";
63+
String updatedInstructorRole = Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER;
64+
Instructor updatedInstructor = new Instructor(typicalCourse, updatedInstructorName, updatedInstructorEmail,
65+
false, null, getEnum(updatedInstructorRole),
66+
new InstructorPrivileges(updatedInstructorRole));
67+
68+
InstructorCreateRequest requestBody = new InstructorCreateRequest(instructorToUpdateId, updatedInstructorName,
69+
updatedInstructorEmail, updatedInstructorRole,
70+
instructorToUpdateDisplayName, typicalInstructorToUpdate.isDisplayedToStudents());
71+
72+
when(mockLogic.updateInstructorCascade(any(String.class), any(InstructorCreateRequest.class)))
73+
.thenReturn(updatedInstructor);
74+
75+
UpdateInstructorAction updateInstructorAction = getAction(requestBody, params);
76+
JsonResult r = getJsonResult(updateInstructorAction);
77+
78+
InstructorData response = (InstructorData) r.getOutput();
79+
80+
assertEquals(updatedInstructor.getName(), response.getName());
81+
assertEquals(updatedInstructor.getEmail(), response.getEmail());
82+
83+
verifySpecifiedTasksAdded(Const.TaskQueue.SEARCH_INDEXING_QUEUE_NAME, 1);
84+
85+
TaskWrapper taskAdded = mockTaskQueuer.getTasksAdded().get(0);
86+
87+
assertEquals(typicalCourse.getId(), taskAdded.getParamMap().get(Const.ParamsNames.COURSE_ID));
88+
assertEquals(updatedInstructor.getEmail(), taskAdded.getParamMap().get(Const.ParamsNames.INSTRUCTOR_EMAIL));
89+
90+
verify(mockLogic, times(1))
91+
.updateToEnsureValidityOfInstructorsForTheCourse(typicalCourse.getId(), updatedInstructor);
92+
}
93+
94+
@Test
95+
void testExecute_invalidEmail_throwsInvalidHttpRequestBodyException() throws Exception {
96+
String instructorToUpdateId = typicalInstructorToUpdate.getGoogleId();
97+
String instructorToUpdateDisplayName = typicalInstructorToUpdate.getDisplayName();
98+
99+
loginAsInstructor(instructorToUpdateId);
100+
101+
String[] params = new String[] {
102+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
103+
};
104+
105+
String updatedInstructorName = "New Instructor";
106+
String invalidEmail = "invalidemail.com";
107+
String updatedInstructorRole = Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER;
108+
InstructorCreateRequest requestBody = new InstructorCreateRequest(instructorToUpdateId, updatedInstructorName,
109+
invalidEmail, updatedInstructorRole,
110+
instructorToUpdateDisplayName, typicalInstructorToUpdate.isDisplayedToStudents());
111+
112+
when(mockLogic.updateInstructorCascade(any(String.class), any(InstructorCreateRequest.class)))
113+
.thenThrow(InvalidParametersException.class);
114+
115+
verifyHttpRequestBodyFailure(requestBody, params);
116+
117+
verifyNoTasksAdded();
118+
}
119+
120+
@Test
121+
void testExecute_noInstructorDisplayed_throwsInvalidOperationException() throws Exception {
122+
String instructorToUpdateId = typicalInstructorToUpdate.getGoogleId();
123+
124+
loginAsInstructor(instructorToUpdateId);
125+
126+
String[] params = new String[] {
127+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
128+
};
129+
130+
String updatedInstructorName = "New Instructor";
131+
String updatedInstructorEmail = "[email protected]";
132+
String updatedInstructorRole = Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER;
133+
InstructorCreateRequest requestBody = new InstructorCreateRequest(instructorToUpdateId, updatedInstructorName,
134+
updatedInstructorEmail, updatedInstructorRole,
135+
null, false);
136+
137+
when(mockLogic.updateInstructorCascade(any(String.class), any(InstructorCreateRequest.class)))
138+
.thenThrow(InstructorUpdateException.class);
139+
140+
verifyInvalidOperation(requestBody, params);
141+
142+
verifyNoTasksAdded();
143+
}
144+
145+
@Test
146+
void testExecute_adminToMasqueradeAsInstructor_success() throws Exception {
147+
String instructorToUpdateId = typicalInstructorToUpdate.getGoogleId();
148+
String instructorToUpdateDisplayName = typicalInstructorToUpdate.getDisplayName();
149+
150+
loginAsAdmin();
151+
152+
String[] params = new String[] {
153+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
154+
};
155+
156+
String updatedInstructorName = "New Instructor";
157+
String updatedInstructorEmail = "[email protected]";
158+
String updatedInstructorRole = Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER;
159+
Instructor updatedInstructor = new Instructor(typicalCourse, updatedInstructorName, updatedInstructorEmail,
160+
false, null, getEnum(updatedInstructorRole),
161+
new InstructorPrivileges(updatedInstructorRole));
162+
163+
InstructorCreateRequest requestBody = new InstructorCreateRequest(instructorToUpdateId, updatedInstructorName,
164+
updatedInstructorEmail, updatedInstructorRole,
165+
instructorToUpdateDisplayName, true);
166+
167+
when(mockLogic.updateInstructorCascade(any(String.class), any(InstructorCreateRequest.class)))
168+
.thenReturn(updatedInstructor);
169+
170+
UpdateInstructorAction updateInstructorAction = getAction(requestBody, params);
171+
JsonResult r = getJsonResult(updateInstructorAction);
172+
173+
InstructorData response = (InstructorData) r.getOutput();
174+
175+
assertEquals(updatedInstructor.getName(), response.getName());
176+
assertEquals(updatedInstructor.getEmail(), response.getEmail());
177+
178+
verifySpecifiedTasksAdded(Const.TaskQueue.SEARCH_INDEXING_QUEUE_NAME, 1);
179+
180+
TaskWrapper taskAdded = mockTaskQueuer.getTasksAdded().get(0);
181+
182+
assertEquals(typicalCourse.getId(), taskAdded.getParamMap().get(Const.ParamsNames.COURSE_ID));
183+
assertEquals(updatedInstructor.getEmail(), taskAdded.getParamMap().get(Const.ParamsNames.INSTRUCTOR_EMAIL));
184+
185+
verify(mockLogic, times(1))
186+
.updateToEnsureValidityOfInstructorsForTheCourse(typicalCourse.getId(), updatedInstructor);
187+
}
188+
189+
@Test
190+
void testExecute_nullHttpParameters_throwsInvalidHttpParameterException() {
191+
String[] params = new String[] {
192+
Const.ParamsNames.COURSE_ID, null,
193+
};
194+
verifyHttpParameterFailure(params);
195+
196+
verifyNoTasksAdded();
197+
}
198+
199+
@Test
200+
void testExecute_nullInstructorEmail_throwsInvalidHttpRequestBodyException() throws Exception {
201+
String instructorToUpdateId = typicalInstructorToUpdate.getGoogleId();
202+
String instructorToUpdateDisplayName = typicalInstructorToUpdate.getDisplayName();
203+
204+
loginAsInstructor(instructorToUpdateId);
205+
206+
String[] params = new String[] {
207+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
208+
};
209+
210+
String updatedInstructorName = "New Instructor";
211+
String updatedInstructorRole = Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER;
212+
InstructorCreateRequest requestBody = new InstructorCreateRequest(instructorToUpdateId, updatedInstructorName,
213+
null, updatedInstructorRole,
214+
instructorToUpdateDisplayName, typicalInstructorToUpdate.isDisplayedToStudents());
215+
216+
when(mockLogic.updateInstructorCascade(any(String.class), any(InstructorCreateRequest.class)))
217+
.thenThrow(InvalidParametersException.class);
218+
219+
verifyHttpRequestBodyFailure(requestBody, params);
220+
221+
verifyNoTasksAdded();
222+
}
223+
224+
@Test
225+
void testAccessControl_noLogin_cannotAccess() {
226+
String[] params = new String[] {
227+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
228+
};
229+
230+
logoutUser();
231+
verifyCannotAccess(params);
232+
}
233+
234+
@Test
235+
void testAccessControl_unregisteredUsers_cannotAccess() {
236+
String[] params = new String[] {
237+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
238+
};
239+
240+
loginAsUnregistered("unregistered user");
241+
verifyCannotAccess(params);
242+
}
243+
244+
@Test
245+
void testAccessControl_students_cannotAccess() {
246+
Student typicalStudent = getTypicalStudent();
247+
String[] params = new String[] {
248+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
249+
};
250+
251+
loginAsStudent(typicalStudent.getGoogleId());
252+
verifyCannotAccess(params);
253+
}
254+
255+
@Test
256+
void testAccessControl_instructorsFromDifferentCourse_cannotAccess() {
257+
Course differentCourse = new Course("different id", "different name", Const.DEFAULT_TIME_ZONE,
258+
"teammates");
259+
Instructor instructorFromDifferentCourse = getTypicalInstructor();
260+
instructorFromDifferentCourse.setCourse(differentCourse);
261+
instructorFromDifferentCourse.setGoogleId("different google id");
262+
263+
String[] params = new String[] {
264+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
265+
};
266+
267+
when(mockLogic.getCourse(typicalCourse.getId())).thenReturn(typicalCourse);
268+
when(mockLogic.getInstructorByGoogleId(differentCourse.getId(), instructorFromDifferentCourse.getGoogleId()))
269+
.thenReturn(instructorFromDifferentCourse);
270+
271+
loginAsInstructor(instructorFromDifferentCourse.getGoogleId());
272+
273+
verifyCannotAccess(params);
274+
}
275+
276+
@Test
277+
void testAccessControl_instructorWithoutCorrectCoursePrivilege_cannotAccess() {
278+
Instructor instructorWithoutCorrectPrivilege = getTypicalInstructor();
279+
instructorWithoutCorrectPrivilege.setGoogleId("no privilege");
280+
instructorWithoutCorrectPrivilege.setEmail("[email protected]");
281+
instructorWithoutCorrectPrivilege.setPrivileges(new InstructorPrivileges(INSTRUCTOR_PERMISSION_ROLE_CUSTOM));
282+
283+
String[] params = new String[] {
284+
Const.ParamsNames.COURSE_ID, typicalCourse.getId(),
285+
};
286+
287+
when(mockLogic.getCourse(typicalCourse.getId())).thenReturn(typicalCourse);
288+
when(mockLogic.getInstructorByGoogleId(typicalCourse.getId(), instructorWithoutCorrectPrivilege.getGoogleId()))
289+
.thenReturn(instructorWithoutCorrectPrivilege);
290+
291+
loginAsInstructor(instructorWithoutCorrectPrivilege.getGoogleId());
292+
293+
verifyCannotAccess(params);
294+
}
295+
296+
}

0 commit comments

Comments
 (0)