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);
}
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());
}
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;
}
}
Aggregations