From 56d5e2b3db150c0ef40286d32035c1d3335083c8 Mon Sep 17 00:00:00 2001 From: Miquel Simon Date: Mon, 25 Nov 2024 18:06:34 +0100 Subject: [PATCH] Migrate AdminConsoleLandingPageTest to the new framework Part of #34494 Signed-off-by: Miquel Simon --- .../admin/AdminConsoleLandingPageTest.java | 78 +++++++++++++++ .../admin/AdminConsoleLandingPageTest.java | 94 ------------------- 2 files changed, 78 insertions(+), 94 deletions(-) create mode 100644 tests/base/src/test/java/org/keycloak/test/admin/AdminConsoleLandingPageTest.java delete mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java diff --git a/tests/base/src/test/java/org/keycloak/test/admin/AdminConsoleLandingPageTest.java b/tests/base/src/test/java/org/keycloak/test/admin/AdminConsoleLandingPageTest.java new file mode 100644 index 000000000000..cf1426929af5 --- /dev/null +++ b/tests/base/src/test/java/org/keycloak/test/admin/AdminConsoleLandingPageTest.java @@ -0,0 +1,78 @@ +package org.keycloak.test.admin; + +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.util.EntityUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.keycloak.test.framework.annotations.InjectHttpClient; +import org.keycloak.test.framework.annotations.InjectKeycloakUrls; +import org.keycloak.test.framework.annotations.KeycloakIntegrationTest; +import org.keycloak.test.framework.server.KeycloakUrls; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@KeycloakIntegrationTest +public class AdminConsoleLandingPageTest { + + @InjectKeycloakUrls + KeycloakUrls keycloakUrls; + + @InjectHttpClient + HttpClient httpClient; + + @Test + public void landingPage() throws IOException { + String body = EntityUtils.toString(httpClient.execute(new HttpGet(keycloakUrls.getBaseUrl().toString() + "/admin/master/console")).getEntity()); + + Map config = getConfig(body); + String authUrl = config.get("authUrl"); + Assertions.assertEquals(keycloakUrls.getBaseUrl().toString()+ "", authUrl); + + String resourceUrl = config.get("resourceUrl"); + Assertions.assertTrue(resourceUrl.matches("/resources/[^/]*/admin/keycloak.v2")); + + String consoleBaseUrl = config.get("consoleBaseUrl"); + Assertions.assertEquals(consoleBaseUrl, "/admin/master/console/"); + + Pattern p = Pattern.compile("link href=\"([^\"]*)\""); + Matcher m = p.matcher(body); + + while(m.find()) { + String url = m.group(1); + Assertions.assertTrue(url.startsWith("/resources/")); + } + + p = Pattern.compile("script src=\"([^\"]*)\""); + m = p.matcher(body); + + while(m.find()) { + String url = m.group(1); + if (url.contains("keycloak.js")) { + Assertions.assertTrue(url.startsWith("/js/"), url); + } else { + Assertions.assertTrue(url.startsWith("/resources/"), url); + } + } + } + + private static Map getConfig(String body) { + Map variables = new HashMap<>(); + String start = ""; + + String config = body.substring(body.indexOf(start) + start.length()); + config = config.substring(0, config.indexOf(end)).trim(); + + Matcher matcher = Pattern.compile(".*\"(.*)\": \"(.*)\"").matcher(config); + while (matcher.find()) { + variables.put(matcher.group(1), matcher.group(2)); + } + + return variables; + } +} diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java deleted file mode 100644 index 6d849c10ce60..000000000000 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package org.keycloak.testsuite.admin; - -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.keycloak.broker.provider.util.SimpleHttp; -import org.keycloak.representations.idm.RealmRepresentation; -import org.keycloak.testsuite.AbstractKeycloakTest; -import org.keycloak.testsuite.broker.util.SimpleHttpDefault; - -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class AdminConsoleLandingPageTest extends AbstractKeycloakTest { - - private CloseableHttpClient client; - - @Before - public void before() { - client = HttpClientBuilder.create().build(); - } - - @After - public void after() { - try { - client.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public void addTestRealms(List testRealms) { - } - - @Test - public void landingPage() throws IOException { - String body = SimpleHttpDefault.doGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/admin/master/console", client).asString(); - - Map config = getConfig(body); - String authUrl = config.get("authUrl"); - Assert.assertEquals(suiteContext.getAuthServerInfo().getContextRoot() + "/auth", authUrl); - - String resourceUrl = config.get("resourceUrl"); - Assert.assertTrue(resourceUrl.matches("/auth/resources/[^/]*/admin/keycloak.v2")); - - String consoleBaseUrl = config.get("consoleBaseUrl"); - Assert.assertEquals(consoleBaseUrl, "/auth/admin/master/console/"); - - Pattern p = Pattern.compile("link href=\"([^\"]*)\""); - Matcher m = p.matcher(body); - - while(m.find()) { - String url = m.group(1); - Assert.assertTrue(url.startsWith("/auth/resources/")); - } - - p = Pattern.compile("script src=\"([^\"]*)\""); - m = p.matcher(body); - - while(m.find()) { - String url = m.group(1); - if (url.contains("keycloak.js")) { - Assert.assertTrue(url, url.startsWith("/auth/js/")); - } else { - Assert.assertTrue(url, url.startsWith("/auth/resources/")); - } - } - } - - private static Map getConfig(String body) { - Map variables = new HashMap<>(); - String start = ""; - - String config = body.substring(body.indexOf(start) + start.length()); - config = config.substring(0, config.indexOf(end)).trim(); - - Matcher matcher = Pattern.compile(".*\"(.*)\": \"(.*)\"").matcher(config); - while (matcher.find()) { - variables.put(matcher.group(1), matcher.group(2)); - } - - return variables; - } - -}