Add ManagedRealm (#31621)

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen 2024-07-26 09:09:34 +02:00 committed by GitHub
parent 6ab3e95ec5
commit bcbff87336
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 192 additions and 98 deletions

View file

@ -5,8 +5,8 @@ import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.common.Profile;
import org.keycloak.representations.info.FeatureRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.server.KeycloakTestServerConfig;
import java.util.Optional;

View file

@ -4,8 +4,8 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import java.util.List;

View file

@ -4,8 +4,8 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import java.util.List;

View file

@ -2,40 +2,40 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.InjectUser;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.ManagedUser;
@KeycloakIntegrationTest
public class GlobalManagedResourcesTest {
@InjectRealm(lifecycle = LifeCycle.GLOBAL)
RealmResource realmResource;
ManagedRealm realm;
@InjectClient(lifecycle = LifeCycle.GLOBAL)
ClientResource clientResource;
ManagedClient client;
@InjectUser(lifecycle = LifeCycle.GLOBAL)
UserResource userResource;
ManagedUser user;
@Test
public void testCreatedRealm() {
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm());
Assertions.assertEquals("default", realm.getName());
}
@Test
public void testCreatedClient() {
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId());
Assertions.assertEquals("default", client.getClientId());
}
@Test
public void testCreatedUser() {
Assertions.assertEquals("default", userResource.toRepresentation().getUsername());
Assertions.assertEquals("default", user.getUsername());
}
}

View file

@ -2,13 +2,13 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import java.util.List;
@ -16,21 +16,23 @@ import java.util.List;
public class ManagedResources2Test {
@InjectRealm(lifecycle = LifeCycle.CLASS)
RealmResource realmResource;
ManagedRealm realm;
@InjectClient
ClientResource clientResource;
ManagedClient client;
@Test
public void testCreatedRealm() {
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm());
Assertions.assertEquals("http://localhost:8080/realms/default", realm.getBaseUrl());
Assertions.assertEquals("default", realm.getName());
Assertions.assertEquals("default", realm.admin().toRepresentation().getRealm());
}
@Test
public void testCreatedClient() {
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId());
Assertions.assertEquals("default", client.getClientId());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("default");
List<ClientRepresentation> clients = realm.admin().clients().findByClientId("default");
Assertions.assertEquals(1, clients.size());
}

View file

@ -2,46 +2,42 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.InjectUser;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.injection.LifeCycle;
import java.util.List;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.ManagedUser;
@KeycloakIntegrationTest
public class ManagedResourcesTest {
@InjectRealm(lifecycle = LifeCycle.CLASS)
RealmResource realmResource;
ManagedRealm realm;
@InjectClient
ClientResource clientResource;
ManagedClient client;
@InjectUser
UserResource userResource;
ManagedUser user;
@Test
public void testCreatedRealm() {
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm());
Assertions.assertEquals("default", realm.getName());
}
@Test
public void testCreatedClient() {
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("default");
Assertions.assertEquals(1, clients.size());
Assertions.assertEquals("default", client.getClientId());
Assertions.assertEquals("default", realm.admin().clients().get(client.getId()).toRepresentation().getClientId());
}
@Test
public void testCreatedUser() {
Assertions.assertEquals("default", userResource.toRepresentation().getUsername());
Assertions.assertEquals("default", user.getUsername());
Assertions.assertEquals("default", realm.admin().users().get(user.getId()).toRepresentation().getUsername());
}
}

View file

