public void testValidate() {
    String invalidLogin = "";
    String validLogin = TestUtils.randomString();

    String invalidPassword = "******";
    String validPassword = "******";

    String invalidEmail = "foobar";
    String validEmail = "*****@*****.**";

    String invalidPrefix = "Foo.";
    String validPrefix = "Sr.";

    // Test invalid values
    command.setLogin(invalidLogin);
    command.setEmail(invalidEmail);
    command.setPassword(invalidPassword);
    command.setPrefix(invalidPrefix);
    command.setFirstNames("testuser");
    command.setLastName("testuser");

    // We should get 4 errors (login, email, password, prefix)
    Object[] errors = command.validate();
    assertEquals(4, errors.length);

    // Test valid values
    command.setLogin(validLogin);
    command.setEmail(validEmail);
    command.setPassword(validPassword);
    command.setPrefix(validPrefix);

    errors = command.validate();
    assertEquals(0, errors.length);
  }
  public void testStore() {
    Org org = UserTestUtils.findNewOrg("testorg");

    String login = TestUtils.randomString();
    command.setLogin(login);
    command.setPassword("password");
    command.setEmail("*****@*****.**");
    command.setPrefix("Dr.");
    command.setFirstNames("Chuck Norris");
    command.setLastName("Texas Ranger");
    command.setOrg(org);
    command.setCompany("Test company");

    Object[] errors = command.validate();
    assertEquals(0, errors.length);

    command.storeNewUser();

    Long uid = command.getUser().getId();
    assertNotNull(uid);

    User result = UserFactory.lookupById(uid);
    assertEquals(login, result.getLogin());
    assertEquals(PageSizeDecorator.getDefaultPageSize(), result.getPageSize());
  }
  public void testLongNames() {
    int maxLogin = UserDefaults.get().getMaxUserLength();
    int maxPassword = UserDefaults.get().getMaxPasswordLength();
    int emailLength = UserDefaults.get().getMaxEmailLength();
    Config.get().setString(UserDefaults.MAX_USER_LENGTH, String.valueOf(5));
    Config.get().setString(UserDefaults.MAX_PASSWORD_LENGTH, String.valueOf(5));
    Config.get().setString(UserDefaults.MAX_EMAIL_LENGTH, String.valueOf(5));

    String invalidLogin = TestUtils.randomString();
    String invalidPassword = "******";
    String invalidEmail = "*****@*****.**";
    String validPrefix = "Sr.";

    // Test invalid values
    command.setLogin(invalidLogin);
    command.setEmail(invalidEmail);
    command.setPassword(invalidPassword);
    command.setPrefix(validPrefix);
    command.setFirstNames("testuser");
    command.setLastName("testuser");
    // We should get 4 errors (login, email, password, prefix)
    Object[] errors = command.validate();
    Config.get().setString(UserDefaults.MAX_USER_LENGTH, String.valueOf(maxLogin));
    Config.get().setString(UserDefaults.MAX_PASSWORD_LENGTH, String.valueOf(maxPassword));
    Config.get().setString(UserDefaults.MAX_EMAIL_LENGTH, String.valueOf(emailLength));
    assertEquals(3, errors.length);
  }
 private void validUsername(String username, CreateUserCommand cmd) {
   // ok now validate
   cmd.setLogin(username);
   Object[] errors = cmd.validate();
   assertNotNull(errors);
   assertEquals(username + " caused failure", 0, errors.length);
 }
 public void testValidatePasswordHasNewlineCharacter() throws Exception {
   command.setLogin("bilbo");
   command.setEmail("*****@*****.**");
   command.setPassword("aaaaa\nb");
   command.setPrefix("Hr.");
   ValidatorError[] errors = command.validate();
   assertEquals(1, errors.length);
 }
Exemple #6
0
  /**
   * Creates a new user
   *
   * @param loggedInUser The current user
   * @param desiredLogin The login for the new user
   * @param desiredPassword The password for the new user
   * @param firstName The first name of the new user
   * @param lastName The last name of the new user
   * @param email The email address for the new user
   * @param usePamAuth Should this user authenticate via PAM?
   * @return Returns 1 if successful (exception otherwise)
   * @throws FaultException A FaultException is thrown if the loggedInUser doesn't have permissions
   *     to create new users in thier org.
   * @xmlrpc.doc Create a new user.
   * @xmlrpc.param #param("string", "sessionKey")
   * @xmlrpc.param #param_desc("string", "desiredLogin", "Desired login name, will fail if already
   *     in use.")
   * @xmlrpc.param #param("string", "desiredPassword")
   * @xmlrpc.param #param("string", "firstName")
   * @xmlrpc.param #param("string", "lastName")
   * @xmlrpc.param #param_desc("string", "email", "User's e-mail address.")
   * @xmlrpc.param #param_desc("int", "usePamAuth", "1 if you wish to use PAM authentication for
   *     this user, 0 otherwise.")
   * @xmlrpc.returntype #return_int_success()
   */
  public int create(
      User loggedInUser,
      String desiredLogin,
      String desiredPassword,
      String firstName,
      String lastName,
      String email,
      Integer usePamAuth)
      throws FaultException {
    // Logged in user must be an org admin and we must be on a sat to do this.
    ensureOrgAdmin(loggedInUser);
    ensurePasswordOrPamAuth(usePamAuth, desiredPassword);

    boolean pamAuth = BooleanUtils.toBoolean(usePamAuth, new Integer(1), new Integer(0));

    if (pamAuth) {
      desiredPassword = getDefaultPasswordForPamAuth();
    }

    CreateUserCommand command = new CreateUserCommand();
    command.setUsePamAuthentication(pamAuth);
    command.setLogin(desiredLogin);
    command.setPassword(desiredPassword);
    command.setFirstNames(firstName);
    command.setLastName(lastName);
    command.setEmail(email);
    command.setOrg(loggedInUser.getOrg());
    command.setCompany(loggedInUser.getCompany());

    // Validate the user to be
    ValidatorError[] errors = command.validate();
    if (errors.length > 0) {
      StringBuilder errorString = new StringBuilder();
      LocalizationService ls = LocalizationService.getInstance();
      // Build a sane error message here
      for (int i = 0; i < errors.length; i++) {
        ValidatorError err = errors[i];
        errorString.append(ls.getMessage(err.getKey(), err.getValues()));
        if (i != errors.length - 1) {
          errorString.append(" :: ");
        }
      }
      // Throw a BadParameterException with our message string
      throw new BadParameterException(errorString.toString());
    }

    command.storeNewUser();
    return 1;
  }