Search in sources :

Example 11 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class UsersControllerTest method testSubmitCreateUserWithActivationLink.

@Test
public void testSubmitCreateUserWithActivationLink() {
    String username = "tom";
    String email = "tom@somewhere.com";
    String password = "PassWord1";
    ExtendedModelMap model = new ExtendedModelMap();
    Principal principal = () -> USER_NAME;
    User u = new User(1L, username, email, null, null, null, null);
    u.setSystemRole(Role.ROLE_USER);
    User pu = new User(USER_NAME, email, password, null, null, null);
    pu.setSystemRole(Role.ROLE_ADMIN);
    PasswordReset reset = new PasswordReset(u);
    when(userService.create(any(User.class))).thenReturn(u);
    when(userService.getUserByUsername(USER_NAME)).thenReturn(pu);
    when(passwordResetService.create(any(PasswordReset.class))).thenReturn(reset);
    String submitCreateUser = controller.submitCreateUser(u, u.getSystemRole().getName(), null, "checked", model, principal);
    assertEquals("redirect:/users/1", submitCreateUser);
    verify(userService).create(any(User.class));
    verify(userService, times(2)).getUserByUsername(USER_NAME);
    verify(passwordResetService).create(any(PasswordReset.class));
    verify(emailController).sendWelcomeEmail(eq(u), eq(pu), eq(reset));
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) DTUser(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser) User(ca.corefacility.bioinformatics.irida.model.user.User) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) Principal(java.security.Principal) Test(org.junit.Test)

Example 12 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetServiceImplIT method testEnsureOnlyOneResetPerUser.

@Test(expected = EntityNotFoundException.class)
@WithMockUser(username = "tester", roles = "ADMIN")
public void testEnsureOnlyOneResetPerUser() {
    PasswordReset pw1 = passwordResetService.create(pw());
    passwordResetService.create(pw());
    passwordResetService.read(pw1.getId());
}
Also used : PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 13 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetServiceImplTest method testCreatePasswordReset.

@Test
public void testCreatePasswordReset() {
    User user = new User();
    PasswordReset passwordReset = new PasswordReset(user);
    when(passwordResetRepository.findByUser(user)).thenReturn(null);
    when(passwordResetRepository.save(passwordReset)).thenReturn(passwordReset);
    passwordResetService.create(passwordReset);
    verify(passwordResetRepository).save(passwordReset);
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) Test(org.junit.Test)

Example 14 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetController method getResetPage.

/**
 * Get the password reset page
 *
 * @param resetId
 *            The ID of the {@link PasswordReset}
 * @param expired
 *            indicates whether we're showing the reset page because of an
 *            expired password or a reset request.
 * @param model
 *            A model for the page
 *
 * @return The string name of the page
 */
@RequestMapping(value = "/{resetId}", method = RequestMethod.GET)
public String getResetPage(@PathVariable String resetId, @RequestParam(required = false, defaultValue = "false") boolean expired, Model model) {
    setAuthentication();
    PasswordReset passwordReset = passwordResetService.read(resetId);
    User user = passwordReset.getUser();
    model.addAttribute("user", user);
    model.addAttribute("passwordReset", passwordReset);
    if (expired) {
        model.addAttribute("expired", true);
    }
    if (!model.containsAttribute("errors")) {
        model.addAttribute("errors", new HashMap<>());
    }
    return PASSWORD_RESET_PAGE;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with PasswordReset

use of ca.corefacility.bioinformatics.irida.model.user.PasswordReset in project irida by phac-nml.

the class PasswordResetController method sendNewPassword.

/**
 * Send the new password for a given password reset
 *
 * @param resetId
 *            The ID of the {@link PasswordReset}
 * @param password
 *            The new password to set
 * @param confirmPassword
 *            Confirm the new password
 * @param model
 *            A model for the given page
 * @param locale
 *            The locale of the request
 *
 * @return The string name of the success view, or on failure the
 *         getResetPage view
 */
@RequestMapping(value = "/{resetId}", method = RequestMethod.POST)
public String sendNewPassword(@PathVariable String resetId, @RequestParam String password, @RequestParam String confirmPassword, Model model, Locale locale) {
    setAuthentication();
    Map<String, String> errors = new HashMap<>();
    // read the reset to verify it exists first
    PasswordReset passwordReset = passwordResetService.read(resetId);
    User user = passwordReset.getUser();
    if (!password.equals(confirmPassword)) {
        errors.put("password", messageSource.getMessage("user.edit.password.match", null, locale));
    }
    if (errors.isEmpty()) {
        // Set the user's authentication to update the password and log them
        // in
        Authentication token = new UsernamePasswordAuthenticationToken(user, password, ImmutableList.of(user.getSystemRole()));
        SecurityContextHolder.getContext().setAuthentication(token);
        try {
            userService.changePassword(user.getId(), password);
        } catch (ConstraintViolationException ex) {
            Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
            for (ConstraintViolation<?> violation : constraintViolations) {
                logger.debug(violation.getMessage());
                String errorKey = violation.getPropertyPath().toString();
                errors.put(errorKey, violation.getMessage());
            }
        } catch (PasswordReusedException ex) {
            errors.put("password", messageSource.getMessage("user.edit.passwordReused", null, locale));
        }
    }
    if (!errors.isEmpty()) {
        model.addAttribute("errors", errors);
        return getResetPage(resetId, false, model);
    } else {
        passwordResetService.delete(resetId);
        String email = Base64.getEncoder().encodeToString(user.getEmail().getBytes());
        return SUCCESS_REDIRECT + email;
    }
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Set(java.util.Set) HashMap(java.util.HashMap) Authentication(org.springframework.security.core.Authentication) ConstraintViolation(javax.validation.ConstraintViolation) PasswordReset(ca.corefacility.bioinformatics.irida.model.user.PasswordReset) ConstraintViolationException(javax.validation.ConstraintViolationException) PasswordReusedException(ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PasswordReset (ca.corefacility.bioinformatics.irida.model.user.PasswordReset)15 User (ca.corefacility.bioinformatics.irida.model.user.User)11 Test (org.junit.Test)10 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)4 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 DTUser (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUser)2 ConstraintViolationException (javax.validation.ConstraintViolationException)2 CredentialsExpiredException (org.springframework.security.authentication.CredentialsExpiredException)2 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)1 PasswordReusedException (ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException)1 Principal (java.security.Principal)1 Locale (java.util.Locale)1 Set (java.util.Set)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ConstraintViolation (javax.validation.ConstraintViolation)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1