forked from cBioPortal/cbioportal
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
275eb25
commit 78c62ac
Showing
4 changed files
with
265 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
package org.cbioportal.test.integration.security; | ||
|
||
import org.cbioportal.PortalApplication; | ||
import org.cbioportal.test.integration.*; | ||
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.jupiter.api.Assertions; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.NoSuchElementException; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.remote.RemoteWebDriver; | ||
import org.openqa.selenium.support.ui.ExpectedConditions; | ||
import org.openqa.selenium.support.ui.WebDriverWait; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.web.context.WebServerInitializedEvent; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
|
@@ -22,20 +17,9 @@ | |
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.util.Assert; | ||
import org.testcontainers.Testcontainers; | ||
import org.testcontainers.containers.Container; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.cbioportal.test.integration.security.OAuth2AuthIntegrationTest.*; | ||
import static org.cbioportal.test.integration.security.util.Util.isHostMappingPresent; | ||
import static org.testcontainers.shaded.org.awaitility.Awaitility.await; | ||
import static org.cbioportal.test.integration.security.OAuth2AuthIntegrationTest.MyKeycloakInitializer; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest( | ||
|
@@ -72,9 +56,9 @@ | |
} | ||
) | ||
@ContextConfiguration(initializers = { | ||
MyMysqlInitializer.class, | ||
OAuth2AuthIntegrationTest.MyMysqlInitializer.class, | ||
MyKeycloakInitializer.class, | ||
PortInitializer.class | ||
OAuth2AuthIntegrationTest.PortInitializer.class | ||
}) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
@DirtiesContext // needed to reuse port 8080 for multiple tests | ||
|
@@ -86,21 +70,6 @@ public class OAuth2AuthIntegrationTest extends AbstractContainerTest { | |
public final static String CBIO_URL_FROM_BROWSER = | ||
String.format("http://host.testcontainers.internal:%d", CBIO_PORT); | ||
|
||
// @ClassRule | ||
// public static SharedMysqlContainer mysqlContainer = SharedMysqlContainer.getInstance(); | ||
// | ||
// @ClassRule | ||
// public static KeycloakContainer keycloakContainer = SharedKeycloakContainer.getInstance(); | ||
// | ||
// @ClassRule | ||
// public static BrowserWebDriverContainer chrome = SharedChromeContainer.getInstance(); | ||
// | ||
// @ClassRule | ||
// public static GenericContainer mongoContainer = SharedMongoContainer.getInstance(); | ||
// | ||
// @ClassRule | ||
// public static GenericContainer sessionService = SharedSessionServiceContainer.getInstance(); | ||
|
||
// Update application properties with connection info on Keycloak container | ||
public static class MyKeycloakInitializer extends OAuth2KeycloakInitializer { | ||
@Override | ||
|
@@ -133,111 +102,24 @@ public void initialize(ConfigurableApplicationContext applicationContext) { | |
} | ||
} | ||
|
||
// 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 a_loginSuccess() { | ||
// String vncAddress = chrome.getVncAddress(); | ||
RemoteWebDriver driver = performLogin(); | ||
WebElement loggedInButton = driver.findElement(By.id("dat-dropdown")); | ||
Assertions.assertEquals("Logged in as [email protected]", loggedInButton.getText()); | ||
Assertions.assertDoesNotThrow( | ||
() -> driver.findElement(By.xpath("//span[.='Breast Invasive Carcinoma (TCGA,Nature 2012)']")), | ||
"Study could not be found on the landing page. Permissions are not correctly passed from IDP to client."); | ||
Util.testLogin(CBIO_URL_FROM_BROWSER, chromedriverContainer); | ||
} | ||
|
||
@Test | ||
public void b_downloadOfflineToken() throws Exception { | ||
RemoteWebDriver driver = performLogin(); | ||
Assertions.assertDoesNotThrow( | ||
() -> driver.findElement(By.id("dat-dropdown")).click(), | ||
"Logged-in menu could not be found on the page."); | ||
driver.findElement(By.linkText("Data Access Token")).click(); | ||
driver.findElement(By.xpath("//button[text()='Download Token']")).click(); | ||
|
||
await().atMost(Duration.ofSeconds(5)).until(downloadedFile()); | ||
|
||
Assertions.assertTrue(downloadedFile().call()); | ||
Util.testDownloadOfflineToken(CBIO_URL_FROM_BROWSER, chromedriverContainer); | ||
} | ||
|
||
@Test | ||
public void c_logoutSuccess() { | ||
RemoteWebDriver driver = performLogin(); | ||
Assertions.assertDoesNotThrow( | ||
() -> driver.findElement(By.id("dat-dropdown")).click(), | ||
"Logout menu could not be found on the page."); | ||
driver.findElement(By.linkText("Sign out")).click(); | ||
Assertions.assertDoesNotThrow( | ||
() -> driver.findElement(By.id("username")), | ||
"IDP login screen not visible on the page. Logout did not work correctly." | ||
); | ||
Util.testLogout(CBIO_URL_FROM_BROWSER, chromedriverContainer); | ||
} | ||
|
||
@Test | ||
public void d_loginAgainSuccess() { | ||
final RemoteWebDriver driver = performLogin(); | ||
Assertions.assertDoesNotThrow( | ||
() -> driver.findElement(By.id("dat-dropdown")).click(), | ||
"Logout menu could not be found on the page."); | ||
driver.findElement(By.linkText("Sign out")).click(); | ||
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); | ||
Assertions.assertDoesNotThrow( | ||
() -> driver.findElement(By.id("username")), | ||
"IDP login screen not visible on the page. Logout did not work correctly." | ||
); | ||
final RemoteWebDriver newDriver = performLogin(); | ||
Assertions.assertDoesNotThrow( | ||
() -> newDriver.findElement(By.id("dat-dropdown")), | ||
"Logged-in menu could not be found on the page. Login did not work correctly."); | ||
} | ||
|
||
private RemoteWebDriver performLogin() { | ||
RemoteWebDriver driver = chromedriverContainer.getWebDriver(); | ||
driver.get(CBIO_URL_FROM_BROWSER); | ||
try { | ||
// when the cbioportal logo is visible, skip login | ||
driver.findElement(By.id("cbioportal-logo")); | ||
} catch (NoSuchElementException e) { | ||
WebElement userNameInput = driver.findElement(By.id("username")); | ||
WebElement passwordInput = driver.findElement(By.id("password")); | ||
WebElement loginButton = driver.findElement(By.id("kc-login")); | ||
userNameInput.sendKeys("testuser"); | ||
passwordInput.sendKeys("P@ssword1"); | ||
loginButton.click(); | ||
} | ||
// wait for the page to load | ||
new WebDriverWait(driver, Duration.ofSeconds(20)).until( | ||
ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@data-test='cancerTypeListContainer']"))); | ||
return driver; | ||
} | ||
|
||
private boolean containerFileExists( | ||
@Nonnull final GenericContainer container, @Nonnull String path) | ||
throws IOException, InterruptedException { | ||
Assert.notNull(container, "Containers is null"); | ||
Assert.isTrue(!path.isEmpty(), "Path string is empty"); | ||
Container.ExecResult r = container.execInContainer("/bin/sh", "-c", | ||
"if [ -f " + path | ||
+ " ] ; then echo '0' ; else (>&2 echo '1') ; fi"); | ||
return !r.getStderr().contains("1"); | ||
} | ||
|
||
private Callable<Boolean> downloadedFile() { | ||
return () -> containerFileExists(chromedriverContainer, | ||
String.format("%s/cbioportal_data_access_token.txt", | ||
SharedChromeContainer.DOWNLOAD_FOLDER)); | ||
Util.testLoginAgain(CBIO_URL_FROM_BROWSER, chromedriverContainer); | ||
} | ||
|
||
} |
Oops, something went wrong.