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 DeleteStudentsActionTest #13205

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
142 changes: 142 additions & 0 deletions src/test/java/teammates/sqlui/webapi/DeleteStudentsActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package teammates.sqlui.webapi;

import static org.mockito.Mockito.when;

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

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

/**
* SUT: {@link DeleteStudentsAction}.
*/
public class DeleteStudentsActionTest extends BaseActionTest<DeleteStudentsAction> {

private static final int DELETE_LIMIT = 3;
private Course course;
private Instructor instructor;

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

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

@BeforeMethod
void setUp() {
course = new Course("course-id", "Course Name", Const.DEFAULT_TIME_ZONE, "institute");
instructor = setupInstructor("instructor-googleId", "name", "[email protected]");

setupMockLogic();
}

private Instructor setupInstructor(String googleId, String name, String email) {
Account account = new Account(googleId, name, email);
InstructorPrivileges instructorPrivileges = new InstructorPrivileges();
instructorPrivileges.updatePrivilege(Const.InstructorPermissions.CAN_MODIFY_STUDENT, true);

Instructor instructor = new Instructor(course, name, email,
false, "", null, instructorPrivileges);
instructor.setAccount(account);
return instructor;
}

private void setupMockLogic() {
when(mockLogic.getCourse(course.getId())).thenReturn(course);
when(mockLogic.getInstructorByGoogleId(course.getId(), instructor.getGoogleId())).thenReturn(instructor);
}

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

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

assertEquals("Successful", actionOutput.getMessage());
}

@Test
void testExecute_randomCourse_failSilently() {
when(mockLogic.getCourse("RANDOM_ID")).thenReturn(null);

String[] params = {
Const.ParamsNames.COURSE_ID, "RANDOM_ID",
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT),
};

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

assertEquals("Successful", actionOutput.getMessage());
}

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

@Test
void testSpecificAccessControl_instructorWithPermission_canAccess() {
loginAsInstructor(instructor.getGoogleId());

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

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(),
};

verifyCannotAccess(params);
}

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

String[] params = {
Const.ParamsNames.COURSE_ID, course.getId(),
Const.ParamsNames.INSTRUCTOR_ID, instructor.getGoogleId(),
};

verifyCannotAccess(params);
}

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

loginAsStudent("student-googleId");
verifyCannotAccess(params);

logoutUser();
verifyCannotAccess(params);
}
}
Loading