@Test public void shouldRejectUserIfUserExists() { PersistedUserValidator persistedUserValidator = new PersistedUserValidator(userService); when(userService.hasUser("admin")).thenReturn(true); List<String> errors = new ArrayList<>(); persistedUserValidator.validate(getExampleStartupForm(), errors, ConfigSource.FILE); assertTrue(errors.contains("server.error.user.exist")); }
@Override public void validate(StartupForm target, List<String> errors, ConfigSource configSource) { if (userService.hasActiveMotechAdmin()) { return; } if (StringUtils.isBlank(target.getAdminLogin())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_LOGIN)); } else if (userService.hasUser(target.getAdminLogin())) { errors.add("server.error.user.exist"); } else if (userService.hasEmail(target.getAdminEmail())) { errors.add("server.error.email.exist"); } if (StringUtils.isBlank(target.getAdminPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_PASSWORD)); } else if (StringUtils.isBlank(target.getAdminConfirmPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_CONFIRM_PASSWORD)); } else if (!target.getAdminPassword().equals(target.getAdminConfirmPassword())) { errors.add("server.error.invalid.password"); } if (!EmailValidator.getInstance().isValid(target.getAdminEmail())) { errors.add("server.error.invalid.email"); } }
PersistedUserValidator implements AbstractValidator { @Override public void validate(StartupForm target, List<String> errors, ConfigSource configSource) { if (userService.hasActiveMotechAdmin()) { return; } if (StringUtils.isBlank(target.getAdminLogin())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_LOGIN)); } else if (userService.hasUser(target.getAdminLogin())) { errors.add("server.error.user.exist"); } else if (userService.hasEmail(target.getAdminEmail())) { errors.add("server.error.email.exist"); } if (StringUtils.isBlank(target.getAdminPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_PASSWORD)); } else if (StringUtils.isBlank(target.getAdminConfirmPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_CONFIRM_PASSWORD)); } else if (!target.getAdminPassword().equals(target.getAdminConfirmPassword())) { errors.add("server.error.invalid.password"); } if (!EmailValidator.getInstance().isValid(target.getAdminEmail())) { errors.add("server.error.invalid.email"); } } }
PersistedUserValidator implements AbstractValidator { @Override public void validate(StartupForm target, List<String> errors, ConfigSource configSource) { if (userService.hasActiveMotechAdmin()) { return; } if (StringUtils.isBlank(target.getAdminLogin())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_LOGIN)); } else if (userService.hasUser(target.getAdminLogin())) { errors.add("server.error.user.exist"); } else if (userService.hasEmail(target.getAdminEmail())) { errors.add("server.error.email.exist"); } if (StringUtils.isBlank(target.getAdminPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_PASSWORD)); } else if (StringUtils.isBlank(target.getAdminConfirmPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_CONFIRM_PASSWORD)); } else if (!target.getAdminPassword().equals(target.getAdminConfirmPassword())) { errors.add("server.error.invalid.password"); } if (!EmailValidator.getInstance().isValid(target.getAdminEmail())) { errors.add("server.error.invalid.email"); } } PersistedUserValidator(MotechUserService userService); }
PersistedUserValidator implements AbstractValidator { @Override public void validate(StartupForm target, List<String> errors, ConfigSource configSource) { if (userService.hasActiveMotechAdmin()) { return; } if (StringUtils.isBlank(target.getAdminLogin())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_LOGIN)); } else if (userService.hasUser(target.getAdminLogin())) { errors.add("server.error.user.exist"); } else if (userService.hasEmail(target.getAdminEmail())) { errors.add("server.error.email.exist"); } if (StringUtils.isBlank(target.getAdminPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_PASSWORD)); } else if (StringUtils.isBlank(target.getAdminConfirmPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_CONFIRM_PASSWORD)); } else if (!target.getAdminPassword().equals(target.getAdminConfirmPassword())) { errors.add("server.error.invalid.password"); } if (!EmailValidator.getInstance().isValid(target.getAdminEmail())) { errors.add("server.error.invalid.email"); } } PersistedUserValidator(MotechUserService userService); @Override void validate(StartupForm target, List<String> errors, ConfigSource configSource); }
PersistedUserValidator implements AbstractValidator { @Override public void validate(StartupForm target, List<String> errors, ConfigSource configSource) { if (userService.hasActiveMotechAdmin()) { return; } if (StringUtils.isBlank(target.getAdminLogin())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_LOGIN)); } else if (userService.hasUser(target.getAdminLogin())) { errors.add("server.error.user.exist"); } else if (userService.hasEmail(target.getAdminEmail())) { errors.add("server.error.email.exist"); } if (StringUtils.isBlank(target.getAdminPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_PASSWORD)); } else if (StringUtils.isBlank(target.getAdminConfirmPassword())) { errors.add(String.format(ERROR_REQUIRED, ADMIN_CONFIRM_PASSWORD)); } else if (!target.getAdminPassword().equals(target.getAdminConfirmPassword())) { errors.add("server.error.invalid.password"); } if (!EmailValidator.getInstance().isValid(target.getAdminEmail())) { errors.add("server.error.invalid.email"); } } PersistedUserValidator(MotechUserService userService); @Override void validate(StartupForm target, List<String> errors, ConfigSource configSource);  }