Testsuite PoC - Added initial config builders (#31659)
Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
parent
30cd88fefe
commit
4d66f3886b
14 changed files with 292 additions and 3 deletions
|
@ -0,0 +1,88 @@
|
|||
package org.keycloak.test.examples;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.keycloak.representations.idm.ClientRepresentation;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
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.ClientConfig;
|
||||
import org.keycloak.test.framework.realm.ManagedClient;
|
||||
import org.keycloak.test.framework.realm.ManagedRealm;
|
||||
import org.keycloak.test.framework.realm.ManagedUser;
|
||||
import org.keycloak.test.framework.realm.RealmConfig;
|
||||
import org.keycloak.test.framework.realm.UserConfig;
|
||||
|
||||
@KeycloakIntegrationTest
|
||||
public class FancyRealmTest {
|
||||
|
||||
@InjectRealm(lifecycle = LifeCycle.CLASS, config = MyRealm.class)
|
||||
ManagedRealm realm;
|
||||
|
||||
@InjectClient(config = MyClient.class)
|
||||
ManagedClient client;
|
||||
|
||||
@InjectUser(config = MyUser.class)
|
||||
ManagedUser user;
|
||||
|
||||
@Test
|
||||
public void testCreatedRealm() {
|
||||
Assertions.assertEquals("default", realm.getName());
|
||||
|
||||
Assertions.assertNotNull(realm.admin().roles().get("role-1").toRepresentation().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatedClient() {
|
||||
Assertions.assertEquals("the-client", client.getClientId());
|
||||
Assertions.assertEquals("the-client", realm.admin().clients().get(client.getId()).toRepresentation().getClientId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatedUser() {
|
||||
Assertions.assertEquals("bobthemob", user.getUsername());
|
||||
Assertions.assertEquals("bobthemob", realm.admin().users().get(user.getId()).toRepresentation().getUsername());
|
||||
}
|
||||
|
||||
static class MyRealm implements RealmConfig {
|
||||
|
||||
@Override
|
||||
public RealmRepresentation getRepresentation() {
|
||||
return builder()
|
||||
.roles("role-1", "role-2")
|
||||
.groups("group-1", "group-2")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
static class MyClient implements ClientConfig {
|
||||
|
||||
@Override
|
||||
public ClientRepresentation getRepresentation() {
|
||||
return builder()
|
||||
.clientId("the-client")
|
||||
.redirectUris("http://127.0.0.1", "http://test")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
static class MyUser implements UserConfig {
|
||||
|
||||
@Override
|
||||
public UserRepresentation getRepresentation() {
|
||||
return builder()
|
||||
.username("bobthemob")
|
||||
.name("Bob", "Mob")
|
||||
.email("bob@mob")
|
||||
.password("password")
|
||||
.roles("role-1", "role-2") // TODO Adding role mappings when creating user is not supported!
|
||||
.groups("/group-1")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -6,4 +6,8 @@ public interface ClientConfig {
|
|||
|
||||
ClientRepresentation getRepresentation();
|
||||
|
||||
default ClientConfigBuilder builder() {
|
||||
return new ClientConfigBuilder();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import org.keycloak.representations.idm.ClientRepresentation;
|
||||
|
||||
public class ClientConfigBuilder {
|
||||
|
||||
private final ClientRepresentation representation;
|
||||
|
||||
public ClientConfigBuilder() {
|
||||
this.representation = new ClientRepresentation();
|
||||
this.representation.setEnabled(true);
|
||||
}
|
||||
|
||||
public ClientConfigBuilder clientId(String clientId) {
|
||||
representation.setClientId(clientId);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientConfigBuilder redirectUris(String... redirectUris) {
|
||||
representation.setRedirectUris(Collections.combine(representation.getRedirectUris(), redirectUris));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientRepresentation build() {
|
||||
return representation;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class Collections {
|
||||
|
||||
private Collections() {
|
||||
}
|
||||
|
||||
static <T> List<T> combine(List<T> l1, List<T> l2) {
|
||||
if (l1 == null) {
|
||||
return new LinkedList<>(l2);
|
||||
} else {
|
||||
l1.addAll(l2);
|
||||
return l1;
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
static <T> List<T> combine(List<T> l1, T... items) {
|
||||
return combine(l1, Arrays.asList(items));
|
||||
}
|
||||
|
||||
static <T> List<T> combine(List<T> l1, Stream<T> items) {
|
||||
return combine(l1, items.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ public class DefaultClientConfig implements ClientConfig {
|
|||
|
||||
@Override
|
||||
public ClientRepresentation getRepresentation() {
|
||||
return new ClientRepresentation();
|
||||
return builder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ public class DefaultRealmConfig implements RealmConfig {
|
|||
|
||||
@Override
|
||||
public RealmRepresentation getRepresentation() {
|
||||
return new RealmRepresentation();
|
||||
return builder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ public class DefaultUserConfig implements UserConfig {
|
|||
|
||||
@Override
|
||||
public UserRepresentation getRepresentation() {
|
||||
return new UserRepresentation();
|
||||
return builder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ManagedUser {
|
||||
|
||||
private final UserRepresentation createdRepresentation;
|
||||
|
@ -22,6 +25,11 @@ public class ManagedUser {
|
|||
return createdRepresentation.getUsername();
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
Optional<CredentialRepresentation> password = createdRepresentation.getCredentials().stream().filter(c -> c.getType().equals(CredentialRepresentation.PASSWORD)).findFirst();
|
||||
return password.map(CredentialRepresentation::getValue).orElse(null);
|
||||
}
|
||||
|
||||
public UserResource admin() {
|
||||
return userResource;
|
||||
}
|
||||
|
|
|
@ -6,4 +6,8 @@ public interface RealmConfig {
|
|||
|
||||
RealmRepresentation getRepresentation();
|
||||
|
||||
default RealmConfigBuilder builder() {
|
||||
return new RealmConfigBuilder();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.RolesRepresentation;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class RealmConfigBuilder {
|
||||
|
||||
private final RealmRepresentation representation;
|
||||
|
||||
public RealmConfigBuilder() {
|
||||
this.representation = new RealmRepresentation();
|
||||
this.representation.setEnabled(true);
|
||||
}
|
||||
|
||||
public RealmConfigBuilder name(String name) {
|
||||
representation.setRealm(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RealmConfigBuilder roles(String... roleNames) {
|
||||
if (representation.getRoles() == null) {
|
||||
representation.setRoles(new RolesRepresentation());
|
||||
}
|
||||
representation.getRoles().setRealm(Collections.combine(representation.getRoles().getRealm(), Arrays.stream(roleNames).map(Representations::toRole)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public RealmConfigBuilder groups(String... groupsNames) {
|
||||
representation.setGroups(Collections.combine(representation.getGroups(), Arrays.stream(groupsNames).map(Representations::toGroup)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public RealmRepresentation build() {
|
||||
return representation;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||
import org.keycloak.representations.idm.GroupRepresentation;
|
||||
import org.keycloak.representations.idm.RoleRepresentation;
|
||||
|
||||
class Representations {
|
||||
|
||||
private Representations() {
|
||||
}
|
||||
|
||||
static RoleRepresentation toRole(String roleName) {
|
||||
RoleRepresentation role = new RoleRepresentation();
|
||||
role.setName(roleName);
|
||||
return role;
|
||||
}
|
||||
|
||||
static GroupRepresentation toGroup(String groupName) {
|
||||
GroupRepresentation group = new GroupRepresentation();
|
||||
group.setName(groupName);
|
||||
return group;
|
||||
}
|
||||
|
||||
static CredentialRepresentation toCredential(String type, String value) {
|
||||
CredentialRepresentation credential = new CredentialRepresentation();
|
||||
credential.setType(type);
|
||||
credential.setValue(value);
|
||||
return credential;
|
||||
}
|
||||
|
||||
}
|
|
@ -6,4 +6,8 @@ public interface UserConfig {
|
|||
|
||||
UserRepresentation getRepresentation();
|
||||
|
||||
default UserConfigBuilder builder() {
|
||||
return new UserConfigBuilder();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
|
||||
public class UserConfigBuilder {
|
||||
|
||||
private final UserRepresentation representation;
|
||||
|
||||
public UserConfigBuilder() {
|
||||
this.representation = new UserRepresentation();
|
||||
this.representation.setEnabled(true);
|
||||
}
|
||||
|
||||
public UserConfigBuilder username(String username) {
|
||||
representation.setUsername(username);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder name(String firstName, String lastName) {
|
||||
representation.setFirstName(firstName);
|
||||
representation.setLastName(lastName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder email(String email) {
|
||||
representation.setEmail(email);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder password(String password) {
|
||||
representation.setCredentials(Collections.combine(representation.getCredentials(), Representations.toCredential(CredentialRepresentation.PASSWORD, password)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder roles(String... roles) {
|
||||
representation.setRealmRoles(Collections.combine(representation.getRealmRoles(), roles));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder groups(String... groups) {
|
||||
representation.setGroups(Collections.combine(representation.getGroups(), groups));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserRepresentation build() {
|
||||
return representation;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package org.keycloak.test.framework.realm;
|
|||
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.representations.idm.RoleRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.test.framework.annotations.InjectUser;
|
||||
import org.keycloak.test.framework.injection.InstanceContext;
|
||||
|
@ -42,6 +43,7 @@ public class UserSupplier implements Supplier<ManagedUser, InjectUser> {
|
|||
|
||||
UserResource userResource = realm.admin().users().get(uuid);
|
||||
userRepresentation.setId(uuid);
|
||||
|
||||
return new ManagedUser(userRepresentation, userResource);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue