Search in sources :

Example 11 with RedirectAttributes

use of org.springframework.web.servlet.mvc.support.RedirectAttributes in project ORCID-Source by ORCID.

the class PasswordResetControllerTest method testPasswordResetLinkValidLinkDirectsToSecurityQuestionScreenWhenSecurityQuestionPresent.

@Test
public void testPasswordResetLinkValidLinkDirectsToSecurityQuestionScreenWhenSecurityQuestionPresent() throws Exception {
    HttpServletRequest servletRequest = mock(HttpServletRequest.class);
    RedirectAttributes redirectAttributes = mock(RedirectAttributes.class);
    when(encryptionManager.decryptForExternalUse(any(String.class))).thenReturn("email=any@orcid.org&issueDate=2070-05-29T17:04:27");
    when(orcidProfileManager.retrieveOrcidProfileByEmail(eq("any@orcid.org"), Matchers.<LoadOptions>any())).thenReturn(orcidWithSecurityQuestion());
    ModelAndView modelAndView = passwordResetController.resetPasswordEmail(servletRequest, "randomString", redirectAttributes);
    assertEquals("password_one_time_reset_optional_security_questions", modelAndView.getViewName());
    verify(redirectAttributes, never()).addFlashAttribute("passwordResetLinkExpired", true);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) ModelAndView(org.springframework.web.servlet.ModelAndView) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 12 with RedirectAttributes

use of org.springframework.web.servlet.mvc.support.RedirectAttributes in project ORCID-Source by ORCID.

the class PasswordResetControllerTest method testSubmitConsolidatedPasswordReset.

@Test
public void testSubmitConsolidatedPasswordReset() throws Exception {
    RedirectAttributes redirectAttributes = mock(RedirectAttributes.class);
    BindingResult bindingResult = mock(BindingResult.class);
    OneTimeResetPasswordForm oneTimeResetPasswordForm = new OneTimeResetPasswordForm();
    oneTimeResetPasswordForm.setEncryptedEmail("encrypted string not expired");
    MockHttpSession session = new MockHttpSession();
    when(servletRequest.getSession()).thenReturn(session);
    when(encryptionManager.decryptForExternalUse(any(String.class))).thenReturn("email=any@orcid.org&issueDate=2070-05-29T17:04:27");
    when(bindingResult.hasErrors()).thenReturn(true);
    when(mockEmailManagerReadOnly.findOrcidIdByEmail("any@orcid.org")).thenReturn("0000-0000-0000-0000");
    oneTimeResetPasswordForm = passwordResetController.submitPasswordReset(servletRequest, servletResponse, oneTimeResetPasswordForm);
    assertFalse(oneTimeResetPasswordForm.getErrors().isEmpty());
    oneTimeResetPasswordForm.setPassword("Password#123");
    when(bindingResult.hasErrors()).thenReturn(false);
    when(orcidProfileManager.retrieveOrcidProfileByEmail(eq("any@orcid.org"), Matchers.<LoadOptions>any())).thenReturn(orcidWithSecurityQuestion());
    oneTimeResetPasswordForm = passwordResetController.submitPasswordReset(servletRequest, servletResponse, oneTimeResetPasswordForm);
    assertTrue(oneTimeResetPasswordForm.getSuccessRedirectLocation().equals("https://testserver.orcid.org/my-orcid") || oneTimeResetPasswordForm.getSuccessRedirectLocation().equals("https://localhost:8443/orcid-web/my-orcid"));
    verify(redirectAttributes, never()).addFlashAttribute("passwordResetLinkExpired", true);
    when(encryptionManager.decryptForExternalUse(any(String.class))).thenReturn("email=any@orcid.org&issueDate=1970-05-29T17:04:27");
    oneTimeResetPasswordForm = passwordResetController.submitPasswordReset(servletRequest, servletResponse, oneTimeResetPasswordForm);
    assertFalse(oneTimeResetPasswordForm.getErrors().isEmpty());
}
Also used : RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) BindingResult(org.springframework.validation.BindingResult) MockHttpSession(org.springframework.mock.web.MockHttpSession) OneTimeResetPasswordForm(org.orcid.frontend.web.forms.OneTimeResetPasswordForm) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 13 with RedirectAttributes

use of org.springframework.web.servlet.mvc.support.RedirectAttributes in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolver method doResolveHandlerMethodException.

/**
 * Find an {@code @ExceptionHandler} method and invoke it to handle the raised exception.
 */
@Override
@Nullable
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {
    ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
        return null;
    }
    if (this.argumentResolvers != null) {
        exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
    }
    if (this.returnValueHandlers != null) {
        exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
    }
    ServletWebRequest webRequest = new ServletWebRequest(request, response);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    ArrayList<Throwable> exceptions = new ArrayList<>();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Using @ExceptionHandler " + exceptionHandlerMethod);
        }
        // Expose causes as provided arguments as well
        Throwable exToExpose = exception;
        while (exToExpose != null) {
            exceptions.add(exToExpose);
            Throwable cause = exToExpose.getCause();
            exToExpose = (cause != exToExpose ? cause : null);
        }
        Object[] arguments = new Object[exceptions.size() + 1];
        // efficient arraycopy call in ArrayList
        exceptions.toArray(arguments);
        arguments[arguments.length - 1] = handlerMethod;
        exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);
    } catch (Throwable invocationEx) {
        // probably an accident (e.g. failed assertion or the like).
        if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
            logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
        }
        // Continue with default processing of the original exception...
        return null;
    }
    if (mavContainer.isRequestHandled()) {
        return new ModelAndView();
    } else {
        ModelMap model = mavContainer.getModel();
        HttpStatus status = mavContainer.getStatus();
        ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
        mav.setViewName(mavContainer.getViewName());
        if (!mavContainer.isViewReference()) {
            mav.setView((View) mavContainer.getView());
        }
        if (model instanceof RedirectAttributes) {
            Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
            RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
        }
        return mav;
    }
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelMap(org.springframework.ui.ModelMap) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Nullable(org.springframework.lang.Nullable)

Aggregations

RedirectAttributes (org.springframework.web.servlet.mvc.support.RedirectAttributes)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 BindingResult (org.springframework.validation.BindingResult)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 Model (org.springframework.ui.Model)5 ModelAndView (org.springframework.web.servlet.ModelAndView)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 IOException (java.io.IOException)3 java.util (java.util)3 Valid (javax.validation.Valid)3 Test (org.junit.Test)3 DBUnitTest (org.orcid.test.DBUnitTest)3 Controller (org.springframework.stereotype.Controller)3 FileNotFoundException (java.io.FileNotFoundException)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2