From 29d683288256dd1b5ec52f342fef4d0d7aa28d5f Mon Sep 17 00:00:00 2001 From: Stan Silvert Date: Wed, 27 Apr 2016 14:09:46 -0400 Subject: [PATCH] KEYCLOAK-2905 Migrate actions package from old testsuite --- .../org/keycloak/testsuite/AssertEvents.java | 5 + .../testsuite/actions/ActionUtil.java | 64 ++++++ .../actions/DummyRequiredActionFactory.java | 0 .../RequiredActionEmailVerificationTest.java | 94 ++++----- .../RequiredActionMultipleActionsTest.java | 50 ++--- .../RequiredActionResetPasswordTest.java | 55 ++--- .../actions/RequiredActionTotpSetupTest.java | 195 +++++++----------- .../RequiredActionUpdateProfileTest.java | 49 ++--- .../actions/TermsAndConditionsTest.java | 57 ++--- .../keycloak/testsuite/util/RealmBuilder.java | 30 +++ .../keycloak/testsuite/util/UserBuilder.java | 10 +- ...cloak.authentication.RequiredActionFactory | 0 12 files changed, 283 insertions(+), 326 deletions(-) create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/ActionUtil.java rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/DummyRequiredActionFactory.java (100%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java (81%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java (71%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java (62%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java (65%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java (85%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java (74%) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/resources/META-INF/services/org.keycloak.authentication.RequiredActionFactory (100%) diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java index 69e4a4c25d..187cc375f7 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java @@ -77,6 +77,11 @@ public class AssertEvents implements TestRule { return event; } + public void assertEmpty() { + EventRepresentation event = fetchNextEvent(); + Assert.assertNull("Empty event queue expected, but there is " + event, event); + } + public void clear() { Response res = context.testingClient.testing().clearQueue(); try { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/ActionUtil.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/ActionUtil.java new file mode 100644 index 0000000000..3b984a7c0c --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/ActionUtil.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @author tags. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package org.keycloak.testsuite.actions; + +import java.util.LinkedList; +import java.util.List; +import org.keycloak.admin.client.Keycloak; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.representations.idm.RequiredActionProviderRepresentation; +import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.testsuite.util.UserBuilder; + +/** + * + * @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc. + */ +public class ActionUtil { + + public static UserRepresentation findUserInRealmRep(RealmRepresentation testRealm, String username) { + for (UserRepresentation rep : testRealm.getUsers()) { + if (rep.getUsername().equals(username)) return rep; + } + + return null; + } + + public static UserRepresentation findUserWithAdminClient(Keycloak adminClient, String username) { + return adminClient.realm("test").users().search(username, null, null, null, 0, 1).get(0); + } + + public static void addRequiredActionForUser(RealmRepresentation testRealm, String userName, String action) { + UserRepresentation user = findUserInRealmRep(testRealm, userName); + UserBuilder.edit(user).requiredAction(action); + } + + public static void addRequiredActionForRealm(RealmRepresentation testRealm, String providerId) { + List requiredActions = testRealm.getRequiredActions(); + if (requiredActions == null) requiredActions = new LinkedList(); + + RequiredActionProviderRepresentation action = new RequiredActionProviderRepresentation(); + action.setAlias(providerId); + action.setProviderId(providerId); + action.setEnabled(true); + action.setDefaultAction(true); + + requiredActions.add(action); + testRealm.setRequiredActions(requiredActions); + } +} diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/DummyRequiredActionFactory.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/DummyRequiredActionFactory.java similarity index 100% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/DummyRequiredActionFactory.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/DummyRequiredActionFactory.java diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java similarity index 81% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java index d2423a1965..7f14fb4dbe 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionEmailVerificationTest.java @@ -18,38 +18,31 @@ package org.keycloak.testsuite.actions; import org.junit.Assert; import org.junit.Before; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.keycloak.common.util.KeycloakUriBuilder; import org.keycloak.events.Details; import org.keycloak.events.Errors; -import org.keycloak.events.Event; import org.keycloak.events.EventType; -import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; -import org.keycloak.services.managers.RealmManager; import org.keycloak.testsuite.AssertEvents; -import org.keycloak.testsuite.MailUtil; -import org.keycloak.testsuite.OAuthClient; import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.InfoPage; import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.RegisterPage; import org.keycloak.testsuite.pages.VerifyEmailPage; -import org.keycloak.testsuite.rule.GreenMailRule; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; -import org.openqa.selenium.WebDriver; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.internet.MimeMessage; import java.io.IOException; +import org.jboss.arquillian.graphene.page.Page; +import org.keycloak.representations.idm.EventRepresentation; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.testsuite.TestRealmKeycloakTest; +import org.keycloak.testsuite.util.GreenMailRule; +import org.keycloak.testsuite.util.MailUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -57,55 +50,38 @@ import static org.junit.Assert.assertTrue; /** * @author Stian Thorgersen */ -public class RequiredActionEmailVerificationTest { - - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(); +public class RequiredActionEmailVerificationTest extends TestRealmKeycloakTest { @Rule - public AssertEvents events = new AssertEvents(keycloakRule); - - @Rule - public WebRule webRule = new WebRule(this); + public AssertEvents events = new AssertEvents(this); @Rule public GreenMailRule greenMail = new GreenMailRule(); - @WebResource - protected WebDriver driver; - - @WebResource - protected OAuthClient oauth; - - @WebResource + @Page protected AppPage appPage; - @WebResource + @Page protected LoginPage loginPage; - @WebResource + @Page protected VerifyEmailPage verifyEmailPage; - @WebResource + @Page protected RegisterPage registerPage; - @WebResource + @Page protected InfoPage infoPage; + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + testRealm.setVerifyEmail(Boolean.TRUE); + ActionUtil.findUserInRealmRep(testRealm, "test-user@localhost").setEmailVerified(Boolean.FALSE); + } + @Before public void before() { oauth.state("mystate"); // have to set this as keycloak validates that state is sent - keycloakRule.configure(new KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { - appRealm.setVerifyEmail(true); - - UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm); - user.setEmailVerified(false); - } - - }); } @Test @@ -118,11 +94,11 @@ public class RequiredActionEmailVerificationTest { Assert.assertEquals(1, greenMail.getReceivedMessages().length); MimeMessage message = greenMail.getReceivedMessages()[0]; - + String verificationUrl = getPasswordResetEmailLink(message); AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost"); - Event sendEvent = emailEvent.assertEvent(); + EventRepresentation sendEvent = emailEvent.assertEvent(); String sessionId = sendEvent.getSessionId(); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); @@ -152,7 +128,7 @@ public class RequiredActionEmailVerificationTest { MimeMessage message = greenMail.getReceivedMessages()[0]; - Event sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).user(userId).detail("username", "verifyemail").detail("email", "email@mail.com").assertEvent(); + EventRepresentation sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).user(userId).detail("username", "verifyemail").detail("email", "email@mail.com").assertEvent(); String sessionId = sendEvent.getSessionId(); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); @@ -177,7 +153,7 @@ public class RequiredActionEmailVerificationTest { Assert.assertEquals(1, greenMail.getReceivedMessages().length); - Event sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost").assertEvent(); + EventRepresentation sendEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost").assertEvent(); String sessionId = sendEvent.getSessionId(); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); @@ -215,7 +191,7 @@ public class RequiredActionEmailVerificationTest { String verificationUrl = getPasswordResetEmailLink(message); AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost"); - Event sendEvent = emailEvent.assertEvent(); + EventRepresentation sendEvent = emailEvent.assertEvent(); String sessionId = sendEvent.getSessionId(); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); @@ -247,7 +223,7 @@ public class RequiredActionEmailVerificationTest { String keyInsteadCodeURL = resendEmailLink.replace("code=", "key="); AssertEvents.ExpectedEvent emailEvent = events.expectRequiredAction(EventType.SEND_VERIFY_EMAIL).detail("email", "test-user@localhost"); - Event sendEvent = emailEvent.assertEvent(); + EventRepresentation sendEvent = emailEvent.assertEvent(); String sessionId = sendEvent.getSessionId(); String mailCodeId = sendEvent.getDetails().get(Details.CODE_ID); @@ -271,24 +247,24 @@ public class RequiredActionEmailVerificationTest { .detail(Details.CODE_ID, mailCodeId) .assertEvent(); } - + public static String getPasswordResetEmailLink(MimeMessage message) throws IOException, MessagingException { Multipart multipart = (Multipart) message.getContent(); - + final String textContentType = multipart.getBodyPart(0).getContentType(); - + assertEquals("text/plain; charset=UTF-8", textContentType); - + final String textBody = (String) multipart.getBodyPart(0).getContent(); - final String textChangePwdUrl = MailUtil.getLink(textBody); - + final String textChangePwdUrl = MailUtils.getLink(textBody); + final String htmlContentType = multipart.getBodyPart(1).getContentType(); - + assertEquals("text/html; charset=UTF-8", htmlContentType); - + final String htmlBody = (String) multipart.getBodyPart(1).getContent(); - final String htmlChangePwdUrl = MailUtil.getLink(htmlBody); - + final String htmlChangePwdUrl = MailUtils.getLink(htmlBody); + assertEquals(htmlChangePwdUrl, textChangePwdUrl); return htmlChangePwdUrl; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java similarity index 71% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java index 1b9d40d314..3c454b0602 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionMultipleActionsTest.java @@ -16,65 +16,43 @@ */ package org.keycloak.testsuite.actions; +import org.jboss.arquillian.graphene.page.Page; import org.junit.Assert; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.keycloak.events.Details; import org.keycloak.events.EventType; -import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; import org.keycloak.models.UserModel.RequiredAction; -import org.keycloak.services.managers.RealmManager; +import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.testsuite.AssertEvents; -import org.keycloak.testsuite.OAuthClient; +import org.keycloak.testsuite.TestRealmKeycloakTest; import org.keycloak.testsuite.pages.*; import org.keycloak.testsuite.pages.AppPage.RequestType; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; -import org.openqa.selenium.WebDriver; /** * @author Stian Thorgersen */ -public class RequiredActionMultipleActionsTest { +public class RequiredActionMultipleActionsTest extends TestRealmKeycloakTest { - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { - UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm); - user.addRequiredAction(RequiredAction.UPDATE_PROFILE); - user.addRequiredAction(RequiredAction.UPDATE_PASSWORD); - } - - }); + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", RequiredAction.UPDATE_PROFILE.name()); + ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", RequiredAction.UPDATE_PASSWORD.name()); + } @Rule - public WebRule webRule = new WebRule(this); + public AssertEvents events = new AssertEvents(this); - @Rule - public AssertEvents events = new AssertEvents(keycloakRule); - - @WebResource - protected WebDriver driver; - - @WebResource - protected OAuthClient oauth; - - @WebResource + @Page protected AppPage appPage; - @WebResource + @Page protected LoginPage loginPage; - @WebResource + @Page protected LoginPasswordUpdatePage changePasswordPage; - @WebResource + @Page protected LoginUpdateProfileEditUsernameAllowedPage updateProfilePage; @Test diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java similarity index 62% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java index 4caf2470c6..92f89fa636 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionResetPasswordTest.java @@ -16,70 +16,47 @@ */ package org.keycloak.testsuite.actions; +import org.jboss.arquillian.graphene.page.Page; import org.junit.Assert; import org.junit.Before; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; -import org.keycloak.events.Event; import org.keycloak.events.EventType; -import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; import org.keycloak.models.UserModel.RequiredAction; -import org.keycloak.services.managers.RealmManager; +import org.keycloak.representations.idm.EventRepresentation; +import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.testsuite.AssertEvents; -import org.keycloak.testsuite.OAuthClient; +import org.keycloak.testsuite.TestRealmKeycloakTest; import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPasswordUpdatePage; -import org.keycloak.testsuite.rule.GreenMailRule; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; -import org.openqa.selenium.WebDriver; +import org.keycloak.testsuite.util.GreenMailRule; /** * @author Stian Thorgersen */ -public class RequiredActionResetPasswordTest { +public class RequiredActionResetPasswordTest extends TestRealmKeycloakTest { - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { - appRealm.setResetPasswordAllowed(true); - - UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm); - user.addRequiredAction(RequiredAction.UPDATE_PASSWORD); - } - - }); + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + testRealm.setResetPasswordAllowed(Boolean.TRUE); + ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", RequiredAction.UPDATE_PASSWORD.name()); + } @Rule - public WebRule webRule = new WebRule(this); - - @Rule - public AssertEvents events = new AssertEvents(keycloakRule); + public AssertEvents events = new AssertEvents(this); @Rule public GreenMailRule greenMail = new GreenMailRule(); - @WebResource - protected WebDriver driver; - - @WebResource - protected OAuthClient oauth; - - @WebResource + @Page protected AppPage appPage; - @WebResource + @Page protected LoginPage loginPage; - @WebResource + @Page protected LoginPasswordUpdatePage changePasswordPage; @Before @@ -100,7 +77,7 @@ public class RequiredActionResetPasswordTest { Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); - Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); + EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent(); oauth.openLogout(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java similarity index 65% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java index bcb9b9db30..5501262088 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionTotpSetupTest.java @@ -16,24 +16,26 @@ */ package org.keycloak.testsuite.actions; +import java.util.LinkedList; +import java.util.List; +import org.jboss.arquillian.graphene.page.Page; import org.junit.Assert; -import org.junit.ClassRule; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.keycloak.events.Details; -import org.keycloak.events.Event; import org.keycloak.events.EventType; -import org.keycloak.models.OTPPolicy; -import org.keycloak.models.RealmModel; -import org.keycloak.models.RequiredActionProviderModel; +import org.keycloak.models.AuthenticationExecutionModel; import org.keycloak.models.UserCredentialModel; import org.keycloak.models.UserModel; import org.keycloak.models.utils.HmacOTP; import org.keycloak.models.utils.TimeBasedOTP; -import org.keycloak.representations.idm.CredentialRepresentation; -import org.keycloak.services.managers.RealmManager; +import org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation; +import org.keycloak.representations.idm.EventRepresentation; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.representations.idm.RequiredActionProviderRepresentation; import org.keycloak.testsuite.AssertEvents; -import org.keycloak.testsuite.OAuthClient; +import org.keycloak.testsuite.TestRealmKeycloakTest; import org.keycloak.testsuite.pages.AccountTotpPage; import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage.RequestType; @@ -41,64 +43,59 @@ import org.keycloak.testsuite.pages.LoginConfigTotpPage; import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginTotpPage; import org.keycloak.testsuite.pages.RegisterPage; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.KeycloakRule.KeycloakSetup; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; -import org.keycloak.utils.CredentialHelper; -import org.openqa.selenium.WebDriver; +import org.keycloak.testsuite.util.RealmBuilder; /** * @author Stian Thorgersen */ -public class RequiredActionTotpSetupTest { +public class RequiredActionTotpSetupTest extends TestRealmKeycloakTest { - private static OTPPolicy originalPolicy; + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + RequiredActionProviderRepresentation requiredAction = new RequiredActionProviderRepresentation(); + requiredAction.setAlias(UserModel.RequiredAction.CONFIGURE_TOTP.name()); + requiredAction.setProviderId(UserModel.RequiredAction.CONFIGURE_TOTP.name()); + requiredAction.setName("Configure Totp"); + requiredAction.setEnabled(true); + requiredAction.setDefaultAction(true); - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { + List requiredActions = new LinkedList<>(); + requiredActions.add(requiredAction); + testRealm.setRequiredActions(requiredActions); + testRealm.setResetPasswordAllowed(Boolean.TRUE); + } - @Override - public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { - CredentialHelper.setRequiredCredential(manager.getSession(), CredentialRepresentation.TOTP, appRealm); - //appRealm.addRequiredCredential(CredentialRepresentation.TOTP); - RequiredActionProviderModel requiredAction = appRealm.getRequiredActionProviderByAlias(UserModel.RequiredAction.CONFIGURE_TOTP.name()); - requiredAction.setDefaultAction(true); - appRealm.updateRequiredActionProvider(requiredAction); - appRealm.setResetPasswordAllowed(true); - originalPolicy = appRealm.getOTPPolicy(); + @Before + public void setOTPAuthRequired() { + for (AuthenticationExecutionInfoRepresentation execution : adminClient.realm("test").flows().getExecutions("browser")) { + String providerId = execution.getProviderId(); + if ("auth-otp-form".equals(providerId)) { + execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED.name()); + adminClient.realm("test").flows().updateExecutions("browser", execution); + } } + } - }); @Rule - public AssertEvents events = new AssertEvents(keycloakRule); + public AssertEvents events = new AssertEvents(this); - @Rule - public WebRule webRule = new WebRule(this); - - @WebResource - protected WebDriver driver; - - @WebResource + @Page protected AppPage appPage; - @WebResource + @Page protected LoginPage loginPage; - @WebResource + @Page protected LoginTotpPage loginTotpPage; - @WebResource + @Page protected LoginConfigTotpPage totpPage; - @WebResource + @Page protected AccountTotpPage accountTotpPage; - @WebResource - protected OAuthClient oauth; - - @WebResource + @Page protected RegisterPage registerPage; protected TimeBasedOTP totp = new TimeBasedOTP(); @@ -111,7 +108,7 @@ public class RequiredActionTotpSetupTest { String userId = events.expectRegister("setupTotp", "email@mail.com").assertEvent().getUserId(); - totpPage.assertCurrent(); + Assert.assertTrue(totpPage.isCurrent()); totpPage.configure(totp.generateTOTP(totpPage.getTotpSecret())); @@ -137,7 +134,7 @@ public class RequiredActionTotpSetupTest { Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); - Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); + EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent(); oauth.openLogout(); @@ -175,7 +172,7 @@ public class RequiredActionTotpSetupTest { events.expectRequiredAction(EventType.UPDATE_TOTP).user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent(); - Event loginEvent = events.expectLogin().user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent(); + EventRepresentation loginEvent = events.expectLogin().user(userId).detail(Details.USERNAME, "setuptotp2").assertEvent(); // Logout oauth.openLogout(); @@ -227,21 +224,15 @@ public class RequiredActionTotpSetupTest { @Test public void setupOtpPolicyChangedTotp8Digits() { // set policy to 8 digits - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - OTPPolicy newPolicy = new OTPPolicy(); - newPolicy.setLookAheadWindow(1); - newPolicy.setDigits(8); - newPolicy.setPeriod(30); - newPolicy.setType(UserCredentialModel.TOTP); - newPolicy.setAlgorithm(HmacOTP.HMAC_SHA1); - newPolicy.setInitialCounter(0); - appRealm.setOTPPolicy(newPolicy); - } - - }); + RealmRepresentation realmRep = adminClient.realm("test").toRepresentation(); + RealmBuilder.edit(realmRep) + .otpLookAheadWindow(1) + .otpDigits(8) + .otpPeriod(30) + .otpType(UserCredentialModel.TOTP) + .otpAlgorithm(HmacOTP.HMAC_SHA1) + .otpInitialCounter(0); + adminClient.realm("test").update(realmRep); loginPage.open(); @@ -258,7 +249,7 @@ public class RequiredActionTotpSetupTest { Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); - Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); + EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent(); oauth.openLogout(); @@ -274,35 +265,19 @@ public class RequiredActionTotpSetupTest { Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); events.expectLogin().assertEvent(); - - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - appRealm.setOTPPolicy(originalPolicy); - } - - }); - } @Test public void setupOtpPolicyChangedHotp() { - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - OTPPolicy newPolicy = new OTPPolicy(); - newPolicy.setLookAheadWindow(0); - newPolicy.setDigits(6); - newPolicy.setPeriod(30); - newPolicy.setType(UserCredentialModel.HOTP); - newPolicy.setAlgorithm(HmacOTP.HMAC_SHA1); - newPolicy.setInitialCounter(0); - appRealm.setOTPPolicy(newPolicy); - } - - }); + RealmRepresentation realmRep = adminClient.realm("test").toRepresentation(); + RealmBuilder.edit(realmRep) + .otpLookAheadWindow(0) + .otpDigits(6) + .otpPeriod(30) + .otpType(UserCredentialModel.HOTP) + .otpAlgorithm(HmacOTP.HMAC_SHA1) + .otpInitialCounter(0); + adminClient.realm("test").update(realmRep); loginPage.open(); @@ -319,7 +294,7 @@ public class RequiredActionTotpSetupTest { Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); - Event loginEvent = events.expectLogin().session(sessionId).assertEvent(); + EventRepresentation loginEvent = events.expectLogin().session(sessionId).assertEvent(); oauth.openLogout(); @@ -338,22 +313,15 @@ public class RequiredActionTotpSetupTest { events.expectLogout(null).session(AssertEvents.isUUID()).assertEvent(); // test lookAheadWindow - - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - OTPPolicy newPolicy = new OTPPolicy(); - newPolicy.setLookAheadWindow(5); - newPolicy.setDigits(6); - newPolicy.setPeriod(30); - newPolicy.setType(UserCredentialModel.HOTP); - newPolicy.setAlgorithm(HmacOTP.HMAC_SHA1); - newPolicy.setInitialCounter(0); - appRealm.setOTPPolicy(newPolicy); - } - - }); + realmRep = adminClient.realm("test").toRepresentation(); + RealmBuilder.edit(realmRep) + .otpLookAheadWindow(5) + .otpDigits(6) + .otpPeriod(30) + .otpType(UserCredentialModel.HOTP) + .otpAlgorithm(HmacOTP.HMAC_SHA1) + .otpInitialCounter(0); + adminClient.realm("test").update(realmRep); loginPage.open(); @@ -365,23 +333,6 @@ public class RequiredActionTotpSetupTest { Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); events.expectLogin().assertEvent(); - - - - - - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - appRealm.setOTPPolicy(originalPolicy); - } - - }); - - } - - } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java similarity index 85% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java index ad95dd013c..2181eeaffb 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/RequiredActionUpdateProfileTest.java @@ -16,60 +16,41 @@ */ package org.keycloak.testsuite.actions; +import org.jboss.arquillian.graphene.page.Page; import org.junit.*; import org.keycloak.events.Details; import org.keycloak.events.EventType; -import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; +import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; -import org.keycloak.services.managers.RealmManager; import org.keycloak.testsuite.AssertEvents; +import org.keycloak.testsuite.TestRealmKeycloakTest; import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginUpdateProfileEditUsernameAllowedPage; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; -import org.openqa.selenium.WebDriver; /** * @author Stian Thorgersen */ -public class RequiredActionUpdateProfileTest { - - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(); +public class RequiredActionUpdateProfileTest extends TestRealmKeycloakTest { @Rule - public WebRule webRule = new WebRule(this); + public AssertEvents events = new AssertEvents(this); - @Rule - public AssertEvents events = new AssertEvents(keycloakRule); - - @WebResource - protected WebDriver driver; - - @WebResource + @Page protected AppPage appPage; - @WebResource + @Page protected LoginPage loginPage; - @WebResource + @Page protected LoginUpdateProfileEditUsernameAllowedPage updateProfilePage; - @Before - public void before() { - keycloakRule.configure(new KeycloakRule.KeycloakSetup() { - @Override - public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { - UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm); - user.addRequiredAction(UserModel.RequiredAction.UPDATE_PROFILE); - UserModel anotherUser = manager.getSession().users().getUserByEmail("john-doh@localhost", appRealm); - anotherUser.addRequiredAction(UserModel.RequiredAction.UPDATE_PROFILE); - } - }); + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + ActionUtil.addRequiredActionForUser(testRealm, "test-user@localhost", UserModel.RequiredAction.UPDATE_PROFILE.name()); + ActionUtil.addRequiredActionForUser(testRealm, "john-doh@localhost", UserModel.RequiredAction.UPDATE_PROFILE.name()); } @Test @@ -90,7 +71,7 @@ public class RequiredActionUpdateProfileTest { events.expectLogin().session(sessionId).assertEvent(); // assert user is really updated in persistent store - UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost"); + UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost"); Assert.assertEquals("New first", user.getFirstName()); Assert.assertEquals("New last", user.getLastName()); Assert.assertEquals("new@email.com", user.getEmail()); @@ -103,7 +84,7 @@ public class RequiredActionUpdateProfileTest { loginPage.login("john-doh@localhost", "password"); - String userId = keycloakRule.getUser("test", "john-doh@localhost").getId(); + String userId = ActionUtil.findUserWithAdminClient(adminClient, "john-doh@localhost").getId(); updateProfilePage.assertCurrent(); @@ -124,7 +105,7 @@ public class RequiredActionUpdateProfileTest { events.expectLogin().detail(Details.USERNAME, "john-doh@localhost").user(userId).session(sessionId).assertEvent(); // assert user is really updated in persistent store - UserRepresentation user = keycloakRule.getUser("test", "new"); + UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "new"); Assert.assertEquals("New first", user.getFirstName()); Assert.assertEquals("New last", user.getLastName()); Assert.assertEquals("john-doh@localhost", user.getEmail()); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java similarity index 74% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java index 78996cb6d1..0647fcdd53 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/actions/TermsAndConditionsTest.java @@ -17,68 +17,55 @@ package org.keycloak.testsuite.actions; import org.junit.Assert; -import org.junit.Before; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.keycloak.authentication.requiredactions.TermsAndConditions; import org.keycloak.events.Details; import org.keycloak.events.Errors; import org.keycloak.events.EventType; -import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; import org.keycloak.representations.idm.UserRepresentation; -import org.keycloak.services.managers.RealmManager; import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.TermsAndConditionsPage; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; -import org.openqa.selenium.WebDriver; import java.util.List; import java.util.Map; +import org.jboss.arquillian.graphene.page.Page; +import org.junit.Before; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.testsuite.TestRealmKeycloakTest; +import org.keycloak.testsuite.util.UserBuilder; import static org.junit.Assert.*; /** * @author Stian Thorgersen */ -public class TermsAndConditionsTest { - - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(); +public class TermsAndConditionsTest extends TestRealmKeycloakTest { @Rule - public WebRule webRule = new WebRule(this); + public AssertEvents events = new AssertEvents(this); - @Rule - public AssertEvents events = new AssertEvents(keycloakRule); - - @WebResource - protected WebDriver driver; - - @WebResource + @Page protected AppPage appPage; - @WebResource + @Page protected LoginPage loginPage; - @WebResource + @Page protected TermsAndConditionsPage termsPage; + @Override + public void configureTestRealm(RealmRepresentation testRealm) { + } + @Before - public void before() { - keycloakRule.configure(new KeycloakRule.KeycloakSetup() { - @Override - public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { - UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm); - user.addRequiredAction(TermsAndConditions.PROVIDER_ID); - } - }); + public void addTermsAndConditionRequiredAction() { + UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost"); + UserBuilder.edit(user).requiredAction(TermsAndConditions.PROVIDER_ID); + adminClient.realm("test").users().get(user.getId()).update(user); } @Test @@ -87,7 +74,7 @@ public class TermsAndConditionsTest { loginPage.login("test-user@localhost", "password"); - termsPage.assertCurrent(); + Assert.assertTrue(termsPage.isCurrent()); termsPage.acceptTerms(); @@ -98,7 +85,7 @@ public class TermsAndConditionsTest { events.expectLogin().session(sessionId).assertEvent(); // assert user attribute is properly set - UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost"); + UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost"); Map> attributes = user.getAttributesAsListValues(); assertNotNull("timestamp for terms acceptance was not stored in user attributes", attributes); List termsAndConditions = attributes.get(TermsAndConditions.USER_ATTRIBUTE); @@ -121,7 +108,7 @@ public class TermsAndConditionsTest { loginPage.login("test-user@localhost", "password"); - termsPage.assertCurrent(); + Assert.assertTrue(termsPage.isCurrent()); termsPage.declineTerms(); @@ -132,7 +119,7 @@ public class TermsAndConditionsTest { // assert user attribute is properly removed - UserRepresentation user = keycloakRule.getUser("test", "test-user@localhost"); + UserRepresentation user = ActionUtil.findUserWithAdminClient(adminClient, "test-user@localhost"); Map> attributes = user.getAttributesAsListValues(); if (attributes != null) { assertNull("expected null for terms acceptance user attribute " + TermsAndConditions.USER_ATTRIBUTE, diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/RealmBuilder.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/RealmBuilder.java index d40a084ae2..0283b65710 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/RealmBuilder.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/RealmBuilder.java @@ -92,6 +92,36 @@ public class RealmBuilder { return this; } + public RealmBuilder otpLookAheadWindow(int i) { + rep.setOtpPolicyLookAheadWindow(i); + return this; + } + + public RealmBuilder otpDigits(int i) { + rep.setOtpPolicyDigits(i); + return this; + } + + public RealmBuilder otpPeriod(int i) { + rep.setOtpPolicyPeriod(i); + return this; + } + + public RealmBuilder otpType(String type) { + rep.setOtpPolicyType(type); + return this; + } + + public RealmBuilder otpAlgorithm(String algorithm) { + rep.setOtpPolicyAlgorithm(algorithm); + return this; + } + + public RealmBuilder otpInitialCounter(int i) { + rep.setOtpPolicyInitialCounter(i); + return this; + } + public RealmRepresentation build() { return rep; } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/UserBuilder.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/UserBuilder.java index 9c62c03fbf..26968337e8 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/UserBuilder.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/UserBuilder.java @@ -109,6 +109,14 @@ public class UserBuilder { return this; } + public UserBuilder requiredAction(String requiredAction) { + if (rep.getRequiredActions() == null) { + rep.setRequiredActions(new LinkedList()); + } + rep.getRequiredActions().add(requiredAction); + return this; + } + public UserBuilder serviceAccountId(String serviceAccountId) { rep.setServiceAccountClientId(serviceAccountId); return this; @@ -132,4 +140,4 @@ public class UserBuilder { return rep; } -} \ No newline at end of file +} diff --git a/testsuite/integration/src/test/resources/META-INF/services/org.keycloak.authentication.RequiredActionFactory b/testsuite/integration-arquillian/tests/base/src/test/resources/META-INF/services/org.keycloak.authentication.RequiredActionFactory similarity index 100% rename from testsuite/integration/src/test/resources/META-INF/services/org.keycloak.authentication.RequiredActionFactory rename to testsuite/integration-arquillian/tests/base/src/test/resources/META-INF/services/org.keycloak.authentication.RequiredActionFactory