-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
DhiraPT
wants to merge
8
commits into
TEAMMATES:master
Choose a base branch
from
DhiraPT:db-migrate-DeleteStudentsActionTest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bee8c47
Add DeleteStudentsActionTest
DhiraPT 13b4ab9
Add instructorInDifferentCourse test
DhiraPT 3bd52f2
Abstract the code
DhiraPT 72f43e7
Separate logged out access control test
DhiraPT aea0328
Fix parameters
DhiraPT 1ee1e97
Remove extra access verification
DhiraPT 5565a2b
Refine tests
DhiraPT dc0fb31
Ensure consistent code styling
DhiraPT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
src/test/java/teammates/sqlui/webapi/DeleteStudentsActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package teammates.sqlui.webapi; | ||
|
||
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.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; | ||
private String instructorId = "instructor-googleId"; | ||
|
||
@Override | ||
protected String getActionUri() { | ||
return Const.ResourceURIs.STUDENTS; | ||
} | ||
|
||
@Override | ||
protected String getRequestMethod() { | ||
return DELETE; | ||
} | ||
|
||
@BeforeMethod | ||
void setUp() { | ||
Mockito.reset(mockLogic); | ||
|
||
course = new Course("course-id", "Course Name", Const.DEFAULT_TIME_ZONE, "institute"); | ||
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.getInstructorByGoogleId(course.getId(), instructorId)).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(); | ||
|
||
verify(mockLogic, times(1)).deleteStudentsInCourseCascade(course.getId()); | ||
assertEquals("Successful", actionOutput.getMessage()); | ||
} | ||
|
||
@Test | ||
void testExecute_courseDoesNotExist_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(); | ||
|
||
verify(mockLogic, times(0)).deleteStudentsInCourseCascade(course.getId()); | ||
assertEquals("Successful", actionOutput.getMessage()); | ||
} | ||
|
||
@Test | ||
void testExecute_noParameters_throwsInvalidParametersException() { | ||
verifyHttpParameterFailure(); | ||
} | ||
|
||
@Test | ||
void testExecute_missingCourseId_throwsInvalidParametersException() { | ||
String[] params = { | ||
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT), | ||
}; | ||
|
||
verifyHttpParameterFailure(params); | ||
} | ||
|
||
@Test | ||
void testExecute_missingLimit_throwsInvalidParametersException() { | ||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, course.getId(), | ||
}; | ||
|
||
verifyHttpParameterFailure(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_instructorWithPermission_canAccess() { | ||
loginAsInstructor(instructorId); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, course.getId(), | ||
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT), | ||
}; | ||
|
||
verifyCanAccess(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_instructorWithInvalidPermission_cannotAccess() { | ||
InstructorPrivileges instructorPrivileges = new InstructorPrivileges(); | ||
instructorPrivileges.updatePrivilege(Const.InstructorPermissions.CAN_MODIFY_STUDENT, false); | ||
instructor.setPrivileges(instructorPrivileges); | ||
|
||
loginAsInstructor(instructorId); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, course.getId(), | ||
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT), | ||
}; | ||
|
||
verifyCannotAccess(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_instructorInDifferentCourse_cannotAccess() { | ||
loginAsInstructor("instructor2-googleId"); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, course.getId(), | ||
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT), | ||
}; | ||
|
||
verifyCannotAccess(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_student_cannotAccess() { | ||
loginAsStudent("student-googleId"); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, course.getId(), | ||
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT), | ||
}; | ||
|
||
verifyCannotAccess(params); | ||
} | ||
|
||
@Test | ||
void testSpecificAccessControl_loggedOut_cannotAccess() { | ||
logoutUser(); | ||
|
||
String[] params = { | ||
Const.ParamsNames.COURSE_ID, course.getId(), | ||
Const.ParamsNames.LIMIT, String.valueOf(DELETE_LIMIT), | ||
}; | ||
|
||
verifyCannotAccess(params); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as #13204 (comment)