Testsuite PoC - Add managed user (#31151)

Closes #31150

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen 2024-07-09 15:20:09 +02:00 committed by GitHub
parent 5fdb572f5f
commit b5468c8b63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 144 additions and 11 deletions

View file

@ -4,11 +4,16 @@ 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.KeycloakIntegrationTest;
import org.keycloak.test.framework.TestClient;
import org.keycloak.test.framework.TestRealm;
import org.keycloak.test.framework.TestUser;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.DefaultClientConfig;
import org.keycloak.test.framework.realm.DefaultRealmConfig;
import org.keycloak.test.framework.realm.DefaultUserConfig;
import java.util.List;
@ -18,20 +23,25 @@ public class GlobalManagedResourcesTest {
@TestRealm(lifecycle = LifeCycle.GLOBAL)
RealmResource realmResource;
@TestClient
@TestClient(lifecycle = LifeCycle.GLOBAL)
ClientResource clientResource;
@TestUser(lifecycle = LifeCycle.GLOBAL)
UserResource userResource;
@Test
public void testCreatedRealm() {
Assertions.assertEquals("DefaultRealmConfig", realmResource.toRepresentation().getRealm());
Assertions.assertEquals(DefaultRealmConfig.class.getSimpleName(), realmResource.toRepresentation().getRealm());
}
@Test
public void testCreatedClient() {
Assertions.assertEquals("GlobalManagedResourcesTest", clientResource.toRepresentation().getClientId());
Assertions.assertEquals(DefaultClientConfig.class.getSimpleName(), clientResource.toRepresentation().getClientId());
}
List<ClientRepresentation> clients = realmResource.clients().findByClientId("GlobalManagedResourcesTest");
Assertions.assertEquals(1, clients.size());
@Test
public void testCreatedUser() {
Assertions.assertEquals(DefaultUserConfig.class.getSimpleName().toLowerCase(), userResource.toRepresentation().getUsername());
}
}

View file

@ -23,14 +23,14 @@ public class ManagedResources2Test {
@Test
public void testCreatedRealm() {
Assertions.assertEquals("ManagedResources2Test", realmResource.toRepresentation().getRealm());
Assertions.assertEquals(ManagedResources2Test.class.getSimpleName(), realmResource.toRepresentation().getRealm());
}
@Test
public void testCreatedClient() {
Assertions.assertEquals("ManagedResources2Test", clientResource.toRepresentation().getClientId());
Assertions.assertEquals(ManagedResources2Test.class.getSimpleName(), clientResource.toRepresentation().getClientId());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("ManagedResources2Test");
List<ClientRepresentation> clients = realmResource.clients().findByClientId(ManagedResources2Test.class.getSimpleName());
Assertions.assertEquals(1, clients.size());
}

View file

@ -4,10 +4,12 @@ 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.KeycloakIntegrationTest;
import org.keycloak.test.framework.TestClient;
import org.keycloak.test.framework.TestRealm;
import org.keycloak.test.framework.TestUser;
import org.keycloak.test.framework.injection.LifeCycle;
import java.util.List;
@ -21,17 +23,25 @@ public class ManagedResourcesTest {
@TestClient
ClientResource clientResource;
@TestUser
UserResource userResource;
@Test
public void testCreatedRealm() {
Assertions.assertEquals("ManagedResourcesTest", realmResource.toRepresentation().getRealm());
Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName(), realmResource.toRepresentation().getRealm());
}
@Test
public void testCreatedClient() {
Assertions.assertEquals("ManagedResourcesTest", clientResource.toRepresentation().getClientId());
Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName(), clientResource.toRepresentation().getClientId());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("ManagedResourcesTest");
List<ClientRepresentation> clients = realmResource.clients().findByClientId(ManagedResourcesTest.class.getSimpleName());
Assertions.assertEquals(1, clients.size());
}
@Test
public void testCreatedUser() {
Assertions.assertEquals(ManagedResourcesTest.class.getSimpleName().toLowerCase(), userResource.toRepresentation().getUsername());
}
}

View file

@ -0,0 +1,22 @@
package org.keycloak.test.framework;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ClientConfig;
import org.keycloak.test.framework.realm.DefaultClientConfig;
import org.keycloak.test.framework.realm.DefaultUserConfig;
import org.keycloak.test.framework.realm.UserConfig;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestUser {
Class<? extends UserConfig> config() default DefaultUserConfig.class;
LifeCycle lifecycle() default LifeCycle.CLASS;
}

View file

@ -0,0 +1,12 @@
package org.keycloak.test.framework.realm;
import org.keycloak.representations.idm.UserRepresentation;
public class DefaultUserConfig implements UserConfig {
@Override
public UserRepresentation getRepresentation() {
return new UserRepresentation();
}
}

View file

@ -0,0 +1,9 @@
package org.keycloak.test.framework.realm;
import org.keycloak.representations.idm.UserRepresentation;
public interface UserConfig {
UserRepresentation getRepresentation();
}

View file

@ -0,0 +1,69 @@
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.TestUser;
import org.keycloak.test.framework.injection.InstanceWrapper;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.injection.Registry;
import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers;
public class UserSupplier implements Supplier<UserResource, TestUser> {
private static final String USER_UUID_KEY = "userUuid";
@Override
public Class<TestUser> getAnnotationClass() {
return TestUser.class;
}
@Override
public Class<UserResource> getValueType() {
return UserResource.class;
}
@Override
public InstanceWrapper<UserResource, TestUser> getValue(Registry registry, TestUser annotation) {
InstanceWrapper<UserResource, TestUser> wrapper = new InstanceWrapper<>(this, annotation);
LifeCycle lifecycle = annotation.lifecycle();
RealmResource realm = registry.getDependency(RealmResource.class, wrapper);
UserConfig config = SupplierHelpers.getInstance(annotation.config());
UserRepresentation userRepresentation = config.getRepresentation();
if (userRepresentation.getUsername() == null) {
String username = lifecycle.equals(LifeCycle.GLOBAL) ? config.getClass().getSimpleName() : registry.getCurrentContext().getRequiredTestClass().getSimpleName();
userRepresentation.setUsername(username);
}
Response response = realm.users().create(userRepresentation);
String path = response.getLocation().getPath();
String userId = path.substring(path.lastIndexOf('/') + 1);
response.close();
wrapper.addNote(USER_UUID_KEY, userId);
UserResource userResource = realm.users().get(userId);
wrapper.setValue(userResource, lifecycle);
return wrapper;
}
@Override
public boolean compatible(InstanceWrapper<UserResource, TestUser> a, InstanceWrapper<UserResource, TestUser> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config()) &&
a.getNote(USER_UUID_KEY, String.class).equals(b.getNote(USER_UUID_KEY, String.class));
}
@Override
public void close(UserResource user) {
user.remove();
}
}

View file

@ -1,6 +1,7 @@
org.keycloak.test.framework.admin.KeycloakAdminClientSupplier
org.keycloak.test.framework.realm.ClientSupplier
org.keycloak.test.framework.realm.RealmSupplier
org.keycloak.test.framework.realm.UserSupplier
org.keycloak.test.framework.server.EmbeddedKeycloakTestServerSupplier
org.keycloak.test.framework.server.DistributionKeycloakTestServerSupplier
org.keycloak.test.framework.server.RemoteKeycloakTestServerSupplier