KEYCLOAK-14889 Create test for clientStorageProviderTimeout

This commit is contained in:
vramik 2020-07-27 19:25:55 +02:00 committed by Pedro Igor
parent cd76ed0d74
commit 7f979ffbcf
3 changed files with 57 additions and 3 deletions

View file

@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jboss.logging.Logger;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -79,6 +80,11 @@ public class HardcodedClientStorageProvider implements ClientStorageProvider, Cl
@Override
public Stream<ClientModel> searchClientsByClientIdStream(RealmModel realm, String clientId, Integer firstResult, Integer maxResults) {
if (Boolean.parseBoolean(component.getConfig().getFirst(HardcodedClientStorageProviderFactory.DELAYED_SEARCH))) try {
Thread.sleep(5000l);
} catch (InterruptedException ex) {
Logger.getLogger(HardcodedClientStorageProvider.class).warn(ex.getCause());
}
if (clientId != null && this.clientId.toLowerCase().contains(clientId.toLowerCase())) {
return Stream.of(new ClientAdapter(realm));
}

View file

@ -49,6 +49,7 @@ public class HardcodedClientStorageProviderFactory implements ClientStorageProvi
public static final String REDIRECT_URI = "redirect_uri";
public static final String CONSENT = "consent";
public static final String DELAYED_SEARCH = "delayed_search";
static {
CONFIG_PROPERTIES = ProviderConfigurationBuilder.create()
@ -70,6 +71,12 @@ public class HardcodedClientStorageProviderFactory implements ClientStorageProvi
.helpText("Is consent required")
.defaultValue("false")
.add()
.property().name(DELAYED_SEARCH)
.type(ProviderConfigProperty.BOOLEAN_TYPE)
.label("Delayes provider by 5s.")
.helpText("If true it delayes search for clients within the provider by 5s.")
.defaultValue(false)
.add()
.build();
}

View file

@ -24,6 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.keycloak.OAuth2Constants;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;
import org.keycloak.events.Details;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
@ -41,6 +42,7 @@ import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
import org.keycloak.testsuite.auth.page.AuthRealm;
import org.keycloak.testsuite.federation.HardcodedClientStorageProviderFactory;
import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.ErrorPage;
@ -61,11 +63,16 @@ import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MINUTE;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.keycloak.testsuite.admin.ApiUtil.findUserByUsername;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
@ -92,7 +99,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
public void configureTestRealm(RealmRepresentation testRealm) {
}
protected String providerId;
private String providerId;
protected String addComponent(ComponentRepresentation component) {
Response resp = adminClient.realm("test").components().add(component);
@ -111,6 +118,7 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
provider.setConfig(new MultivaluedHashMap<>());
provider.getConfig().putSingle(HardcodedClientStorageProviderFactory.CLIENT_ID, "hardcoded-client");
provider.getConfig().putSingle(HardcodedClientStorageProviderFactory.REDIRECT_URI, oauth.getRedirectUri());
provider.getConfig().putSingle(HardcodedClientStorageProviderFactory.DELAYED_SEARCH, Boolean.toString(false));
providerId = addComponent(provider);
}
@ -123,9 +131,42 @@ public class ClientStorageTest extends AbstractTestRealmKeycloakTest {
oauth.clientId("hardcoded-client");
}
@Test(timeout = 4000)
public void testSearchTimeout() {
String hardcodedClient = HardcodedClientStorageProviderFactory.PROVIDER_ID;
String delayedSearch = HardcodedClientStorageProviderFactory.DELAYED_SEARCH;
String providerId = this.providerId;
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName(AuthRealm.TEST);
assertThat(session.clientStorageManager()
.searchClientsByClientIdStream(realm, "client", null, null)
.map(ClientModel::getClientId)
.collect(Collectors.toList()),
allOf(
hasItem(hardcodedClient),
hasItem("root-url-client"))
);
//update the provider to simulate delay during the search
ComponentModel memoryProvider = realm.getComponent(providerId);
memoryProvider.getConfig().putSingle(delayedSearch, Boolean.toString(true));
realm.updateComponent(memoryProvider);
});
testingClient.server().run(session -> {
// search for clients and check hardcoded-client is not present
assertThat(session.clientStorageManager()
.searchClientsByClientIdStream(session.realms().getRealmByName(AuthRealm.TEST), "client", null, null)
.map(ClientModel::getClientId)
.collect(Collectors.toList()),
allOf(
not(hasItem(hardcodedClient)),
hasItem("root-url-client")
));
});
}
@Test
public void testClientStats() throws Exception {