Search in sources :

Example 6 with ResetViewData

use of org.motechproject.server.web.dto.ResetViewData in project motech by motech.

the class ResetControllerTest method testResetInvalidToken.

@Test
public void testResetInvalidToken() throws Exception {
    ResetViewData expected = getResetViewData(true, true, asList("server.reset.invalidToken"), getResetForm(TOKEN, PASSWORD, PASSWORD));
    doThrow(new InvalidTokenException()).when(recoveryService).resetPassword(TOKEN, PASSWORD, PASSWORD);
    controller.perform(post("/forgotreset").locale(Locale.ENGLISH).body(new ObjectMapper().writeValueAsBytes(getResetForm(TOKEN, PASSWORD, PASSWORD))).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(new ObjectMapper().writeValueAsString(expected)));
    verify(recoveryService).resetPassword(TOKEN, PASSWORD, PASSWORD);
}
Also used : InvalidTokenException(org.motechproject.security.exception.InvalidTokenException) ResetViewData(org.motechproject.server.web.dto.ResetViewData) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 7 with ResetViewData

use of org.motechproject.server.web.dto.ResetViewData in project motech by motech.

the class ResetController method getResetViewData.

@RequestMapping(value = "/forgotresetviewdata", method = RequestMethod.GET)
@ResponseBody
public ResetViewData getResetViewData(final HttpServletRequest request) {
    ResetViewData viewData = new ResetViewData();
    ResetForm form = new ResetForm();
    String token = request.getParameter("token");
    if (recoveryService.validateToken(token)) {
        form.setToken(token);
        viewData.setInvalidToken(false);
    } else {
        viewData.setInvalidToken(true);
    }
    viewData.setResetForm(form);
    viewData.setResetSucceed(false);
    viewData.setPageLang(cookieLocaleResolver.resolveLocale(request));
    return viewData;
}
Also used : ResetViewData(org.motechproject.server.web.dto.ResetViewData) ResetForm(org.motechproject.server.web.form.ResetForm) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with ResetViewData

use of org.motechproject.server.web.dto.ResetViewData in project motech by motech.

the class ResetController method reset.

@RequestMapping(value = "/forgotreset", method = RequestMethod.POST)
@ResponseBody
public ResetViewData reset(@RequestBody ResetForm form, final HttpServletRequest request) {
    ResetViewData viewData = new ResetViewData();
    viewData.setResetForm(form);
    viewData.setPageLang(cookieLocaleResolver.resolveLocale(request));
    viewData.setInvalidToken(false);
    List<String> errors = resetFormValidator.validate(form);
    if (!errors.isEmpty()) {
        viewData.setResetSucceed(false);
        viewData.setErrors(errors);
        return viewData;
    } else {
        try {
            recoveryService.resetPassword(form.getToken(), form.getPassword(), form.getPasswordConfirmation());
        } catch (InvalidTokenException e) {
            LOGGER.debug("Reset with invalid token attempted", e);
            errors.add("server.reset.invalidToken");
            viewData.setInvalidToken(true);
        } catch (RuntimeException e) {
            LOGGER.error("Error while reseting passsword", e);
            errors.add("server.reset.error");
        }
        viewData.setResetSucceed(true);
        viewData.setErrors(errors);
    }
    return viewData;
}
Also used : InvalidTokenException(org.motechproject.security.exception.InvalidTokenException) ResetViewData(org.motechproject.server.web.dto.ResetViewData) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ResetViewData (org.motechproject.server.web.dto.ResetViewData)8 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)5 Test (org.junit.Test)5 ResetForm (org.motechproject.server.web.form.ResetForm)3 InvalidTokenException (org.motechproject.security.exception.InvalidTokenException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2