Skip to content

Commit

Permalink
[#12770] UpdateStudentActionTest:testExecute split up into multiple s…
Browse files Browse the repository at this point in the history
…maller test cases
  • Loading branch information
puspanjalipradhan committed Feb 22, 2024
1 parent c507049 commit 9c7022f
Showing 1 changed file with 105 additions and 29 deletions.
134 changes: 105 additions & 29 deletions src/test/java/teammates/ui/webapi/UpdateStudentActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ protected String getRequestMethod() {
return PUT;
}

@Override
@Test
public void testExecute() throws Exception {
private void testExecute_withInvalidParameters() {
InstructorAttributes instructor1OfCourse1 = typicalBundle.instructors.get("instructor1OfCourse1");
StudentAttributes student1InCourse1 = typicalBundle.students.get("student1InCourse1");

Expand All @@ -56,9 +54,13 @@ public void testExecute() throws Exception {
};
verifyHttpParameterFailure(invalidParams);
verifyNoTasksAdded();
}

private JsonResult saveAndUpdateStudent(InstructorAttributes instructor1OfCourse1,
StudentAttributes student1InCourse1,
String newStudentEmail) {
______TS("Typical case, successful edit and save student detail");
String newStudentEmail = "[email protected]";

String newStudentTeam = "new student's team";
String newStudentComments = "this is new comment after editing";
StudentUpdateRequest updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), newStudentEmail,
Expand All @@ -70,7 +72,19 @@ public void testExecute() throws Exception {
};

UpdateStudentAction updateAction = getAction(updateRequest, submissionParams);
JsonResult actionOutput = getJsonResult(updateAction);
return getJsonResult(updateAction);
}

@Test
public void testExecute_saveEditStudentDetails() {

InstructorAttributes instructor1OfCourse1 = typicalBundle.instructors.get("instructor1OfCourse1");
StudentAttributes student1InCourse1 = typicalBundle.students.get("student1InCourse1");
String newStudentEmail = "[email protected]";
String instructorId = instructor1OfCourse1.getGoogleId();
loginAsInstructor(instructorId);

JsonResult actionOutput = saveAndUpdateStudent(instructor1OfCourse1, student1InCourse1, newStudentEmail);

MessageOutput msgOutput = (MessageOutput) actionOutput.getOutput();
assertEquals("Student has been updated and email sent", msgOutput.getMessage());
Expand All @@ -83,13 +97,60 @@ public void testExecute() throws Exception {
assertEquals(newStudentEmail, email.getRecipient());

verifySpecifiedTasksAdded(Const.TaskQueue.SEARCH_INDEXING_QUEUE_NAME, 1);
}

@Test
public void testExecute_studentDoesNotExist() {

InstructorAttributes instructor1OfCourse1 = typicalBundle.instructors.get("instructor1OfCourse1");
StudentAttributes student1InCourse1 = typicalBundle.students.get("student1InCourse1");

String instructorId = instructor1OfCourse1.getGoogleId();
loginAsInstructor(instructorId);
String newStudentEmail = "[email protected]";
saveAndUpdateStudent(instructor1OfCourse1, student1InCourse1, newStudentEmail);
______TS("Error case, student does not exist");

String nonExistentEmailForStudent = "[email protected]";

StudentUpdateRequest updateRequest = new StudentUpdateRequest(student1InCourse1.getName(),
student1InCourse1.getEmail(),
student1InCourse1.getTeam(),
student1InCourse1.getSection(),
student1InCourse1.getComments(),
false);

String[] submissionParams = new String[] {
Const.ParamsNames.COURSE_ID, instructor1OfCourse1.getCourseId(),
Const.ParamsNames.STUDENT_EMAIL, nonExistentEmailForStudent,
};

EntityNotFoundException enfe = verifyEntityNotFound(updateRequest, submissionParams);
assertEquals(UpdateStudentAction.STUDENT_NOT_FOUND_FOR_EDIT, enfe.getMessage());

verifyNoTasksAdded();
}

@Test
public void testExecute_withNonTrimmedInputAfterSuccessfulEntry() {
InstructorAttributes instructor1OfCourse1 = typicalBundle.instructors.get("instructor1OfCourse1");
StudentAttributes student1InCourse1 = typicalBundle.students.get("student1InCourse1");

String instructorId = instructor1OfCourse1.getGoogleId();
loginAsInstructor(instructorId);
String newStudentEmail = "[email protected]";
saveAndUpdateStudent(instructor1OfCourse1, student1InCourse1, newStudentEmail);

______TS("Typical case, successful edit and save student detail with spaces to be trimmed");
String newStudentEmailToBeTrimmed = " [email protected] "; // after trim, this is equal to newStudentEmail
String newStudentTeamToBeTrimmed = " New team ";
String newStudentCommentsToBeTrimmed = " this is new comment after editing ";
updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), newStudentEmailToBeTrimmed,
newStudentTeamToBeTrimmed, student1InCourse1.getSection(), newStudentCommentsToBeTrimmed, true);
StudentUpdateRequest updateRequest = new StudentUpdateRequest(student1InCourse1.getName(),
newStudentEmailToBeTrimmed,
newStudentTeamToBeTrimmed,
student1InCourse1.getSection(),
newStudentCommentsToBeTrimmed,
true);

String[] submissionParamsToBeTrimmed = new String[] {
Const.ParamsNames.COURSE_ID, instructor1OfCourse1.getCourseId(),
Expand All @@ -102,39 +163,63 @@ public void testExecute() throws Exception {
MessageOutput msgTrimmedOutput = (MessageOutput) outputToBeTrimmed.getOutput();
assertEquals("Student has been updated", msgTrimmedOutput.getMessage());
verifyNoEmailsSent();
}

@Test
public void testExecute_withInvalidEmailParameter() throws ReflectiveOperationException {

InstructorAttributes instructor1OfCourse1 = typicalBundle.instructors.get("instructor1OfCourse1");
StudentAttributes student1InCourse1 = typicalBundle.students.get("student1InCourse1");

String instructorId = instructor1OfCourse1.getGoogleId();
loginAsInstructor(instructorId);
String newStudentEmail = "[email protected]";
saveAndUpdateStudent(instructor1OfCourse1, student1InCourse1, newStudentEmail);

______TS("Error case, invalid email parameter (email has too many characters)");

String invalidStudentEmail = StringHelperExtension.generateStringOfLength(255 - "@gmail.tmt".length())
+ "@gmail.tmt";
assertEquals(FieldValidator.EMAIL_MAX_LENGTH + 1, invalidStudentEmail.length());

updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), invalidStudentEmail,
StudentUpdateRequest updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), invalidStudentEmail,
student1InCourse1.getTeam(), student1InCourse1.getSection(), student1InCourse1.getComments(), false);

