use of teammates.common.util.FieldValidator in project teammates by TEAMMATES.
the class InstructorCourseInstructorEditSaveActionTest method testExecuteAndPostProcess.
@Override
@Test
public void testExecuteAndPostProcess() {
InstructorAttributes instructorToEdit = typicalBundle.instructors.get("instructor1OfCourse1");
String instructorId = instructorToEdit.googleId;
String courseId = instructorToEdit.courseId;
String adminUserId = "admin.user";
gaeSimulation.loginAsInstructor(instructorId);
______TS("Typical case: edit instructor successfully");
String newInstructorName = "newName";
String newInstructorEmail = "newEmail@email.com";
String[] submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.INSTRUCTOR_ID, instructorId, Const.ParamsNames.INSTRUCTOR_NAME, newInstructorName, Const.ParamsNames.INSTRUCTOR_EMAIL, newInstructorEmail, Const.ParamsNames.INSTRUCTOR_ROLE_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_DISPLAY_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER };
InstructorCourseInstructorEditSaveAction saveAction = getAction(submissionParams);
RedirectResult redirectResult = getRedirectResult(saveAction);
AssertHelper.assertContains(Const.ActionURIs.INSTRUCTOR_COURSE_EDIT_PAGE, redirectResult.getDestinationWithParams());
assertFalse(redirectResult.isError);
assertEquals(String.format(Const.StatusMessages.COURSE_INSTRUCTOR_EDITED, newInstructorName), redirectResult.getStatusMessage());
InstructorAttributes editedInstructor = instructorsLogic.getInstructorForGoogleId(courseId, instructorId);
assertEquals(newInstructorName, editedInstructor.name);
assertEquals(newInstructorEmail, editedInstructor.email);
assertTrue(editedInstructor.isAllowedForPrivilege(Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_COURSE));
assertTrue(editedInstructor.isAllowedForPrivilege(Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_INSTRUCTOR));
assertTrue(editedInstructor.isAllowedForPrivilege(Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION));
assertTrue(editedInstructor.isAllowedForPrivilege(Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_STUDENT));
String expectedLogSegment = "Instructor <span class=\"bold\"> " + newInstructorName + "</span>" + " for Course <span class=\"bold\">[" + courseId + "]</span> edited.<br>" + "New Name: " + newInstructorName + "<br>New Email: " + newInstructorEmail;
AssertHelper.assertContains(expectedLogSegment, saveAction.getLogMessage());
______TS("Failure case: edit failed due to invalid parameters");
String invalidEmail = "wrongEmail.com";
submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.INSTRUCTOR_ID, instructorId, Const.ParamsNames.INSTRUCTOR_NAME, instructorToEdit.name, Const.ParamsNames.INSTRUCTOR_EMAIL, invalidEmail, Const.ParamsNames.INSTRUCTOR_ROLE_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_DISPLAY_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_COURSE, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_INSTRUCTOR, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_STUDENT, "true" };
saveAction = getAction(submissionParams);
redirectResult = getRedirectResult(saveAction);
AssertHelper.assertContains(Const.ActionURIs.INSTRUCTOR_COURSE_EDIT_PAGE, redirectResult.getDestinationWithParams());
assertTrue(redirectResult.isError);
String expectedErrorMessage = new FieldValidator().getInvalidityInfoForEmail(invalidEmail);
assertEquals(expectedErrorMessage, redirectResult.getStatusMessage());
AssertHelper.assertContains(expectedErrorMessage, saveAction.getLogMessage());
______TS("Masquerade mode: edit instructor successfully");
gaeSimulation.loginAsAdmin(adminUserId);
newInstructorName = "newName2";
newInstructorEmail = "newEmail2@email.com";
submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.INSTRUCTOR_ID, instructorId, Const.ParamsNames.INSTRUCTOR_NAME, newInstructorName, Const.ParamsNames.INSTRUCTOR_EMAIL, newInstructorEmail, Const.ParamsNames.INSTRUCTOR_ROLE_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_DISPLAY_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_COURSE, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_INSTRUCTOR, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_STUDENT, "true" };
saveAction = getAction(addUserIdToParams(instructorId, submissionParams));
redirectResult = getRedirectResult(saveAction);
AssertHelper.assertContains(Const.ActionURIs.INSTRUCTOR_COURSE_EDIT_PAGE, redirectResult.getDestinationWithParams());
assertFalse(redirectResult.isError);
assertEquals(String.format(Const.StatusMessages.COURSE_INSTRUCTOR_EDITED, newInstructorName), redirectResult.getStatusMessage());
editedInstructor = instructorsLogic.getInstructorForGoogleId(courseId, instructorId);
assertEquals(newInstructorEmail, editedInstructor.email);
assertEquals(newInstructorName, editedInstructor.name);
expectedLogSegment = "Instructor <span class=\"bold\"> " + newInstructorName + "</span>" + " for Course <span class=\"bold\">[" + courseId + "]</span> edited.<br>" + "New Name: " + newInstructorName + "<br>New Email: " + newInstructorEmail;
AssertHelper.assertContains(expectedLogSegment, saveAction.getLogMessage());
// remove the new instructor entity that was created
CoursesLogic.inst().deleteCourseCascade("icieat.courseId");
______TS("Unsuccessful case: test null course id parameter");
submissionParams = new String[] { Const.ParamsNames.INSTRUCTOR_ID, instructorId, Const.ParamsNames.INSTRUCTOR_NAME, newInstructorName, Const.ParamsNames.INSTRUCTOR_EMAIL, newInstructorEmail, Const.ParamsNames.INSTRUCTOR_ROLE_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_DISPLAY_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_COURSE, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_INSTRUCTOR, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_STUDENT, "true" };
try {
saveAction = getAction(submissionParams);
getRedirectResult(saveAction);
} catch (NullPostParameterException e) {
assertEquals(String.format(Const.StatusCodes.NULL_POST_PARAMETER, Const.ParamsNames.COURSE_ID), e.getMessage());
}
______TS("Unsuccessful case: test null instructor name parameter");
submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.INSTRUCTOR_ID, instructorId, Const.ParamsNames.INSTRUCTOR_EMAIL, newInstructorEmail, Const.ParamsNames.INSTRUCTOR_ROLE_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_DISPLAY_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_COURSE, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_INSTRUCTOR, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_STUDENT, "true" };
try {
saveAction = getAction(submissionParams);
getRedirectResult(saveAction);
} catch (NullPostParameterException e) {
assertEquals(String.format(Const.StatusCodes.NULL_POST_PARAMETER, Const.ParamsNames.INSTRUCTOR_NAME), e.getMessage());
}
______TS("Unsuccessful case: test null instructor email parameter");
submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.INSTRUCTOR_ID, instructorId, Const.ParamsNames.INSTRUCTOR_NAME, newInstructorName, Const.ParamsNames.INSTRUCTOR_ROLE_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_DISPLAY_NAME, Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER, Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_COURSE, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_INSTRUCTOR, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_SESSION, "true", Const.ParamsNames.INSTRUCTOR_PERMISSION_MODIFY_STUDENT, "true" };
try {
saveAction = getAction(submissionParams);
getRedirectResult(saveAction);
} catch (NullPostParameterException e) {
assertEquals(String.format(Const.StatusCodes.NULL_POST_PARAMETER, Const.ParamsNames.INSTRUCTOR_EMAIL), e.getMessage());
}
}
use of teammates.common.util.FieldValidator in project teammates by TEAMMATES.
the class CoursesLogic method validateAndCreateCourseAttributes.
/**
* Checks that {@code courseTimeZone} is valid and then returns a {@link CourseAttributes}.
* Field validation is usually done in {@link CoursesDb} by calling {@link CourseAttributes#getInvalidityInfo()}.
* However, a {@link CourseAttributes} cannot be created with an invalid time zone string.
* Hence, validation of this field is carried out here.
*
* @throws InvalidParametersException containing error messages for all fields if {@code courseTimeZone} is invalid
*/
private CourseAttributes validateAndCreateCourseAttributes(String courseId, String courseName, String courseTimeZone) throws InvalidParametersException {
// Imitate `CourseAttributes.getInvalidityInfo`
FieldValidator validator = new FieldValidator();
String timeZoneErrorMessage = validator.getInvalidityInfoForTimeZone(courseTimeZone);
if (!timeZoneErrorMessage.isEmpty()) {
// Leave validation of other fields to `CourseAttributes.getInvalidityInfo`
CourseAttributes dummyCourse = CourseAttributes.builder(courseId, courseName, Const.DEFAULT_TIME_ZONE).build();
List<String> errors = dummyCourse.getInvalidityInfo();
errors.add(timeZoneErrorMessage);
// Imitate exception throwing in `CoursesDb`
throw new InvalidParametersException(errors);
}
// If time zone field is valid, leave validation of other fields to `CoursesDb` like usual
return CourseAttributes.builder(courseId, courseName, ZoneId.of(courseTimeZone)).build();
}
use of teammates.common.util.FieldValidator in project teammates by TEAMMATES.
the class StudentProfileAttributes method getInvalidityInfo.
@Override
public List<String> getInvalidityInfo() {
FieldValidator validator = new FieldValidator();
List<String> errors = new ArrayList<>();
addNonEmptyError(validator.getInvalidityInfoForGoogleId(googleId), errors);
if (!StringHelper.isEmpty(shortName)) {
addNonEmptyError(validator.getInvalidityInfoForPersonName(shortName), errors);
}
if (!StringHelper.isEmpty(email)) {
addNonEmptyError(validator.getInvalidityInfoForEmail(email), errors);
}
if (!StringHelper.isEmpty(institute)) {
addNonEmptyError(validator.getInvalidityInfoForInstituteName(institute), errors);
}
if (!StringHelper.isEmpty(nationality)) {
addNonEmptyError(validator.getInvalidityInfoForNationality(nationality), errors);
}
addNonEmptyError(validator.getInvalidityInfoForGender(gender), errors);
Assumption.assertNotNull(this.pictureKey);
return errors;
}
use of teammates.common.util.FieldValidator in project teammates by TEAMMATES.
the class AccountAttributes method getInvalidityInfo.
@Override
public List<String> getInvalidityInfo() {
FieldValidator validator = new FieldValidator();
List<String> errors = new ArrayList<>();
addNonEmptyError(validator.getInvalidityInfoForPersonName(name), errors);
addNonEmptyError(validator.getInvalidityInfoForGoogleId(googleId), errors);
addNonEmptyError(validator.getInvalidityInfoForEmail(email), errors);
addNonEmptyError(validator.getInvalidityInfoForInstituteName(institute), errors);
Assumption.assertNotNull("Non-null value expected for studentProfile", this.studentProfile);
// only check profile if the account is proper
if (errors.isEmpty()) {
errors.addAll(this.studentProfile.getInvalidityInfo());
}
// No validation for isInstructor and createdAt fields.
return errors;
}
use of teammates.common.util.FieldValidator in project teammates by TEAMMATES.
the class CourseAttributes method getInvalidityInfo.
@Override
public List<String> getInvalidityInfo() {
FieldValidator validator = new FieldValidator();
List<String> errors = new ArrayList<>();
addNonEmptyError(validator.getInvalidityInfoForCourseId(getId()), errors);
addNonEmptyError(validator.getInvalidityInfoForCourseName(getName()), errors);
return errors;
}
Aggregations