8000 [Test Framework] Fixing ManagedClient updateWithCleanup, when using non-managed client with attachTo. by lhanusov · Pull Request #48109 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;

Expand Down Expand Up @@ -71,6 +72,18 @@ public void close(InstanceContext<ManagedClient, InjectClient> instanceContext)
try {
instanceContext.getValue().admin().remove();
} catch (NotFoundException ex) {}
} else {
if (instanceContext.getValue().isForCleanup()) {
try {
ClientRepresentation clientRep = instanceContext.getValue().getClientRepForCleanup();
if (clientRep.getClientId().equals("admin-cli")) {
throw new NotSupportedException("Cleanup for admin-cli client via updateWithCleanup is not supported, please use cleanup().add(c -> c...) instead!");
}
instanceContext.getValue().admin().remove();
instanceContext.getDependency(ManagedRealm.class, instanceContext.getAnnotation().realmRef()).admin().clients().create(clientRep);
} catch (NotFoundException ex) {
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ public class ManagedClient extends ManagedTestResource {

private final ClientRepresentation createdRepresentation;
private final ClientResource clientResource;
private final ClientRepresentation clientRepForCleanup;

private ManagedClientCleanup cleanup;
private boolean forCleanup = false;

public ManagedClient(ClientRepresentation createdRepresentation, ClientResource clientResource) {
this.createdRepresentation = createdRepresentation;
this.clientResource = clientResource;
this.clientRepForCleanup = clientResource.toRepresentation();
}

/**
Expand Down Expand Up @@ -62,6 +65,8 @@ public ClientResource admin() {
public void updateWithCleanup(ManagedClient.ClientUpdate... updates) {
ClientRepresentation rep = admin().toRepresentation();

forCleanup = true;

// TODO Admin v2 - Setting a field to `null` is ignored when updating the client (for example `adminUrl`), which
// makes it impossible to reset to the original. For now we are just re-creating the client by marking it as dirty
// cleanup().resetToOriginalRepresentation(rep);
Expand All @@ -81,6 +86,14 @@ public void updateWithCleanup(ManagedClient.ClientUpdate... updates) {
// updated.getAttributes().keySet().stream().filter(k -> !original.getAttributes().containsKey(k)).forEach(k -> original.getAttributes().put(k, ""));
}

public boolean isForCleanup() {
return forCleanup;
}

public ClientRepresentation getClientRepForCleanup() {
return clientRepForCleanup;
}

public ManagedClientCleanup cleanup() {
if (cleanup == null) {
cleanup = new ManagedClientCleanup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.realm.ManagedClient;
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.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@KeycloakIntegrationTest
@TestMethodOrder(MethodOrderer.MethodName.class)
public class AttachToTest {

@InjectRealm(config = AttachToTestRealmConfig.class)
ManagedRealm managedRealm;

@InjectAdminClient
Keycloak adminClient;

@InjectRealm(attachTo = "master")
@InjectRealm(ref = "master", attachTo = "master")
ManagedRealm attachedRealm;

@InjectClient
Expand All @@ -29,6 +35,9 @@ public class AttachToTest {
@InjectClient(ref = "admin-cli", attachTo = "admin-cli")
ManagedClient attachedClient;

@InjectClient(ref = "my-client", attachTo = "my-client")
ManagedClient myClient;

@Test
public void aAttachedRealm() {
Assertions.assertEquals("master", attachedRealm.getName());
Expand All @@ -43,7 +52,7 @@ public void bRealmCleanup() {

@Test
public void cManagedClient() {
Assertions.assertEquals("default", adminClient.realm("master").clients().get(managedClient.getId()).toRepresentation().getClientId());
Assertions.assertEquals("default", adminClient.realm("default").clients().get(managedClient.getId()).toRepresentation().getClientId());
}

@Test
Expand All @@ -57,4 +66,27 @@ public void eManagedClient() {
Assertions.assertEquals("default", managedClient.getClientId());
Assertions.assertEquals("default", managedClient.admin().toRepresentation().getClientId());
}

@Test
@Order(1)
public void testingUpdateWithCleanupWithAttachToChanged() {
myClient.updateWithCleanup(c -> c.description("new description"));
Assertions.assertNotNull(myClient.admin().toRepresentation().getDescription());
}

@Test
@Order(2)
public void testingUpdateWithCleanupWithAttachToOriginal() {
Assertions.assertNull(myClient.admin().toRepresentation().getDescription());
}

private static class AttachToTestRealmConfig implements RealmConfig {

@Override
public RealmConfigBuilder configure(RealmConfigBuilder builder) {
builder.addClient("my-client");

return builder;
}
}
}
Loading
0