submissionParams = new String[] {
String[] submissionParams = new String[] {
Const.ParamsNames.COURSE_ID, instructor1OfCourse1.getCourseId(),
Const.ParamsNames.STUDENT_EMAIL, newStudentEmail,
};

InvalidHttpRequestBodyException ihrbe = verifyHttpRequestBodyFailure(updateRequest, submissionParams);

assertEquals(getPopulatedErrorMessage(FieldValidator.EMAIL_ERROR_MESSAGE, invalidStudentEmail,
FieldValidator.EMAIL_FIELD_NAME, FieldValidator.REASON_TOO_LONG,
FieldValidator.EMAIL_MAX_LENGTH),
FieldValidator.EMAIL_FIELD_NAME, FieldValidator.REASON_TOO_LONG,
FieldValidator.EMAIL_MAX_LENGTH),
ihrbe.getMessage());

verifyNoTasksAdded();
}

@Test
public void testExecute_emailAlreadyTaken() {

InstructorAttributes instructor1OfCourse1 = typicalBundle.instructors.get("instructor1OfCourse1");
StudentAttributes student1InCourse1 = typicalBundle.students.get("student1InCourse1");

String instructorId = instructor1OfCourse1.getGoogleId();
loginAsInstructor(instructorId);
String newStudentEmail = "[email protected]";
saveAndUpdateStudent(instructor1OfCourse1, student1InCourse1, newStudentEmail);

______TS("Error case, invalid email parameter (email already taken by others)");

StudentAttributes student2InCourse1 = typicalBundle.students.get("student2InCourse1");
String takenStudentEmail = student2InCourse1.getEmail();

updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), takenStudentEmail,
StudentUpdateRequest updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), takenStudentEmail,
student1InCourse1.getTeam(), student1InCourse1.getSection(), student1InCourse1.getComments(), false);

submissionParams = new String[] {
String[] submissionParams = new String[] {
Const.ParamsNames.COURSE_ID, instructor1OfCourse1.getCourseId(),
Const.ParamsNames.STUDENT_EMAIL, newStudentEmail,
};
Expand All @@ -147,22 +232,6 @@ public void testExecute() throws Exception {
// deleting edited student
logic.deleteAccountCascade(student2InCourse1.getGoogleId());
logic.deleteAccountCascade(student1InCourse1.getGoogleId());

______TS("Error case, student does not exist");

String nonExistentEmailForStudent = "[email protected]";
updateRequest = new StudentUpdateRequest(student1InCourse1.getName(), student1InCourse1.getEmail(),
student1InCourse1.getTeam(), student1InCourse1.getSection(), student1InCourse1.getComments(), false);

submissionParams = new String[] {
Const.ParamsNames.COURSE_ID, instructor1OfCourse1.getCourseId(),
Const.ParamsNames.STUDENT_EMAIL, nonExistentEmailForStudent,
};

EntityNotFoundException enfe = verifyEntityNotFound(updateRequest, submissionParams);
assertEquals(UpdateStudentAction.STUDENT_NOT_FOUND_FOR_EDIT, enfe.getMessage());

verifyNoTasksAdded();
}

@Test
Expand Down Expand Up @@ -275,6 +344,13 @@ public void testExecute_withEmptySectionName_shouldBeUpdatedWithDefaultSectionNa
assertEquals(student5InCourse1.getComments(), actualStudent.getComments());
}

@Override

@Test
public void testExecute() throws Exception {
testExecute_withInvalidParameters();
}

@Override
@Test
protected void testAccessControl() throws Exception {
Expand Down

0 comments on commit 9c7022f

Please sign in to comment.