PasswordHistory implements UserConstants { boolean updatePasswordHistory(@NotNull Tree userTree, @NotNull String password) throws ConstraintViolationException, AccessDeniedException { boolean updated = false; if (isEnabled) { checkPasswordInHistory(userTree, password); updated = shiftPasswordHistory(userTree); } return updated; } PasswordHistory(@NotNull ConfigurationParameters config);   }
@Test public void testDisabledPasswordHistory() throws Exception { User user = getTestUser(); Tree userTree = root.getTree(user.getPath()); List<ConfigurationParameters> configs = ImmutableList.of( ConfigurationParameters.EMPTY, ConfigurationParameters.of(PARAM_PASSWORD_HISTORY_SIZE, PASSWORD_HISTORY_DISABLED_SIZE), ConfigurationParameters.of(PARAM_PASSWORD_HISTORY_SIZE, -1), ConfigurationParameters.of(PARAM_PASSWORD_HISTORY_SIZE, Integer.MIN_VALUE) ); for (ConfigurationParameters config : configs) { PasswordHistory disabledHistory = new PasswordHistory(config); assertFalse(isEnabled(disabledHistory)); assertFalse(disabledHistory.updatePasswordHistory(userTree, user.getID())); } }
@Test(expected = ConstraintViolationException.class) public void testCheckPasswordHistory() throws Exception { Tree userTree = root.getTree(getTestUser().getPath()); PasswordHistory history = new PasswordHistory(CONFIG); assertTrue(isEnabled(history)); assertEquals(10, getMaxSize(history).longValue()); history.updatePasswordHistory(userTree, getTestUser().getID()); }
@Test public void testConfigurationChange() throws Exception { User user = getTestUser(); Tree userTree = root.getTree(user.getPath()); PasswordHistory history = new PasswordHistory(CONFIG); for (String pw : PASSWORDS) { assertTrue(history.updatePasswordHistory(userTree, pw)); } assertEquals(10, getHistory(user).size()); history = new PasswordHistory(ConfigurationParameters.of(PARAM_PASSWORD_HISTORY_SIZE, 5)); List<String> oldPwds = getHistory(user); assertEquals(10, oldPwds.size()); Iterables.skip(oldPwds, 6); history.updatePasswordHistory(userTree, oldPwds.iterator().next()); assertEquals(5, getHistory(user).size()); history = new PasswordHistory(CONFIG); history.updatePasswordHistory(userTree, "newPwd"); assertEquals(6, getHistory(user).size()); }
@Test public void testEnableDisable() throws Exception { User user = getTestUser(); Tree userTree = root.getTree(user.getPath()); PasswordHistory history = new PasswordHistory(CONFIG); for (String pw : PASSWORDS) { assertTrue(history.updatePasswordHistory(userTree, pw)); } assertEquals(10, getHistory(user).size()); history = new PasswordHistory(ConfigurationParameters.EMPTY); history.updatePasswordHistory(userTree, PASSWORDS[8]); assertEquals(10, getHistory(user).size()); }
@Test public void testSingleTypeHistoryProperty() throws Exception { Tree userTree = root.getTree(getTestUser().getPath()); Tree pwdNode = TreeUtil.getOrAddChild(userTree, REP_PWD, NT_REP_PASSWORD); pwdNode.setProperty(REP_PWD_HISTORY, "singleValuedProperty"); assertFalse(pwdNode.getProperty(REP_PWD_HISTORY).isArray()); assertFalse(pwdNode.getProperty(REP_PWD_HISTORY).getType().isArray()); PasswordHistory history = new PasswordHistory(CONFIG); assertTrue(history.updatePasswordHistory(userTree, "anyOtherPassword")); assertTrue(pwdNode.getProperty(REP_PWD_HISTORY).isArray()); assertTrue(pwdNode.getProperty(REP_PWD_HISTORY).getType().isArray()); }
@Test public void testUpdateMissingPwHash() throws Exception { User u = getUserManager(root).createUser("uid", null); PasswordHistory ph = new PasswordHistory(ConfigurationParameters.of(UserConstants.PARAM_PASSWORD_HISTORY_SIZE, UserConstants.PASSWORD_HISTORY_DISABLED_SIZE+1)); assertFalse(ph.updatePasswordHistory(root.getTree(u.getPath()), "pw")); }