Migrate EventConfigTest to the new testsuite (#37092)

* Move EventConfigTest.java to the new testsuite

Signed-off-by: Simon Vacek <simonvacky@email.cz>

* Move EventConfigTest.java to the new testsuite

Part of: #34494

Signed-off-by: Simon Vacek <simonvacky@email.cz>

---------

Signed-off-by: Simon Vacek <simonvacky@email.cz>
This commit is contained in:
Šimon Vacek 2025-02-12 07:20:53 +01:00 committed by GitHub
parent 7e5540896b
commit f5470716fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 165 additions and 127 deletions

View File

@ -87,6 +87,19 @@ public class RealmConfigBuilder {
return this;
}
public RealmConfigBuilder enabledEventTypes(String... enabledEventTypes) {
if (rep.getEnabledEventTypes() == null) {
rep.setEnabledEventTypes(new LinkedList<>());
}
rep.getEnabledEventTypes().addAll(List.of(enabledEventTypes));
return this;
}
public RealmConfigBuilder overwriteEnabledEventTypes(String... enabledEventTypes) {
rep.setEnabledEventTypes(List.of(enabledEventTypes));
return this;
}
public RealmConfigBuilder eventsListeners(String... eventListeners) {
if (rep.getEventsListeners() == null) {
rep.setEventsListeners(new LinkedList<>());
@ -95,6 +108,16 @@ public class RealmConfigBuilder {
return this;
}
public RealmConfigBuilder overwriteEventsListeners(String... eventListeners) {
rep.setEventsListeners(List.of(eventListeners));
return this;
}
public RealmConfigBuilder eventsExpiration(long eventsExpiration) {
rep.setEventsExpiration(eventsExpiration);
return this;
}
public RealmConfigBuilder roles(String... roleNames) {
if (rep.getRoles() == null) {
rep.setRoles(new RolesRepresentation());

View File

@ -0,0 +1,142 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.keycloak.tests.admin.event;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.keycloak.representations.idm.RealmEventsConfigRepresentation;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.RealmConfig;
import org.keycloak.testframework.realm.RealmConfigBuilder;
import java.util.List;
/**
* Test updates to the events configuration.
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
@KeycloakIntegrationTest
public class EventConfigTest {
@InjectRealm(config = EventConfigRealmConfig.class)
ManagedRealm configRealm;
RealmEventsConfigRepresentation configRep;
@BeforeEach
public void setConfigRep() {
configRep = configRealm.admin().getRealmEventsConfig();
}
@Test
public void defaultEventConfigTest() {
configRealm.updateWithCleanup(r -> r.eventsEnabled(false).adminEventsEnabled(false));
configRep = configRealm.admin().getRealmEventsConfig();
Assertions.assertFalse(configRep.isAdminEventsDetailsEnabled());
Assertions.assertFalse(configRep.isAdminEventsEnabled());
Assertions.assertFalse(configRep.isEventsEnabled());
List<String> eventListeners = configRep.getEventsListeners();
Assertions.assertEquals(1, eventListeners.size());
Assertions.assertEquals("jboss-logging", eventListeners.get(0));
}
@Test
public void enableEventsTest() {
Assertions.assertTrue(configRep.isEventsEnabled());
Assertions.assertTrue(configRep.isAdminEventsEnabled());
}
@Test
public void addRemoveListenerTest() {
configRealm.updateWithCleanup(r -> r.eventsEnabled(false).adminEventsEnabled(false).overwriteEventsListeners());
configRep = configRealm.admin().getRealmEventsConfig();
Assertions.assertEquals(0, configRep.getEventsListeners().size());
configRealm.updateWithCleanup(r -> r.overwriteEventsListeners("email"));
configRep = configRealm.admin().getRealmEventsConfig();
List<String> eventListeners = configRep.getEventsListeners();
Assertions.assertEquals(1, eventListeners.size());
Assertions.assertEquals("email", eventListeners.get(0));
}
@Test
public void loginEventSettingsTest() {
Assertions.assertTrue(hasEventType("LOGIN"));
Assertions.assertTrue(hasEventType("LOGOUT"));
Assertions.assertTrue(hasEventType("CLIENT_DELETE_ERROR"));
int defaultEventCount = configRep.getEnabledEventTypes().size();
configRealm.updateWithCleanup(r -> r.enabledEventTypes("CLIENT_DELETE", "CLIENT_DELETE_ERROR"));
List<String> enabledEventTypes = configRealm.admin().getRealmEventsConfig().getEnabledEventTypes();
Assertions.assertEquals(2, enabledEventTypes.size());
// remove all event types
configRealm.updateWithCleanup(RealmConfigBuilder::overwriteEnabledEventTypes);
// removing all event types restores default events
Assertions.assertEquals(defaultEventCount, configRealm.admin().getRealmEventsConfig().getEnabledEventTypes().size());
}
private boolean hasEventType(String eventType) {
for (String event : configRep.getEnabledEventTypes()) {
if (eventType.equals(event)) {
return true;
}
}
return false;
}
@Test
public void includeRepresentationTest() {
Assertions.assertTrue(configRep.isAdminEventsEnabled());
Assertions.assertFalse(configRep.isAdminEventsDetailsEnabled());
configRealm.updateWithCleanup(r -> r.adminEventsDetailsEnabled(true));
Assertions.assertTrue(configRealm.admin().getRealmEventsConfig().isAdminEventsDetailsEnabled());
}
@Test
public void setLoginEventExpirationTest() {
Assertions.assertNull(configRep.getEventsExpiration());
long oneHour = 3600;
configRealm.updateWithCleanup(r -> r.eventsExpiration(oneHour));
Assertions.assertEquals(oneHour, configRealm.admin().getRealmEventsConfig().getEventsExpiration());
}
public static class EventConfigRealmConfig implements RealmConfig {
@Override
public RealmConfigBuilder configure(RealmConfigBuilder realm) {
return realm.eventsEnabled(true).adminEventsEnabled(true);
}
}
}

View File

@ -1,127 +0,0 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.keycloak.testsuite.admin.event;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Test updates to the events configuration.
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class EventConfigTest extends AbstractEventTest {
@Test
public void defaultEventConfigTest() {
assertFalse(configRep.isAdminEventsDetailsEnabled());
assertFalse(configRep.isAdminEventsEnabled());
assertFalse(configRep.isEventsEnabled());
List<String> eventListeners = configRep.getEventsListeners();
assertEquals(1, eventListeners.size());
assertEquals("jboss-logging", eventListeners.get(0));
}
@Test
public void enableEventsTest() {
enableEvents();
assertTrue(configRep.isEventsEnabled());
assertTrue(configRep.isAdminEventsEnabled());
}
@Test
public void addRemoveListenerTest() {
configRep.setEventsListeners(Collections.EMPTY_LIST);
saveConfig();
assertEquals(0, configRep.getEventsListeners().size());
configRep.setEventsListeners(Arrays.asList("email"));
saveConfig();
List<String> eventListeners = configRep.getEventsListeners();
assertEquals(1, eventListeners.size());
assertEquals("email", eventListeners.get(0));
}
@Test
public void loginEventSettingsTest() {
enableEvents();
assertTrue(hasEventType("LOGIN"));
assertTrue(hasEventType("LOGOUT"));
assertTrue(hasEventType("CLIENT_DELETE_ERROR"));
int defaultEventCount = configRep.getEnabledEventTypes().size();
configRep.setEnabledEventTypes(Arrays.asList("CLIENT_DELETE", "CLIENT_DELETE_ERROR"));
saveConfig();
List<String> enabledEventTypes = configRep.getEnabledEventTypes();
assertEquals(2, enabledEventTypes.size());
// remove all event types
configRep.setEnabledEventTypes(Collections.EMPTY_LIST);
saveConfig();
// removing all event types restores default events
assertEquals(defaultEventCount, configRep.getEnabledEventTypes().size());
}
private boolean hasEventType(String eventType) {
for (String event : configRep.getEnabledEventTypes()) {
if (eventType.equals(event)) return true;
}
return false;
}
@Test
public void includeRepresentationTest() {
enableEvents();
assertTrue(configRep.isAdminEventsEnabled());
assertFalse(configRep.isAdminEventsDetailsEnabled());
configRep.setAdminEventsDetailsEnabled(Boolean.TRUE);
saveConfig();
assertTrue(configRep.isAdminEventsDetailsEnabled());
}
@Test
public void setLoginEventExpirationTest() {
enableEvents();
assertNull(configRep.getEventsExpiration());
Long oneHour = 3600L;
configRep.setEventsExpiration(oneHour);
saveConfig();
assertEquals(oneHour, configRep.getEventsExpiration());
}
}