mirror of
https://github.com/keycloak/keycloak.git
synced 2026-05-28 04:13:22 -04:00
Support adding clients and users to RealmConfigBuilder (#35542)
Closes #35541 Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
parent
13e3439246
commit
6e8a7a844f
4 changed files with 113 additions and 0 deletions
|
|
@ -20,6 +20,11 @@ public class ClientConfigBuilder {
|
|||
return new ClientConfigBuilder(rep);
|
||||
}
|
||||
|
||||
public ClientConfigBuilder enabled(boolean enabled) {
|
||||
rep.setEnabled(enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientConfigBuilder clientId(String clientId) {
|
||||
rep.setClientId(clientId);
|
||||
return this;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package org.keycloak.test.framework.realm;
|
||||
|
||||
import org.keycloak.representations.idm.ClientRepresentation;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.RolesRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -32,6 +34,24 @@ public class RealmConfigBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
public ClientConfigBuilder addClient(String clientId) {
|
||||
if (rep.getClients() == null) {
|
||||
rep.setClients(new LinkedList<>());
|
||||
}
|
||||
ClientRepresentation client = new ClientRepresentation();
|
||||
rep.getClients().add(client);
|
||||
return ClientConfigBuilder.update(client).enabled(true).clientId(clientId);
|
||||
}
|
||||
|
||||
public UserConfigBuilder addUser(String username) {
|
||||
if (rep.getUsers() == null) {
|
||||
rep.setUsers(new LinkedList<>());
|
||||
}
|
||||
UserRepresentation user = new UserRepresentation();
|
||||
rep.getUsers().add(user);
|
||||
return UserConfigBuilder.update(user).enabled(true).username(username);
|
||||
}
|
||||
|
||||
public RealmConfigBuilder registrationEmailAsUsername(boolean registrationEmailAsUsername) {
|
||||
rep.setRegistrationEmailAsUsername(registrationEmailAsUsername);
|
||||
return this;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ package org.keycloak.test.framework.realm;
|
|||
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserConfigBuilder {
|
||||
|
||||
private final UserRepresentation rep;
|
||||
|
|
@ -21,6 +25,11 @@ public class UserConfigBuilder {
|
|||
return new UserConfigBuilder(rep);
|
||||
}
|
||||
|
||||
public UserConfigBuilder enabled(boolean enabled) {
|
||||
rep.setEnabled(enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder username(String username) {
|
||||
rep.setUsername(username);
|
||||
return this;
|
||||
|
|
@ -52,6 +61,17 @@ public class UserConfigBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder clientRoles(String client, String... roles) {
|
||||
if (rep.getClientRoles() == null) {
|
||||
rep.setClientRoles(new HashMap<>());
|
||||
}
|
||||
if (!rep.getClientRoles().containsKey(client)) {
|
||||
rep.getClientRoles().put(client, new LinkedList<>());
|
||||
}
|
||||
rep.getClientRoles().get(client).addAll(List.of(roles));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserConfigBuilder groups(String... groups) {
|
||||
rep.setGroups(Collections.combine(rep.getGroups(), groups));
|
||||
return this;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
package org.keycloak.test.examples;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.keycloak.models.AdminRoles;
|
||||
import org.keycloak.models.Constants;
|
||||
import org.keycloak.representations.idm.ClientRepresentation;
|
||||
import org.keycloak.representations.idm.MappingsRepresentation;
|
||||
import org.keycloak.representations.idm.RoleRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.test.framework.annotations.InjectRealm;
|
||||
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
|
||||
import org.keycloak.test.framework.realm.ManagedRealm;
|
||||
import org.keycloak.test.framework.realm.RealmConfig;
|
||||
import org.keycloak.test.framework.realm.RealmConfigBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@KeycloakIntegrationTest
|
||||
public class RealmWithClientAndUserTest {
|
||||
|
||||
@InjectRealm(config = RealmWithClientAndUser.class)
|
||||
ManagedRealm realm;
|
||||
|
||||
@Test
|
||||
public void testRealmWithClientAndUser() {
|
||||
List<ClientRepresentation> clients = realm.admin().clients().findByClientId("myclient");
|
||||
Assertions.assertEquals(1, clients.size());
|
||||
|
||||
ClientRepresentation client = clients.get(0);
|
||||
Assertions.assertTrue(client.isEnabled());
|
||||
Assertions.assertTrue(client.isDirectAccessGrantsEnabled());
|
||||
Assertions.assertEquals("mysecret", client.getSecret());
|
||||
|
||||
List<UserRepresentation> users = realm.admin().users().search("myadmin");
|
||||
Assertions.assertEquals(1, users.size());
|
||||
|
||||
UserRepresentation user = users.get(0);
|
||||
Assertions.assertTrue(user.isEnabled());
|
||||
Assertions.assertEquals("My", user.getFirstName());
|
||||
Assertions.assertEquals("Admin", user.getLastName());
|
||||
Assertions.assertEquals("myadmin@localhost", user.getEmail());
|
||||
Assertions.assertTrue(user.isEmailVerified());
|
||||
|
||||
MappingsRepresentation roles = realm.admin().users().get(user.getId()).roles().getAll();
|
||||
Assertions.assertEquals(1, roles.getClientMappings().get(Constants.REALM_MANAGEMENT_CLIENT_ID).getMappings().size());
|
||||
}
|
||||
|
||||
public static class RealmWithClientAndUser implements RealmConfig {
|
||||
|
||||
@Override
|
||||
public RealmConfigBuilder configure(RealmConfigBuilder realm) {
|
||||
realm.addClient("myclient")
|
||||
.secret("mysecret")
|
||||
.directAccessGrants();
|
||||
|
||||
realm.addUser("myadmin")
|
||||
.name("My", "Admin")
|
||||
.email("myadmin@localhost")
|
||||
.emailVerified()
|
||||
.password("mypassword")
|
||||
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN);
|
||||
|
||||
return realm;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue