mirror of
https://github.com/keycloak/keycloak.git
synced 2026-01-25 16:42:34 +00:00
[Test framework] AdminEventAuthDetailsTest migration (#37910)
Signed-off-by: Lukas Hanusovsky <lhanusov@redhat.com>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* 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.admin.client.Keycloak;
|
||||
import org.keycloak.events.admin.OperationType;
|
||||
import org.keycloak.events.admin.ResourceType;
|
||||
import org.keycloak.models.AdminRoles;
|
||||
import org.keycloak.models.Constants;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.testframework.annotations.InjectAdminClient;
|
||||
import org.keycloak.testframework.annotations.InjectAdminEvents;
|
||||
import org.keycloak.testframework.annotations.InjectRealm;
|
||||
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
|
||||
import org.keycloak.testframework.events.AdminEventAssertion;
|
||||
import org.keycloak.testframework.events.AdminEvents;
|
||||
import org.keycloak.testframework.realm.ManagedRealm;
|
||||
import org.keycloak.testframework.realm.RealmConfig;
|
||||
import org.keycloak.testframework.realm.RealmConfigBuilder;
|
||||
import org.keycloak.tests.utils.admin.ApiUtil;
|
||||
import org.keycloak.tests.utils.admin.AdminEventPaths;
|
||||
import org.opentest4j.AssertionFailedError;
|
||||
|
||||
/**
|
||||
* Test authDetails in admin events
|
||||
*
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
@KeycloakIntegrationTest
|
||||
public class AdminEventAuthDetailsTest {
|
||||
|
||||
@InjectRealm(config = AdminEventsAuthDetailsRealmConfig.class)
|
||||
ManagedRealm managedRealm;
|
||||
|
||||
@InjectAdminClient(ref = "admin", mode = InjectAdminClient.Mode.MANAGED_REALM, client = "client", user = "admin")
|
||||
Keycloak adminClient;
|
||||
|
||||
@InjectAdminEvents
|
||||
AdminEvents adminEvents;
|
||||
|
||||
private String clientUuid;
|
||||
private String adminId;
|
||||
private String appUserId;
|
||||
private String adminCliUuid;
|
||||
|
||||
@BeforeEach
|
||||
public void initConfig() {
|
||||
clientUuid = adminClient.realm(managedRealm.getName()).clients().findByClientId("client").get(0).getId();
|
||||
adminId = adminClient.realm(managedRealm.getName()).users().search("admin", true).get(0).getId();
|
||||
appUserId = adminClient.realm(managedRealm.getName()).users().search("app-user", true).get(0).getId();
|
||||
adminCliUuid = ApiUtil.findClientByClientId(managedRealm.admin(), Constants.ADMIN_CLI_CLIENT_ID).toRepresentation().getId();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthSuccess() {
|
||||
testClient(adminClient, appUserId, managedRealm.getId(), clientUuid, adminId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthFailure() {
|
||||
Assertions.assertThrows(AssertionFailedError.class, () -> testClient(adminClient, appUserId, managedRealm.getId(), adminCliUuid, adminId));
|
||||
}
|
||||
|
||||
private void testClient(Keycloak admin, String userId, String expectedRealmId, String expectedClientUuid, String expectedUserId) {
|
||||
UserRepresentation rep = admin.realm(managedRealm.getName()).users().get(userId).toRepresentation();
|
||||
rep.setEmail("foo@email.org");
|
||||
admin.realm(managedRealm.getName()).users().get(userId).update(rep);
|
||||
|
||||
AdminEventAssertion.assertEvent(adminEvents.poll(), OperationType.UPDATE, AdminEventPaths.userResourcePath(userId), rep, ResourceType.USER)
|
||||
.auth(expectedRealmId, expectedClientUuid, expectedUserId);
|
||||
}
|
||||
|
||||
public static class AdminEventsAuthDetailsRealmConfig implements RealmConfig {
|
||||
|
||||
@Override
|
||||
public RealmConfigBuilder configure(RealmConfigBuilder realm) {
|
||||
realm.addClient("client").name("client").publicClient(true).directAccessGrants();
|
||||
realm.addUser("admin").password("password").name("My", "Admin").email("admin@localhost")
|
||||
.emailVerified().clientRoles(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN);
|
||||
realm.addUser("app-user").password("password");
|
||||
|
||||
return realm;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* 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.Before;
|
||||
import org.junit.ComparisonFailure;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.keycloak.admin.client.Keycloak;
|
||||
import org.keycloak.admin.client.resource.RealmResource;
|
||||
import org.keycloak.events.admin.OperationType;
|
||||
import org.keycloak.events.admin.ResourceType;
|
||||
import org.keycloak.models.AdminRoles;
|
||||
import org.keycloak.models.Constants;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.testsuite.AbstractAuthTest;
|
||||
import org.keycloak.testsuite.Assert;
|
||||
import org.keycloak.testsuite.admin.ApiUtil;
|
||||
import org.keycloak.testsuite.util.AdminEventPaths;
|
||||
import org.keycloak.testsuite.util.AssertAdminEvents;
|
||||
import org.keycloak.testsuite.util.ClientBuilder;
|
||||
import org.keycloak.testsuite.util.RealmBuilder;
|
||||
import org.keycloak.testsuite.util.UserBuilder;
|
||||
import org.keycloak.testsuite.utils.tls.TLSUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.After;
|
||||
|
||||
import static org.keycloak.testsuite.auth.page.AuthRealm.ADMIN;
|
||||
import static org.keycloak.testsuite.auth.page.AuthRealm.MASTER;
|
||||
import static org.keycloak.testsuite.util.ServerURLs.getAuthServerContextRoot;
|
||||
|
||||
/**
|
||||
* Test authDetails in admin events
|
||||
*
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
public class AdminEventAuthDetailsTest extends AbstractAuthTest {
|
||||
|
||||
@Rule
|
||||
public AssertAdminEvents assertAdminEvents = new AssertAdminEvents(this);
|
||||
|
||||
private String masterAdminCliUuid;
|
||||
private String masterAdminUserId;
|
||||
private String masterAdminUser2Id;
|
||||
|
||||
private String testRealmId;
|
||||
private String masterRealmId;
|
||||
private String client1Uuid;
|
||||
private String adminCliUuid;
|
||||
private String admin1Id;
|
||||
private String admin2Id;
|
||||
private String appUserId;
|
||||
|
||||
@Override
|
||||
public void addTestRealms(List<RealmRepresentation> testRealms) {
|
||||
RealmBuilder realm = RealmBuilder.create().name("test").testEventListener();
|
||||
client1Uuid = KeycloakModelUtils.generateId();
|
||||
realm.client(ClientBuilder.create().id(client1Uuid).clientId("client1").publicClient().directAccessGrants());
|
||||
|
||||
admin1Id = KeycloakModelUtils.generateId();
|
||||
realm.user(UserBuilder.create().id(admin1Id).username("admin1").password("password").role(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN));
|
||||
|
||||
admin2Id = KeycloakModelUtils.generateId();
|
||||
realm.user(UserBuilder.create().id(admin2Id).username("admin2").password("password").role(Constants.REALM_MANAGEMENT_CLIENT_ID, AdminRoles.REALM_ADMIN));
|
||||
|
||||
appUserId = KeycloakModelUtils.generateId();
|
||||
realm.user(UserBuilder.create().id(appUserId).username("app-user").password("password"));
|
||||
|
||||
testRealms.add(realm.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importTestRealms() {
|
||||
super.importTestRealms();
|
||||
client1Uuid = adminClient.realm("test").clients().findByClientId("client1").get(0).getId();
|
||||
admin1Id = adminClient.realm("test").users().search("admin1", true).get(0).getId();
|
||||
admin2Id = adminClient.realm("test").users().search("admin2", true).get(0).getId();
|
||||
appUserId = adminClient.realm("test").users().search("app-user", true).get(0).getId();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initConfig() {
|
||||
RealmResource masterRealm = adminClient.realm(MASTER);
|
||||
masterRealmId = masterRealm.toRepresentation().getId();
|
||||
masterAdminCliUuid = ApiUtil.findClientByClientId(masterRealm, Constants.ADMIN_CLI_CLIENT_ID).toRepresentation().getId();
|
||||
masterAdminUserId = ApiUtil.findUserByUsername(masterRealm, "admin").getId();
|
||||
masterAdminUser2Id = ApiUtil.createUserAndResetPasswordWithAdminClient(masterRealm, UserBuilder.create().username("admin2").build(), "password");
|
||||
masterRealm.users().get(masterAdminUser2Id).roles().realmLevel().add(Collections.singletonList(masterRealm.roles().get("admin").toRepresentation()));
|
||||
|
||||
RealmResource testRealm = adminClient.realm("test");
|
||||
testRealmId = testRealm.toRepresentation().getId();
|
||||
adminCliUuid = ApiUtil.findClientByClientId(testRealm, Constants.ADMIN_CLI_CLIENT_ID).toRepresentation().getId();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
adminClient.realm(MASTER).users().get(masterAdminUser2Id).remove();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuth() {
|
||||
testClient(MASTER, ADMIN, ADMIN, Constants.ADMIN_CLI_CLIENT_ID, masterRealmId, masterAdminCliUuid, masterAdminUserId);
|
||||
testClient(MASTER, "admin2", "password", Constants.ADMIN_CLI_CLIENT_ID, masterRealmId, masterAdminCliUuid, masterAdminUser2Id);
|
||||
|
||||
testClient("test", "admin1", "password", Constants.ADMIN_CLI_CLIENT_ID, testRealmId, adminCliUuid, admin1Id);
|
||||
testClient("test", "admin2", "password", Constants.ADMIN_CLI_CLIENT_ID, testRealmId, adminCliUuid, admin2Id);
|
||||
testClient("test", "admin1", "password", "client1", testRealmId, client1Uuid, admin1Id);
|
||||
testClient("test", "admin2", "password", "client1", testRealmId, client1Uuid, admin2Id);
|
||||
|
||||
// Should fail due to different client UUID
|
||||
try {
|
||||
testClient("test", "admin1", "password", "client1", testRealmId, adminCliUuid, admin1Id);
|
||||
Assert.fail("Not expected to pass");
|
||||
} catch (ComparisonFailure expected) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// Should fail due to different user ID
|
||||
try {
|
||||
testClient("test", "admin1", "password", "client1", testRealmId, client1Uuid, admin2Id);
|
||||
Assert.fail("Not expected to pass");
|
||||
} catch (ComparisonFailure expected) {
|
||||
// expected
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void testClient(String realmName, String username, String password, String clientId, String expectedRealmId, String expectedClientUuid, String expectedUserId) {
|
||||
try (Keycloak keycloak = Keycloak.getInstance(getAuthServerContextRoot() + "/auth",
|
||||
realmName, username, password, clientId, TLSUtils.initializeTLS())) {
|
||||
UserRepresentation rep = UserBuilder.create().id(appUserId).username("app-user").email("foo@email.org").build();
|
||||
keycloak.realm("test").users().get(appUserId).update(rep);
|
||||
|
||||
assertAdminEvents.expect()
|
||||
.realmId(testRealmId)
|
||||
.operationType(OperationType.UPDATE)
|
||||
.resourcePath(AdminEventPaths.userResourcePath(appUserId))
|
||||
.resourceType(ResourceType.USER)
|
||||
.representation(rep)
|
||||
.authDetails(expectedRealmId, expectedClientUuid, expectedUserId)
|
||||
.assertEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user