Search in sources :

Example 1 with UserAttributeUpdater

use of org.keycloak.testsuite.updaters.UserAttributeUpdater in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailInNewBrowserWhileLoggedInFirstBrowser.

@Test
public void verifyEmailInNewBrowserWhileLoggedInFirstBrowser() throws IOException, MessagingException {
    UserAttributeUpdater userAttributeUpdater = new UserAttributeUpdater(testRealm().users().get(testUserId));
    userAttributeUpdater.setEmailVerified(true).update();
    final String testRealmName = testRealm().toRepresentation().getRealm();
    accountPage.setAuthRealm(testRealmName);
    oauth.realm(testRealmName).clientId("account").redirectUri(getAuthServerRoot() + "realms/" + testRealmName + "/account");
    loginPage.open();
    loginPage.login("test-user@localhost", "password");
    accountPage.assertCurrent();
    userAttributeUpdater.setEmailVerified(false).setRequiredActions(RequiredAction.VERIFY_EMAIL).update();
    // this will result in email verification
    loginPage.open();
    verifyEmailPage.assertCurrent();
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getLastReceivedMessage();
    String verificationUrl = getPasswordResetEmailLink(message);
    // confirm in the second browser
    driver2.navigate().to(verificationUrl);
    // follow the link
    final WebElement proceedLink = driver2.findElement(By.linkText("» Click here to proceed"));
    assertThat(proceedLink, Matchers.notNullValue());
    proceedLink.click();
    // confirmation in the second browser
    assertThat(driver2.getPageSource(), Matchers.containsString("kc-info-message"));
    assertThat(driver2.getPageSource(), Matchers.containsString("Your email address has been verified."));
    final WebElement backToApplicationLink = driver2.findElement(By.linkText("« Back to Application"));
    assertThat(backToApplicationLink, Matchers.notNullValue());
    backToApplicationLink.click();
    // login page should be shown in the second browser
    assertThat(driver2.getPageSource(), Matchers.containsString("kc-login"));
    assertThat(driver2.getPageSource(), Matchers.containsString("Sign in"));
    // email should be verified and required actions empty
    UserRepresentation user = testRealm().users().get(testUserId).toRepresentation();
    Assert.assertTrue(user.isEmailVerified());
    Assert.assertThat(user.getRequiredActions(), Matchers.empty());
    // after refresh in the first browser the account console should be shown
    driver.navigate().refresh();
    accountPage.assertCurrent();
}
Also used : UserAttributeUpdater(org.keycloak.testsuite.updaters.UserAttributeUpdater) MimeMessage(javax.mail.internet.MimeMessage) WebElement(org.openqa.selenium.WebElement) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Example 2 with UserAttributeUpdater

use of org.keycloak.testsuite.updaters.UserAttributeUpdater in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailDuringAuthFlow.

@Test
public void verifyEmailDuringAuthFlow() throws IOException, MessagingException {
    try (Closeable u = new UserAttributeUpdater(testRealm().users().get(testUserId)).setEmailVerified(false).setRequiredActions(RequiredAction.VERIFY_EMAIL).update()) {
        accountPage.setAuthRealm(testRealm().toRepresentation().getRealm());
        accountPage.navigateTo();
        loginPage.assertCurrent();
        loginPage.login("test-user@localhost", "password");
        verifyEmailPage.assertCurrent();
        Assert.assertEquals(1, greenMail.getReceivedMessages().length);
        MimeMessage message = greenMail.getLastReceivedMessage();
        String verificationUrl = getPasswordResetEmailLink(message);
        driver.navigate().to(verificationUrl.trim());
        accountPage.assertCurrent();
    }
}
Also used : UserAttributeUpdater(org.keycloak.testsuite.updaters.UserAttributeUpdater) MimeMessage(javax.mail.internet.MimeMessage) Closeable(java.io.Closeable) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Example 3 with UserAttributeUpdater

use of org.keycloak.testsuite.updaters.UserAttributeUpdater in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailDuringAuthFlowAfterLogout.

@Test
public void verifyEmailDuringAuthFlowAfterLogout() throws IOException, MessagingException {
    try (Closeable u = new UserAttributeUpdater(testRealm().users().get(testUserId)).setEmailVerified(true).update()) {
        accountPage.setAuthRealm(testRealm().toRepresentation().getRealm());
        accountPage.navigateTo();
        loginPage.assertCurrent();
        loginPage.login("test-user@localhost", "password");
        accountPage.assertCurrent();
        driver.navigate().to(oauth.getLogoutUrl().redirectUri(accountPage.buildUri().toString()).build());
        loginPage.assertCurrent();
        verifyEmailDuringAuthFlow();
    }
}
Also used : UserAttributeUpdater(org.keycloak.testsuite.updaters.UserAttributeUpdater) Closeable(java.io.Closeable) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Example 4 with UserAttributeUpdater

