From 0efa3c50973b951a2ea81f4ae73e47ef841fa6de Mon Sep 17 00:00:00 2001 From: Garth <244253+xgp@users.noreply.github.com> Date: Mon, 13 Nov 2023 21:07:54 +0100 Subject: [PATCH] switched eventTypes to FetchType.EAGER in WebhookEntity (#43) --- .../HttpSenderEventListenerProvider.java | 3 ++ .../events/ScriptEventListenerProvider.java | 2 +- .../model/jpa/entity/WebhookEntity.java | 4 +- .../resources/WebhooksResourceTest.java | 48 +++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/phasetwo/keycloak/events/HttpSenderEventListenerProvider.java b/src/main/java/io/phasetwo/keycloak/events/HttpSenderEventListenerProvider.java index b76a83b..8989125 100644 --- a/src/main/java/io/phasetwo/keycloak/events/HttpSenderEventListenerProvider.java +++ b/src/main/java/io/phasetwo/keycloak/events/HttpSenderEventListenerProvider.java @@ -86,6 +86,9 @@ protected void send( log.warnf("Sending failure (Server response:%d)", status); throw new SenderException(true); } + } catch (SenderException se) { + // rethrow existing SenderException + throw se; } catch (Exception e) { log.warnf(e, "Sending exception to %s", targetUri); throw new SenderException(false, e); diff --git a/src/main/java/io/phasetwo/keycloak/events/ScriptEventListenerProvider.java b/src/main/java/io/phasetwo/keycloak/events/ScriptEventListenerProvider.java index b17d3b4..bdbfa66 100644 --- a/src/main/java/io/phasetwo/keycloak/events/ScriptEventListenerProvider.java +++ b/src/main/java/io/phasetwo/keycloak/events/ScriptEventListenerProvider.java @@ -26,7 +26,7 @@ public class ScriptEventListenerProvider implements EventListenerProvider, Confi protected final KeycloakSession session; protected final boolean scriptsDisabled; - + public ScriptEventListenerProvider(KeycloakSession session) { this.session = session; this.scriptsDisabled = Boolean.parseBoolean(System.getenv(SCRIPTS_DISABLED_ENV)); diff --git a/src/main/java/io/phasetwo/keycloak/model/jpa/entity/WebhookEntity.java b/src/main/java/io/phasetwo/keycloak/model/jpa/entity/WebhookEntity.java index bfb671a..3190a44 100644 --- a/src/main/java/io/phasetwo/keycloak/model/jpa/entity/WebhookEntity.java +++ b/src/main/java/io/phasetwo/keycloak/model/jpa/entity/WebhookEntity.java @@ -36,12 +36,12 @@ public class WebhookEntity { @Column(name = "ALGORITHM") protected String algorithm; - @ElementCollection + @ElementCollection(fetch = FetchType.EAGER) @Column(name = "VALUE") @CollectionTable( name = "WEBHOOK_EVENT_TYPES", joinColumns = {@JoinColumn(name = "WEBHOOK_ID")}) - protected Set eventTypes = new HashSet(); + protected Set eventTypes = new HashSet<>(); @Column(name = "CREATED_BY_USER_ID") protected String createdBy; diff --git a/src/test/java/io/phasetwo/keycloak/resources/WebhooksResourceTest.java b/src/test/java/io/phasetwo/keycloak/resources/WebhooksResourceTest.java index 9b2b760..66db038 100644 --- a/src/test/java/io/phasetwo/keycloak/resources/WebhooksResourceTest.java +++ b/src/test/java/io/phasetwo/keycloak/resources/WebhooksResourceTest.java @@ -17,6 +17,7 @@ import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import lombok.extern.jbosslog.JBossLog; @@ -59,6 +60,10 @@ public void testAddGetWebhook() throws Exception { .auth(keycloak.tokenManager().getAccessTokenString()) .asResponse(); assertThat(response.getStatus(), is(200)); + /* + String r = response.asString(); + log.infof("got webhook response %s", r); + */ WebhookRepresentation rep = response.asJson(new TypeReference() {}); assertNotNull(rep); assertTrue(rep.isEnabled()); @@ -76,6 +81,49 @@ public void testAddGetWebhook() throws Exception { assertThat(response.getStatus(), is(204)); } + /** https://github.com/p2-inc/keycloak-events/issues/42 */ + @Test + public void testAddGetWebhookEventTypes() throws Exception { + Keycloak keycloak = server.client(); + + String url = "https://pipedream.m.pipedream.net"; + Set types = + ImmutableSet.of( + "access.REMOVE_TOTP", + "access.UPDATE_TOTP", + "access.LOGIN", + "access.LOGOUT", + "access.REGISTER", + "access.UPDATE_PASSWORD", + "access.VERIFY_EMAIL", + "access.SEND_VERIFY_EMAIL", + "access.RESET_PASSWORD"); + String id = createWebhook(keycloak, httpClient, baseUrl(), url, "A3jt6D8lz", types); + + SimpleHttp.Response response = + SimpleHttp.doGet(baseUrl() + "/" + urlencode(id), httpClient) + .auth(keycloak.tokenManager().getAccessTokenString()) + .asResponse(); + assertThat(response.getStatus(), is(200)); + + WebhookRepresentation rep = response.asJson(new TypeReference() {}); + assertNotNull(rep); + assertTrue(rep.isEnabled()); + assertNotNull(rep.getId()); + assertNotNull(rep.getCreatedAt()); + assertNotNull(rep.getCreatedBy()); + assertThat(rep.getRealm(), is("master")); + assertThat(rep.getUrl(), is(url)); + assertThat(rep.getEventTypes().size(), is(9)); + assertNull(rep.getSecret()); + + response = + SimpleHttp.doDelete(baseUrl() + "/" + urlencode(id), httpClient) + .auth(keycloak.tokenManager().getAccessTokenString()) + .asResponse(); + assertThat(response.getStatus(), is(204)); + } + @Test public void testUpdateGetWebhook() throws Exception { Keycloak keycloak = server.client();