mirror of
https://github.com/keycloak/keycloak.git
synced 2026-05-26 13:50:48 +00:00
[Test Framework] Fix of AdminClientSupplier for ManagedRealm. (#46287)
Signed-off-by: Lukas Hanusovsky <lhanusov@redhat.com>
This commit is contained in:
+2
-2
@@ -25,7 +25,7 @@ public class AdminClientSupplier implements Supplier<Keycloak, InjectAdminClient
|
||||
public List<Dependency> getDependencies(RequestedInstance<Keycloak, InjectAdminClient> instanceContext) {
|
||||
DependenciesBuilder builder = DependenciesBuilder.create(AdminClientFactory.class);
|
||||
if (instanceContext.getAnnotation().mode().equals(InjectAdminClient.Mode.MANAGED_REALM)) {
|
||||
builder.add(ManagedRealm.class);
|
||||
builder.add(ManagedRealm.class, instanceContext.getAnnotation().realmRef());
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
@@ -42,7 +42,7 @@ public class AdminClientSupplier implements Supplier<Keycloak, InjectAdminClient
|
||||
if (mode.equals(InjectAdminClient.Mode.BOOTSTRAP)) {
|
||||
adminBuilder.realm("master").clientId(Config.getAdminClientId()).clientSecret(Config.getAdminClientSecret());
|
||||
} else if (mode.equals(InjectAdminClient.Mode.MANAGED_REALM)) {
|
||||
ManagedRealm managedRealm = instanceContext.getDependency(ManagedRealm.class);
|
||||
ManagedRealm managedRealm = instanceContext.getDependency(ManagedRealm.class, instanceContext.getAnnotation().realmRef());
|
||||
adminBuilder.realm(managedRealm.getName());
|
||||
|
||||
String clientId = !annotation.client().isEmpty() ? annotation.client() : null;
|
||||
|
||||
+3
-1
@@ -26,17 +26,19 @@ public @interface InjectAdminClient {
|
||||
/**
|
||||
* <code>BOOTSTRAP</code> attaches to the master realm and global test client, while <code>MANAGED_REALM</code>
|
||||
* attaches to a managed realm using the specified client or user. When using <code>MANAGED_REALM</code> either
|
||||
* client or user has to be set
|
||||
* client or user must be set in the {@link org.keycloak.testframework.realm.ManagedRealm} instance using the {@link org.keycloak.testframework.realm.RealmConfig} implementation
|
||||
*/
|
||||
Mode mode() default Mode.BOOTSTRAP;
|
||||
|
||||
/**
|
||||
* The client to authenticate as
|
||||
* The client must be configured within the {@link org.keycloak.testframework.realm.ManagedRealm} instance, {@link org.keycloak.testframework.realm.ManagedClient} is not supported
|
||||
*/
|
||||
String client() default "";
|
||||
|
||||
/**
|
||||
* The user to authenticate as
|
||||
* The user must be configured within the {@link org.keycloak.testframework.realm.ManagedRealm} instance, {@link org.keycloak.testframework.realm.ManagedUser} is not supported
|
||||
*/
|
||||
String user() default "";
|
||||
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package org.keycloak.test.examples;
|
||||
|
||||
|
||||
|
||||
import org.keycloak.admin.client.Keycloak;
|
||||
import org.keycloak.models.AdminRoles;
|
||||
import org.keycloak.models.Constants;
|
||||
import org.keycloak.testframework.annotations.InjectAdminClient;
|
||||
import org.keycloak.testframework.annotations.InjectEvents;
|
||||
import org.keycloak.testframework.annotations.InjectRealm;
|
||||
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
|
||||
import org.keycloak.testframework.events.EventAssertion;
|
||||
import org.keycloak.testframework.events.Events;
|
||||
import org.keycloak.testframework.realm.ManagedRealm;
|
||||
import org.keycloak.testframework.realm.RealmConfig;
|
||||
import org.keycloak.testframework.realm.RealmConfigBuilder;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@KeycloakIntegrationTest
|
||||
public class AdminClientTest {
|
||||
|
||||
@InjectRealm(ref = "customrealm", config = CustomRealmConf.class)
|
||||
ManagedRealm customRealm;
|
||||
|
||||
@InjectAdminClient
|
||||
Keycloak adminClient;
|
||||
|
||||
@InjectAdminClient(mode = InjectAdminClient.Mode.MANAGED_REALM, ref = "customclient", realmRef = "customrealm", client = "realmclient", user = "realmuser")
|
||||
Keycloak customAdminClient;
|
||||
|
||||
@InjectEvents(ref = "customevents", realmRef = "customrealm")
|
||||
Events customRealmEvents;
|
||||
|
||||
@Test
|
||||
public void testDefaultAdminClient() {
|
||||
Assertions.assertFalse(adminClient.tokenManager().getAccessToken().getToken().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdminClientWithNoAccessAsUser() {
|
||||
Assertions.assertFalse(customAdminClient.tokenManager().getAccessToken().getToken().isEmpty());
|
||||
EventAssertion.assertSuccess(customRealmEvents.poll()).clientId("realmclient").details("username", "realmuser");
|
||||
}
|
||||
|
||||
private static class CustomRealmConf implements RealmConfig {
|
||||
@Override
|
||||
public RealmConfigBuilder configure(RealmConfigBuilder realm) {
|
||||
realm.addUser("realmuser")
|
||||
.password("realmuser")
|
||||
.name("Realm", "User").email("realm@user").emailVerified(true)
|
||||
.clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN);
|
||||
realm.addClient("realmclient")
|
||||
.secret("realmclientsecret").directAccessGrantsEnabled(true);
|
||||
return realm;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user