diff --git a/test-framework/core/src/main/java/org/keycloak/testframework/realm/ManagedClient.java b/test-framework/core/src/main/java/org/keycloak/testframework/realm/ManagedClient.java index 4e9db964914..353d0786c68 100644 --- a/test-framework/core/src/main/java/org/keycloak/testframework/realm/ManagedClient.java +++ b/test-framework/core/src/main/java/org/keycloak/testframework/realm/ManagedClient.java @@ -34,7 +34,11 @@ public class ManagedClient extends ManagedTestResource { public void updateWithCleanup(ManagedClient.ClientUpdate... updates) { ClientRepresentation rep = admin().toRepresentation(); - cleanup().resetToOriginalRepresentation(rep); + + // 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); + dirty(); ClientConfigBuilder configBuilder = ClientConfigBuilder.update(rep); for (ManagedClient.ClientUpdate update : updates) { @@ -44,8 +48,10 @@ public class ManagedClient extends ManagedTestResource { ClientRepresentation updated = configBuilder.build(); admin().update(updated); - ClientRepresentation original = cleanup().getOriginalRepresentation(); - updated.getAttributes().keySet().stream().filter(k -> !original.getAttributes().containsKey(k)).forEach(k -> original.getAttributes().put(k, "")); + // TODO It's possible to delete attributes by setting their value to an empty string, but due to the above this + // is not a complete solution to resetting to the original + // ClientRepresentation original = cleanup().getOriginalRepresentation(); + // updated.getAttributes().keySet().stream().filter(k -> !original.getAttributes().containsKey(k)).forEach(k -> original.getAttributes().put(k, "")); } public ManagedClientCleanup cleanup() { diff --git a/test-framework/examples/tests/src/test/java/org/keycloak/test/examples/ClientRollbackTest.java b/test-framework/examples/tests/src/test/java/org/keycloak/test/examples/ClientRollbackTest.java index 47e1f576f62..3bef7a16204 100644 --- a/test-framework/examples/tests/src/test/java/org/keycloak/test/examples/ClientRollbackTest.java +++ b/test-framework/examples/tests/src/test/java/org/keycloak/test/examples/ClientRollbackTest.java @@ -11,6 +11,8 @@ import org.keycloak.testframework.realm.ClientConfig; import org.keycloak.testframework.realm.ClientConfigBuilder; import org.keycloak.testframework.realm.ManagedClient; +import java.util.List; + @KeycloakIntegrationTest @TestMethodOrder(MethodOrderer.MethodName.class) public class ClientRollbackTest { @@ -19,19 +21,22 @@ public class ClientRollbackTest { ManagedClient client; @Test - public void testAddAttributeWithRollback() { + public void test1UpdateWithRollback() { client.updateWithCleanup(u -> u.attribute("one", "two").attribute("two", "two")); - - ClientRepresentation rep = client.admin().toRepresentation(); - Assertions.assertEquals("two", rep.getAttributes().get("one")); - Assertions.assertTrue(rep.getAttributes().containsKey("two")); + client.updateWithCleanup(u -> u.adminUrl("http://something")); + client.updateWithCleanup(u -> u.redirectUris("http://something")); + client.updateWithCleanup(u -> u.attribute("three", "three")); } @Test - public void testAttributeNotSet() { - ClientRepresentation rep = client.admin().toRepresentation(); - Assertions.assertEquals("one", rep.getAttributes().get("one")); - Assertions.assertFalse(rep.getAttributes().containsKey("two")); + public void test2CheckRollback() { + ClientRepresentation current = client.admin().toRepresentation(); + + Assertions.assertEquals("one", current.getAttributes().get("one")); + Assertions.assertFalse(current.getAttributes().containsKey("two")); + Assertions.assertFalse(current.getAttributes().containsKey("three")); + Assertions.assertNull(current.getAdminUrl()); + Assertions.assertTrue(current.getRedirectUris().isEmpty()); } public static class ClientWithSingleAttribute implements ClientConfig {