Skip to content

Commit

Permalink
provide some more convenience methods/fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dasniko committed Aug 18, 2024
1 parent 33d8405 commit f9ef617
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions utils/src/main/java/de/keycloak/test/TestBase.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
package de.keycloak.test;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import dasniko.testcontainers.keycloak.KeycloakContainer;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import org.keycloak.OAuth2Constants;
import org.keycloak.constants.ServiceUrlConstants;
import org.keycloak.utils.MediaType;

@SuppressWarnings({"unused", "SameParameterValue"})
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings({"unused", "SameParameterValue", "UnusedReturnValue"})
public class TestBase {

protected ValidatableResponse requestToken(KeycloakContainer keycloak, String realm, String username, String password) {
return requestToken(keycloak, realm, username, password, 200);
}

protected ValidatableResponse requestToken(KeycloakContainer keycloak, String realm, String username, String password, int expectedStatusCode) {
String tokenEndpoint = getOpenIDConfiguration(keycloak, realm)
.extract().path("token_endpoint");
return RestAssured.given()
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.formParam(OAuth2Constants.USERNAME, username)
.formParam(OAuth2Constants.PASSWORD, password)
.formParam(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD)
.formParam(OAuth2Constants.CLIENT_ID, KeycloakContainer.ADMIN_CLI_CLIENT)
.formParam(OAuth2Constants.SCOPE, OAuth2Constants.SCOPE_OPENID)
.when().post(tokenEndpoint)
.then().statusCode(expectedStatusCode);
}

protected ValidatableResponse getOpenIDConfiguration(KeycloakContainer keycloak, String realm) {
return RestAssured.given().pathParam("realm-name", realm)
.when().get(keycloak.getAuthServerUrl() + ServiceUrlConstants.DISCOVERY_URL)
.then().statusCode(200);
}
protected final ObjectMapper mapper = new ObjectMapper();
protected final TypeReference<HashMap<String, Object>> mapTypeRef = new TypeReference<>() {};

protected ValidatableResponse requestToken(KeycloakContainer keycloak, String realm, String username, String password) {
return requestToken(keycloak, realm, username, password, 200);
}

protected ValidatableResponse requestToken(KeycloakContainer keycloak, String realm, String username, String password, int expectedStatusCode) {
String tokenEndpoint = getOpenIDConfiguration(keycloak, realm)
.extract().path("token_endpoint");
return RestAssured.given()
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.formParam(OAuth2Constants.USERNAME, username)
.formParam(OAuth2Constants.PASSWORD, password)
.formParam(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD)
.formParam(OAuth2Constants.CLIENT_ID, KeycloakContainer.ADMIN_CLI_CLIENT)
.formParam(OAuth2Constants.SCOPE, OAuth2Constants.SCOPE_OPENID)
.when().post(tokenEndpoint)
.then().statusCode(expectedStatusCode);
}

protected ValidatableResponse getOpenIDConfiguration(KeycloakContainer keycloak, String realm) {
return RestAssured.given().pathParam("realm-name", realm)
.when().get(keycloak.getAuthServerUrl() + ServiceUrlConstants.DISCOVERY_URL)
.then().statusCode(200);
}

protected Map<String, Object> parseToken(String token) throws IOException {
byte[] tokenPayload = Base64.getDecoder().decode(token.split("\\.")[1]);
return mapper.readValue(tokenPayload, mapTypeRef);
}
}

0 comments on commit f9ef617

Please sign in to comment.