-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Change the code style of AADSeleniumITHelper #18378
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
Merged
chenrujun
merged 19 commits into
Azure:master
from
chenrujun:change-the-code-style-of-selenium-test
Dec 29, 2020
Merged
Changes from all commits
Commits
Show all changes
19 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 64ad42d
Simplify code of AADSeleniumITHelper.
rujche 8b06fbc
In AADSeleniumITHelper:
rujche 8dfc13d
Fix error about app properties.
rujche d58ee31
Change the order of static code block.
rujche a1c35fd
No logic change, just change class name.
rujche 0d1fc0f
Update properties.
rujche f2cb8f1
Change package name for AADRoleIT.
rujche dba4e8f
No logic change, just make property easier to read.
rujche e194625
No logic change, just code order.
rujche 416923b
Fix assert error in AADAccessTokenScopesIT.
rujche eabd011
Fix assert error in AADRoleIT.
rujche f4224f9
Change the style of forbidden endpoint.
rujche d43ca77
Merge branch 'master' into change-the-code-style-of-selenium-test
rujche 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
115 changes: 115 additions & 0 deletions
115
...e-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.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,115 @@ | ||
| 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 java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| 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 AADSeleniumITHelper { | ||
|
|
||
| private final String username; | ||
| private final String password; | ||
| private final AppRunner app; | ||
| private final WebDriver driver; | ||
| private static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<>(); | ||
|
|
||
| static { | ||
| DEFAULT_PROPERTIES.put("azure.activedirectory.tenant-id", System.getenv(AAD_TENANT_ID_1)); | ||
| DEFAULT_PROPERTIES.put("azure.activedirectory.client-id", System.getenv(AAD_MULTI_TENANT_CLIENT_ID)); | ||
| DEFAULT_PROPERTIES.put("azure.activedirectory.client-secret", System.getenv(AAD_MULTI_TENANT_CLIENT_SECRET)); | ||
| DEFAULT_PROPERTIES.put("azure.activedirectory.user-group.allowed-groups", "group1"); | ||
| DEFAULT_PROPERTIES.put("azure.activedirectory.post-logout-redirect-uri", "http://localhost:${server.port}"); | ||
|
|
||
| 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("Unrecognized osName. osName = " + System.getProperty("os.name")); | ||
| } | ||
| } catch (InterruptedException | IOException e) { | ||
| throw new RuntimeException(e); | ||
| } finally { | ||
| if (process != null) { | ||
| process.destroy(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public AADSeleniumITHelper(Class<?> appClass, Map<String, String> properties) throws InterruptedException { | ||
| username = System.getenv(AAD_USER_NAME_1); | ||
| password = System.getenv(AAD_USER_PASSWORD_1); | ||
| app = new AppRunner(appClass); | ||
| DEFAULT_PROPERTIES.forEach(app::property); | ||
| properties.forEach(app::property); | ||
|
|
||
| ChromeOptions options = new ChromeOptions(); | ||
| options.addArguments("--headless"); | ||
| options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage"); | ||
| this.driver = new ChromeDriver(options); | ||
|
|
||
| this.app.start(); | ||
| login(); | ||
| } | ||
|
|
||
| private void login() throws InterruptedException { | ||
| WebDriverWait wait = new WebDriverWait(this.driver, 10); | ||
| driver.get(app.root() + "oauth2/authorization/azure"); | ||
| wait.until(presenceOfElementLocated(By.name("loginfmt"))).sendKeys(username + Keys.ENTER); | ||
| Thread.sleep(10000); | ||
| driver.findElement(By.name("passwd")).sendKeys(password + Keys.ENTER); | ||
| Thread.sleep(10000); | ||
| driver.findElement(By.cssSelector("input[type='submit']")).click(); | ||
| Thread.sleep(10000); | ||
| } | ||
|
|
||
| public String httpGet(String endpoint) throws InterruptedException { | ||
| driver.get((app.root() + endpoint)); | ||
| Thread.sleep(1000); | ||
| return driver.findElement(By.tagName("body")).getText(); | ||
| } | ||
|
|
||
| public void logoutTest() throws InterruptedException { | ||
| WebDriverWait wait = new WebDriverWait(driver, 10); | ||
| driver.get(app.root() + "logout"); | ||
| wait.until(presenceOfElementLocated(By.cssSelector("button[type='submit']"))).click(); | ||
| Thread.sleep(10000); | ||
| String cssSelector = "div[data-test-id='" + username + "']"; | ||
| driver.findElement(By.cssSelector(cssSelector)).click(); | ||
| Thread.sleep(10000); | ||
| String id = driver.findElement(By.cssSelector("div[tabindex='0']")).getAttribute("data-test-id"); | ||
| Assert.assertEquals(username, id); | ||
| } | ||
| } | ||
107 changes: 0 additions & 107 deletions
107
...ure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/SeleniumTestUtils.java
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
...src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.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,95 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.test.aad.selenium.access.token.scopes; | ||
|
|
||
| import com.azure.test.aad.selenium.AADSeleniumITHelper; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
| import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
| import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient; | ||
| import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| public class AADAccessTokenScopesIT { | ||
|
|
||
| @Test | ||
| public void testAccessTokenScopes() throws InterruptedException { | ||
| Map<String, String> arguments = new HashMap<>(); | ||
| arguments.put( | ||
| "azure.activedirectory.authorization-clients.office.scopes", | ||
| "https://manage.office.com/ActivityFeed.Read, https://manage.office.com/ActivityFeed.ReadDlp, " | ||
| + "https://manage.office.com/ServiceHealth.Read"); | ||
| arguments.put( | ||
| "azure.activedirectory.authorization-clients.graph.scopes", | ||
| "https://graph.microsoft.com/User.Read, https://graph.microsoft.com/Directory.AccessAsUser.All"); | ||
| AADSeleniumITHelper aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, arguments); | ||
|
|
||
| String httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/azure"); | ||
| Assert.assertTrue(httpResponse.contains("profile")); | ||
| Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); | ||
| Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/User.Read")); | ||
|
|
||
| httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/graph"); | ||
| Assert.assertTrue(httpResponse.contains("profile")); | ||
| Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); | ||
| Assert.assertTrue(httpResponse.contains("https://graph.microsoft.com/User.Read")); | ||
|
|
||
| httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/office"); | ||
| Assert.assertFalse(httpResponse.contains("profile")); | ||
| Assert.assertTrue(httpResponse.contains("https://manage.office.com/ActivityFeed.Read")); | ||
| Assert.assertTrue(httpResponse.contains("https://manage.office.com/ActivityFeed.ReadDlp")); | ||
| Assert.assertTrue(httpResponse.contains("https://manage.office.com/ServiceHealth.Read")); | ||
|
|
||
| httpResponse = aadSeleniumITHelper.httpGet("arm"); | ||
| Assert.assertNotEquals(httpResponse, "arm"); | ||
| } | ||
chenrujun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) | ||
| @SpringBootApplication | ||
| @RestController | ||
| public static class DumbApp { | ||
|
|
||
| @GetMapping(value = "accessTokenScopes/azure") | ||
| public Set<String> azure( | ||
| @RegisteredOAuth2AuthorizedClient("azure") OAuth2AuthorizedClient authorizedClient) { | ||
| return Optional.of(authorizedClient) | ||
| .map(OAuth2AuthorizedClient::getAccessToken) | ||
| .map(OAuth2AccessToken::getScopes) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| @GetMapping(value = "accessTokenScopes/graph") | ||
| public Set<String> graph( | ||
| @RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient authorizedClient) { | ||
| return Optional.of(authorizedClient) | ||
| .map(OAuth2AuthorizedClient::getAccessToken) | ||
| .map(OAuth2AccessToken::getScopes) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| @GetMapping(value = "accessTokenScopes/office") | ||
| public Set<String> office( | ||
| @RegisteredOAuth2AuthorizedClient("office") OAuth2AuthorizedClient authorizedClient) { | ||
| return Optional.of(authorizedClient) | ||
| .map(OAuth2AuthorizedClient::getAccessToken) | ||
| .map(OAuth2AccessToken::getScopes) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| @GetMapping(value = "arm") | ||
| public String arm( | ||
| @RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) { | ||
| return "arm"; | ||
chenrujun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
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.