@Test
  public void registerUserMissingOrInvalidEmail_emailAsUsername() {
    configureRelamRegistrationEmailAsUsername(true);

    try {
      loginPage.open();
      loginPage.clickRegister();
      registerPage.assertCurrent();

      registerPage.registerWithEmailAsUsername(
          "firstName", "lastName", null, "password", "password");
      registerPage.assertCurrent();
      assertEquals("Please specify email.", registerPage.getError());
      events
          .expectRegister(null, null)
          .removeDetail("username")
          .removeDetail("email")
          .error("invalid_registration")
          .assertEvent();

      registerPage.registerWithEmailAsUsername(
          "firstName", "lastName", "registerUserInvalidEmailemail", "password", "password");
      registerPage.assertCurrent();
      assertEquals("Invalid email address.", registerPage.getError());
      events
          .expectRegister("registerUserInvalidEmailemail", "registerUserInvalidEmailemail")
          .error("invalid_registration")
          .assertEvent();
    } finally {
      configureRelamRegistrationEmailAsUsername(false);
    }
  }
  @Test
  public void registerUserInvalidPasswordConfirm() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(
        "firstName",
        "lastName",
        "registerUserInvalidPasswordConfirm@email",
        "registerUserInvalidPasswordConfirm",
        "password",
        "invalid");

    registerPage.assertCurrent();
    assertEquals("Password confirmation doesn't match.", registerPage.getError());

    // assert form keeps form fields on error
    assertEquals("firstName", registerPage.getFirstName());
    assertEquals("lastName", registerPage.getLastName());
    assertEquals("registerUserInvalidPasswordConfirm@email", registerPage.getEmail());
    assertEquals("registerUserInvalidPasswordConfirm", registerPage.getUsername());
    assertEquals("", registerPage.getPassword());
    assertEquals("", registerPage.getPasswordConfirm());

    events
        .expectRegister(
            "registerUserInvalidPasswordConfirm", "registerUserInvalidPasswordConfirm@email")
        .removeDetail(Details.USERNAME)
        .removeDetail(Details.EMAIL)
        .user((String) null)
        .error("invalid_registration")
        .assertEvent();
  }
  @Test
  public void registerExistingUser() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(
        "firstName",
        "lastName",
        "registerExistingUser@email",
        "test-user@localhost",
        "password",
        "password");

    registerPage.assertCurrent();
    assertEquals("Username already exists.", registerPage.getError());

    // assert form keeps form fields on error
    assertEquals("firstName", registerPage.getFirstName());
    assertEquals("lastName", registerPage.getLastName());
    assertEquals("registerExistingUser@email", registerPage.getEmail());
    assertEquals("", registerPage.getUsername());
    assertEquals("", registerPage.getPassword());
    assertEquals("", registerPage.getPasswordConfirm());

    events
        .expectRegister("test-user@localhost", "registerExistingUser@email")
        .removeDetail(Details.EMAIL)
        .user((String) null)
        .error("username_in_use")
        .assertEvent();
  }
  @Test
  public void registerUserSuccess_emailAsUsername() {
    configureRelamRegistrationEmailAsUsername(true);

    try {
      loginPage.open();
      loginPage.clickRegister();
      registerPage.assertCurrent();

      registerPage.registerWithEmailAsUsername(
          "firstName", "lastName", "registerUserSuccessE@email", "password", "password");

      assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());

      String userId =
          events
              .expectRegister("registerUserSuccessE@email", "registerUserSuccessE@email")
              .assertEvent()
              .getUserId();
      events
          .expectLogin()
          .detail("username", "registerusersuccesse@email")
          .user(userId)
          .assertEvent();

      UserModel user = getUser(userId);
      Assert.assertNotNull(user);
      Assert.assertNotNull(user.getCreatedTimestamp());
      // test that timestamp is current with 10s tollerance
      Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000);

    } finally {
      configureRelamRegistrationEmailAsUsername(false);
    }
  }
  @Test
  public void registerUserManyErrors() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(null, null, null, null, null, null);

    registerPage.assertCurrent();

    assertEquals(
        "Please specify username.\n"
            + "Please specify first name.\n"
            + "Please specify last name.\n"
            + "Please specify email.\n"
            + "Please specify password.",
        registerPage.getError());

    events
        .expectRegister(null, "registerUserMissingUsername@email")
        .removeDetail(Details.USERNAME)
        .removeDetail(Details.EMAIL)
        .error("invalid_registration")
        .assertEvent();
  }
  @Test
  public void registerUserSuccess() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(
        "firstName",
        "lastName",
        "registerUserSuccess@email",
        "registerUserSuccess",
        "password",
        "password");

    assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());

    String userId =
        events
            .expectRegister("registerUserSuccess", "registerUserSuccess@email")
            .assertEvent()
            .getUserId();
    events.expectLogin().detail("username", "registerusersuccess").user(userId).assertEvent();

    UserModel user = getUser(userId);
    Assert.assertNotNull(user);
    Assert.assertNotNull(user.getCreatedTimestamp());
    // test that timestamp is current with 10s tollerance
    Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000);
    // test user info is set from form
    assertEquals("registerusersuccess", user.getUsername());
    assertEquals("registerusersuccess@email", user.getEmail());
    assertEquals("firstName", user.getFirstName());
    assertEquals("lastName", user.getLastName());
  }
  @Test
  public void registerUserMissingPassword() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(
        "firstName",
        "lastName",
        "registerUserMissingPassword@email",
        "registerUserMissingPassword",
        null,
        null);

    registerPage.assertCurrent();
    assertEquals("Please specify password.", registerPage.getError());

    events
        .expectRegister("registerUserMissingPassword", "registerUserMissingPassword@email")
        .removeDetail(Details.USERNAME)
        .removeDetail(Details.EMAIL)
        .user((String) null)
        .error("invalid_registration")
        .assertEvent();
  }
  @Test
  public void registerUserMissingEmail() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(
        "firstName", "lastName", null, "registerUserMissingEmail", "password", "password");
    registerPage.assertCurrent();
    assertEquals("Please specify email.", registerPage.getError());
    events
        .expectRegister("registerUserMissingEmail", null)
        .removeDetail("email")
        .error("invalid_registration")
        .assertEvent();
  }
