@Test public void testResetPassword() throws Exception { String username = "******"; User user = userManager.getUserByUsername(username); String token = userManager.generateRecoveryToken(user); String password = "******"; Wiser wiser = startWiser(getSmtpPort()); ResultActions update = mockMvc .perform( post("/updatePassword") .param("username", username) .param("token", token) .param("password", password)) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/")); wiser.stop(); assertTrue(wiser.getMessages().size() == 1); MvcResult result = update.andReturn(); MockHttpSession session = (MockHttpSession) result.getRequest().getSession(); assertNotNull(session.getAttribute(BaseFormController.MESSAGES_KEY)); assertNull(session.getAttribute(BaseFormController.ERRORS_MESSAGES_KEY)); }
@Test public void testSearch() throws Exception { // reindex before searching UserManager userManager = (UserManager) applicationContext.getBean("userManager"); userManager.reindex(); ModelAndView mav = c.handleRequest("admin"); Map m = mav.getModel(); List results = (List) m.get(Constants.USER_LIST); assertNotNull(results); assertTrue(results.size() >= 1); assertEquals("admin/userList", mav.getViewName()); }
@Test public void testShowResetPasswordForm() throws Exception { String username = "******"; User user = userManager.getUserByUsername(username); String token = userManager.generateRecoveryToken(user); ResultActions update = mockMvc .perform(get("/updatePassword").param("username", username).param("token", token)) .andExpect(status().isOk()) .andExpect(view().name("updatePasswordForm")); MvcResult result = update.andReturn(); MockHttpSession session = (MockHttpSession) result.getRequest().getSession(); assertNull(session.getAttribute(BaseFormController.ERRORS_MESSAGES_KEY)); }
@Test public void testSave() throws Exception { UserManager userManager = (UserManager) applicationContext.getBean("userManager"); User user = userManager.getUserByUsername("user"); user.setPassword("user"); user.setConfirmPassword("user"); action.setUser(user); action.setFrom("list"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("encryptPass", "true"); ServletActionContext.setRequest(request); assertEquals("input", action.save()); assertNotNull(action.getUser()); assertFalse(action.hasActionErrors()); }
@Test public void testSearch() throws Exception { // reindex before searching UserManager userManager = (UserManager) applicationContext.getBean("userManager"); userManager.reindex(); Map<String, Object> model = mockMvc .perform((get("/admin/users")).param("q", "admin")) .andExpect(status().isOk()) .andExpect(model().attributeExists(Constants.USER_LIST)) .andExpect(view().name("admin/userList")) .andReturn() .getModelAndView() .getModel(); List results = (List) model.get(Constants.USER_LIST); assertNotNull(results); assertTrue(results.size() >= 1); }
@Test public void testSaveConflictingUser() throws Exception { UserManager userManager = (UserManager) applicationContext.getBean("userManager"); User user = userManager.getUserByUsername("user"); user.setPassword("user"); user.setConfirmPassword("user"); // e-mail address from existing user User existingUser = (User) userManager.getUsers().get(0); user.setEmail(existingUser.getEmail()); action.setUser(user); action.setFrom("list"); Integer originalVersionNumber = user.getVersion(); log.debug("original version #: " + originalVersionNumber); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("encryptPass", "true"); ServletActionContext.setRequest(request); assertEquals("input", action.save()); assertNotNull(action.getUser()); assertEquals(originalVersionNumber, user.getVersion()); assertTrue(action.hasActionErrors()); }
@RequestMapping(method = RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest request) throws Exception { log.debug("entering 'handleRequest' method..."); String username = request.getParameter("username"); MessageSourceAccessor text = new MessageSourceAccessor(messageSource, request.getLocale()); // ensure that the username has been sent if (username == null) { log.warn("Username not specified, notifying user that it's a required field."); request.setAttribute( "error", text.getMessage("errors.required", text.getMessage("user.username"))); return new ModelAndView("login"); } log.debug("Processing Password Hint..."); // look up the user's information try { User user = userManager.getUserByUsername(username); StringBuffer msg = new StringBuffer(); msg.append("Your password hint is: ").append(user.getPasswordHint()); msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(request)); message.setTo(user.getEmail()); String subject = '[' + text.getMessage("webapp.name") + "] " + text.getMessage("user.passwordHint"); message.setSubject(subject); message.setText(msg.toString()); mailEngine.send(message); saveMessage( request, text.getMessage("login.passwordHint.sent", new Object[] {username, user.getEmail()})); } catch (UsernameNotFoundException e) { log.warn(e.getMessage()); saveError(request, text.getMessage("login.passwordHint.error", new Object[] {username})); } catch (MailException me) { log.warn(me.getMessage()); saveError(request, me.getCause().getLocalizedMessage()); } return new ModelAndView(new RedirectView(request.getContextPath())); }