use of teammates.common.datatransfer.UserType in project teammates by TEAMMATES.
the class GateKeeperTest method testGetCurrentUser.
// TODO: test isUserLoggedIn method
@Test
public void testGetCurrentUser() throws Exception {
______TS("admin+instructor+student");
InstructorAttributes instructor = dataBundle.instructors.get("instructor1OfCourse1");
CourseAttributes course = dataBundle.courses.get("typicalCourse2");
gaeSimulation.loginAsAdmin(instructor.googleId);
// also make this user a student of another course
StudentAttributes instructorAsStudent = StudentAttributes.builder(course.getId(), "Instructor As Student", "instructorasstudent@yahoo.com").withSection("Section 1").withTeam("Team 1").withComments("").build();
instructorAsStudent.googleId = instructor.googleId;
new Logic().createStudentWithoutDocument(instructorAsStudent);
UserType user = gateKeeper.getCurrentUser();
assertEquals(instructor.googleId, user.id);
assertTrue(user.isAdmin);
assertTrue(user.isInstructor);
assertTrue(user.isStudent);
______TS("unregistered");
gaeSimulation.loginUser("unknown");
user = gateKeeper.getCurrentUser();
assertEquals("unknown", user.id);
assertFalse(user.isAdmin);
assertFalse(user.isInstructor);
assertFalse(user.isStudent);
______TS("not logged in");
// check for user not logged in
gaeSimulation.logoutUser();
assertNull(gateKeeper.getCurrentUser());
}
use of teammates.common.datatransfer.UserType in project teammates by TEAMMATES.
the class LogMessageGeneratorTest method generateLogMessage_normalPageAction.
@Test
public void generateLogMessage_normalPageAction() {
______TS("Not login");
String url = Const.ActionURIs.INSTRUCTOR_HOME_PAGE;
Map<String, String[]> paramMap = new HashMap<>();
String logMessage = "TEAMMATESLOG|||instructorHomePage|||instructorHomePage|||true|||Unknown|||Unknown" + "|||Unknown|||Unknown|||Not authorized|||/page/instructorHomePage";
String generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, null, null, null, "Not authorized");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
______TS("Not google login but with key (failure)");
url = Const.ActionURIs.STUDENT_COURSE_JOIN;
paramMap = generateRequestParamsWithRegKey();
logMessage = "TEAMMATESLOG|||studentCourseJoin|||studentCourseJoin|||true|||Unknown|||Unknown|||" + "Unknown|||Unknown|||Not authorized|||/page/studentCourseJoin";
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, null, null, null, "Not authorized");
AssertHelper.assertLogMessageEqualsForUnregisteredStudentUser(logMessage, generatedMessage, "student@email.com", "CS2103");
______TS("Not google login but with key (success)");
url = Const.ActionURIs.STUDENT_COURSE_JOIN + "?user=test@email.com&course=1";
logMessage = "TEAMMATESLOG|||studentCourseJoin|||studentCourseJoin|||true|||Unregistered:CS2103|||Joe" + "|||Unknown|||student@email|||Join Course|||" + url;
StudentAttributes student = StudentAttributes.builder("CS2103", "Joe", "student@email").withSection("section1").withTeam("team1").withComments("comments").withGoogleId("unknownGoogleId").build();
// auth success : unregistered student will be passed
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, null, null, student, "Join Course");
AssertHelper.assertLogMessageEqualsForUnregisteredStudentUser(logMessage, generatedMessage, "student@email.com", "CS2103");
// --------------- Google login ---------------
______TS("Google login (No account)");
url = Const.ActionURIs.STUDENT_HOME_PAGE + "?course=A&user=test";
paramMap = new HashMap<>();
logMessage = "TEAMMATESLOG|||studentHomePage|||studentHomePage|||true|||Unregistered|||Unknown" + "|||googleId|||Unknown|||Try student home|||" + url;
UserType userType = new UserType("googleId");
// userType and account will be passed for logged-in user
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, null, null, "Try student home");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
______TS("Google login (Student)");
String logTemplate = "TEAMMATESLOG|||%1$s|||%1$s|||true|||%2$s|||david" + "|||googleId|||david@email.com|||View Result|||/page/%1$s";
url = Const.ActionURIs.STUDENT_HOME_PAGE;
logMessage = String.format(logTemplate, "studentHomePage", "Student");
userType.isStudent = true;
AccountAttributes acc = AccountAttributes.builder().withGoogleId("googleId").withName("david").withEmail("david@email.com").withInstitute("NUS").withIsInstructor(false).withDefaultStudentProfileAttributes("idOfNewStudent").build();
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View Result");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
______TS("Google login (Instructor and Student auto detect)");
userType.isInstructor = true;
url = Const.ActionURIs.STUDENT_FEEDBACK_RESULTS_PAGE;
logMessage = String.format(logTemplate, "studentFeedbackResultsPage", "Student");
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View Result");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
url = Const.ActionURIs.INSTRUCTOR_COURSE_EDIT_PAGE;
logMessage = String.format(logTemplate, "instructorCourseEditPage", "Instructor");
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View Result");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
______TS("Google login (Admin role auto detect)");
userType.isAdmin = true;
url = Const.ActionURIs.STUDENT_FEEDBACK_RESULTS_PAGE;
logMessage = String.format(logTemplate, "studentFeedbackResultsPage", "Student");
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View Result");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
url = Const.ActionURIs.INSTRUCTOR_COURSES_PAGE;
logMessage = String.format(logTemplate, "instructorCoursesPage", "Instructor");
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View Result");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
url = Const.ActionURIs.ADMIN_ACTIVITY_LOG_PAGE;
logMessage = "TEAMMATESLOG|||adminActivityLogPage|||adminActivityLogPage|||true|||Admin|||david" + "|||googleId|||david@email.com|||View Result|||/admin/adminActivityLogPage";
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View Result");
AssertHelper.assertLogMessageEquals(logMessage, generatedMessage);
______TS("Google login (Admin Masquerade Mode)");
url = Const.ActionURIs.INSTRUCTOR_COURSES_PAGE;
userType.isAdmin = true;
acc = AccountAttributes.builder().withGoogleId("anotherGoogleId").withName("david").withEmail("david@email.com").withInstitute("NUS").withIsInstructor(false).withDefaultStudentProfileAttributes("anotherGoogleId").build();
logMessage = "TEAMMATESLOG|||instructorCoursesPage|||instructorCoursesPage|||true|||Instructor(M)|||david" + "|||anotherGoogleId|||david@email.com|||View comments|||/page/instructorCoursesPage";
// masquerade: userType and account don't have the same google id
generatedMessage = logCenter.generatePageActionLogMessage(url, paramMap, userType, acc, null, "View comments");
AssertHelper.assertLogMessageEqualsInMasqueradeMode(logMessage, generatedMessage, userType.id);
}
use of teammates.common.datatransfer.UserType in project teammates by TEAMMATES.
the class PublicImageServlet method doPost.
@SuppressWarnings("unchecked")
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
String url = HttpRequestHelper.getRequestedUrl(req);
UserType userType = new GateKeeper().getCurrentUser();
Map<String, String[]> requestParameters = req.getParameterMap();
String blobKey = HttpRequestHelper.getValueFromParamMap(requestParameters, Const.ParamsNames.BLOB_KEY);
Assumption.assertPostParamNotNull(Const.ParamsNames.BLOB_KEY, blobKey);
try {
if (blobKey.isEmpty()) {
String message = "Failed to serve image with URL : blobKey is missing";
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
log.info(new LogMessageGenerator().generateBasicActivityLogMessage(url, params, message, userType));
resp.sendError(1, "No image found");
} else {
resp.setContentType("image/png");
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
blobstoreService.serve(new BlobKey(blobKey), resp);
// TODO : restrict image request to those "public" files only
String message = "Public image request with URL: <br>" + "<a href=\"" + url + "\" target=\"_blank\" rel=\"noopener noreferrer\" >" + url + "</a>";
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
log.info(new LogMessageGenerator().generateBasicActivityLogMessage(url, params, message, userType));
}
} catch (IOException ioe) {
Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, ioe, userType));
} catch (Exception e) {
log.severe("Exception occured while performing " + Const.PublicActionNames.PUBLIC_IMAGE_SERVE_ACTION + ": " + TeammatesException.toStringWithStackTrace(e));
}
}
Aggregations