Skip to content

Commit

Permalink
KEYCLOAK-16533 Improve FluentTestsHelper to better support QS testing
Browse files Browse the repository at this point in the history
  • Loading branch information
vmuzikar authored and Bruno Oliveira da Silva committed Dec 10, 2020
1 parent f053675 commit e5232e0
Showing 1 changed file with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.keycloak.test.builders.ClientBuilder.AccessType.PUBLIC;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -36,6 +37,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.RoleResource;
import org.keycloak.client.registration.Auth;
import org.keycloak.client.registration.ClientRegistration;
Expand All @@ -58,7 +60,7 @@
* <p>
* Usage example:
* <pre>{@code
* new TestsHelper()
* new FluentTestsHelper()
* .init()
* .createDirectGrantClient("direct-grant-client")
* .deleteClient("direct-grant-client")
Expand All @@ -69,7 +71,7 @@
* }</pre>
* </p>
*/
public class FluentTestsHelper {
public class FluentTestsHelper implements Closeable {

protected static class ClientData {
private final ClientRepresentation clientRepresentation;
Expand Down Expand Up @@ -151,7 +153,7 @@ public FluentTestsHelper(String keycloakBaseUrl, String adminUserName, String ad
* @return <code>this</code>
*/
public FluentTestsHelper init() {
keycloak = getKeycloakInstance(keycloakBaseUrl, adminRealm, adminUserName, adminPassword, adminClient);
keycloak = createKeycloakInstance(keycloakBaseUrl, adminRealm, adminUserName, adminPassword, adminClient);
accessToken = generateInitialAccessToken();
isInitialized = true;
return this;
Expand All @@ -164,10 +166,20 @@ public boolean 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);
}

/**
* For more complex test scenarios
*
* @return Keycloak Client instance
*/
public Keycloak getKeycloakInstance() {
assert isInitialized;
return keycloak;
}

protected String generateInitialAccessToken() {
ClientInitialAccessCreatePresentation rep = new ClientInitialAccessCreatePresentation();
rep.setCount(2);
Expand Down Expand Up @@ -256,6 +268,17 @@ public FluentTestsHelper importTestRealm(File realmJsonPath) throws IOException
public FluentTestsHelper importTestRealm(InputStream stream) throws IOException {
ObjectMapper mapper = new ObjectMapper();
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);
testRealm = realmRepresentation.getRealm();
accessToken = generateInitialAccessToken();
Expand All @@ -269,10 +292,21 @@ public FluentTestsHelper importTestRealm(InputStream stream) throws IOException
* @return <code>this</code>
*/
public FluentTestsHelper deleteRealm(String realmName) {
assert isInitialized;
keycloak.realms().realm(realmName).remove();
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.
*
Expand All @@ -281,6 +315,7 @@ public FluentTestsHelper deleteRealm(String realmName) {
* @return <code>this</code>
*/
public FluentTestsHelper createTestUser(String username, String password) {
assert isInitialized;
UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setUsername(username);
userRepresentation.setEnabled(true);
Expand All @@ -303,6 +338,7 @@ public FluentTestsHelper createTestUser(String username, String password) {
* @return <code>this</code>
*/
public FluentTestsHelper assignRoleWithUser(String userName, String roleName) {
assert isInitialized;
if (keycloak.realms().realm(testRealm).roles().get(roleName) == null) {
RoleRepresentation representation = new RoleRepresentation();
representation.setName(roleName);
Expand All @@ -321,6 +357,7 @@ public FluentTestsHelper assignRoleWithUser(String userName, String roleName) {
* @return <code>this</code>
*/
public FluentTestsHelper deleteRole(String roleName) {
assert isInitialized;
RoleResource role = keycloak.realms().realm(testRealm).roles().get(roleName);
if (role != null) {
keycloak.realms().realm(testRealm).roles().deleteRole(roleName);
Expand All @@ -335,14 +372,19 @@ public FluentTestsHelper deleteRole(String roleName) {
* @return <code>this</code>
*/
public FluentTestsHelper deleteTestUser(String userName) {
assert isInitialized;
UserRepresentation userInKeycloak = keycloak.realms().realm(testRealm).users().search(userName).get(0);
if (userInKeycloak != null) {
keycloak.realms().realm(testRealm).users().delete(userInKeycloak.getId());
}
return this;
}

protected String getCreatedId(Response response) {
/**
* @param response
* @return ID of the created record
*/
public String getCreatedId(Response response) {
URI location = response.getLocation();
if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
Response.StatusType statusInfo = response.getStatusInfo();
Expand Down Expand Up @@ -419,4 +461,40 @@ public String getToken() {
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;
}
}

0 comments on commit e5232e0

Please sign in to comment.