@ -2,42 +2,42 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.RealmConfig;
@KeycloakIntegrationTest
public class MultipleInstancesTest {
@InjectRealm
RealmResource realm1;
ManagedRealm realm1;
@InjectRealm
RealmResource realm2;
ManagedRealm realm2;
@InjectRealm(ref = "another", config = CustomRealmConfig.class)
RealmResource realm3;
ManagedRealm realm3;
@InjectClient(ref = "client1")
ClientResource client;
ManagedClient client;
@InjectClient
ClientResource client2;
ManagedClient client2;
@Test
public void testMultipleInstances() {
Assertions.assertEquals("default", realm1.toRepresentation().getRealm());
Assertions.assertEquals("default", realm2.toRepresentation().getRealm());
Assertions.assertEquals("default", realm1.getName());
Assertions.assertEquals("default", realm2.getName());
Assertions.assertEquals(realm1, realm2);
Assertions.assertEquals("another", realm3.toRepresentation().getRealm());
Assertions.assertEquals("another", realm3.getName());
Assertions.assertEquals("client1", client.toRepresentation().getClientId());
Assertions.assertEquals("default", client2.toRepresentation().getClientId());
Assertions.assertEquals("client1", client.getClientId());
Assertions.assertEquals("default", client2.getClientId());
}

View file

@ -1,9 +1,9 @@
package org.keycloak.test.examples;
import org.junit.jupiter.api.Test;
import org.keycloak.test.framework.annotations.InjectPage;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.page.LoginPage;
import org.keycloak.test.framework.annotations.InjectPage;
import org.keycloak.test.framework.page.WelcomePage;
@KeycloakIntegrationTest

View file

@ -4,10 +4,10 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.page.WelcomePage;
import org.keycloak.test.framework.annotations.InjectWebDriver;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.page.WelcomePage;
import org.openqa.selenium.WebDriver;
import java.util.List;

View file

@ -0,0 +1,16 @@
package org.keycloak.test.framework.realm;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Assertions;
public class ApiUtil {
public static String handleCreatedResponse(Response response) {
Assertions.assertEquals(201, response.getStatus());
String path = response.getLocation().getPath();
String uuid = path.substring(path.lastIndexOf('/') + 1);
response.close();
return uuid;
}
}

View file

@ -2,7 +2,6 @@ package org.keycloak.test.framework.realm;
import jakarta.ws.rs.core.Response;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.injection.InstanceContext;
@ -10,9 +9,7 @@ import org.keycloak.test.framework.injection.RequestedInstance;
import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers;
public class ClientSupplier implements Supplier<ClientResource, InjectClient> {
private static final String CLIENT_UUID_KEY = "clientUuid";
public class ClientSupplier implements Supplier<ManagedClient, InjectClient> {
@Override
public Class<InjectClient> getAnnotationClass() {
@ -20,13 +17,13 @@ public class ClientSupplier implements Supplier<ClientResource, InjectClient> {
}
@Override
public Class<ClientResource> getValueType() {
return ClientResource.class;
public Class<ManagedClient> getValueType() {
return ManagedClient.class;
}
@Override
public ClientResource getValue(InstanceContext<ClientResource, InjectClient> instanceContext) {
RealmResource realm = instanceContext.getDependency(RealmResource.class);
public ManagedClient getValue(InstanceContext<ManagedClient, InjectClient> instanceContext) {
ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class);
ClientConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config());
ClientRepresentation clientRepresentation = config.getRepresentation();
@ -36,26 +33,22 @@ public class ClientSupplier implements Supplier<ClientResource, InjectClient> {
clientRepresentation.setClientId(clientId);
}
Response response = realm.clients().create(clientRepresentation);
Response response = realm.admin().clients().create(clientRepresentation);
String uuid = ApiUtil.handleCreatedResponse(response);
clientRepresentation.setId(uuid);
String path = response.getLocation().getPath();
String clientId = path.substring(path.lastIndexOf('/') + 1);
response.close();
instanceContext.addNote(CLIENT_UUID_KEY, clientId);
return realm.clients().get(clientId);
ClientResource clientResource = realm.admin().clients().get(uuid);
return new ManagedClient(clientRepresentation, clientResource);
}
@Override
public boolean compatible(InstanceContext<ClientResource, InjectClient> a, RequestedInstance<ClientResource, InjectClient> b) {
public boolean compatible(InstanceContext<ManagedClient, InjectClient> a, RequestedInstance<ManagedClient, InjectClient> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config());
}
@Override
public void close(InstanceContext<ClientResource, InjectClient> instanceContext) {
instanceContext.getValue().remove();
public void close(InstanceContext<ManagedClient, InjectClient> instanceContext) {
instanceContext.getValue().admin().remove();
}
}

View file

@ -0,0 +1,28 @@
package org.keycloak.test.framework.realm;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.ClientRepresentation;
public class ManagedClient {
private final ClientRepresentation createdRepresentation;
private final ClientResource clientResource;
public ManagedClient(ClientRepresentation createdRepresentation, ClientResource clientResource) {
this.createdRepresentation = createdRepresentation;
this.clientResource = clientResource;
}
public String getId() {
return createdRepresentation.getId();
}
public String getClientId() {
return createdRepresentation.getClientId();
}
public ClientResource admin() {
return clientResource;
}
}

View file

@ -0,0 +1,30 @@
package org.keycloak.test.framework.realm;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
public class ManagedRealm {
private final String baseUrl;
private final RealmRepresentation createdRepresentation;
private final RealmResource realmResource;
public ManagedRealm(String baseUrl, RealmRepresentation createdRepresentation, RealmResource realmResource) {
this.baseUrl = baseUrl;
this.createdRepresentation = createdRepresentation;
this.realmResource = realmResource;
}
public String getBaseUrl() {
return baseUrl;
}
public String getName() {
return createdRepresentation.getRealm();
}
public RealmResource admin() {
return realmResource;
}
}

View file

@ -0,0 +1,29 @@
package org.keycloak.test.framework.realm;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.UserRepresentation;
public class ManagedUser {
private final UserRepresentation createdRepresentation;
private final UserResource userResource;
public ManagedUser(UserRepresentation createdRepresentation, UserResource userResource) {
this.createdRepresentation = createdRepresentation;
this.userResource = userResource;
}
public String getId() {
return createdRepresentation.getId();
}
public String getUsername() {
return createdRepresentation.getUsername();
}
public UserResource admin() {
return userResource;
}
}

View file

@ -8,8 +8,9 @@ import org.keycloak.test.framework.injection.InstanceContext;
import org.keycloak.test.framework.injection.RequestedInstance;
import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers;
import org.keycloak.test.framework.server.KeycloakTestServer;
public class RealmSupplier implements Supplier<RealmResource, InjectRealm> {
public class RealmSupplier implements Supplier<ManagedRealm, InjectRealm> {
private static final String REALM_NAME_KEY = "realmName";
@ -19,12 +20,13 @@ public class RealmSupplier implements Supplier<RealmResource, InjectRealm> {
}
@Override
public Class<RealmResource> getValueType() {
return RealmResource.class;
public Class<ManagedRealm> getValueType() {
return ManagedRealm.class;
}
@Override
public RealmResource getValue(InstanceContext<RealmResource, InjectRealm> instanceContext) {
public ManagedRealm getValue(InstanceContext<ManagedRealm, InjectRealm> instanceContext) {
KeycloakTestServer server = instanceContext.getDependency(KeycloakTestServer.class);
Keycloak adminClient = instanceContext.getDependency(Keycloak.class);
RealmConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config());
@ -43,17 +45,18 @@ public class RealmSupplier implements Supplier<RealmResource, InjectRealm> {
// TODO Token needs to be invalidated after creating realm to have roles for new realm in the token. Maybe lightweight access tokens could help.
adminClient.tokenManager().invalidate(adminClient.tokenManager().getAccessTokenString());
return adminClient.realm(realmRepresentation.getRealm());
RealmResource realmResource = adminClient.realm(realmRepresentation.getRealm());
return new ManagedRealm(server.getBaseUrl() + "/realms/" + realmName, realmRepresentation, realmResource);
}
@Override
public boolean compatible(InstanceContext<RealmResource, InjectRealm> a, RequestedInstance<RealmResource, InjectRealm> b) {
public boolean compatible(InstanceContext<ManagedRealm, InjectRealm> a, RequestedInstance<ManagedRealm, InjectRealm> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config());
}
@Override
public void close(InstanceContext<RealmResource, InjectRealm> instanceContext) {
instanceContext.getValue().remove();
public void close(InstanceContext<ManagedRealm, InjectRealm> instanceContext) {
instanceContext.getValue().admin().remove();
}
}

View file

@ -1,7 +1,6 @@
package org.keycloak.test.framework.realm;
import jakarta.ws.rs.core.Response;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.test.framework.annotations.InjectUser;
@ -10,7 +9,7 @@ import org.keycloak.test.framework.injection.RequestedInstance;
import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers;
public class UserSupplier implements Supplier<UserResource, InjectUser> {
public class UserSupplier implements Supplier<ManagedUser, InjectUser> {
private static final String USER_UUID_KEY = "userUuid";
@ -20,13 +19,13 @@ public class UserSupplier implements Supplier<UserResource, InjectUser> {
}
@Override
public Class<UserResource> getValueType() {
return UserResource.class;
public Class<ManagedUser> getValueType() {
return ManagedUser.class;
}
@Override
public UserResource getValue(InstanceContext<UserResource, InjectUser> instanceContext) {
RealmResource realm = instanceContext.getDependency(RealmResource.class);
public ManagedUser getValue(InstanceContext<ManagedUser, InjectUser> instanceContext) {
ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class);
UserConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config());
UserRepresentation userRepresentation = config.getRepresentation();
@ -36,26 +35,24 @@ public class UserSupplier implements Supplier<UserResource, InjectUser> {
userRepresentation.setUsername(username);
}
Response response = realm.users().create(userRepresentation);
Response response = realm.admin().users().create(userRepresentation);
String uuid = ApiUtil.handleCreatedResponse(response);
String path = response.getLocation().getPath();
String userId = path.substring(path.lastIndexOf('/') + 1);
instanceContext.addNote(USER_UUID_KEY, uuid);
response.close();
instanceContext.addNote(USER_UUID_KEY, userId);
return realm.users().get(userId);
UserResource userResource = realm.admin().users().get(uuid);
userRepresentation.setId(uuid);
return new ManagedUser(userRepresentation, userResource);
}
@Override
public boolean compatible(InstanceContext<UserResource, InjectUser> a, RequestedInstance<UserResource, InjectUser> b) {
public boolean compatible(InstanceContext<ManagedUser, InjectUser> a, RequestedInstance<ManagedUser, InjectUser> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config());
}
@Override
public void close(InstanceContext<UserResource, InjectUser> instanceContext) {
instanceContext.getValue().remove();
public void close(InstanceContext<ManagedUser, InjectUser> instanceContext) {
instanceContext.getValue().admin().remove();
}
}