KEYCLOAK-16533 Improve FluentTestsHelper to better support QS testing

This commit is contained in:
vmuzikar 2020-12-08 14:46:46 +01:00 committed by Bruno Oliveira da Silva
parent f053675e50
commit e5232e0674

View file

@ -18,6 +18,7 @@ package org.keycloak.test;
import static org.keycloak.test.builders.ClientBuilder.AccessType.PUBLIC; import static org.keycloak.test.builders.ClientBuilder.AccessType.PUBLIC;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@ -36,6 +37,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.RoleResource; import org.keycloak.admin.client.resource.RoleResource;
import org.keycloak.client.registration.Auth; import org.keycloak.client.registration.Auth;
import org.keycloak.client.registration.ClientRegistration; import org.keycloak.client.registration.ClientRegistration;
@ -58,7 +60,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* <p> * <p>
* Usage example: * Usage example:
* <pre>{@code * <pre>{@code
* new TestsHelper() * new FluentTestsHelper()
* .init() * .init()
* .createDirectGrantClient("direct-grant-client") * .createDirectGrantClient("direct-grant-client")
* .deleteClient("direct-grant-client") * .deleteClient("direct-grant-client")
@ -69,7 +71,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* }</pre> * }</pre>
* </p> * </p>
*/ */
public class FluentTestsHelper { public class FluentTestsHelper implements Closeable {
protected static class ClientData { protected static class ClientData {
private final ClientRepresentation clientRepresentation; private final ClientRepresentation clientRepresentation;
@ -151,7 +153,7 @@ public class FluentTestsHelper {
* @return <code>this</code> * @return <code>this</code>
*/ */
public FluentTestsHelper init() { public FluentTestsHelper init() {
keycloak = getKeycloakInstance(keycloakBaseUrl, adminRealm, adminUserName, adminPassword, adminClient); keycloak = createKeycloakInstance(keycloakBaseUrl, adminRealm, adminUserName, adminPassword, adminClient);
accessToken = generateInitialAccessToken(); accessToken = generateInitialAccessToken();
isInitialized = true; isInitialized = true;
return this; return this;
@ -164,10 +166,20 @@ public class FluentTestsHelper {
return isInitialized; return isInitialized;
} }
protected Keycloak getKeycloakInstance(String keycloakBaseUrl, String realm, String username, String password, String clientId) { protected Keycloak createKeycloakInstance(String keycloakBaseUrl, String realm, String username, String password, String clientId) {
return Keycloak.getInstance(keycloakBaseUrl, realm, username, password, clientId); return Keycloak.getInstance(keycloakBaseUrl, realm, username, password, clientId);
} }
/**
* For more complex test scenarios
*
* @return Keycloak Client instance
*/
public Keycloak getKeycloakInstance() {
assert isInitialized;
return keycloak;
}
protected String generateInitialAccessToken() { protected String generateInitialAccessToken() {
ClientInitialAccessCreatePresentation rep = new ClientInitialAccessCreatePresentation(); ClientInitialAccessCreatePresentation rep = new ClientInitialAccessCreatePresentation();
rep.setCount(2); rep.setCount(2);
@ -256,6 +268,17 @@ public class FluentTestsHelper {
public FluentTestsHelper importTestRealm(InputStream stream) throws IOException { public FluentTestsHelper importTestRealm(InputStream stream) throws IOException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
RealmRepresentation realmRepresentation = mapper.readValue(stream, RealmRepresentation.class); RealmRepresentation realmRepresentation = mapper.readValue(stream, RealmRepresentation.class);
return createTestRealm(realmRepresentation);
}
/**
* Creates a test realm.
*
* @param realmRepresentation A test realm representation.
* @return <code>this</code>
*/
public FluentTestsHelper createTestRealm(RealmRepresentation realmRepresentation) {
assert isInitialized;
keycloak.realms().create(realmRepresentation); keycloak.realms().create(realmRepresentation);
testRealm = realmRepresentation.getRealm(); testRealm = realmRepresentation.getRealm();
accessToken = generateInitialAccessToken(); accessToken = generateInitialAccessToken();
@ -269,10 +292,21 @@ public class FluentTestsHelper {
* @return <code>this</code> * @return <code>this</code>
*/ */
public FluentTestsHelper deleteRealm(String realmName) { public FluentTestsHelper deleteRealm(String realmName) {
assert isInitialized;
keycloak.realms().realm(realmName).remove(); keycloak.realms().realm(realmName).remove();
return this; return this;
} }
/**
* Deletes the test realm. Meant to be called after testing has finished.
*
* @return <code>this</code>
*/
public FluentTestsHelper deleteTestRealm() {
deleteRealm(testRealm);
return this;
}
/** /**
* Creates a test user. * Creates a test user.
* *
@ -281,6 +315,7 @@ public class FluentTestsHelper {
* @return <code>this</code> * @return <code>this</code>
*/ */
public FluentTestsHelper createTestUser(String username, String password) { public FluentTestsHelper createTestUser(String username, String password) {
assert isInitialized;
UserRepresentation userRepresentation = new UserRepresentation(); UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setUsername(username); userRepresentation.setUsername(username);
userRepresentation.setEnabled(true); userRepresentation.setEnabled(true);
@ -303,6 +338,7 @@ public class FluentTestsHelper {
* @return <code>this</code> * @return <code>this</code>
*/ */
public FluentTestsHelper assignRoleWithUser(String userName, String roleName) { public FluentTestsHelper assignRoleWithUser(String userName, String roleName) {
assert isInitialized;
if (keycloak.realms().realm(testRealm).roles().get(roleName) == null) { if (keycloak.realms().realm(testRealm).roles().get(roleName) == null) {
RoleRepresentation representation = new RoleRepresentation(); RoleRepresentation representation = new RoleRepresentation();
representation.setName(roleName); representation.setName(roleName);
@ -321,6 +357,7 @@ public class FluentTestsHelper {
* @return <code>this</code> * @return <code>this</code>
*/ */
public FluentTestsHelper deleteRole(String roleName) { public FluentTestsHelper deleteRole(String roleName) {
assert isInitialized;
RoleResource role = keycloak.realms().realm(testRealm).roles().get(roleName); RoleResource role = keycloak.realms().realm(testRealm).roles().get(roleName);
if (role != null) { if (role != null) {
keycloak.realms().realm(testRealm).roles().deleteRole(roleName); keycloak.realms().realm(testRealm).roles().deleteRole(roleName);
@ -335,6 +372,7 @@ public class FluentTestsHelper {
* @return <code>this</code> * @return <code>this</code>
*/ */
public FluentTestsHelper deleteTestUser(String userName) { public FluentTestsHelper deleteTestUser(String userName) {
assert isInitialized;
UserRepresentation userInKeycloak = keycloak.realms().realm(testRealm).users().search(userName).get(0); UserRepresentation userInKeycloak = keycloak.realms().realm(testRealm).users().search(userName).get(0);
if (userInKeycloak != null) { if (userInKeycloak != null) {
keycloak.realms().realm(testRealm).users().delete(userInKeycloak.getId()); keycloak.realms().realm(testRealm).users().delete(userInKeycloak.getId());
@ -342,7 +380,11 @@ public class FluentTestsHelper {
return this; return this;
} }
protected String getCreatedId(Response response) { /**
* @param response
* @return ID of the created record
*/
public String getCreatedId(Response response) {
URI location = response.getLocation(); URI location = response.getLocation();
if (!response.getStatusInfo().equals(Response.Status.CREATED)) { if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
Response.StatusType statusInfo = response.getStatusInfo(); Response.StatusType statusInfo = response.getStatusInfo();
@ -419,4 +461,40 @@ public class FluentTestsHelper {
return keycloak.tokenManager().getAccessTokenString(); return keycloak.tokenManager().getAccessTokenString();
} }
public String getKeycloakBaseUrl() {
return keycloakBaseUrl;
}
public String getAdminUserName() {
return adminUserName;
}
public String getAdminPassword() {
return adminPassword;
}
public String getAdminClientId() {
return adminClient;
}
public String getAdminRealmName() {
return adminRealm;
}
public String getTestRealmName() {
return testRealm;
}
public RealmResource getTestRealmResource() {
assert isInitialized;
return keycloak.realm(testRealm);
}
@Override
public void close() {
if (keycloak != null && !keycloak.isClosed()) {
keycloak.close();
}
isInitialized = false;
}
} }