Skip to content

Commit

Permalink
KEYCLOAK-2906 Migrate forms package to new testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
ssilvert committed May 4, 2016
1 parent 5abe06a commit c392f66
Show file tree
Hide file tree
Showing 25 changed files with 953 additions and 810 deletions.
@@ -0,0 +1,38 @@
/*
* 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.drone;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jboss.arquillian.drone.api.annotation.Qualifier;

/**
* Taken from Drone example https://docs.jboss.org/author/display/ARQ/Drone
* This allows you to have more than one instance of a Drone WebDriver.
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Qualifier
public @interface Different {
}
Expand Up @@ -30,7 +30,7 @@
public class AppPage extends AbstractPage { public class AppPage extends AbstractPage {


public static final String AUTH_SERVER_URL = "http://localhost:8180/auth"; public static final String AUTH_SERVER_URL = "http://localhost:8180/auth";
public static final String baseUrl = "http://localhost:8180/auth/realms/master/app"; public static final String baseUrl = "http://localhost:8180/auth/realms/master/app/auth";


@FindBy(id = "account") @FindBy(id = "account")
private WebElement accountLink; private WebElement accountLink;
Expand Down
Expand Up @@ -21,6 +21,8 @@
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;


import java.util.List; import java.util.List;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.UserRepresentation;


import static org.keycloak.testsuite.admin.AbstractAdminTest.loadJson; import static org.keycloak.testsuite.admin.AbstractAdminTest.loadJson;


Expand All @@ -32,6 +34,36 @@
*/ */
public abstract class TestRealmKeycloakTest extends AbstractKeycloakTest { public abstract class TestRealmKeycloakTest extends AbstractKeycloakTest {


protected UserRepresentation findUserInRealmRep(RealmRepresentation testRealm, String userName) {
for (UserRepresentation user : testRealm.getUsers()) {
if (user.getUsername().equals(userName)) return user;
}

return null;
}

protected ClientRepresentation findClientInRealmRep(RealmRepresentation testRealm, String clientId) {
for (ClientRepresentation client : testRealm.getClients()) {
if (client.getClientId().equals(clientId)) return client;
}

return null;
}

protected RealmResource testRealm() {
return adminClient.realm("test");
}

protected UserRepresentation findUser(String userNameOrEmail) {
List<UserRepresentation> repList = testRealm().users().search(userNameOrEmail, -1, -1);
if (repList.size() != 1) throw new IllegalStateException("User search expected one result. Found " + repList.size() + " users.");
return repList.get(0);
}

protected void updateUser(UserRepresentation user) {
testRealm().users().get(user.getId()).update(user);
}

protected ClientRepresentation findTestApp(RealmRepresentation testRealm) { protected ClientRepresentation findTestApp(RealmRepresentation testRealm) {
for (ClientRepresentation client : testRealm.getClients()) { for (ClientRepresentation client : testRealm.getClients()) {
if (client.getClientId().equals("test-app")) return client; if (client.getClientId().equals("test-app")) return client;
Expand Down
Expand Up @@ -74,6 +74,8 @@ public void testClientAuthenticatorProviders() {
"Validates client based on signed JWT issued by client and signed with the Client private key"); "Validates client based on signed JWT issued by client and signed with the Client private key");
addProviderInfo(expected, "client-secret", "Client Id and Secret", "Validates client based on 'client_id' and " + addProviderInfo(expected, "client-secret", "Client Id and Secret", "Validates client based on 'client_id' and " +
"'client_secret' sent either in request parameters or in 'Authorization: Basic' header"); "'client_secret' sent either in request parameters or in 'Authorization: Basic' header");
addProviderInfo(expected, "testsuite-client-passthrough", "Testsuite Dummy Client Validation", "Testsuite dummy authenticator, " +
"which automatically authenticates hardcoded client (like 'test-app' )");


compareProviders(expected, result); compareProviders(expected, result);
} }
Expand Down Expand Up @@ -119,6 +121,10 @@ private List<Map<String, Object>> expectedAuthProviders() {
"Will also set it if execution is OPTIONAL and the OTP is currently configured for it."); "Will also set it if execution is OPTIONAL and the OTP is currently configured for it.");
addProviderInfo(result, "reset-password", "Reset Password", "Sets the Update Password required action if execution is REQUIRED. " + addProviderInfo(result, "reset-password", "Reset Password", "Sets the Update Password required action if execution is REQUIRED. " +
"Will also set it if execution is OPTIONAL and the password is currently configured for it."); "Will also set it if execution is OPTIONAL and the password is currently configured for it.");
addProviderInfo(result, "testsuite-dummy-passthrough", "Testsuite Dummy Pass Thru",
"Testsuite Dummy authenticator. Just passes through and is hardcoded to a specific user");
addProviderInfo(result, "testsuite-dummy-registration", "Testsuite Dummy Pass Thru",
"Testsuite Dummy authenticator. Just passes through and is hardcoded to a specific user");
return result; return result;
} }


