Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12048] Migrate Tests for DeleteStudentActionTest #13204

Merged
merged 16 commits into from
Feb 18, 2025
Merged
Changes from 8 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
252 changes: 252 additions & 0 deletions src/test/java/teammates/sqlui/webapi/DeleteStudentActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package teammates.sqlui.webapi;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.mockito.Mockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.InstructorPrivileges;
import teammates.common.util.Const;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Student;
import teammates.ui.output.MessageOutput;
import teammates.ui.webapi.DeleteStudentAction;

/**
* SUT: {@link DeleteStudentAction}.
*/
public class DeleteStudentActionTest extends BaseActionTest<DeleteStudentAction> {

private Course course;
private Student student;
private Instructor instructor;
private String studentId = "student-googleId";
private String instructorId = "instructor-googleId";

@Override
protected String getActionUri() {
return Const.ResourceURIs.STUDENT;
}

@Override
protected String getRequestMethod() {
return DELETE;
}

@BeforeMethod
void setUp() {
Mockito.reset(mockLogic);

course = new Course("course-id", "Course Name", Const.DEFAULT_TIME_ZONE, "institute");
student = new Student(course, "Student Name", "[email protected]", "Some comments");
InstructorPrivileges instructorPrivileges = new InstructorPrivileges();
instructorPrivileges.updatePrivilege(Const.InstructorPermissions.CAN_MODIFY_STUDENT, true);
instructor = new Instructor(course, "Instructor Name", "[email protected]",
false, "", null, instructorPrivileges);

setupMockLogic();
}

private void setupMockLogic() {
when(mockLogic.getCourse(course.getId())).thenReturn(course);
when(mockLogic.getStudentByGoogleId(course.getId(), studentId)).thenReturn(student);
when(mockLogic.getStudentForEmail(course.getId(), student.getEmail())).thenReturn(student);
when(mockLogic.getInstructorByGoogleId(course.getId(), instructorId)).thenReturn(instructor);
}

@Test
void testExecute_deleteStudentByEmail_success() {
String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_EMAIL, student.getEmail(),
};

DeleteStudentAction action = getAction(params);
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, never()).getStudentByGoogleId(any(), any());
verify(mockLogic, times(1)).deleteStudentCascade(course.getId(), student.getEmail());
assertEquals("Student is successfully deleted.", actionOutput.getMessage());
}

@Test
void testExecute_deleteStudentById_success() {
String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

DeleteStudentAction action = getAction(params);
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, times(1)).getStudentByGoogleId(course.getId(), studentId);
verify(mockLogic, times(1)).deleteStudentCascade(course.getId(), student.getEmail());
assertEquals("Student is successfully deleted.", actionOutput.getMessage());
}

@Test
void testExecute_nonExistentCourse_failSilently() {
when(mockLogic.getCourse("RANDOM_COURSE")).thenReturn(null);

String[] params = {
Const.ParamsNames.COURSE_ID, "RANDOM_COURSE",
Const.ParamsNames.STUDENT_ID, studentId,
};

DeleteStudentAction action = getAction(params);
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, times(1)).getStudentByGoogleId("RANDOM_COURSE", studentId);
verify(mockLogic, never()).deleteStudentCascade(any(), any());
assertEquals("Student is successfully deleted.", actionOutput.getMessage());
}

@Test
void testExecute_nonExistentStudentId_failSilently() {
when(mockLogic.getStudentByGoogleId(course.getId(), "RANDOM_STUDENT")).thenReturn(null);

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, "RANDOM_STUDENT",
};

DeleteStudentAction action = getAction(params);
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, times(1)).getStudentByGoogleId(course.getId(), "RANDOM_STUDENT");
verify(mockLogic, never()).deleteStudentCascade(any(), any());
assertEquals("Student is successfully deleted.", actionOutput.getMessage());
}

@Test
void testExecute_nonExistentStudentEmail_failSilently() {
when(mockLogic.getStudentForEmail(course.getId(), "RANDOM_EMAIL")).thenReturn(null);

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_EMAIL, "RANDOM_EMAIL",
};

DeleteStudentAction action = getAction(params);
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, never()).getStudentByGoogleId(any(), any());
verify(mockLogic, times(1)).deleteStudentCascade(course.getId(), "RANDOM_EMAIL");
verify(mockLogic, never()).deleteStudentCascade(course.getId(), student.getEmail());
assertEquals("Student is successfully deleted.", actionOutput.getMessage());
}

@Test
void testExecute_noParameters_throwsInvalidParametersException() {
verifyHttpParameterFailure();
}

@Test
void testExecute_missingStudentIdOrEmail_throwsInvalidHttpParameterException() {
String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
};

verifyHttpParameterFailure(params);
}

@Test
void testExecute_missingCourseIdWithStudentId_throwsInvalidHttpParameterException() {
String[] params = {
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyHttpParameterFailure(params);
}

@Test
void testExecute_missingCourseIdWithStudentEmail_throwsInvalidHttpParameterException() {
String[] params = {
Const.ParamsNames.STUDENT_EMAIL, student.getEmail(),
};

verifyHttpParameterFailure(params);
}

@Test
void testSpecificAccessControl_admin_canAccess() {
loginAsAdmin();

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyCanAccess(params);
}

@Test
void testSpecificAccessControl_instructorWithPermission_canAccess() {
loginAsInstructor(instructorId);

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyCanAccess(params);
}

@Test
void testSpecificAccessControl_instructorWithInvalidPermission_cannotAccess() {
InstructorPrivileges instructorPrivileges = new InstructorPrivileges();
instructorPrivileges.updatePrivilege(Const.InstructorPermissions.CAN_MODIFY_STUDENT, false);
instructor.setPrivileges(instructorPrivileges);

loginAsInstructor(instructor.getGoogleId());

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyCannotAccess(params);
}

@Test
void testSpecificAccessControl_instructorInDifferentCourse_cannotAccess() {
loginAsInstructor("instructor2-googleId");

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyCannotAccess(params);
}

@Test
void testSpecificAccessControl_student_cannotAccess() {
loginAsStudent(studentId);

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyCannotAccess(params);
}

@Test
void testSpecificAccessControl_loggedOut_cannotAccess() {
logoutUser();

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.STUDENT_ID, studentId,
};

verifyCannotAccess(params);
}
}
Loading