diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/auth/page/login/TermsAndConditions.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/auth/page/login/TermsAndConditions.java new file mode 100644 index 0000000000..a4c425eac2 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/auth/page/login/TermsAndConditions.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * 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.auth.page.login; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +/** + * @author Stian Thorgersen + */ +public class TermsAndConditions extends LoginActions { + + @FindBy(id = "kc-accept") + private WebElement acceptButton; + + @FindBy(id = "kc-decline") + private WebElement declineButton; + + @FindBy(id = "kc-terms-text") + private WebElement textElem; + + @Override + public boolean isCurrent() { + return driver.getTitle().equals("Terms and Conditions"); + } + + public void acceptTerms() { + acceptButton.click(); + } + public void declineTerms() { + declineButton.click(); + } + + public String getAcceptButtonText() { + return acceptButton.getAttribute("value"); + } + + public String getDeclineButtonText() { + return declineButton.getAttribute("value"); + } + + public String getText() { + return textElem.getText(); + } + +} diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractKeycloakTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractKeycloakTest.java index 1e5145cc79..bbab45015e 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractKeycloakTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AbstractKeycloakTest.java @@ -16,9 +16,9 @@ */ package org.keycloak.testsuite; -import java.io.File; import org.keycloak.testsuite.arquillian.TestContext; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; import javax.ws.rs.NotFoundException; @@ -32,11 +32,16 @@ import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.keycloak.admin.client.Keycloak; +import org.keycloak.admin.client.resource.AuthenticationManagementResource; import org.keycloak.admin.client.resource.RealmResource; import org.keycloak.admin.client.resource.RealmsResource; +import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.admin.client.resource.UsersResource; import org.keycloak.models.Constants; import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.representations.idm.RequiredActionProviderRepresentation; import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.testsuite.admin.ApiUtil; import org.keycloak.testsuite.arquillian.AuthServerTestEnricher; import org.keycloak.testsuite.arquillian.SuiteContext; import org.keycloak.testsuite.auth.page.WelcomePage; @@ -50,10 +55,8 @@ import static org.keycloak.testsuite.auth.page.AuthRealm.MASTER; import org.keycloak.testsuite.auth.page.account.Account; import org.keycloak.testsuite.auth.page.login.OIDCLogin; import org.keycloak.testsuite.auth.page.login.UpdatePassword; -import org.keycloak.testsuite.util.Timer; import org.keycloak.testsuite.util.WaitUtils; import static org.keycloak.testsuite.admin.Users.setPasswordFor; -import static org.keycloak.testsuite.admin.Users.setPasswordFor; /** * @@ -206,4 +209,63 @@ public abstract class AbstractKeycloakTest { return adminClient.realms(); } + public void createRealm(String realm) { + try { + RealmResource realmResource = adminClient.realms().realm(realm); + // Throws NotFoundException in case the realm does not exist! Ugly but there + // does not seem to be a way to this just by asking. + RealmRepresentation realmRepresentation = realmResource.toRepresentation(); + } catch (NotFoundException ex) { + RealmRepresentation realmRepresentation = new RealmRepresentation(); + realmRepresentation.setRealm(realm); + realmRepresentation.setEnabled(true); + realmRepresentation.setRegistrationAllowed(true); + adminClient.realms().create(realmRepresentation); + +// List requiredActions = adminClient.realm(realm).flows().getRequiredActions(); +// for (RequiredActionProviderRepresentation a : requiredActions) { +// a.setEnabled(false); +// a.setDefaultAction(false); +// adminClient.realm(realm).flows().updateRequiredAction(a.getAlias(), a); +// } + } + } + + public String createUser(String realm, String username, String password, String ... requiredActions) { + List requiredUserActions = Arrays.asList(requiredActions); + + UserRepresentation homer = new UserRepresentation(); + homer.setEnabled(true); + homer.setUsername(username); + homer.setRequiredActions(requiredUserActions); + + return ApiUtil.createUserAndResetPasswordWithAdminClient(adminClient.realm(realm), homer, password); + } + + public void setRequiredActionEnabled(String realm, String requiredAction, boolean enabled, boolean defaultAction) { + AuthenticationManagementResource managementResource = adminClient.realm(realm).flows(); + + RequiredActionProviderRepresentation action = managementResource.getRequiredAction(requiredAction); + action.setEnabled(enabled); + action.setDefaultAction(defaultAction); + + managementResource.updateRequiredAction(requiredAction, action); + } + + public void setRequiredActionEnabled(String realm, String userId, String requiredAction, boolean enabled) { + UsersResource usersResource = adminClient.realm(realm).users(); + + UserResource userResource = usersResource.get(userId); + UserRepresentation userRepresentation = userResource.toRepresentation(); + + List requiredActions = userRepresentation.getRequiredActions(); + if (enabled && !requiredActions.contains(requiredAction)) { + requiredActions.add(requiredAction); + } else if (!enabled && requiredActions.contains(requiredAction)) { + requiredActions.remove(requiredAction); + } + + userResource.update(userRepresentation); + } + } diff --git a/testsuite/integration-arquillian/tests/other/console/pom.xml b/testsuite/integration-arquillian/tests/other/console/pom.xml index b6818145ae..077655339f 100644 --- a/testsuite/integration-arquillian/tests/other/console/pom.xml +++ b/testsuite/integration-arquillian/tests/other/console/pom.xml @@ -30,5 +30,33 @@ integration-arquillian-tests-console Admin Console UI Tests - + + + + + + org.apache.maven.plugins + maven-resources-plugin + + + copy-theme-files + process-resources + + copy-resources + + + ${keycloak.home}/themes + + + src/main/resources/themes + + + + + + + + + + diff --git a/testsuite/integration-arquillian/tests/other/console/src/main/resources/themes/qe/login/messages/messages_en.properties b/testsuite/integration-arquillian/tests/other/console/src/main/resources/themes/qe/login/messages/messages_en.properties new file mode 100644 index 0000000000..b9060b4203 --- /dev/null +++ b/testsuite/integration-arquillian/tests/other/console/src/main/resources/themes/qe/login/messages/messages_en.properties @@ -0,0 +1,4 @@ +doAccept=Yes +doDecline=No + +termsText=

See QA for more information.

\ No newline at end of file diff --git a/testsuite/integration-arquillian/tests/other/console/src/main/resources/themes/qe/login/theme.properties b/testsuite/integration-arquillian/tests/other/console/src/main/resources/themes/qe/login/theme.properties new file mode 100644 index 0000000000..f1dbb7215d --- /dev/null +++ b/testsuite/integration-arquillian/tests/other/console/src/main/resources/themes/qe/login/theme.properties @@ -0,0 +1 @@ +parent=base \ No newline at end of file diff --git a/testsuite/integration-arquillian/tests/other/console/src/test/java/org/keycloak/testsuite/console/authentication/actions/TermsAndConditionsTest.java b/testsuite/integration-arquillian/tests/other/console/src/test/java/org/keycloak/testsuite/console/authentication/actions/TermsAndConditionsTest.java new file mode 100644 index 0000000000..f518784f99 --- /dev/null +++ b/testsuite/integration-arquillian/tests/other/console/src/test/java/org/keycloak/testsuite/console/authentication/actions/TermsAndConditionsTest.java @@ -0,0 +1,166 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2013 Red Hat, Inc. and/or its affiliates. + * + * 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.console.authentication.actions; + +import java.util.ArrayList; +import java.util.List; +import org.jboss.arquillian.graphene.page.Page; +import org.junit.Assert; +import org.junit.Test; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.representations.idm.CredentialRepresentation; +import org.keycloak.representations.idm.RealmRepresentation; + +import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.testsuite.auth.page.login.Registration; +import org.keycloak.testsuite.auth.page.login.TermsAndConditions; +import org.keycloak.testsuite.console.AbstractConsoleTest; +import org.keycloak.testsuite.console.page.authentication.RequiredActions; + +/** + * + */ +public class TermsAndConditionsTest extends AbstractConsoleTest { + + private static final String TERMS_TEXT = "Terms and conditions to be defined"; + + private static final String REALM = "TermsAndConditions"; + + private static final String BART = "Bart"; + + private static final String BART_PASS = "Ay caramba!"; + + private static final String HOMER = "Homer"; + + private static final String HOMER_PASS = "Mmm donuts."; + + @Page + private TermsAndConditions termsAndConditionsPage; + + @Page + private Registration registrationPage; + + @Override + public void beforeConsoleTest() { + // no operation - we don't need 'admin' user for this test. + } + + @Override + public void setDefaultPageUriParameters() { + super.setDefaultPageUriParameters(); + testRealmPage.setAuthRealm(REALM); + testRealmAdminConsolePage.setAdminRealm(REALM); + } + + @Override + public void addTestRealms(List testRealms) { + RealmRepresentation testRealmRep = new RealmRepresentation(); + testRealmRep.setRealm(REALM); + testRealmRep.setEnabled(true); + testRealms.add(testRealmRep); + } + + @Test + public void testExistingUser() { + // create user + String userId = createUser(REALM, HOMER, HOMER_PASS); + + // test t&c - log in and make sure t&c is not displayed + testRealmAdminConsolePage.navigateTo(); + testRealmLoginPage.form().login(HOMER, HOMER_PASS); + testRealmAdminConsolePage.logOut(); + + // enable terms + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, true, false); + setRequiredActionEnabled(REALM, userId, RequiredActions.TERMS_AND_CONDITIONS, true); + + // test t&c - log in and accept + testRealmLoginPage.form().login(HOMER, HOMER_PASS); + Assert.assertEquals(TERMS_TEXT, termsAndConditionsPage.getText()); + termsAndConditionsPage.declineTerms(); + + testRealmLoginPage.form().login(HOMER, HOMER_PASS); + Assert.assertEquals(TERMS_TEXT, termsAndConditionsPage.getText()); + + termsAndConditionsPage.acceptTerms(); + testRealmAdminConsolePage.logOut(); + + testRealmLoginPage.form().login(HOMER, HOMER_PASS); + testRealmAdminConsolePage.logOut(); + + // disable terms + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, false, false); + } + + @Test + public void testAdminCreatedUser() { + // enable terms + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, true, false); + + // create user + String userId = createUser(REALM, BART, BART_PASS); + setRequiredActionEnabled(REALM, userId, RequiredActions.TERMS_AND_CONDITIONS, true); + + // test t&c + testRealmAdminConsolePage.navigateTo(); + testRealmLoginPage.form().login(BART, BART_PASS); + Assert.assertTrue(termsAndConditionsPage.isCurrent()); + + // disable terms + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, false, false); + } + + @Test + public void testSelfRegisteredUser() { + // enable self-registration + RealmResource realmResource = adminClient.realm(REALM); + RealmRepresentation realmRepresentation = realmResource.toRepresentation(); + realmRepresentation.setRegistrationAllowed(true); + realmResource.update(realmRepresentation); + + // enable terms + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, true, true); + + // self-register + CredentialRepresentation mrBurnsPassword = new CredentialRepresentation(); + mrBurnsPassword.setType(CredentialRepresentation.PASSWORD); + mrBurnsPassword.setValue("Excellent."); + + List credentials = new ArrayList(); + credentials.add(mrBurnsPassword); + + UserRepresentation mrBurns = new UserRepresentation(); + mrBurns.setUsername("mrburns"); + mrBurns.setFirstName("Montgomery"); + mrBurns.setLastName("Burns"); + mrBurns.setEmail("mburns@keycloak.org"); + mrBurns.setCredentials(credentials); + + testRealmAdminConsolePage.navigateTo(); + testRealmLoginPage.form().register(); + + registrationPage.register(mrBurns); + + // test t&c + Assert.assertTrue(termsAndConditionsPage.isCurrent()); + + // disable terms + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, false, false); + } + +} \ No newline at end of file diff --git a/testsuite/integration-arquillian/tests/other/console/src/test/java/org/keycloak/testsuite/console/themes/TermsAndConditionsThemeTest.java b/testsuite/integration-arquillian/tests/other/console/src/test/java/org/keycloak/testsuite/console/themes/TermsAndConditionsThemeTest.java new file mode 100644 index 0000000000..69e0c8a4b7 --- /dev/null +++ b/testsuite/integration-arquillian/tests/other/console/src/test/java/org/keycloak/testsuite/console/themes/TermsAndConditionsThemeTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * 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.console.themes; + +import java.util.List; +import org.jboss.arquillian.graphene.page.Page; +import org.junit.Assert; +import org.junit.Test; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.testsuite.auth.page.login.TermsAndConditions; +import org.keycloak.testsuite.console.AbstractConsoleTest; +import org.keycloak.testsuite.console.page.authentication.RequiredActions; + +/** + * + */ +public class TermsAndConditionsThemeTest extends AbstractConsoleTest { + + private static final String REALM = "CustomLook"; + + private static final String HOMER = "Homer"; + + private static final String HOMER_PASS = "Mmm donuts."; + + @Page + private TermsAndConditions termsAndConditionsPage; + + @Override + public void setDefaultPageUriParameters() { + super.setDefaultPageUriParameters(); + testRealmPage.setAuthRealm(REALM); + testRealmAdminConsolePage.setAdminRealm(REALM); + } + + @Override + public void addTestRealms(List testRealms) { + RealmRepresentation testRealmRep = new RealmRepresentation(); + testRealmRep.setRealm(REALM); + testRealmRep.setEnabled(true); + testRealms.add(testRealmRep); + } + + @Override + public void beforeConsoleTest() { + // no operation - we don't need 'admin' user for this test. + } + + @Test + public void testTermsAndConditions() { + String userId = createUser(REALM, HOMER, HOMER_PASS); + setRequiredActionEnabled(REALM, RequiredActions.TERMS_AND_CONDITIONS, true, false); + setRequiredActionEnabled(REALM, userId, RequiredActions.TERMS_AND_CONDITIONS, true); + + RealmResource realmResource = adminClient.realm(REALM); + RealmRepresentation realmRepresentation = realmResource.toRepresentation(); + realmRepresentation.setLoginTheme("qe"); + realmResource.update(realmRepresentation); + + testRealmAdminConsolePage.navigateTo(); + testRealmLoginPage.form().login(HOMER, HOMER_PASS); + + Assert.assertTrue(termsAndConditionsPage.isCurrent()); + Assert.assertTrue(termsAndConditionsPage.getText().contains("See QA for more information.")); + Assert.assertEquals("Yes", termsAndConditionsPage.getAcceptButtonText()); + Assert.assertEquals("No", termsAndConditionsPage.getDeclineButtonText()); + } + +}