Skip to content
Snippets Groups Projects
Verified Commit 25a52f40 authored by Ira ¯\_(ツ)_/¯'s avatar Ira ¯\_(ツ)_/¯
Browse files

Add health checks and token generation; add authenticated request test

parent 8c1aa1fb
No related branches found
No related tags found
No related merge requests found
package org.archlinux.keycloak.mailpass.rest;
import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.connection.waiting.HealthChecks;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.post;
import static org.junit.Assert.assertEquals;
import java.text.MessageFormat;
public class MailPassResourceIntegrationTest {
private final static String ROOT_URI = "http://localhost:8080/auth/realms/master/mailpass/roleauth";
private final static String ROOT_URI = "http://localhost:{0,number,#}/auth/realms/master/mailpass/roleauth";
private final static String TOKEN_GEN_URI = "http://localhost:{0,number,#}/auth/realms/master/protocol/openid-connect/token";
private static String token;
private static int port;
@ClassRule
public static DockerComposeRule docker = DockerComposeRule.builder()
.file("src/integrationTest/resources/docker-compose.yml").build();
.file("src/integrationTest/resources/docker-compose.yml").waitingForService("keycloak", HealthChecks
.toRespondOverHttp(8080, (port) -> port.inFormat("http://$HOST:$EXTERNAL_PORT/auth/realms/master")))
.build();
@BeforeClass
public static void setup() {
port = docker.containers().container("keycloak").port(8080).getExternalPort();
Response response = given().accept(ContentType.JSON).contentType(ContentType.URLENC)
.body("grant_type=password&username=admin&password=admin&client_id=admin-cli")
.post(MessageFormat.format(TOKEN_GEN_URI, port));
token = response.jsonPath().getString("access_token");
}
@Test
public void testNonAuthenticatedAccess() {
Response response = post(ROOT_URI + "/compute-password-hash");
public void testNonAuthenticatedAccessReturns401() {
Response response = given().contentType(ContentType.JSON).body("{ \"password\": \"password\" }").when()
.post(MessageFormat.format(ROOT_URI, port) + "/compute-password-hash");
assertEquals(401, response.getStatusCode());
}
@Test
public void testAuthenticatedAccessReturns200() {
Response response = given().header("Authorization", "Bearer " + token).contentType(ContentType.JSON)
.body("{ \"password\": \"password\" }").when()
.post(MessageFormat.format(ROOT_URI, port) + "/compute-password-hash");
assertEquals(201, response.getStatusCode());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment