From 9ee5a51b5cf9281231a56b82f5a7f1d0d0054ae0 Mon Sep 17 00:00:00 2001 From: pvannierop Date: Tue, 9 Jan 2024 17:36:31 +0100 Subject: [PATCH] test refactor --- .../security/AbstractContainerTest.java | 19 +++--- .../security/OAuth2AuthIntegrationTest.java | 53 ++--------------- .../OAuth2ResourceServerIntegrationTest.java | 58 +------------------ .../security/SamlAuthIntegrationTest.java | 53 ++--------------- 4 files changed, 21 insertions(+), 162 deletions(-) diff --git a/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java b/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java index e85703a5a55..c148365687c 100644 --- a/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/AbstractContainerTest.java @@ -5,6 +5,7 @@ import org.cbioportal.test.integration.OAuth2KeycloakInitializer; import org.cbioportal.test.integration.SamlKeycloakInitializer; import org.openqa.selenium.chrome.ChromeOptions; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationListener; @@ -68,8 +69,7 @@ public class AbstractContainerTest { sessionServiceContainer.setPortBindings(ImmutableList.of(String.format("%s:5000", SESSION_SERVICE_PORT))); mongoContainer = new GenericContainer(DockerImageName.parse(MONGO_IMAGE_VERSION)) - .withEnv("MONGO_INITDB_DATABASE", "session_service") - .withReuse(true); + .withEnv("MONGO_INITDB_DATABASE", "session_service"); mongoContainer.setPortBindings(ImmutableList.of(String.format("%s:27017", MONGO_PORT, MONGO_PORT))); keycloakContainer = new KeycloakContainer(KEYCLOAK_IMAGE_VERSION) @@ -77,8 +77,7 @@ public class AbstractContainerTest { .withAdminUsername("admin") .withAdminPassword("admin") .withEnv("KC_HOSTNAME", "host.testcontainers.internal") - .withEnv("KC_HOSTNAME_ADMIN", "localhost") - .withReuse(true); + .withEnv("KC_HOSTNAME_ADMIN", "localhost"); keycloakContainer.setPortBindings(ImmutableList.of(String.format("%s:8080", KEYCLOAK_PORT))); mockServerContainer = new GenericContainer(MOCKSERVER_IMAGE_VERSION) @@ -88,8 +87,7 @@ public class AbstractContainerTest { mysqlContainer = (MySQLContainer) new MySQLContainer(MYSQL_IMAGE_VERSION) .withClasspathResourceMapping("cgds.sql", "/docker-entrypoint-initdb.d/a_schema.sql", BindMode.READ_ONLY) .withClasspathResourceMapping("seed_mini.sql", "/docker-entrypoint-initdb.d/b_seed.sql", BindMode.READ_ONLY) - .withStartupTimeout(Duration.ofMinutes(10)) - .withReuse(true); + .withStartupTimeout(Duration.ofMinutes(10)); mysqlContainer.setPortBindings(ImmutableList.of(String.format("%s:3306", MYSQL_PORT))); ChromeOptions options = new ChromeOptions(); @@ -104,8 +102,7 @@ public class AbstractContainerTest { // activate this to record movies of the tests (greate for debugging) .withRecordingMode(RECORD_ALL, new File("/home/pnp300")) .withAccessToHost(true) - .withCapabilities(options) - .withReuse(true); + .withCapabilities(options); sessionServiceContainer.start(); mongoContainer.start(); @@ -149,9 +146,13 @@ public static class PortInitializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { + TestPropertyValues values = TestPropertyValues.of( + "server.port=" + CBIO_PORT + ); + values.applyTo(applicationContext); applicationContext.addApplicationListener( (ApplicationListener) event -> { - Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, SESSION_SERVICE_PORT); + Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, MONGO_PORT); }); } } diff --git a/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java b/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java index f46d6859f6f..5cfe2ca673c 100644 --- a/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/OAuth2AuthIntegrationTest.java @@ -1,25 +1,18 @@ package org.cbioportal.test.integration.security; import org.cbioportal.PortalApplication; -import org.cbioportal.test.integration.MysqlInitializer; -import org.cbioportal.test.integration.OAuth2KeycloakInitializer; import org.cbioportal.test.integration.security.util.Util; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.context.WebServerInitializedEvent; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import org.testcontainers.Testcontainers; -import static org.cbioportal.test.integration.security.OAuth2AuthIntegrationTest.MyKeycloakInitializer; +import static org.cbioportal.test.integration.security.AbstractContainerTest.*; @RunWith(SpringRunner.class) @SpringBootTest( @@ -28,9 +21,6 @@ ) @TestPropertySource( properties = { - "app.name=cbioportal", - "server.port=8080", - "filter_groups_by_appname=false", "authenticate=oauth2", "dat.method=oauth2", // DB settings (also see MysqlInitializer) @@ -56,51 +46,16 @@ } ) @ContextConfiguration(initializers = { - OAuth2AuthIntegrationTest.MyMysqlInitializer.class, - MyKeycloakInitializer.class, - OAuth2AuthIntegrationTest.PortInitializer.class + MyMysqlInitializer.class, + MyOAuth2KeycloakInitializer.class, + PortInitializer.class }) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @DirtiesContext // needed to reuse port 8080 for multiple tests public class OAuth2AuthIntegrationTest extends AbstractContainerTest { - private final static int CBIO_PORT = 8080; - private final static int KC_PORT = 8084; - private final static int SESSION_PORT = 27017; public final static String CBIO_URL_FROM_BROWSER = String.format("http://host.testcontainers.internal:%d", CBIO_PORT); - - // Update application properties with connection info on Keycloak container - public static class MyKeycloakInitializer extends OAuth2KeycloakInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, keycloakContainer); - } - } - - // Update application properties with connection info on Mysql container - public static class MyMysqlInitializer extends MysqlInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, mysqlContainer); - } - } - - // Expose the ports for the cBioPortal Spring application and keycloak inside - // the Chrome container. Each address is available on http://host.testcontainers.internal: - // in the browser container. - public static class PortInitializer implements - ApplicationContextInitializer { - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - applicationContext.addApplicationListener( - (ApplicationListener) event -> { - Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT); - }); - } - } @Test public void a_loginSuccess() { diff --git a/src/test/java/org/cbioportal/test/integration/security/OAuth2ResourceServerIntegrationTest.java b/src/test/java/org/cbioportal/test/integration/security/OAuth2ResourceServerIntegrationTest.java index 7a5d61a4f54..02e0aba1a62 100644 --- a/src/test/java/org/cbioportal/test/integration/security/OAuth2ResourceServerIntegrationTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/OAuth2ResourceServerIntegrationTest.java @@ -24,14 +24,10 @@ package org.cbioportal.test.integration.security; -import dasniko.testcontainers.keycloak.KeycloakContainer; import org.cbioportal.PortalApplication; -import org.cbioportal.test.integration.*; import org.cbioportal.test.integration.security.util.HttpHelper; import org.json.JSONArray; import org.json.JSONException; -import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; @@ -40,27 +36,22 @@ import org.mockserver.model.HttpResponse; import org.mockserver.model.StringBody; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.utility.DockerImageName; import java.io.IOException; import java.net.URLEncoder; import static org.cbioportal.test.integration.security.OAuth2ResourceServerIntegrationTest.MyMysqlInitializer; import static org.cbioportal.test.integration.security.util.TokenHelper.encodeWithoutSigning; -import static org.cbioportal.test.integration.security.util.Util.isHostMappingPresent; import static org.junit.Assert.assertEquals; /** - * Tests protection of API endpoints by SAML auth + * Tests protection of API endpoints by OIDC data access token */ -// This starts a live portal instance on a random port (imported via @LocalServerPort) @RunWith(SpringRunner.class) @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, @@ -68,8 +59,6 @@ ) @TestPropertySource( properties = { - "app.name=cbioportal", - "server.port=8080", "authenticate=saml", "dat.method=oauth2", // DB settings @@ -90,58 +79,15 @@ } ) @ContextConfiguration(initializers = { - MyMysqlInitializer.class, + MyMysqlInitializer.class }) @DirtiesContext // needed to reuse port 8080 for multiple tests public class OAuth2ResourceServerIntegrationTest extends AbstractContainerTest { - private final static int CBIO_PORT = 8080; public final static String CBIO_URL_FROM_BROWSER = String.format("http://localhost:%d", CBIO_PORT); private final static String tokenUriPath = "/realms/cbio/protocol/openid-connect/token"; - - public final static DockerImageName MOCKSERVER_IMAGE = DockerImageName.parse("mockserver/mockserver") - .withTag("5.15.0"); -// -//// @ClassRule -// public static SharedMysqlContainer mysqlContainer = SharedMysqlContainer.getInstance(); -// -//// @ClassRule -// public static KeycloakContainer keycloakContainer = SharedKeycloakContainer.getInstance(); -// -//// @ClassRule -// public static GenericContainer mongoContainer = SharedMongoContainer.getInstance(); -// -//// @ClassRule -// public static GenericContainer sessionService = SharedSessionServiceContainer.getInstance(); -// -//// @ClassRule -// public static GenericContainer mockKeycloakServer = MockServerContainer.getInstance(); - // Update application properties with connection info on Mysql container - public static class MyMysqlInitializer extends MysqlInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, mysqlContainer); - } - } - - // TODO - move all boilerplate code to a base class - // For these tests to work, the host name 'host.testcontainers.internal' - // must be mapped to 'localhost' in /etc/hosts/ file. - static { - String hostToCheck = "host.testcontainers.internal"; - String ipAddressToCheck = "localhost"; - try { - if (!isHostMappingPresent(hostToCheck, ipAddressToCheck)) { - throw new IllegalStateException(hostToCheck + " is not mapped to " + ipAddressToCheck + " in /etc/hosts. Please add this mapping."); - } - } catch (IOException e) { - throw new RuntimeException("Unable to read /etc/hosts file.", e); - } - } - @Test public void testAccessForbiddenForAnonymousUser() throws IOException { HttpHelper.HttpResponse response = HttpHelper.sendGetRequest(CBIO_URL_FROM_BROWSER + "/api/studies", null, null); diff --git a/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java b/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java index f4a3265c1e7..f5ae67eb281 100644 --- a/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java +++ b/src/test/java/org/cbioportal/test/integration/security/SamlAuthIntegrationTest.java @@ -1,23 +1,18 @@ package org.cbioportal.test.integration.security; import org.cbioportal.PortalApplication; -import org.cbioportal.test.integration.MysqlInitializer; -import org.cbioportal.test.integration.SamlKeycloakInitializer; import org.cbioportal.test.integration.security.util.Util; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.context.WebServerInitializedEvent; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import org.testcontainers.Testcontainers; + +import static org.cbioportal.test.integration.security.AbstractContainerTest.*; @RunWith(SpringRunner.class) @SpringBootTest( @@ -26,9 +21,6 @@ ) @TestPropertySource( properties = { - "app.name=cbioportal", - "server.port=8080", - "filter_groups_by_appname=false", "authenticate=saml", "dat.method=oauth2", // DB settings (also see MysqlInitializer) @@ -50,52 +42,17 @@ } ) @ContextConfiguration(initializers = { - SamlAuthIntegrationTest.MyMysqlInitializer.class, - SamlAuthIntegrationTest.MySamlKeycloakInitializer.class, - SamlAuthIntegrationTest.PortInitializer.class + MyMysqlInitializer.class, + MySamlKeycloakInitializer.class, + PortInitializer.class }) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @DirtiesContext // needed to reuse port 8080 for multiple tests public class SamlAuthIntegrationTest extends AbstractContainerTest { - private final static int CBIO_PORT = 8080; - private final static int KC_PORT = 8084; - private final static int SESSION_PORT = 27017; public final static String CBIO_URL_FROM_BROWSER = String.format("http://host.testcontainers.internal:%d", CBIO_PORT); - // Update application properties with connection info on Keycloak container - public static class MySamlKeycloakInitializer extends SamlKeycloakInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, keycloakContainer); - } - } - - // Update application properties with connection info on Mysql container - public static class MyMysqlInitializer extends MysqlInitializer { - @Override - public void initialize( - ConfigurableApplicationContext configurableApplicationContext) { - super.initializeImpl(configurableApplicationContext, mysqlContainer); - } - } - - // Expose the ports for the cBioPortal Spring application and keycloak inside - // the Chrome container. Each address is available on http://host.testcontainers.internal: - // in the browser container. - public static class PortInitializer implements - ApplicationContextInitializer { - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - applicationContext.addApplicationListener( - (ApplicationListener) event -> { - Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT); - }); - } - } - @Test public void a_loginSuccess() { Util.testLogin(CBIO_URL_FROM_BROWSER, chromedriverContainer);