@Test
  public void resetPasswordWithPasswordPolicy() throws IOException, MessagingException {
    keycloakRule.configure(
        new KeycloakRule.KeycloakSetup() {
          @Override
          public void config(
              RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
            appRealm.setPasswordPolicy(new PasswordPolicy("length"));
          }
        });

    loginPage.open();
    loginPage.resetPassword();

    resetPasswordPage.assertCurrent();

    resetPasswordPage.changePassword("login-test");

    resetPasswordPage.assertCurrent();

    Assert.assertEquals(
        "You should receive an email shortly with further instructions.",
        resetPasswordPage.getSuccessMessage());

    Assert.assertEquals(1, greenMail.getReceivedMessages().length);

    MimeMessage message = greenMail.getReceivedMessages()[0];

    String body = (String) message.getContent();
    String changePasswordUrl = MailUtil.getLink(body);

    String sessionId =
        events
            .expectRequiredAction("send_reset_password")
            .user(userId)
            .detail(Details.USERNAME, "login-test")
            .detail(Details.EMAIL, "*****@*****.**")
            .assertEvent()
            .getSessionId();

    driver.navigate().to(changePasswordUrl.trim());

    updatePasswordPage.assertCurrent();

    updatePasswordPage.changePassword("invalid", "invalid");

    Assert.assertEquals("Invalid password: minimum length 8", resetPasswordPage.getErrorMessage());

    updatePasswordPage.changePassword(
        "resetPasswordWithPasswordPolicy", "resetPasswordWithPasswordPolicy");

    events
        .expectRequiredAction("update_password")
        .user(userId)
        .session(sessionId)
        .detail(Details.USERNAME, "login-test")
        .assertEvent();

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

    Event loginEvent =
        events
            .expectLogin()
            .user(userId)
            .session(sessionId)
            .detail(Details.USERNAME, "login-test")
            .assertEvent();

    oauth.openLogout();

    events.expectLogout(loginEvent.getSessionId()).user(userId).session(sessionId).assertEvent();

    loginPage.open();

    loginPage.login("login-test", "resetPasswordWithPasswordPolicy");

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

    events.expectLogin().user(userId).detail(Details.USERNAME, "login-test").assertEvent();
  }
  private void resetPassword(String username) throws IOException, MessagingException {
    loginPage.open();
    loginPage.resetPassword();

    resetPasswordPage.assertCurrent();

    resetPasswordPage.changePassword(username);

    resetPasswordPage.assertCurrent();

    String sessionId =
        events
            .expectRequiredAction("send_reset_password")
            .user(userId)
            .detail(Details.USERNAME, username)
            .detail(Details.EMAIL, "*****@*****.**")
            .assertEvent()
            .getSessionId();

    Assert.assertEquals(
        "You should receive an email shortly with further instructions.",
        resetPasswordPage.getSuccessMessage());

    Assert.assertEquals(1, greenMail.getReceivedMessages().length);

    MimeMessage message = greenMail.getReceivedMessages()[0];

    String body = (String) message.getContent();
    String changePasswordUrl = MailUtil.getLink(body);

    driver.navigate().to(changePasswordUrl.trim());

    updatePasswordPage.assertCurrent();

    updatePasswordPage.changePassword("resetPassword", "resetPassword");

    events
        .expectRequiredAction("update_password")
        .user(userId)
        .session(sessionId)
        .detail(Details.USERNAME, username)
        .assertEvent();

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

    Event loginEvent =
        events
            .expectLogin()
            .user(userId)
            .session(sessionId)
            .detail(Details.USERNAME, username)
            .assertEvent();

    oauth.openLogout();

    events.expectLogout(loginEvent.getSessionId()).user(userId).session(sessionId).assertEvent();

    loginPage.open();

    loginPage.login("login-test", "resetPassword");

    events.expectLogin().user(userId).detail(Details.USERNAME, "login-test").assertEvent();

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