Exemple #9
0
  @Test
  public void sessions() {
    loginPage.open();
    loginPage.clickRegister();

    registerPage.register(
        "view", "sessions", "view-sessions@localhost", "view-sessions", "password", "password");

    Event registerEvent =
        events.expectRegister("view-sessions", "view-sessions@localhost").assertEvent();
    String userId = registerEvent.getUserId();

    events.expectLogin().user(userId).detail(Details.USERNAME, "view-sessions").assertEvent();

    sessionsPage.open();

    Assert.assertTrue(sessionsPage.isCurrent());

    List<List<String>> sessions = sessionsPage.getSessions();
    Assert.assertEquals(1, sessions.size());
    Assert.assertEquals("127.0.0.1", sessions.get(0).get(0));

    // Create second session
    WebDriver driver2 = WebRule.createWebDriver();
    try {
      OAuthClient oauth2 = new OAuthClient(driver2);
      oauth2.state("mystate");
      oauth2.doLogin("view-sessions", "password");

      Event login2Event =
          events.expectLogin().user(userId).detail(Details.USERNAME, "view-sessions").assertEvent();

      sessionsPage.open();
      sessions = sessionsPage.getSessions();
      Assert.assertEquals(2, sessions.size());

      sessionsPage.logoutAll();

      events.expectLogout(registerEvent.getSessionId());
      events.expectLogout(login2Event.getSessionId());
    } finally {
      driver2.close();
    }
  }
  @Test
  public void registerUserInvalidEmail() {
    loginPage.open();
    loginPage.clickRegister();
    registerPage.assertCurrent();

    registerPage.register(
        "firstName",
        "lastName",
        "registerUserInvalidEmailemail",
        "registerUserInvalidEmail",
        "password",
        "password");
    registerPage.assertCurrent();
    assertEquals("registerUserInvalidEmailemail", registerPage.getEmail());
    assertEquals("Invalid email address.", registerPage.getError());
    events
        .expectRegister("registerUserInvalidEmail", "registerUserInvalidEmailemail")
        .error("invalid_registration")
        .assertEvent();
  }
  @Test
  public void registerExistingUser_emailAsUsername() {
    configureRelamRegistrationEmailAsUsername(true);

    try {
      loginPage.open();
      loginPage.clickRegister();
      registerPage.assertCurrent();

      registerPage.registerWithEmailAsUsername(
          "firstName", "lastName", "test-user@localhost", "password", "password");

      registerPage.assertCurrent();
      assertEquals("Email already exists.", registerPage.getError());

      events
          .expectRegister("test-user@localhost", "test-user@localhost")
          .user((String) null)
          .error("email_in_use")
          .assertEvent();
    } finally {
      configureRelamRegistrationEmailAsUsername(false);
    }
  }
  @Test
  public void registerPasswordPolicy() {
    keycloakRule.configure(
        new KeycloakRule.KeycloakSetup() {
          @Override
          public void config(
              RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
            appRealm.setPasswordPolicy(new PasswordPolicy("length"));
          }
        });

    try {
      loginPage.open();
      loginPage.clickRegister();
      registerPage.assertCurrent();

      registerPage.register(
          "firstName",
          "lastName",
          "registerPasswordPolicy@email",
          "registerPasswordPolicy",
          "pass",
          "pass");

      registerPage.assertCurrent();
      assertEquals("Invalid password: minimum length 8.", registerPage.getError());

      events
          .expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email")
          .removeDetail(Details.USERNAME)
          .removeDetail(Details.EMAIL)
          .user((String) null)
          .error("invalid_registration")
          .assertEvent();

      registerPage.register(
          "firstName",
          "lastName",
          "registerPasswordPolicy@email",
          "registerPasswordPolicy",
          "password",
          "password");
      assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());

      String userId =
          events
              .expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email")
              .assertEvent()
              .getUserId();

      events
          .expectLogin()
          .user(userId)
          .detail(Details.USERNAME, "registerpasswordpolicy")
          .assertEvent();
    } finally {
      keycloakRule.configure(
          new KeycloakRule.KeycloakSetup() {
            @Override
            public void config(
                RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
              appRealm.setPasswordPolicy(new PasswordPolicy(null));
            }
          });
    }
  }