Expand Down
@@ -0,0 +1,42 @@
/*
* 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.forms;

import org.keycloak.representations.idm.AuthenticationFlowRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.TestRealmKeycloakTest;

/**
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public abstract class AbstractFlowTest extends TestRealmKeycloakTest {

protected AuthenticationFlowRepresentation findFlowByAlias(String alias) {
for (AuthenticationFlowRepresentation rep : testRealm().flows().getFlows()) {
if (rep.getAlias().equals(alias)) return rep;
}

return null;
}

protected void setRegistrationFlow(AuthenticationFlowRepresentation flow) {
RealmRepresentation realm = testRealm().toRepresentation();
realm.setRegistrationFlow(flow.getAlias());
testRealm().update(realm);
}
}
74 changes: 29 additions & 45 deletions ...cloak/testsuite/forms/BruteForceTest.java → ...cloak/testsuite/forms/BruteForceTest.java 100755 → 100644
Expand Up @@ -18,94 +18,78 @@


import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.Errors; import org.keycloak.events.Errors;
import org.keycloak.models.Constants; import org.keycloak.models.Constants;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.utils.TimeBasedOTP; import org.keycloak.models.utils.TimeBasedOTP;
import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.OAuthClient;
import org.keycloak.testsuite.pages.AppPage; import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.AppPage.RequestType; import org.keycloak.testsuite.pages.AppPage.RequestType;
import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.pages.LoginTotpPage; import org.keycloak.testsuite.pages.LoginTotpPage;
import org.keycloak.testsuite.pages.RegisterPage; import org.keycloak.testsuite.pages.RegisterPage;
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.ws.rs.client.Client; import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Collections; import org.jboss.arquillian.graphene.page.Page;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.testsuite.TestRealmKeycloakTest;
import org.keycloak.testsuite.util.GreenMailRule;
import org.keycloak.testsuite.util.OAuthClient;


/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/ */
public class BruteForceTest { public class BruteForceTest extends TestRealmKeycloakTest {


@ClassRule @Override
public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakSetup() { public void configureTestRealm(RealmRepresentation testRealm) {
UserRepresentation user = findUserInRealmRep(testRealm, "test-user@localhost");
CredentialRepresentation credRep = new CredentialRepresentation();
credRep.setType(CredentialRepresentation.TOTP);
credRep.setValue("totpSecret");
user.getCredentials().add(credRep);
user.setTotp(Boolean.TRUE);


@Override testRealm.setBruteForceProtected(true);
public void config(RealmManager manager, RealmModel defaultRealm, RealmModel appRealm) { testRealm.setFailureFactor(2);
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);


UserCredentialModel credentials = new UserCredentialModel(); findClientInRealmRep(testRealm, "test-app").setDirectAccessGrantsEnabled(true);
credentials.setType(CredentialRepresentation.TOTP); }
credentials.setValue("totpSecret");
user.updateCredential(credentials);

user.setOtpEnabled(true);
appRealm.setEventsListeners(Collections.singleton("dummy"));

appRealm.setBruteForceProtected(true);
appRealm.setFailureFactor(2);


appRealm.getClientByClientId("test-app").setDirectAccessGrantsEnabled(true); @Before
} public void config() {


}); }


@Rule
public AssertEvents events = new AssertEvents(keycloakRule);


@Rule @Rule
public WebRule webRule = new WebRule(this); public AssertEvents events = new AssertEvents(this);


@Rule @Rule
public GreenMailRule greenMail = new GreenMailRule(); public GreenMailRule greenMail = new GreenMailRule();


@WebResource @Page
protected WebDriver driver;

@WebResource
protected AppPage appPage; protected AppPage appPage;


@WebResource @Page
protected LoginPage loginPage; protected LoginPage loginPage;


@WebResource @Page
private RegisterPage registerPage; private RegisterPage registerPage;


@WebResource @Page
protected LoginTotpPage loginTotpPage; protected LoginTotpPage loginTotpPage;


@WebResource
protected OAuthClient oauth;

private TimeBasedOTP totp = new TimeBasedOTP(); private TimeBasedOTP totp = new TimeBasedOTP();


private int lifespan; private int lifespan;
Expand Down

0 comments on commit c392f66

Please sign in to comment.