private void loginSuccessAndLogout(String username, String password) {
   loginPage.open();
   loginPage.login(username, password);
   Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
   Assert.assertNotNull(oauth.getCurrentQuery().get(OAuth2Constants.CODE));
   oauth.openLogout();
 }
  @Test
  public void resetPasswordWithPasswordPolicy() throws IOException, MessagingException {
    keycloakRule.update(
        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(EventType.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(EventType.UPDATE_PASSWORD)
        .user(userId)
        .session(sessionId)
        .detail(Details.USERNAME, "login-test")
        .assertEvent();

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

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

    oauth.openLogout();

    events.expectLogout(sessionId).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(EventType.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(EventType.UPDATE_PASSWORD)
        .user(userId)
        .session(sessionId)
        .detail(Details.USERNAME, username)
        .assertEvent();

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

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

    oauth.openLogout();

    events.expectLogout(sessionId).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());
  }