Skip to content

Commit

Permalink
Merge pull request #19658 from sberyozkin/keycloak-test-admin
Browse files Browse the repository at this point in the history
Add KeycloakTestAdmin
  • Loading branch information
stuartwdouglas authored Aug 25, 2021
2 parents 936a66d + 49178e2 commit 5930131
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package io.quarkus.test.junit;

import static io.quarkus.test.junit.IntegrationTestUtil.*;
import static io.quarkus.test.junit.IntegrationTestUtil.determineBuildOutputDirectory;
import static io.quarkus.test.junit.IntegrationTestUtil.determineTestProfileAndProperties;
import static io.quarkus.test.junit.IntegrationTestUtil.doProcessTestInstance;
import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed;
import static io.quarkus.test.junit.IntegrationTestUtil.getSysPropsToRestore;
import static io.quarkus.test.junit.IntegrationTestUtil.handleDevServices;
import static io.quarkus.test.junit.IntegrationTestUtil.readQuarkusArtifactProperties;
import static io.quarkus.test.junit.IntegrationTestUtil.startLauncher;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.Collections;
Expand Down Expand Up @@ -208,23 +215,42 @@ private void injectTestContext(Object testInstance) {
for (Field f : c.getDeclaredFields()) {
if (f.getType().equals(QuarkusIntegrationTest.Context.class)) {
try {
Map<String, String> devServicesPropsCopy = devServicesProps.isEmpty() ? Collections.emptyMap()
: Collections.unmodifiableMap(devServicesProps);
QuarkusIntegrationTest.Context testContext = new DefaultQuarkusIntegrationTestContext(
devServicesPropsCopy);
f.setAccessible(true);
f.set(testInstance, testContext);
f.set(testInstance, createTestContext());
return;
} catch (Exception e) {
throw new RuntimeException("Unable to set field '" + f.getName()
+ "' with the proper test context", e);
}
} else {
Constructor<?> testContextAware = null;
try {
testContextAware = f.getType().getConstructor(QuarkusIntegrationTest.Context.class);
} catch (Exception t) {
continue;
}
if (testContextAware != null) {
try {
Object testContextAwareInstance = testContextAware.newInstance(createTestContext());
f.setAccessible(true);
f.set(testInstance, testContextAwareInstance);
} catch (Exception e) {
throw new RuntimeException("Unable to set field '" + f.getName()
+ "' with the proper test context", e);
}
}
}
}
c = c.getSuperclass();
}
}

private QuarkusIntegrationTest.Context createTestContext() {
Map<String, String> devServicesPropsCopy = devServicesProps.isEmpty() ? Collections.emptyMap()
: Collections.unmodifiableMap(devServicesProps);
return new DefaultQuarkusIntegrationTestContext(devServicesPropsCopy);
}

private void throwBootFailureException() {
if (firstException != null) {
Throwable throwable = firstException;
Expand Down
4 changes: 4 additions & 0 deletions test-framework/keycloak-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-common</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.test.keycloak.server;

import org.eclipse.microprofile.config.ConfigProvider;
import org.keycloak.representations.AccessTokenResponse;

import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.RestAssured;

public class KeycloakTestAdmin {

private final static String AUTH_SERVER_URL_PROP = "quarkus.oidc.auth-server-url";
private final static String CLIENT_ID_PROP = "quarkus.oidc.client-id";
private final static String CLIENT_SECRET_PROP = "quarkus.oidc.credentials.secret";

static {
RestAssured.useRelaxedHTTPSValidation();
}

private QuarkusIntegrationTest.Context testContext;

public KeycloakTestAdmin() {

}

public KeycloakTestAdmin(QuarkusIntegrationTest.Context testContext) {
this.testContext = testContext;
}

public String getAccessToken(String userName) {
return getAccessToken(userName, getClientId());
}

public String getAccessToken(String userName, String clientId) {
return getAccessToken(userName, userName, clientId);
}

public String getAccessToken(String userName, String userSecret, String clientId) {
return getAccessToken(userName, userSecret, clientId, getClientSecret());
}

public String getAccessToken(String userName, String userSecret, String clientId, String clientSecret) {
return RestAssured.given().param("grant_type", "password")
.param("username", userName)
.param("password", userSecret)
.param("client_id", clientId)
.param("client_secret", clientSecret)
.when()
.post(getAuthServerUrl() + "/protocol/openid-connect/token")
.as(AccessTokenResponse.class).getToken();
}

private String getClientId() {
return getPropertyValue(CLIENT_ID_PROP, "quarkus-app");
}

private String getClientSecret() {
return getPropertyValue(CLIENT_SECRET_PROP, "secret");
}

private String getAuthServerUrl() {
String authServerUrl = getPropertyValue(AUTH_SERVER_URL_PROP, null);
if (authServerUrl == null) {
throw new ConfigurationException(AUTH_SERVER_URL_PROP + " is not configured");
}
return authServerUrl;
}

private String getPropertyValue(String prop, String defaultValue) {
return ConfigProvider.getConfig().getOptionalValue(prop, String.class)
.orElseGet(() -> getDevProperty(prop, defaultValue));
}

private String getDevProperty(String prop, String defaultValue) {
String value = testContext == null ? null : testContext.devServicesProperties().get(prop);
return value == null ? defaultValue : value;
}

}

0 comments on commit 5930131

Please sign in to comment.