Refactor and improve tests for federated client authentication (#42720)

Closes #42718

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen
2025-09-18 11:30:01 +02:00
committed by GitHub
parent 2d34ebe33e
commit 37a99154a5
9 changed files with 343 additions and 265 deletions

View File

@@ -0,0 +1,60 @@
package org.keycloak.testframework.events;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.keycloak.events.EventType;
import org.keycloak.representations.idm.EventRepresentation;
public class EventAssertion {
private final EventRepresentation event;
protected EventAssertion(EventRepresentation event) {
Assertions.assertNotNull(event, "Event was null");
Assertions.assertNotNull(event.getId(), "Event id was null");
this.event = event;
}
public static EventAssertion assertSuccess(EventRepresentation event) {
Assertions.assertFalse(event.getType().endsWith("_ERROR"), "Expected successful event");
return new EventAssertion(event);
}
public static EventAssertion assertError(EventRepresentation event) {
Assertions.assertTrue(event.getType().endsWith("_ERROR"), "Expected error event");
return new EventAssertion(event);
}
public EventAssertion error(String error) {
Assertions.assertEquals(error, event.getError());
return this;
}
public EventAssertion type(EventType type) {
Assertions.assertEquals(type, EventType.valueOf(event.getType()));
return this;
}
public EventAssertion clientId(String clientId) {
Assertions.assertEquals(clientId, event.getClientId());
return this;
}
public EventAssertion details(String key, String value) {
if (value != null) {
MatcherAssert.assertThat(event.getDetails(), Matchers.hasEntry(key, value));
} else {
withoutDetails(key);
}
return this;
}
public EventAssertion withoutDetails(String... keys) {
for (String key : keys) {
MatcherAssert.assertThat(event.getDetails(), Matchers.not(Matchers.hasKey(key)));
}
return this;
}
}

View File

@@ -1,6 +1,8 @@
package org.keycloak.testframework.realm;
import org.keycloak.admin.client.resource.IdentityProviderResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.IdentityProviderRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testframework.injection.ManagedTestResource;
@@ -56,6 +58,17 @@ public class ManagedRealm extends ManagedTestResource {
admin().update(configBuilder.build());
}
public void updateIdentityProviderWithCleanup(String alias, IdentityProviderUpdate update) {
IdentityProviderResource resource = realmResource.identityProviders().get(alias);
IdentityProviderRepresentation original = resource.toRepresentation();
IdentityProviderRepresentation updated = RepresentationUtils.clone(original);
update.update(updated);
resource.update(updated);
cleanup().add(r -> r.identityProviders().get(alias).update(original));
}
public ManagedRealmCleanup cleanup() {
if (cleanup == null) {
cleanup = new ManagedRealmCleanup();
@@ -77,4 +90,10 @@ public class ManagedRealm extends ManagedTestResource {
}
public interface IdentityProviderUpdate {
void update(IdentityProviderRepresentation rep);
}
}