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