Skip to content

Commit

Permalink
test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pvannierop committed Jan 9, 2024
1 parent 78c62ac commit 9ee5a51
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,17 +69,15 @@ 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)
.withRealmImportFile("security/keycloak-configuration-generated.json")
.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)
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -149,9 +146,13 @@ public static class PortInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"server.port=" + CBIO_PORT
);
values.applyTo(applicationContext);
applicationContext.addApplicationListener(
(ApplicationListener<WebServerInitializedEvent>) event -> {
Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, SESSION_SERVICE_PORT);
Testcontainers.exposeHostPorts(CBIO_PORT, KEYCLOAK_PORT, MONGO_PORT);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand All @@ -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:<port>
// in the browser container.
public static class PortInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(
(ApplicationListener<WebServerInitializedEvent>) event -> {
Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT);
});
}
}

@Test
public void a_loginSuccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,36 +36,29 @@
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,
classes = {PortalApplication.class}
)
@TestPropertySource(
properties = {
"app.name=cbioportal",
"server.port=8080",
"authenticate=saml",
"dat.method=oauth2",
// DB settings
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand All @@ -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:<port>
// in the browser container.
public static class PortInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(
(ApplicationListener<WebServerInitializedEvent>) event -> {
Testcontainers.exposeHostPorts(CBIO_PORT, KC_PORT, SESSION_PORT);
});
}
}

@Test
public void a_loginSuccess() {
Util.testLogin(CBIO_URL_FROM_BROWSER, chromedriverContainer);
Expand Down

0 comments on commit 9ee5a51

Please sign in to comment.