-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add logout test #18360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add logout test #18360
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
384c2ad
Add logout test
jacko9et 5144538
Merge branch 'master' of github.com:Azure/azure-sdk-for-java into dev
jacko9et e45cf8f
modify selenium tests
jacko9et 28417a8
Merge branch 'master' of github.com:Azure/azure-sdk-for-java into dev
jacko9et 24c1603
modify selenium tests
jacko9et bb04b7f
Rename AuthorizationProperties to AuthorizationClientProperties
jacko9et File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
207 changes: 207 additions & 0 deletions
207
.../azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| package com.azure.test.aad.selenium; | ||
|
|
||
| import com.azure.test.utils.AppRunner; | ||
| import org.junit.Assert; | ||
| import org.openqa.selenium.By; | ||
| import org.openqa.selenium.Keys; | ||
| import org.openqa.selenium.WebDriver; | ||
| import org.openqa.selenium.chrome.ChromeDriver; | ||
| import org.openqa.selenium.chrome.ChromeDriverService; | ||
| import org.openqa.selenium.chrome.ChromeOptions; | ||
| import org.openqa.selenium.support.ui.WebDriverWait; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
| import java.util.function.Consumer; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static com.azure.test.aad.AADTestUtils.AAD_MULTI_TENANT_CLIENT_ID; | ||
| import static com.azure.test.aad.AADTestUtils.AAD_MULTI_TENANT_CLIENT_SECRET; | ||
| import static com.azure.test.aad.AADTestUtils.AAD_TENANT_ID_1; | ||
| import static com.azure.test.aad.AADTestUtils.AAD_USER_NAME_1; | ||
| import static com.azure.test.aad.AADTestUtils.AAD_USER_PASSWORD_1; | ||
| import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; | ||
|
|
||
| public class AADLoginRunner { | ||
|
|
||
| public static final String DEFAULT_USERNAME = System.getenv(AAD_USER_NAME_1); | ||
| private static final String DEFAULT_PASSWORD = System.getenv(AAD_USER_PASSWORD_1); | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(AADLoginRunner.class); | ||
|
|
||
| static { | ||
| final String directory = "src/test/resources/driver/"; | ||
| final String chromedriverLinux = "chromedriver_linux64"; | ||
| final String chromedriverWin32 = "chromedriver_win32.exe"; | ||
| final String chromedriverMac = "chromedriver_mac64"; | ||
| String osName = System.getProperty("os.name").toLowerCase(); | ||
| Process process = null; | ||
| try { | ||
| File dir = new File(directory); | ||
| if (Pattern.matches("linux.*", osName)) { | ||
| process = Runtime.getRuntime().exec("chmod +x " + chromedriverLinux, null, dir); | ||
| process.waitFor(); | ||
| System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverLinux); | ||
| } else if (Pattern.matches("windows.*", osName)) { | ||
| System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverWin32); | ||
| } else if (Pattern.matches("mac.*", osName)) { | ||
| process = Runtime.getRuntime().exec("chmod +x " + chromedriverMac, null, dir); | ||
| process.waitFor(); | ||
| System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, directory + chromedriverMac); | ||
| } else { | ||
| throw new IllegalStateException("Can not recognize osName. osName = " + System.getProperty("os" | ||
| + ".name")); | ||
| } | ||
| } catch (InterruptedException | IOException e) { | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| if (process != null) { | ||
| process.destroy(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private final AppRunner app; | ||
| private final WebDriver driver; | ||
| private final String password; | ||
| private final String username; | ||
| private final WebDriverWait wait; | ||
|
|
||
| private AADLoginRunner(String username, String password, AppRunner app, WebDriver driver) { | ||
| this.username = username; | ||
| this.password = password; | ||
| this.app = app; | ||
| this.driver = driver; | ||
| this.wait = new WebDriverWait(this.driver, 10); | ||
| } | ||
|
|
||
| public static AADLoginRunnerConfiguration build(Class<?> appClass) { | ||
| return new AADLoginRunnerConfiguration(appClass); | ||
| } | ||
|
|
||
| public void run(BrowserCommandWithAppRunner command) { | ||
| try { | ||
| this.app.start(); | ||
| command.login(login()) | ||
| .andThen((app, driver, wait) -> LOGGER.info("Test ===> {}.{}() has finished running.", | ||
| Thread.currentThread().getStackTrace()[6].getClassName(), | ||
| Thread.currentThread().getStackTrace()[6].getMethodName())) | ||
|
jacko9et marked this conversation as resolved.
Outdated
|
||
| .run(this.app, this.driver, this.wait); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| if (this.driver != null) { | ||
| this.driver.quit(); | ||
| } | ||
| if (this.app != null) { | ||
| this.app.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private BrowserCommandWithAppRunner login() { | ||
| return (app, driver, wait) -> { | ||
| driver.get(app.root() + "oauth2/authorization/azure"); | ||
| wait.until(presenceOfElementLocated(By.name("loginfmt"))) | ||
| .sendKeys(this.username + Keys.ENTER); | ||
| Thread.sleep(10000); | ||
|
|
||
| driver.findElement(By.name("passwd")) | ||
| .sendKeys(this.password + Keys.ENTER); | ||
| Thread.sleep(10000); | ||
|
|
||
| driver.findElement(By.cssSelector("input[type='submit']")).click(); | ||
| Thread.sleep(10000); | ||
| }; | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface BrowserCommandWithAppRunner { | ||
|
|
||
| default BrowserCommandWithAppRunner andThen(BrowserCommandWithAppRunner after) { | ||
| Objects.requireNonNull(after); | ||
| return (AppRunner app, WebDriver driver, WebDriverWait wait) -> { | ||
| run(app, driver, wait); | ||
| after.run(app, driver, wait); | ||
| }; | ||
| } | ||
|
|
||
| default BrowserCommandWithAppRunner login(BrowserCommandWithAppRunner login) { | ||
| Objects.requireNonNull(login); | ||
| return (AppRunner app, WebDriver driver, WebDriverWait wait) -> { | ||
| login.run(app, driver, wait); | ||
| run(app, driver, wait); | ||
| }; | ||
| } | ||
|
|
||
| void run(AppRunner app, WebDriver driver, WebDriverWait wait) throws Exception; | ||
| } | ||
|
|
||
| public static class AADLoginRunnerConfiguration { | ||
| private final AppRunner app; | ||
| private final WebDriver driver; | ||
| private Consumer<AppRunner> configure; | ||
|
|
||
| private AADLoginRunnerConfiguration(Class<?> appClass) { | ||
| this.configure = defautlConfigure(); | ||
|
|
||
| ChromeOptions options = new ChromeOptions(); | ||
| options.addArguments("--headless"); | ||
| options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage"); | ||
| this.driver = new ChromeDriver(options); | ||
|
|
||
| this.app = new AppRunner(appClass); | ||
| } | ||
|
|
||
| public AADLoginRunnerConfiguration configure(Consumer<AppRunner> configure) { | ||
|
jacko9et marked this conversation as resolved.
|
||
| this.configure = configure; | ||
| return this; | ||
| } | ||
|
|
||
| public AADLoginRunner login(String username, String password) { | ||
| this.configure.accept(this.app); | ||
|
jacko9et marked this conversation as resolved.
|
||
| return new AADLoginRunner(username, password, this.app, this.driver); | ||
| } | ||
|
|
||
| public AADLoginRunner login() { | ||
| return login(DEFAULT_USERNAME, DEFAULT_PASSWORD); | ||
| } | ||
|
|
||
| private static Consumer<AppRunner> defautlConfigure() { | ||
| return app -> { | ||
| app.property("azure.activedirectory.tenant-id", System.getenv(AAD_TENANT_ID_1)); | ||
| app.property("azure.activedirectory.client-id", System.getenv(AAD_MULTI_TENANT_CLIENT_ID)); | ||
| app.property("azure.activedirectory.client-secret", System.getenv(AAD_MULTI_TENANT_CLIENT_SECRET)); | ||
| app.property("azure.activedirectory.user-group.allowed-groups", "group1"); | ||
| app.property("azure.activedirectory.post-logout-redirect-uri", "http://localhost:${server.port}"); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| public static class EasyTester { | ||
| private final AppRunner app; | ||
| private final WebDriver driver; | ||
|
|
||
| public EasyTester(AppRunner app, WebDriver driver) { | ||
| this.app = app; | ||
| this.driver = driver; | ||
| } | ||
|
|
||
| public void assertEquals(String uri, String expected) throws InterruptedException { | ||
| Assert.assertEquals(expected, get(uri)); | ||
| } | ||
|
|
||
| public void assertNotEquals(String uri, String expected) throws InterruptedException { | ||
| Assert.assertNotEquals(expected, get(uri)); | ||
| } | ||
|
|
||
| private String get(String uri) throws InterruptedException { | ||
| this.driver.get(this.app.root() + uri); | ||
| Thread.sleep(1000); | ||
| return this.driver.findElement(By.tagName("body")).getText(); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.