use of org.keycloak.testsuite.updaters.UserAttributeUpdater in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailWhileLoggedIn.

@Test
public void verifyEmailWhileLoggedIn() throws IOException, MessagingException {
    UserAttributeUpdater userAttributeUpdater = new UserAttributeUpdater(testRealm().users().get(testUserId));
    userAttributeUpdater.setEmailVerified(true).update();
    final String testRealmName = testRealm().toRepresentation().getRealm();
    accountPage.setAuthRealm(testRealmName);
    oauth.realm(testRealmName).clientId("account").redirectUri(getAuthServerRoot() + "realms/" + testRealmName + "/account");
    loginPage.open();
    loginPage.login("test-user@localhost", "password");
    accountPage.assertCurrent();
    userAttributeUpdater.setEmailVerified(false).setRequiredActions(RequiredAction.VERIFY_EMAIL).update();
    // this will result in email verification
    loginPage.open();
    verifyEmailPage.assertCurrent();
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getLastReceivedMessage();
    String verificationUrl = getPasswordResetEmailLink(message);
    // confirm
    driver.navigate().to(verificationUrl);
    // back to account, already logged in
    accountPage.assertCurrent();
    // email should be verified and required actions empty
    UserRepresentation user = testRealm().users().get(testUserId).toRepresentation();
    Assert.assertTrue(user.isEmailVerified());
    Assert.assertThat(user.getRequiredActions(), Matchers.empty());
}
Also used : UserAttributeUpdater(org.keycloak.testsuite.updaters.UserAttributeUpdater) MimeMessage(javax.mail.internet.MimeMessage) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Example 5 with UserAttributeUpdater

use of org.keycloak.testsuite.updaters.UserAttributeUpdater in project keycloak by keycloak.

the class RequiredActionEmailVerificationTest method verifyEmailViaAuthSessionWhileLoggedIn.

@Test
public void verifyEmailViaAuthSessionWhileLoggedIn() throws IOException, MessagingException {
    Assume.assumeTrue("Works only on auth-server-undertow", AuthServerTestEnricher.AUTH_SERVER_CONTAINER.equals(AuthServerTestEnricher.AUTH_SERVER_CONTAINER_DEFAULT));
    UserAttributeUpdater userAttributeUpdater = new UserAttributeUpdater(testRealm().users().get(testUserId));
    userAttributeUpdater.setEmailVerified(false).update();
    final String testRealmName = testRealm().toRepresentation().getRealm();
    accountPage.setAuthRealm(testRealmName);
    oauth.realm(testRealmName).clientId("account").redirectUri(getAuthServerRoot() + "realms/" + testRealmName + "/account");
    loginPage.open();
    String authSessionId = AuthenticationSessionFailoverClusterTest.getAuthSessionCookieValue(driver);
    String realmId = testRealm().toRepresentation().getId();
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealm(realmId);
        RootAuthenticationSessionModel ras = session.authenticationSessions().getRootAuthenticationSession(realm, authSessionId);
        assertThat("Expecting single auth session", ras.getAuthenticationSessions().keySet(), Matchers.hasSize(1));
        ras.getAuthenticationSessions().forEach((id, as) -> as.addRequiredAction(RequiredAction.VERIFY_EMAIL));
    });
    loginPage.login("test-user@localhost", "password");
    verifyEmailPage.assertCurrent();
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getLastReceivedMessage();
    String verificationUrl = getPasswordResetEmailLink(message);
    // confirm
    driver.navigate().to(verificationUrl);
    // back to account, already logged in
    accountPage.assertCurrent();
    // email should be verified and required actions empty
    UserRepresentation user = testRealm().users().get(testUserId).toRepresentation();
    Assert.assertTrue(user.isEmailVerified());
    Assert.assertThat(user.getRequiredActions(), Matchers.empty());
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserAttributeUpdater(org.keycloak.testsuite.updaters.UserAttributeUpdater) MimeMessage(javax.mail.internet.MimeMessage) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest) AuthenticationSessionFailoverClusterTest(org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)

Aggregations

Test (org.junit.Test)11 UserAttributeUpdater (org.keycloak.testsuite.updaters.UserAttributeUpdater)11 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)9 AuthenticationSessionFailoverClusterTest (org.keycloak.testsuite.cluster.AuthenticationSessionFailoverClusterTest)9 MimeMessage (javax.mail.internet.MimeMessage)8 Closeable (java.io.Closeable)6 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)3 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)2 RoleScopeUpdater (org.keycloak.testsuite.updaters.RoleScopeUpdater)2 WebElement (org.openqa.selenium.WebElement)2 RealmModel (org.keycloak.models.RealmModel)1 RootAuthenticationSessionModel (org.keycloak.sessions.RootAuthenticationSessionModel)1