-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide some more convenience methods/fields
- Loading branch information
Showing
1 changed file
with
39 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |