From 3e3fb6277000a372201dd4a8b869690a5b05b519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Barto=C5=A1?= Date: Wed, 4 Oct 2023 12:50:18 +0200 Subject: [PATCH] Improve test coverage for Admin hostname properties (#23535) Closes #23534 --- .../it/cli/dist/HostnameDistTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/HostnameDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/HostnameDistTest.java index 5b615d3115..db4eebbd2c 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/HostnameDistTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/HostnameDistTest.java @@ -19,6 +19,7 @@ package org.keycloak.it.cli.dist; import io.quarkus.test.junit.main.Launch; import io.restassured.RestAssured; +import org.apache.commons.lang3.StringUtils; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.jupiter.api.BeforeAll; @@ -28,7 +29,10 @@ import org.keycloak.it.junit5.extension.RawDistOnly; import org.keycloak.protocol.oidc.representations.OIDCConfigurationRepresentation; import org.keycloak.quarkus.runtime.services.resources.DebugHostnameSettingsResource; +import java.util.function.Consumer; + import static io.restassured.RestAssured.when; +import static org.hamcrest.MatcherAssert.assertThat; @DistributionTest(keepAlive = true, enableTls = true, defaultOptions = { "--http-enabled=true" }) @RawDistOnly(reason = "Containers are immutable") @@ -153,6 +157,27 @@ public class HostnameDistTest { public void testHostnameAdminSet() { when().get("https://mykeycloak.org:8443/admin/master/console").then().body(Matchers.containsString("\"authUrl\": \"https://mykeycloakadmin.org:8443\"")); when().get("https://mykeycloak.org:8443/realms/master/protocol/openid-connect/auth?client_id=security-admin-console&redirect_uri=https://mykeycloakadmin.org:8443/admin/master/console&state=02234324-d91e-4bf2-8396-57498e96b12a&response_mode=fragment&response_type=code&scope=openid&nonce=f8f3812e-e349-4bbf-8d15-cbba4927f5e5&code_challenge=7qjD_v11WGkt1ig-ZFHxJdrEvuTlzjFRgRGQ_5ADcko&code_challenge_method=S256").then().body(Matchers.containsString("Sign in to your account")); + + when().get("http://localhost:8080/admin/master/console").then().body(Matchers.containsString("\"authUrl\": \"http://mykeycloakadmin.org:8080\"")); + } + + @Test + @Launch({"start", "--hostname=mykeycloak.org", "--hostname-debug=true"}) + public void testHostnameAdminFromHeaders() { + when().get("https://mykeycloak.org:8443/admin/master/console").then().body(Matchers.containsString("\"authUrl\": \"https://mykeycloak.org:8443\"")); + when().get("https://mykeycloak.org:8443/realms/master/protocol/openid-connect/auth?client_id=security-admin-console&redirect_uri=https://mykeycloak.org:8443/admin/master/console&state=02234324-d91e-4bf2-8396-57498e96b12a&response_mode=fragment&response_type=code&scope=openid&nonce=f8f3812e-e349-4bbf-8d15-cbba4927f5e5&code_challenge=7qjD_v11WGkt1ig-ZFHxJdrEvuTlzjFRgRGQ_5ADcko&code_challenge_method=S256").then().body(Matchers.containsString("Sign in to your account")); + + // Admin URL should be resolved from headers + when().get("http://localhost:8080/admin/master/console").then().body(Matchers.containsString("\"authUrl\": \"http://localhost:8080\"")); + when().get("http://localhost:8080/realms/master/protocol/openid-connect/auth?client_id=security-admin-console&redirect_uri=http://localhost:8080/admin/master/console&state=02234324-d91e-4bf2-8396-57498e96b12a&response_mode=fragment&response_type=code&scope=openid&nonce=f8f3812e-e349-4bbf-8d15-cbba4927f5e5&code_challenge=7qjD_v11WGkt1ig-ZFHxJdrEvuTlzjFRgRGQ_5ADcko&code_challenge_method=S256").then().body(Matchers.containsString("Sign in to your account")); + + Consumer assertDebugAdmin = (url) -> { + final var body = StringUtils.deleteWhitespace(when().get(url + "/realms/master/hostname-debug").then().extract().response().body().asString()); + assertThat(body, Matchers.containsString("Admin" + url)); + }; + + assertDebugAdmin.accept("https://mykeycloak.org:8443"); + assertDebugAdmin.accept("http://localhost:8080"); } @Test @@ -171,6 +196,7 @@ public class HostnameDistTest { @Launch({ "start", "--proxy=edge", "--hostname=mykeycloak.org", "--hostname-admin-url=http://mykeycloakadmin.org:1234" }) public void testAdminUrl() { when().get("https://mykeycloak.org:8443").then().body(Matchers.containsString("http://mykeycloakadmin.org:1234/admin/")); + when().get("http://localhost:8080/admin/master/console").then().body(Matchers.containsString("\"authUrl\": \"http://mykeycloakadmin.org:1234\"")); } @Test