Search in sources :

Example 1 with EntityNotFoundException

use of teammates.common.exception.EntityNotFoundException in project teammates by TEAMMATES.

the class StudentProfilePictureActionTest method testActionWithEmailAndCourseNoStudent.

private void testActionWithEmailAndCourseNoStudent() {
    ______TS("Failure case: student does not exist");
    String[] submissionParams = new String[] { Const.ParamsNames.STUDENT_EMAIL, StringHelper.encrypt("random-email"), Const.ParamsNames.COURSE_ID, StringHelper.encrypt(student.course) };
    StudentProfilePictureAction action = getAction(submissionParams);
    try {
        action.executeAndPostProcess();
        signalFailureToDetectException("Entity Does not exist");
    } catch (EntityNotFoundException enfe) {
        assertEquals("student with " + student.course + "/random-email", enfe.getMessage());
    }
}
Also used : StudentProfilePictureAction(teammates.ui.controller.StudentProfilePictureAction) EntityNotFoundException(teammates.common.exception.EntityNotFoundException)

Example 2 with EntityNotFoundException

use of teammates.common.exception.EntityNotFoundException in project teammates by TEAMMATES.

the class InstructorFeedbackPreviewAsInstructorActionTest method testExecuteAndPostProcess.

@Override
@Test
public void testExecuteAndPostProcess() {
    InstructorAttributes instructor1 = typicalBundle.instructors.get("instructor1OfCourse1");
    InstructorAttributes instructor2 = typicalBundle.instructors.get("instructor2OfCourse1");
    InstructorAttributes instructorHelper = typicalBundle.instructors.get("helperOfCourse1");
    String idOfInstructor1 = instructor1.googleId;
    String idOfInstructor2 = instructor2.googleId;
    String idOfInstructorHelper = instructorHelper.googleId;
    gaeSimulation.loginAsInstructor(idOfInstructor1);
    ______TS("typical success case");
    String feedbackSessionName = "First feedback session";
    String courseId = "idOfTypicalCourse1";
    String previewAsEmail = instructor2.email;
    String[] submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.FEEDBACK_SESSION_NAME, feedbackSessionName, Const.ParamsNames.PREVIEWAS, previewAsEmail };
    InstructorFeedbackPreviewAsInstructorAction paia = getAction(submissionParams);
    ShowPageResult showPageResult = getShowPageResult(paia);
    assertEquals(getPageResultDestination(Const.ViewURIs.INSTRUCTOR_FEEDBACK_SUBMISSION_EDIT, false, idOfInstructor1), showPageResult.getDestinationWithParams());
    assertEquals("", showPageResult.getStatusMessage());
    AssertHelper.assertLogMessageEquals("TEAMMATESLOG|||instructorFeedbackPreviewAsInstructor|||instructorFeedbackPreviewAsInstructor" + "|||true|||Instructor|||Instructor 1 of Course 1" + "|||" + idOfInstructor1 + "|||instr1@course1.tmt|||" + "Preview feedback session as instructor (" + instructor2.email + ")<br>" + "Session Name: First feedback session<br>Course ID: " + instructor1.courseId + "|||/page/instructorFeedbackPreviewAsInstructor", paia.getLogMessage());
    gaeSimulation.loginAsInstructor(idOfInstructor2);
    ______TS("typical success case");
    feedbackSessionName = "First feedback session";
    courseId = "idOfTypicalCourse1";
    previewAsEmail = instructor1.email;
    submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.FEEDBACK_SESSION_NAME, feedbackSessionName, Const.ParamsNames.PREVIEWAS, previewAsEmail };
    paia = getAction(submissionParams);
    showPageResult = getShowPageResult(paia);
    assertEquals(getPageResultDestination(Const.ViewURIs.INSTRUCTOR_FEEDBACK_SUBMISSION_EDIT, false, idOfInstructor2), showPageResult.getDestinationWithParams());
    assertEquals("", showPageResult.getStatusMessage());
    AssertHelper.assertLogMessageEquals("TEAMMATESLOG|||instructorFeedbackPreviewAsInstructor|||instructorFeedbackPreviewAsInstructor" + "|||true|||Instructor|||Instructor 2 of Course 1" + "|||" + idOfInstructor2 + "|||instr2@course1.tmt|||" + "Preview feedback session as instructor (" + instructor1.email + ")<br>" + "Session Name: First feedback session<br>Course ID: " + instructor1.courseId + "|||/page/instructorFeedbackPreviewAsInstructor", paia.getLogMessage());
    gaeSimulation.loginAsInstructor(idOfInstructorHelper);
    ______TS("failure: not enough privilege");
    feedbackSessionName = "First feedback session";
    courseId = "idOfTypicalCourse1";
    previewAsEmail = instructor2.email;
    submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.FEEDBACK_SESSION_NAME, feedbackSessionName, Const.ParamsNames.PREVIEWAS, previewAsEmail };
    try {
        paia = getAction(submissionParams);
        showPageResult = getShowPageResult(paia);
    } catch (UnauthorizedAccessException e) {
        assertEquals("Feedback session [First feedback session] is not accessible to instructor [" + instructorHelper.email + "] for privilege [canmodifysession]", e.getMessage());
    }
    gaeSimulation.loginAsInstructor(idOfInstructor1);
    ______TS("failure: non-existent previewas email");
    previewAsEmail = "non-existentEmail@course13212.tmt";
    submissionParams = new String[] { Const.ParamsNames.COURSE_ID, courseId, Const.ParamsNames.FEEDBACK_SESSION_NAME, feedbackSessionName, Const.ParamsNames.PREVIEWAS, previewAsEmail };
    try {
        paia = getAction(submissionParams);
        showPageResult = getShowPageResult(paia);
        signalFailureToDetectException();
    } catch (EntityNotFoundException enfe) {
        assertEquals("Instructor Email " + previewAsEmail + " does not exist in " + courseId + ".", enfe.getMessage());
    }
}
Also used : InstructorFeedbackPreviewAsInstructorAction(teammates.ui.controller.InstructorFeedbackPreviewAsInstructorAction) ShowPageResult(teammates.ui.controller.ShowPageResult) UnauthorizedAccessException(teammates.common.exception.UnauthorizedAccessException) EntityNotFoundException(teammates.common.exception.EntityNotFoundException) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes) Test(org.testng.annotations.Test)

Example 3 with EntityNotFoundException

use of teammates.common.exception.EntityNotFoundException in project teammates by TEAMMATES.

the class InstructorCourseRemindActionTest method executeAndAssertEntityNotFoundException.

private void executeAndAssertEntityNotFoundException(String instructorId, String[] submissionParams) {
    try {
        InstructorCourseRemindAction remindAction = getAction(addUserIdToParams(instructorId, submissionParams));
        remindAction.executeAndPostProcess();
        signalFailureToDetectException(" - EntityNotFoundException");
    } catch (EntityNotFoundException e) {
        ignoreExpectedException();
    }
}
Also used : InstructorCourseRemindAction(teammates.ui.controller.InstructorCourseRemindAction) EntityNotFoundException(teammates.common.exception.EntityNotFoundException)

Example 4 with EntityNotFoundException

use of teammates.common.exception.EntityNotFoundException in project teammates by TEAMMATES.

the class ControllerServlet method doPost.

@Override
// used as fallback
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public final void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    UserType userType = new GateKeeper().getCurrentUser();
    String url = HttpRequestHelper.getRequestedUrl(req);
    Map<String, String[]> params = HttpRequestHelper.getParameterMap(req);
    try {
        /* We are using the Template Method Design Pattern here.
             * This method contains the high level logic of the request processing.
             * Concrete details of the processing steps are to be implemented by child
             * classes, based on request-specific needs.
             */
        long startTime = System.currentTimeMillis();
        log.info("Request received : [" + req.getMethod() + "] " + req.getRequestURL().toString() + ":" + HttpRequestHelper.printRequestParameters(req));
        log.info("User agent : " + req.getHeader("User-Agent"));
        Action c = new ActionFactory().getAction(req);
        if (c.isValidUser()) {
            ActionResult actionResult = c.executeAndPostProcess();
            actionResult.writeSessionTokenToCookieIfRequired(req, resp);
            actionResult.send(req, resp);
        } else {
            resp.sendRedirect(c.getAuthenticationRedirectUrl());
        }
        long timeTaken = System.currentTimeMillis() - startTime;
        // This is the log message that is used to generate the 'activity log' for the admin.
        log.info(c.getLogMessage() + "|||" + timeTaken);
    } catch (PageNotFoundException e) {
        log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
        cleanUpStatusMessageInSession(req);
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ACTION_NOT_FOUND_PAGE, params, url));
    } catch (EntityNotFoundException e) {
        log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
        cleanUpStatusMessageInSession(req);
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ENTITY_NOT_FOUND_PAGE, params, url));
    } catch (FeedbackSessionNotVisibleException e) {
        log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
        cleanUpStatusMessageInSession(req);
        req.getSession().setAttribute(Const.ParamsNames.FEEDBACK_SESSION_NOT_VISIBLE, e.getStartTimeString());
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.FEEDBACK_SESSION_NOT_VISIBLE, params, url));
    } catch (InvalidOriginException e) {
        log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
        cleanUpStatusMessageInSession(req);
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.INVALID_ORIGIN, params, url));
    } catch (UnauthorizedAccessException e) {
        log.warning(new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, userType));
        cleanUpStatusMessageInSession(req);
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.UNAUTHORIZED, params, url));
    } catch (DeadlineExceededException | DatastoreTimeoutException e) {
        /*This exception may not be caught because GAE kills
              the request soon after throwing it. In that case, the error
              message in the log will be emailed to the admin by a separate
              cron job.*/
        cleanUpStatusMessageInSession(req);
        log.severe("Deadline exceeded exception caught by ControllerServlet : " + TeammatesException.toStringWithStackTrace(e));
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.DEADLINE_EXCEEDED_ERROR_PAGE, params, url));
    } catch (InvalidPostParametersException e) {
        String requestUrl = req.getRequestURL().toString();
        log.info(e.getMessage());
        cleanUpStatusMessageInSession(req);
        List<StatusMessage> statusMessagesToUser = new ArrayList<>();
        statusMessagesToUser.add(new StatusMessage(Const.StatusMessages.NULL_POST_PARAMETER_MESSAGE, StatusMessageColor.WARNING));
        req.getSession().setAttribute(Const.ParamsNames.STATUS_MESSAGES_LIST, statusMessagesToUser);
        if (requestUrl.contains("/instructor")) {
            resp.sendRedirect(Const.ActionURIs.INSTRUCTOR_HOME_PAGE);
        } else if (requestUrl.contains("/student")) {
            resp.sendRedirect(Const.ActionURIs.STUDENT_HOME_PAGE);
        } else if (requestUrl.contains("/admin")) {
            resp.sendRedirect(Const.ActionURIs.ADMIN_HOME_PAGE);
        } else {
            cleanUpStatusMessageInSession(req);
            resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ERROR_PAGE, params, url));
        }
    } catch (Throwable t) {
        /* Log only stack trace to prevent delay in termination of request
             * which can result in GAE shutting down the instance.
             * Note that severe logs are sent by email automatically in the cron job auto/compileLogs.
             */
        log.severe("Unexpected exception caught by ControllerServlet : " + TeammatesException.toStringWithStackTrace(t));
        cleanUpStatusMessageInSession(req);
        resp.sendRedirect(appendParamsToErrorPageUrl(Const.ViewURIs.ERROR_PAGE, params, url));
    }
}
Also used : InvalidPostParametersException(teammates.common.exception.InvalidPostParametersException) LogMessageGenerator(teammates.common.util.LogMessageGenerator) DeadlineExceededException(com.google.apphosting.api.DeadlineExceededException) EntityNotFoundException(teammates.common.exception.EntityNotFoundException) DatastoreTimeoutException(com.google.appengine.api.datastore.DatastoreTimeoutException) StatusMessage(teammates.common.util.StatusMessage) PageNotFoundException(teammates.common.exception.PageNotFoundException) FeedbackSessionNotVisibleException(teammates.common.exception.FeedbackSessionNotVisibleException) InvalidOriginException(teammates.common.exception.InvalidOriginException) UnauthorizedAccessException(teammates.common.exception.UnauthorizedAccessException) GateKeeper(teammates.logic.api.GateKeeper) ArrayList(java.util.ArrayList) List(java.util.List) UserType(teammates.common.datatransfer.UserType)

Example 5 with EntityNotFoundException

use of teammates.common.exception.EntityNotFoundException in project teammates by TEAMMATES.

the class StudentFeedbackSubmissionEditPageActionTest method testExecuteAndPostProcess.

@Override
@Test
public void testExecuteAndPostProcess() throws Exception {
    StudentAttributes student1InCourse1 = dataBundle.students.get("student1InCourse1");
    StudentAttributes unregStudent = StudentAttributes.builder("idOfTypicalCourse1", "Unreg Student", "unreg@stud.ent").withSection("1").withTeam("Team0.1").withComments("asdf").build();
    gaeSimulation.loginAsStudent(student1InCourse1.googleId);
    ______TS("not enough parameters");
    verifyAssumptionFailure();
    FeedbackSessionAttributes session1InCourse1 = dataBundle.feedbackSessions.get("session1InCourse1");
    String[] submissionParams = new String[] { Const.ParamsNames.FEEDBACK_SESSION_NAME, session1InCourse1.getFeedbackSessionName(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    verifyAssumptionFailure(submissionParams);
    submissionParams = new String[] { Const.ParamsNames.COURSE_ID, session1InCourse1.getCourseId(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    verifyAssumptionFailure(submissionParams);
    ______TS("Test null feedback session name parameter");
    submissionParams = new String[] { Const.ParamsNames.COURSE_ID, session1InCourse1.getCourseId(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    StudentFeedbackSubmissionEditPageAction pageAction;
    RedirectResult redirectResult;
    try {
        pageAction = getAction(submissionParams);
        redirectResult = getRedirectResult(pageAction);
        signalFailureToDetectException("Did not detect that parameters are null.");
    } catch (NullPostParameterException e) {
        assertEquals(String.format(Const.StatusCodes.NULL_POST_PARAMETER, Const.ParamsNames.FEEDBACK_SESSION_NAME), e.getMessage());
    }
    ______TS("Test null course id parameter");
    submissionParams = new String[] { Const.ParamsNames.FEEDBACK_SESSION_NAME, session1InCourse1.getFeedbackSessionName(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    try {
        pageAction = getAction(submissionParams);
        redirectResult = getRedirectResult(pageAction);
        signalFailureToDetectException("Did not detect that parameters are null.");
    } catch (NullPostParameterException e) {
        assertEquals(String.format(Const.StatusCodes.NULL_POST_PARAMETER, Const.ParamsNames.COURSE_ID), e.getMessage());
    }
    ______TS("typical success case for registered student");
    submissionParams = new String[] { Const.ParamsNames.COURSE_ID, session1InCourse1.getCourseId(), Const.ParamsNames.FEEDBACK_SESSION_NAME, session1InCourse1.getFeedbackSessionName(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    pageAction = getAction(submissionParams);
    ShowPageResult pageResult = getShowPageResult(pageAction);
    assertEquals(getPageResultDestination(Const.ViewURIs.STUDENT_FEEDBACK_SUBMISSION_EDIT, false, student1InCourse1.googleId), pageResult.getDestinationWithParams());
    assertFalse(pageResult.isError);
    assertEquals(Const.StatusMessages.FEEDBACK_SUBMISSIONS_CAN_SUBMIT_PARTIAL_ANSWER, pageResult.getStatusMessage());
    ______TS("feedbacksession deleted");
    FeedbackSessionsDb feedbackSessionsDb = new FeedbackSessionsDb();
    feedbackSessionsDb.deleteEntity(session1InCourse1);
    String[] params = new String[] { Const.ParamsNames.COURSE_ID, session1InCourse1.getCourseId(), Const.ParamsNames.FEEDBACK_SESSION_NAME, session1InCourse1.getFeedbackSessionName(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    pageAction = getAction(params);
    redirectResult = getRedirectResult(pageAction);
    assertEquals(getPageResultDestination(Const.ActionURIs.STUDENT_HOME_PAGE, false, "student1InCourse1"), redirectResult.getDestinationWithParams());
    assertFalse(redirectResult.isError);
    assertEquals(Const.StatusMessages.FEEDBACK_SESSION_DELETED_NO_ACCESS, redirectResult.getStatusMessage());
    // for unregistered student
    StudentsDb stDb = new StudentsDb();
    stDb.createStudentWithoutDocument(unregStudent);
    unregStudent = stDb.getStudentForEmail("idOfTypicalCourse1", "unreg@stud.ent");
    gaeSimulation.logoutUser();
    params = new String[] { Const.ParamsNames.COURSE_ID, session1InCourse1.getCourseId(), Const.ParamsNames.FEEDBACK_SESSION_NAME, session1InCourse1.getFeedbackSessionName(), Const.ParamsNames.REGKEY, StringHelper.encrypt(unregStudent.key), Const.ParamsNames.STUDENT_EMAIL, unregStudent.email };
    try {
        pageAction = getAction(params);
        AssertHelper.assertLogMessageEqualsForUnregisteredStudentUser("TEAMMATESLOG|||studentFeedbackSubmissionEditPage|||studentFeedbackSubmissionEditPage|||" + "true|||Unregistered:idOfTypicalCourse1|||Unreg Student|||Unknown|||unreg@stud.ent|||" + "Unknown|||/page/studentFeedbackSubmissionEditPage", pageAction.getLogMessage(), unregStudent.email, unregStudent.course);
        redirectResult = getRedirectResult(pageAction);
        signalFailureToDetectException("EntityDoesNotExist");
    } catch (EntityNotFoundException enfe) {
        assertEquals("unregistered student trying to access non-existent session", enfe.getMessage());
    }
    stDb.deleteStudent("idOfTypicalCourse1", "unreg@stud.ent");
    ______TS("typical success case for unregistered student");
    gaeSimulation.loginAsStudent(student1InCourse1.googleId);
    removeAndRestoreTypicalDataBundle();
    session1InCourse1 = dataBundle.feedbackSessions.get("session1InCourse1");
    params = new String[] { Const.ParamsNames.COURSE_ID, session1InCourse1.getCourseId(), Const.ParamsNames.FEEDBACK_SESSION_NAME, session1InCourse1.getFeedbackSessionName(), Const.ParamsNames.USER_ID, student1InCourse1.googleId };
    pageAction = getAction(params);
    pageResult = getShowPageResult(pageAction);
    assertEquals(getPageResultDestination(Const.ViewURIs.STUDENT_FEEDBACK_SUBMISSION_EDIT, false, student1InCourse1.googleId), pageResult.getDestinationWithParams());
    assertFalse(pageResult.isError);
    assertEquals(Const.StatusMessages.FEEDBACK_SUBMISSIONS_CAN_SUBMIT_PARTIAL_ANSWER, pageResult.getStatusMessage());
    ______TS("masquerade mode");
    gaeSimulation.loginAsAdmin("admin.user");
    pageAction = getAction(params);
    pageResult = getShowPageResult(pageAction);
    assertEquals(getPageResultDestination(Const.ViewURIs.STUDENT_FEEDBACK_SUBMISSION_EDIT, false, student1InCourse1.googleId), pageResult.getDestinationWithParams());
    assertFalse(pageResult.isError);
    assertEquals(Const.StatusMessages.FEEDBACK_SUBMISSIONS_CAN_SUBMIT_PARTIAL_ANSWER, pageResult.getStatusMessage());
    ______TS("student has not joined course");
    gaeSimulation.loginAsStudent(student1InCourse1.googleId);
    student1InCourse1.googleId = null;
    new StudentsDb().updateStudentWithoutSearchability(student1InCourse1.course, student1InCourse1.email, student1InCourse1.name, student1InCourse1.team, student1InCourse1.section, student1InCourse1.email, student1InCourse1.googleId, student1InCourse1.comments);
    pageAction = getAction(params);
    redirectResult = getRedirectResult(pageAction);
    assertEquals(getPageResultDestination(Const.ActionURIs.STUDENT_HOME_PAGE, true, "student1InCourse1"), redirectResult.getDestinationWithParams());
    assertTrue(redirectResult.isError);
    assertEquals("You are not registered in the course " + session1InCourse1.getCourseId(), redirectResult.getStatusMessage());
}
Also used : FeedbackSessionAttributes(teammates.common.datatransfer.attributes.FeedbackSessionAttributes) FeedbackSessionsDb(teammates.storage.api.FeedbackSessionsDb) NullPostParameterException(teammates.common.exception.NullPostParameterException) ShowPageResult(teammates.ui.controller.ShowPageResult) RedirectResult(teammates.ui.controller.RedirectResult) StudentFeedbackSubmissionEditPageAction(teammates.ui.controller.StudentFeedbackSubmissionEditPageAction) EntityNotFoundException(teammates.common.exception.EntityNotFoundException) StudentAttributes(teammates.common.datatransfer.attributes.StudentAttributes) StudentsDb(teammates.storage.api.StudentsDb) Test(org.testng.annotations.Test)

Aggregations

EntityNotFoundException (teammates.common.exception.EntityNotFoundException)9 Test (org.testng.annotations.Test)5 ShowPageResult (teammates.ui.controller.ShowPageResult)5 InstructorAttributes (teammates.common.datatransfer.attributes.InstructorAttributes)4 UnauthorizedAccessException (teammates.common.exception.UnauthorizedAccessException)4 StudentAttributes (teammates.common.datatransfer.attributes.StudentAttributes)3 DatastoreTimeoutException (com.google.appengine.api.datastore.DatastoreTimeoutException)1 DeadlineExceededException (com.google.apphosting.api.DeadlineExceededException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UserType (teammates.common.datatransfer.UserType)1 FeedbackSessionAttributes (teammates.common.datatransfer.attributes.FeedbackSessionAttributes)1 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)1 FeedbackSessionNotVisibleException (teammates.common.exception.FeedbackSessionNotVisibleException)1 InvalidOriginException (teammates.common.exception.InvalidOriginException)1 InvalidPostParametersException (teammates.common.exception.InvalidPostParametersException)1 NullPostParameterException (teammates.common.exception.NullPostParameterException)1 PageNotFoundException (teammates.common.exception.PageNotFoundException)1 LogMessageGenerator (teammates.common.util.LogMessageGenerator)1 StatusMessage (teammates.common.util.StatusMessage)1