Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ResponseEntity<Map<String, Object>> changeStudentPassword(Authentication auth,
@RequestParam String newStudentPassword)
throws ObjectNotFoundException, IncorrectPasswordException {
Run run = runService.retrieveById(runId);
if (runService.hasWritePermission(auth, run)) {
if (isAllowed(auth, run, studentId)) {
User teacherUser = userService.retrieveUserByUsername(auth.getName());
boolean isTeacherGoogleUser = teacherUser.getUserDetails().isGoogleUser();
if (isTeacherPasswordCorrect(isTeacherGoogleUser, teacherUser, teacherPassword)) {
Expand All @@ -58,6 +58,12 @@ ResponseEntity<Map<String, Object>> changeStudentPassword(Authentication auth,
}
}

private boolean isAllowed(Authentication auth, Run run, Long studentId)
throws ObjectNotFoundException {
return runService.hasWritePermission(auth, run)
&& run.isStudentAssociatedToThisRun(userService.retrieveById(studentId));
}

private boolean isTeacherPasswordCorrect(boolean isGoogleUser, User teacherUser,
String password) {
return isGoogleUser || userService.isPasswordCorrect(teacherUser, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void changeStudentPassword_NoWritePermission_ThrowAccessDenied() throws E
private void setupChangeStudentPasswordExpect() throws Exception {
expect(runService.retrieveById(runId1)).andReturn(run1);
expect(runService.hasWritePermission(teacherAuth, run1)).andReturn(true);
expect(userService.retrieveById(student1Id)).andReturn(student1);
expect(userService.retrieveUserByUsername(TEACHER_USERNAME)).andReturn(teacher1);
}

Expand Down Expand Up @@ -115,6 +116,36 @@ public void changeStudentPassword_InvalidPassword_ReturnError() throws Exception
verifyServices();
}

@Test
public void changeStudentPassword_StudentNotInRun_ThrowAccessDenied() throws Exception {
expect(runService.retrieveById(runId1)).andReturn(run1);
expect(runService.hasWritePermission(teacherAuth, run1)).andReturn(true);
expect(userService.retrieveById(student2Id)).andReturn(student2);
replayServices();
try {
controller.changeStudentPassword(teacherAuth, runId1, student2Id, TEACHER_PASSWORD_CORRECT,
STUDENT_PASSWORD_VALID);
fail("Expected AccessDeniedException to be thrown");
} catch (AccessDeniedException e) {
}
verifyServices();
}

@Test
public void changeStudentPassword_TargetUserIsTeacher_ThrowAccessDenied() throws Exception {
expect(runService.retrieveById(runId1)).andReturn(run1);
expect(runService.hasWritePermission(teacherAuth, run1)).andReturn(true);
expect(userService.retrieveById(teacher2Id)).andReturn(teacher2);
replayServices();
try {
controller.changeStudentPassword(teacherAuth, runId1, teacher2Id, TEACHER_PASSWORD_CORRECT,
STUDENT_PASSWORD_VALID);
fail("Expected AccessDeniedException to be thrown");
} catch (AccessDeniedException e) {
}
verifyServices();
}

@Test
public void changeStudentPassword_validTeacherPassword_ChangeStudentPassword() throws Exception {
setupChangeStudentPasswordExpect();
Expand Down
Loading