From 384c2ad3550277e56d1ec09773c554436b0521e3 Mon Sep 17 00:00:00 2001 From: Jack Lu Date: Fri, 25 Dec 2020 16:55:35 +0800 Subject: [PATCH 01/16] Add logout test --- .../aad/{selenium => }/login/AADLoginIT.java | 43 ++-- .../test/aad/selenium/AADLoginRunner.java | 207 ++++++++++++++++++ 2 files changed, 231 insertions(+), 19 deletions(-) rename sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/{selenium => }/login/AADLoginIT.java (58%) create mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/login/AADLoginIT.java similarity index 58% rename from sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java rename to sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/login/AADLoginIT.java index e31f95f6a53e..f084ba5ac4e9 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/login/AADLoginIT.java @@ -1,12 +1,12 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -package com.azure.test.aad.selenium.login; +package com.azure.test.aad.login; -import com.azure.test.aad.selenium.SeleniumTestUtils; -import com.azure.test.utils.AppRunner; +import com.azure.test.aad.selenium.AADLoginRunner; import org.junit.Assert; import org.junit.Test; +import org.openqa.selenium.By; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -18,9 +18,8 @@ import org.springframework.web.bind.annotation.RestController; import java.security.Principal; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; + +import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; public class AADLoginIT { @@ -28,20 +27,26 @@ public class AADLoginIT { @Test public void loginTest() { + AADLoginRunner.build(DumbApp.class).login().run((app, driver, wait) -> { + AADLoginRunner.EasyTester tester = new AADLoginRunner.EasyTester(app, driver); + tester.assertEquals("api/home", "home"); + tester.assertEquals("api/group1", "group1"); + tester.assertNotEquals("api/status403", "error"); + }); + } - try (AppRunner app = new AppRunner(DumbApp.class)) { - SeleniumTestUtils.addProperty(app); - List endPoints = new ArrayList<>(); - endPoints.add("api/home"); - endPoints.add("api/group1"); - endPoints.add("api/status403"); - Map result = SeleniumTestUtils.get(app, endPoints); - Assert.assertEquals("home", result.get("api/home")); - Assert.assertEquals("group1", result.get("api/group1")); - Assert.assertNotEquals("error", result.get("api/status403")); - } - - + @Test + public void logoutTest() { + AADLoginRunner.build(DumbApp.class).login().run((app, driver, wait) -> { + driver.get(app.root() + "logout"); + wait.until(presenceOfElementLocated(By.cssSelector("button[type='submit']"))).click(); + Thread.sleep(10000); + String cssSelector = "div[data-test-id='" + AADLoginRunner.DEFAULT_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(AADLoginRunner.DEFAULT_USERNAME, id); + }); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java new file mode 100644 index 000000000000..dd3d1951d452 --- /dev/null +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java @@ -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())) + .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 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 configure) { + this.configure = configure; + return this; + } + + public AADLoginRunner login(String username, String password) { + this.configure.accept(this.app); + return new AADLoginRunner(username, password, this.app, this.driver); + } + + public AADLoginRunner login() { + return login(DEFAULT_USERNAME, DEFAULT_PASSWORD); + } + + private static Consumer 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(); + } + } +} From e45cf8f9aa374510c8a9207cddc4f0374379c5e7 Mon Sep 17 00:00:00 2001 From: Jack Lu Date: Mon, 28 Dec 2020 11:47:22 +0800 Subject: [PATCH 02/16] modify selenium tests --- .../test/aad/selenium/AADLoginRunner.java | 87 ++++---------- .../test/aad/selenium/SeleniumTestUtils.java | 107 ----------------- .../token/scopes/AccessTokenScopesIT.java | 108 ++++++++++-------- .../aad/{ => selenium}/login/AADLoginIT.java | 26 +++-- 4 files changed, 103 insertions(+), 225 deletions(-) delete mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/SeleniumTestUtils.java rename sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/{ => selenium}/login/AADLoginIT.java (76%) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java index dd3d1951d452..1b9ef33f69cf 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java @@ -1,7 +1,6 @@ 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; @@ -9,8 +8,6 @@ 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; @@ -30,8 +27,6 @@ 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"; @@ -68,14 +63,12 @@ public class AADLoginRunner { 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) { @@ -85,11 +78,20 @@ public static AADLoginRunnerConfiguration build(Class 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())) - .run(this.app, this.driver, this.wait); + command.login((app, driver) -> { + WebDriverWait wait = new WebDriverWait(this.driver, 10); + 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); + }).run(this.app, this.driver); } catch (Exception e) { throw new RuntimeException(e); } finally { @@ -102,42 +104,18 @@ public void run(BrowserCommandWithAppRunner command) { } } - 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); + return (AppRunner app, WebDriver driver) -> { + login.run(app, driver); + run(app, driver); }; } - void run(AppRunner app, WebDriver driver, WebDriverWait wait) throws Exception; + void run(AppRunner app, WebDriver driver) throws Exception; } public static class AADLoginRunnerConfiguration { @@ -161,6 +139,11 @@ public AADLoginRunnerConfiguration configure(Consumer configure) { return this; } + public AADLoginRunnerConfiguration extendsDefault(Consumer configure) { + this.configure = defautlConfigure().andThen(configure); + return this; + } + public AADLoginRunner login(String username, String password) { this.configure.accept(this.app); return new AADLoginRunner(username, password, this.app, this.driver); @@ -180,28 +163,4 @@ private static Consumer defautlConfigure() { }; } } - - 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(); - } - } } diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/SeleniumTestUtils.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/SeleniumTestUtils.java deleted file mode 100644 index ea0ba145182a..000000000000 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/SeleniumTestUtils.java +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.test.aad.selenium; - -import com.azure.test.utils.AppRunner; -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.List; -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 SeleniumTestUtils { - - 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 (IllegalStateException e) { - throw e; - } catch (InterruptedException | IOException e) { - throw new RuntimeException(e); - } finally { - if (process != null) { - process.destroy(); - } - } - } - - public static Map get(AppRunner app, List endPoints) { - - Map result = new HashMap<>(); - ChromeOptions options = new ChromeOptions(); - options.addArguments("--incognito"); - options.addArguments("--headless"); - options.addArguments("--no-sandbox"); - options.addArguments("--disable-dev-shm-usage"); - WebDriver driver = new ChromeDriver(options); - WebDriverWait wait = new WebDriverWait(driver, 10); - app.start(); - try { - driver.get(app.root() + endPoints.get(0)); - wait.until(presenceOfElementLocated(By.name("loginfmt"))) - .sendKeys(System.getenv(AAD_USER_NAME_1) + Keys.ENTER); - Thread.sleep(10000); - driver.findElement(By.name("passwd")) - .sendKeys(System.getenv(AAD_USER_PASSWORD_1) + Keys.ENTER); - Thread.sleep(10000); - driver.findElement(By.cssSelector("input[type='submit']")).click(); - Thread.sleep(10000); - result.put(endPoints.get(0), driver.findElement(By.tagName("body")).getText()); - endPoints.remove(0); - for (String endPoint : endPoints) { - driver.get(app.root() + endPoint); - Thread.sleep(1000); - result.put(endPoint, driver.findElement(By.tagName("body")).getText()); - } - return result; - } catch (InterruptedException e) { - throw new RuntimeException(e); - } finally { - driver.quit(); - } - } - - public static void addProperty(AppRunner 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"); - } - -} diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java index ab08ce0e0c3f..5fc7e1cbacc6 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java @@ -1,13 +1,12 @@ - // 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.SeleniumTestUtils; -import com.azure.test.utils.AppRunner; +import com.azure.test.aad.selenium.AADLoginRunner; import org.junit.Assert; import org.junit.Test; +import org.openqa.selenium.By; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; @@ -16,38 +15,53 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.*; +import java.util.Optional; +import java.util.Set; public class AccessTokenScopesIT { @Test public void testAccessTokenScopes() { - try (AppRunner app = new AppRunner(DumbApp.class)) { - SeleniumTestUtils.addProperty(app); - app.property("azure.activedirectory.authorization.office.scopes", "https://manage.office.com/ActivityFeed.Read , https://manage.office.com/ActivityFeed.ReadDlp , https://manage.office.com/ServiceHealth.Read"); - app.property("azure.activedirectory.authorization.graph.scopes", "https://graph.microsoft.com/User.Read , https://graph.microsoft.com/Directory.AccessAsUser.All"); - List endPoints = new ArrayList<>(); - endPoints.add("accessTokenScopes/azure"); - endPoints.add("accessTokenScopes/office"); - endPoints.add("accessTokenScopes/graph"); - endPoints.add("accessTokenScopes/arm"); - Map result = SeleniumTestUtils.get(app, endPoints); - - Assert.assertFalse(result.get("accessTokenScopes/office").contains("profile")); - Assert.assertTrue(result.get("accessTokenScopes/office").contains("https://manage.office.com/ActivityFeed.Read")); - Assert.assertTrue(result.get("accessTokenScopes/office").contains("https://manage.office.com/ActivityFeed.ReadDlp")); - Assert.assertTrue(result.get("accessTokenScopes/office").contains("https://manage.office.com/ServiceHealth.Read")); - - Assert.assertTrue(result.get("accessTokenScopes/azure").contains("profile")); - Assert.assertTrue(result.get("accessTokenScopes/azure").contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); - Assert.assertTrue(result.get("accessTokenScopes/azure").contains("https://graph.microsoft.com/User.Read")); - - Assert.assertTrue(result.get("accessTokenScopes/graph").contains("profile")); - Assert.assertTrue(result.get("accessTokenScopes/graph").contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); - Assert.assertTrue(result.get("accessTokenScopes/graph").contains("https://graph.microsoft.com/User.Read")); - - Assert.assertNotEquals("error", result.get("api/arm")); - } + AADLoginRunner.build(DumbApp.class).extendsDefault(app -> { + + app.property("azure.activedirectory.authorization.office.scopes", + "https://manage.office.com/ActivityFeed.Read , " + + "https://manage.office.com/ActivityFeed.ReadDlp , " + + "https://manage.office.com/ServiceHealth.Read"); + app.property("azure.activedirectory.authorization.graph.scopes", + "https://graph.microsoft.com/User.Read , " + + "https://graph.microsoft.com/Directory.AccessAsUser.All"); + + }).login().run((app, driver) -> { + + driver.get((app.root() + "accessTokenScopes/azure")); + Thread.sleep(1000); + String result = driver.findElement(By.tagName("body")).getText(); + Assert.assertTrue(result.contains("profile")); + Assert.assertTrue(result.contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); + Assert.assertTrue(result.contains("https://graph.microsoft.com/User.Read")); + + driver.get((app.root() + "accessTokenScopes/office")); + Thread.sleep(1000); + result = driver.findElement(By.tagName("body")).getText(); + Assert.assertFalse(result.contains("profile")); + Assert.assertTrue(result.contains("https://manage.office.com/ActivityFeed.Read")); + Assert.assertTrue(result.contains("https://manage.office.com/ActivityFeed.ReadDlp")); + Assert.assertTrue(result.contains("https://manage.office.com/ServiceHealth.Read")); + + driver.get((app.root() + "accessTokenScopes/graph")); + Thread.sleep(1000); + result = driver.findElement(By.tagName("body")).getText(); + Assert.assertTrue(result.contains("profile")); + Assert.assertTrue(result.contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); + Assert.assertTrue(result.contains("https://graph.microsoft.com/User.Read")); + + driver.get((app.root() + "accessTokenScopes/arm")); + Thread.sleep(1000); + result = driver.findElement(By.tagName("body")).getText(); + Assert.assertNotEquals("error", result); + + }); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) @@ -55,37 +69,37 @@ public void testAccessTokenScopes() { @RestController public static class DumbApp { - @GetMapping(value = "accessTokenScopes/office") - public Set office( - @RegisteredOAuth2AuthorizedClient("office") OAuth2AuthorizedClient authorizedClient) { - return Optional.of(authorizedClient) - .map(OAuth2AuthorizedClient::getAccessToken) - .map(OAuth2AccessToken::getScopes) - .orElse(null); + @GetMapping(value = "accessTokenScopes/arm") + public String arm( + @RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) { + return "error"; } @GetMapping(value = "accessTokenScopes/azure") public Set azure( @RegisteredOAuth2AuthorizedClient("azure") OAuth2AuthorizedClient authorizedClient) { return Optional.of(authorizedClient) - .map(OAuth2AuthorizedClient::getAccessToken) - .map(OAuth2AccessToken::getScopes) - .orElse(null); + .map(OAuth2AuthorizedClient::getAccessToken) + .map(OAuth2AccessToken::getScopes) + .orElse(null); } @GetMapping(value = "accessTokenScopes/graph") public Set graph( @RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient authorizedClient) { return Optional.of(authorizedClient) - .map(OAuth2AuthorizedClient::getAccessToken) - .map(OAuth2AccessToken::getScopes) - .orElse(null); + .map(OAuth2AuthorizedClient::getAccessToken) + .map(OAuth2AccessToken::getScopes) + .orElse(null); } - @GetMapping(value = "accessTokenScopes/arm") - public String arm( - @RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) { - return "error"; + @GetMapping(value = "accessTokenScopes/office") + public Set office( + @RegisteredOAuth2AuthorizedClient("office") OAuth2AuthorizedClient authorizedClient) { + return Optional.of(authorizedClient) + .map(OAuth2AuthorizedClient::getAccessToken) + .map(OAuth2AccessToken::getScopes) + .orElse(null); } } diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/login/AADLoginIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java similarity index 76% rename from sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/login/AADLoginIT.java rename to sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java index f084ba5ac4e9..58a3fcf8f0cc 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/login/AADLoginIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java @@ -1,12 +1,13 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -package com.azure.test.aad.login; +package com.azure.test.aad.selenium.login; import com.azure.test.aad.selenium.AADLoginRunner; import org.junit.Assert; import org.junit.Test; import org.openqa.selenium.By; +import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -27,17 +28,28 @@ public class AADLoginIT { @Test public void loginTest() { - AADLoginRunner.build(DumbApp.class).login().run((app, driver, wait) -> { - AADLoginRunner.EasyTester tester = new AADLoginRunner.EasyTester(app, driver); - tester.assertEquals("api/home", "home"); - tester.assertEquals("api/group1", "group1"); - tester.assertNotEquals("api/status403", "error"); + AADLoginRunner.build(DumbApp.class).login().run((app, driver) -> { + driver.get((app.root() + "api/home")); + Thread.sleep(1000); + String result = driver.findElement(By.tagName("body")).getText(); + Assert.assertEquals("home", result); + + driver.get((app.root() + "api/group1")); + Thread.sleep(1000); + result = driver.findElement(By.tagName("body")).getText(); + Assert.assertEquals("group1", result); + + driver.get((app.root() + "api/status403")); + Thread.sleep(1000); + result = driver.findElement(By.tagName("body")).getText(); + Assert.assertNotEquals("error", result); }); } @Test public void logoutTest() { - AADLoginRunner.build(DumbApp.class).login().run((app, driver, wait) -> { + AADLoginRunner.build(DumbApp.class).login().run((app, driver) -> { + WebDriverWait wait = new WebDriverWait(driver, 10); driver.get(app.root() + "logout"); wait.until(presenceOfElementLocated(By.cssSelector("button[type='submit']"))).click(); Thread.sleep(10000); From 24c1603255856a4367ce2c6165da7055779fb80f Mon Sep 17 00:00:00 2001 From: Jack Lu Date: Mon, 28 Dec 2020 15:00:35 +0800 Subject: [PATCH 03/16] modify selenium tests --- .../com/azure/test/aad/selenium/AADLoginRunner.java | 12 ++++++------ .../access/token/scopes/AccessTokenScopesIT.java | 6 +++--- .../azure/test/aad/selenium/login/AADLoginIT.java | 6 ++++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java index 1b9ef33f69cf..32ea02e61a1c 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java @@ -24,9 +24,6 @@ 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); - static { final String directory = "src/test/resources/driver/"; final String chromedriverLinux = "chromedriver_linux64"; @@ -72,6 +69,7 @@ private AADLoginRunner(String username, String password, AppRunner app, WebDrive } public static AADLoginRunnerConfiguration build(Class appClass) { + Objects.requireNonNull(appClass); return new AADLoginRunnerConfiguration(appClass); } @@ -139,18 +137,20 @@ public AADLoginRunnerConfiguration configure(Consumer configure) { return this; } - public AADLoginRunnerConfiguration extendsDefault(Consumer configure) { - this.configure = defautlConfigure().andThen(configure); + public AADLoginRunnerConfiguration extendsConfigure(Consumer configure) { + this.configure = this.configure.andThen(configure); return this; } public AADLoginRunner login(String username, String password) { + Objects.requireNonNull(username); + Objects.requireNonNull(password); this.configure.accept(this.app); return new AADLoginRunner(username, password, this.app, this.driver); } public AADLoginRunner login() { - return login(DEFAULT_USERNAME, DEFAULT_PASSWORD); + return login(System.getenv(AAD_USER_NAME_1), System.getenv(AAD_USER_PASSWORD_1)); } private static Consumer defautlConfigure() { diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java index 5fc7e1cbacc6..5edb0b749d27 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java @@ -22,13 +22,13 @@ public class AccessTokenScopesIT { @Test public void testAccessTokenScopes() { - AADLoginRunner.build(DumbApp.class).extendsDefault(app -> { + AADLoginRunner.build(DumbApp.class).extendsConfigure(app -> { - app.property("azure.activedirectory.authorization.office.scopes", + app.property("azure.activedirectory.authorization-clients.office.scopes", "https://manage.office.com/ActivityFeed.Read , " + "https://manage.office.com/ActivityFeed.ReadDlp , " + "https://manage.office.com/ServiceHealth.Read"); - app.property("azure.activedirectory.authorization.graph.scopes", + app.property("azure.activedirectory.authorization-clients.graph.scopes", "https://graph.microsoft.com/User.Read , " + "https://graph.microsoft.com/Directory.AccessAsUser.All"); diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java index 58a3fcf8f0cc..0fa03cca319b 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java @@ -20,6 +20,7 @@ import java.security.Principal; +import static com.azure.test.aad.AADTestUtils.AAD_USER_NAME_1; import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; public class AADLoginIT { @@ -49,15 +50,16 @@ public void loginTest() { @Test public void logoutTest() { AADLoginRunner.build(DumbApp.class).login().run((app, driver) -> { + final String username = System.getenv(AAD_USER_NAME_1); 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='" + AADLoginRunner.DEFAULT_USERNAME + "']"; + 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(AADLoginRunner.DEFAULT_USERNAME, id); + Assert.assertEquals(username, id); }); } From bb04b7f4476b69a57291f0e16e485f0a3bfd9f61 Mon Sep 17 00:00:00 2001 From: Jack Lu Date: Mon, 28 Dec 2020 15:43:32 +0800 Subject: [PATCH 04/16] Rename AuthorizationProperties to AuthorizationClientProperties --- .../aad/webapi/AADResourceServerOboConfiguration.java | 4 ++-- .../azure/spring/aad/webapp/AADWebAppConfiguration.java | 8 ++++---- ...Properties.java => AuthorizationClientProperties.java} | 2 +- .../autoconfigure/aad/AADAuthenticationProperties.java | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) rename sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/{AuthorizationProperties.java => AuthorizationClientProperties.java} (92%) diff --git a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapi/AADResourceServerOboConfiguration.java b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapi/AADResourceServerOboConfiguration.java index 925c6db3a372..4d28ee783e4b 100644 --- a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapi/AADResourceServerOboConfiguration.java +++ b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapi/AADResourceServerOboConfiguration.java @@ -4,7 +4,7 @@ package com.azure.spring.aad.webapi; import com.azure.spring.aad.webapp.AADAuthorizationServerEndpoints; -import com.azure.spring.aad.webapp.AuthorizationProperties; +import com.azure.spring.aad.webapp.AuthorizationClientProperties; import com.azure.spring.autoconfigure.aad.AADAuthenticationProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -60,7 +60,7 @@ public OAuth2AuthorizedClientRepository oAuth2AuthorizedClientRepository( public List createOboClients() { List result = new ArrayList<>(); for (String name : properties.getAuthorizationClients().keySet()) { - AuthorizationProperties authorizationProperties = properties.getAuthorizationClients().get(name); + AuthorizationClientProperties authorizationProperties = properties.getAuthorizationClients().get(name); ClientRegistration.Builder builder = createClientBuilder(name); builder.scope(authorizationProperties.getScopes()); result.add(builder.build()); diff --git a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java index eb137b5e70e1..3a5a2a5067c6 100644 --- a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java +++ b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AADWebAppConfiguration.java @@ -76,7 +76,7 @@ private AzureClientRegistration createDefaultClient() { private Set allScopes() { Set result = accessTokenScopes(); - for (AuthorizationProperties authProperties : properties.getAuthorizationClients().values()) { + for (AuthorizationClientProperties authProperties : properties.getAuthorizationClients().values()) { if (!authProperties.isOnDemand()) { result.addAll(authProperties.getScopes()); } @@ -94,7 +94,7 @@ private Set accessTokenScopes() { } private void addAzureConfiguredScopes(Set result) { - AuthorizationProperties azureProperties = + AuthorizationClientProperties azureProperties = properties.getAuthorizationClients().get(AZURE_CLIENT_REGISTRATION_ID); if (azureProperties != null) { result.addAll(azureProperties.getScopes()); @@ -119,13 +119,13 @@ private List createAuthzClients() { continue; } - AuthorizationProperties authz = properties.getAuthorizationClients().get(name); + AuthorizationClientProperties authz = properties.getAuthorizationClients().get(name); result.add(createClientBuilder(name, authz)); } return result; } - private ClientRegistration createClientBuilder(String id, AuthorizationProperties authz) { + private ClientRegistration createClientBuilder(String id, AuthorizationClientProperties authz) { ClientRegistration.Builder result = createClientBuilder(id); List scopes = authz.getScopes(); if (authz.isOnDemand()) { diff --git a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AuthorizationProperties.java b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AuthorizationClientProperties.java similarity index 92% rename from sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AuthorizationProperties.java rename to sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AuthorizationClientProperties.java index 0119d47e1e42..501d1d8f5037 100644 --- a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AuthorizationProperties.java +++ b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/aad/webapp/AuthorizationClientProperties.java @@ -8,7 +8,7 @@ /** * Properties for an oauth2 client. */ -public class AuthorizationProperties { +public class AuthorizationClientProperties { private List scopes; diff --git a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java index c4ba1e0a603e..81fd074b7e48 100644 --- a/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java +++ b/sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/aad/AADAuthenticationProperties.java @@ -3,7 +3,7 @@ package com.azure.spring.autoconfigure.aad; -import com.azure.spring.aad.webapp.AuthorizationProperties; +import com.azure.spring.aad.webapp.AuthorizationClientProperties; import com.nimbusds.jose.jwk.source.RemoteJWKSet; import java.util.ArrayList; import java.util.Collections; @@ -104,7 +104,7 @@ public class AADAuthenticationProperties { private String graphMembershipUri = "https://graph.microsoft.com/v1.0/me/memberOf"; - private Map authorizationClients = new HashMap<>(); + private Map authorizationClients = new HashMap<>(); @DeprecatedConfigurationProperty( reason = "Configuration moved to UserGroup class to keep UserGroup properties together", @@ -310,11 +310,11 @@ public void setGraphMembershipUri(String graphMembershipUri) { this.graphMembershipUri = graphMembershipUri; } - public Map getAuthorizationClients() { + public Map getAuthorizationClients() { return authorizationClients; } - public void setAuthorizationClients(Map authorizationClients) { + public void setAuthorizationClients(Map authorizationClients) { this.authorizationClients = authorizationClients; } From 64ad42d98099303ebcae75d1ab5a71c070c81482 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Mon, 28 Dec 2020 20:16:48 +0800 Subject: [PATCH 05/16] Simplify code of AADSeleniumITHelper. --- .../test/aad/selenium/AADLoginRunner.java | 166 ------------------ .../aad/selenium/AADSeleniumITHelper.java | 138 +++++++++++++++ .../token/scopes/AccessTokenScopesIT.java | 85 +++++---- .../aad/selenium/login/AADLoginAndRoleIT.java | 56 ++++++ .../test/aad/selenium/login/AADLoginIT.java | 89 ---------- .../test/aad/selenium/logout/AADLogoutIT.java | 41 +++++ 6 files changed, 276 insertions(+), 299 deletions(-) delete mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java create mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java create mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java delete mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java create mode 100644 sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/logout/AADLogoutIT.java diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java deleted file mode 100644 index 32ea02e61a1c..000000000000 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADLoginRunner.java +++ /dev/null @@ -1,166 +0,0 @@ -package com.azure.test.aad.selenium; - -import com.azure.test.utils.AppRunner; -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.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 { - - 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 AADLoginRunner(String username, String password, AppRunner app, WebDriver driver) { - this.username = username; - this.password = password; - this.app = app; - this.driver = driver; - } - - public static AADLoginRunnerConfiguration build(Class appClass) { - Objects.requireNonNull(appClass); - return new AADLoginRunnerConfiguration(appClass); - } - - public void run(BrowserCommandWithAppRunner command) { - try { - this.app.start(); - command.login((app, driver) -> { - WebDriverWait wait = new WebDriverWait(this.driver, 10); - 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); - }).run(this.app, this.driver); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - if (this.driver != null) { - this.driver.quit(); - } - if (this.app != null) { - this.app.close(); - } - } - } - - @FunctionalInterface - public interface BrowserCommandWithAppRunner { - - default BrowserCommandWithAppRunner login(BrowserCommandWithAppRunner login) { - Objects.requireNonNull(login); - return (AppRunner app, WebDriver driver) -> { - login.run(app, driver); - run(app, driver); - }; - } - - void run(AppRunner app, WebDriver driver) throws Exception; - } - - public static class AADLoginRunnerConfiguration { - private final AppRunner app; - private final WebDriver driver; - private Consumer 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 configure) { - this.configure = configure; - return this; - } - - public AADLoginRunnerConfiguration extendsConfigure(Consumer configure) { - this.configure = this.configure.andThen(configure); - return this; - } - - public AADLoginRunner login(String username, String password) { - Objects.requireNonNull(username); - Objects.requireNonNull(password); - this.configure.accept(this.app); - return new AADLoginRunner(username, password, this.app, this.driver); - } - - public AADLoginRunner login() { - return login(System.getenv(AAD_USER_NAME_1), System.getenv(AAD_USER_PASSWORD_1)); - } - - private static Consumer 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}"); - }; - } - } -} diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java new file mode 100644 index 000000000000..c3e92a0c1f2c --- /dev/null +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java @@ -0,0 +1,138 @@ +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.Collections; +import java.util.HashMap; +import java.util.List; +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 Map arguments; + private final AppRunner app; + private final WebDriver driver; + private static final Map DEFAULT_ARGUMENTS = new HashMap<>(); + + public AADSeleniumITHelper(Class appClass, Map arguments) throws InterruptedException { + this.username = System.getenv(AAD_USER_NAME_1); + this.password = System.getenv(AAD_USER_PASSWORD_1); + this.arguments = new HashMap<>(DEFAULT_ARGUMENTS); + this.arguments.putAll(arguments); + this.app = new AppRunner(appClass); + arguments.forEach(app::property); + + ChromeOptions options = new ChromeOptions(); + options.addArguments("--headless"); + options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage"); + this.driver = new ChromeDriver(options); + + login(); + } + + static { + DEFAULT_ARGUMENTS.put("azure.activedirectory.tenant-id", System.getenv(AAD_TENANT_ID_1)); + DEFAULT_ARGUMENTS.put("azure.activedirectory.client-id", System.getenv(AAD_MULTI_TENANT_CLIENT_ID)); + DEFAULT_ARGUMENTS.put("azure.activedirectory.client-secret", System.getenv(AAD_MULTI_TENANT_CLIENT_SECRET)); + DEFAULT_ARGUMENTS.put("azure.activedirectory.user-group.allowed-groups", "group1"); + DEFAULT_ARGUMENTS.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(); + } + } + } + + 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(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); + } + + public void httpGetAndAssertContains(String endpoint, + List containedValues) throws InterruptedException { + httpGetAndAssert(endpoint, containedValues, Collections.emptyList()); + } + + public void httpGetAndAssertNotContains(String endpoint, + List containedValues) throws InterruptedException { + httpGetAndAssert(endpoint, Collections.emptyList(), containedValues); + } + + public void httpGetAndAssert(String endpoint, + List containedValues, + List notContainedValues) throws InterruptedException { + driver.get((app.root() + endpoint)); + Thread.sleep(1000); + String actualValue = driver.findElement(By.tagName("body")).getText(); + containedValues.forEach( + containedValue -> Assert.assertTrue(actualValue.contains(containedValue))); + notContainedValues.forEach( + notContainedValue -> Assert.assertFalse(actualValue.contains(notContainedValue))); + } + + 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); + } +} diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java index 5edb0b749d27..29da2cec1510 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java @@ -3,10 +3,8 @@ package com.azure.test.aad.selenium.access.token.scopes; -import com.azure.test.aad.selenium.AADLoginRunner; -import org.junit.Assert; +import com.azure.test.aad.selenium.AADSeleniumITHelper; import org.junit.Test; -import org.openqa.selenium.By; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; @@ -15,53 +13,52 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.util.Set; public class AccessTokenScopesIT { @Test - public void testAccessTokenScopes() { - AADLoginRunner.build(DumbApp.class).extendsConfigure(app -> { - - app.property("azure.activedirectory.authorization-clients.office.scopes", - "https://manage.office.com/ActivityFeed.Read , " - + "https://manage.office.com/ActivityFeed.ReadDlp , " - + "https://manage.office.com/ServiceHealth.Read"); - app.property("azure.activedirectory.authorization-clients.graph.scopes", - "https://graph.microsoft.com/User.Read , " - + "https://graph.microsoft.com/Directory.AccessAsUser.All"); - - }).login().run((app, driver) -> { - - driver.get((app.root() + "accessTokenScopes/azure")); - Thread.sleep(1000); - String result = driver.findElement(By.tagName("body")).getText(); - Assert.assertTrue(result.contains("profile")); - Assert.assertTrue(result.contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); - Assert.assertTrue(result.contains("https://graph.microsoft.com/User.Read")); - - driver.get((app.root() + "accessTokenScopes/office")); - Thread.sleep(1000); - result = driver.findElement(By.tagName("body")).getText(); - Assert.assertFalse(result.contains("profile")); - Assert.assertTrue(result.contains("https://manage.office.com/ActivityFeed.Read")); - Assert.assertTrue(result.contains("https://manage.office.com/ActivityFeed.ReadDlp")); - Assert.assertTrue(result.contains("https://manage.office.com/ServiceHealth.Read")); - - driver.get((app.root() + "accessTokenScopes/graph")); - Thread.sleep(1000); - result = driver.findElement(By.tagName("body")).getText(); - Assert.assertTrue(result.contains("profile")); - Assert.assertTrue(result.contains("https://graph.microsoft.com/Directory.AccessAsUser.All")); - Assert.assertTrue(result.contains("https://graph.microsoft.com/User.Read")); - - driver.get((app.root() + "accessTokenScopes/arm")); - Thread.sleep(1000); - result = driver.findElement(By.tagName("body")).getText(); - Assert.assertNotEquals("error", result); - - }); + public void testAccessTokenScopes() throws InterruptedException { + Map 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); + + aadSeleniumITHelper.httpGetAndAssertContains( + "accessTokenScopes/azure", + Arrays.asList( + "profile", + "https://graph.microsoft.com/Directory.AccessAsUser.All", + "https://graph.microsoft.com/User.Read")); + + aadSeleniumITHelper.httpGetAndAssertContains( + "accessTokenScopes/graph", + Arrays.asList( + "profile", + "https://graph.microsoft.com/Directory.AccessAsUser.All", + "https://graph.microsoft.com/User.Read")); + + aadSeleniumITHelper.httpGetAndAssert( + "accessTokenScopes/office", + Arrays.asList( + "https://manage.office.com/ActivityFeed.Read", + "https://manage.office.com/ActivityFeed.ReadDlp", + "https://manage.office.com/ServiceHealth.Read"), + Collections.singletonList("profile")); + + aadSeleniumITHelper.httpGetAndAssertNotContains( + "accessTokenScopes/arm", + Collections.singletonList("error")); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java new file mode 100644 index 000000000000..92e14cce086f --- /dev/null +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.test.aad.selenium.login; + +import com.azure.test.aad.selenium.AADSeleniumITHelper; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.security.Principal; +import java.util.Collections; + +public class AADLoginAndRoleIT { + + private static final Logger LOGGER = LoggerFactory.getLogger(AADLoginAndRoleIT.class); + + @Test + public void roleTest() throws InterruptedException { + AADSeleniumITHelper aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, Collections.emptyMap()); + aadSeleniumITHelper.httpGetAndAssertContains("api/home", Collections.singletonList("home")); + aadSeleniumITHelper.httpGetAndAssertContains("api/group1", Collections.singletonList("group1")); + aadSeleniumITHelper.httpGetAndAssertNotContains("api/status403", Collections.singletonList("error")); + } + + @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) + @SpringBootApplication + @RestController + public static class DumbApp { + + @GetMapping(value = "/api/home") + public ResponseEntity home(Principal principal) { + LOGGER.info(((OAuth2AuthenticationToken) principal).getAuthorities().toString()); + return ResponseEntity.ok("home"); + } + + @PreAuthorize("hasRole('ROLE_group1')") + @GetMapping(value = "/api/group1") + public ResponseEntity group1() { + return ResponseEntity.ok("group1"); + } + + @PreAuthorize("hasRole('ROLE_fdsaliieammQiovlikIOWssIEURsafjFelasdfe')") + @GetMapping(value = "/api/status403") + public ResponseEntity status403() { + return ResponseEntity.ok("error"); + } + } +} diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java deleted file mode 100644 index 0fa03cca319b..000000000000 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginIT.java +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.test.aad.selenium.login; - -import com.azure.test.aad.selenium.AADLoginRunner; -import org.junit.Assert; -import org.junit.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.support.ui.WebDriverWait; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; -import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.security.Principal; - -import static com.azure.test.aad.AADTestUtils.AAD_USER_NAME_1; -import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated; - -public class AADLoginIT { - - private static final Logger LOGGER = LoggerFactory.getLogger(AADLoginIT.class); - - @Test - public void loginTest() { - AADLoginRunner.build(DumbApp.class).login().run((app, driver) -> { - driver.get((app.root() + "api/home")); - Thread.sleep(1000); - String result = driver.findElement(By.tagName("body")).getText(); - Assert.assertEquals("home", result); - - driver.get((app.root() + "api/group1")); - Thread.sleep(1000); - result = driver.findElement(By.tagName("body")).getText(); - Assert.assertEquals("group1", result); - - driver.get((app.root() + "api/status403")); - Thread.sleep(1000); - result = driver.findElement(By.tagName("body")).getText(); - Assert.assertNotEquals("error", result); - }); - } - - @Test - public void logoutTest() { - AADLoginRunner.build(DumbApp.class).login().run((app, driver) -> { - final String username = System.getenv(AAD_USER_NAME_1); - 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); - }); - } - - @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) - @SpringBootApplication - @RestController - public static class DumbApp { - - @PreAuthorize("hasRole('ROLE_group1')") - @GetMapping(value = "/api/group1") - public ResponseEntity group1() { - return ResponseEntity.ok("group1"); - } - - @GetMapping(value = "/api/home") - public ResponseEntity home(Principal principal) { - LOGGER.info(((OAuth2AuthenticationToken) principal).getAuthorities().toString()); - return ResponseEntity.ok("home"); - } - - @PreAuthorize("hasRole('ROLE_fdsaliieammQiovlikIOWssIEURsafjFelasdfe')") - @GetMapping(value = "/api/status403") - public ResponseEntity status403() { - return ResponseEntity.ok("error"); - } - } -} diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/logout/AADLogoutIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/logout/AADLogoutIT.java new file mode 100644 index 000000000000..10253ea48cfa --- /dev/null +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/logout/AADLogoutIT.java @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.test.aad.selenium.logout; + +import com.azure.test.aad.selenium.AADSeleniumITHelper; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.ResponseEntity; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.security.Principal; +import java.util.Collections; + +public class AADLogoutIT { + + private static final Logger LOGGER = LoggerFactory.getLogger(AADLogoutIT.class); + + @Test + public void logoutTest() throws InterruptedException { + AADSeleniumITHelper aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, Collections.emptyMap()); + aadSeleniumITHelper.logoutTest(); + } + + @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) + @SpringBootApplication + @RestController + public static class DumbApp { + + @GetMapping(value = "/api/home") + public ResponseEntity home(Principal principal) { + LOGGER.info(((OAuth2AuthenticationToken) principal).getAuthorities().toString()); + return ResponseEntity.ok("home"); + } + } +} From 8b06fbc9e69a4270a7b41e39e71f7709a10c8b66 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 07:29:32 +0800 Subject: [PATCH 06/16] In AADSeleniumITHelper: 1. Delete httpGetAndAssert, use httpGet instead. 2. Add "this.app.start()" --- .../aad/selenium/AADSeleniumITHelper.java | 23 ++--------- .../token/scopes/AccessTokenScopesIT.java | 40 ++++++++----------- .../aad/selenium/login/AADLoginAndRoleIT.java | 10 +++-- 3 files changed, 26 insertions(+), 47 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java index c3e92a0c1f2c..20a94edc74bd 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java @@ -12,9 +12,7 @@ import java.io.File; import java.io.IOException; -import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -47,6 +45,7 @@ public AADSeleniumITHelper(Class appClass, Map arguments) thr options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage"); this.driver = new ChromeDriver(options); + this.app.start(); login(); } @@ -102,26 +101,10 @@ private void login() throws InterruptedException { Thread.sleep(10000); } - public void httpGetAndAssertContains(String endpoint, - List containedValues) throws InterruptedException { - httpGetAndAssert(endpoint, containedValues, Collections.emptyList()); - } - - public void httpGetAndAssertNotContains(String endpoint, - List containedValues) throws InterruptedException { - httpGetAndAssert(endpoint, Collections.emptyList(), containedValues); - } - - public void httpGetAndAssert(String endpoint, - List containedValues, - List notContainedValues) throws InterruptedException { + public String httpGet(String endpoint) throws InterruptedException { driver.get((app.root() + endpoint)); Thread.sleep(1000); - String actualValue = driver.findElement(By.tagName("body")).getText(); - containedValues.forEach( - containedValue -> Assert.assertTrue(actualValue.contains(containedValue))); - notContainedValues.forEach( - notContainedValue -> Assert.assertFalse(actualValue.contains(notContainedValue))); + return driver.findElement(By.tagName("body")).getText(); } public void logoutTest() throws InterruptedException { diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java index 29da2cec1510..f7491942cc98 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java @@ -4,6 +4,7 @@ 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; @@ -13,8 +14,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -34,31 +33,24 @@ public void testAccessTokenScopes() throws InterruptedException { + "https://graph.microsoft.com/Directory.AccessAsUser.All"); AADSeleniumITHelper aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, arguments); - aadSeleniumITHelper.httpGetAndAssertContains( - "accessTokenScopes/azure", - Arrays.asList( - "profile", - "https://graph.microsoft.com/Directory.AccessAsUser.All", - "https://graph.microsoft.com/User.Read")); + 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")); - aadSeleniumITHelper.httpGetAndAssertContains( - "accessTokenScopes/graph", - Arrays.asList( - "profile", - "https://graph.microsoft.com/Directory.AccessAsUser.All", - "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")); - aadSeleniumITHelper.httpGetAndAssert( - "accessTokenScopes/office", - Arrays.asList( - "https://manage.office.com/ActivityFeed.Read", - "https://manage.office.com/ActivityFeed.ReadDlp", - "https://manage.office.com/ServiceHealth.Read"), - Collections.singletonList("profile")); + 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")); - aadSeleniumITHelper.httpGetAndAssertNotContains( - "accessTokenScopes/arm", - Collections.singletonList("error")); + httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/arm"); + Assert.assertFalse(httpResponse.contains("error")); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java index 92e14cce086f..c9e5db06510a 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java @@ -4,6 +4,7 @@ package com.azure.test.aad.selenium.login; import com.azure.test.aad.selenium.AADSeleniumITHelper; +import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,9 +26,12 @@ public class AADLoginAndRoleIT { @Test public void roleTest() throws InterruptedException { AADSeleniumITHelper aadSeleniumITHelper = new AADSeleniumITHelper(DumbApp.class, Collections.emptyMap()); - aadSeleniumITHelper.httpGetAndAssertContains("api/home", Collections.singletonList("home")); - aadSeleniumITHelper.httpGetAndAssertContains("api/group1", Collections.singletonList("group1")); - aadSeleniumITHelper.httpGetAndAssertNotContains("api/status403", Collections.singletonList("error")); + String httpResponse = aadSeleniumITHelper.httpGet("api/home"); + Assert.assertTrue(httpResponse.contains("home")); + httpResponse = aadSeleniumITHelper.httpGet("api/group1"); + Assert.assertTrue(httpResponse.contains("group1")); + httpResponse = aadSeleniumITHelper.httpGet("api/status403"); + Assert.assertFalse(httpResponse.contains("error")); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) From 8dfc13d6265569c4f2e210cc9d8ae30e257b4122 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 08:07:19 +0800 Subject: [PATCH 07/16] Fix error about app properties. --- .../aad/selenium/AADSeleniumITHelper.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java index 20a94edc74bd..bb34d8366431 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java @@ -27,18 +27,17 @@ public class AADSeleniumITHelper { private final String username; private final String password; - private final Map arguments; private final AppRunner app; private final WebDriver driver; private static final Map DEFAULT_ARGUMENTS = new HashMap<>(); - public AADSeleniumITHelper(Class appClass, Map arguments) throws InterruptedException { - this.username = System.getenv(AAD_USER_NAME_1); - this.password = System.getenv(AAD_USER_PASSWORD_1); - this.arguments = new HashMap<>(DEFAULT_ARGUMENTS); - this.arguments.putAll(arguments); - this.app = new AppRunner(appClass); - arguments.forEach(app::property); + public AADSeleniumITHelper(Class appClass, Map properties) throws InterruptedException { + username = System.getenv(AAD_USER_NAME_1); + password = System.getenv(AAD_USER_PASSWORD_1); + Map appProperties = new HashMap<>(DEFAULT_ARGUMENTS); + appProperties.putAll(properties); + app = new AppRunner(appClass); + appProperties.forEach(app::property); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); @@ -89,14 +88,10 @@ public AADSeleniumITHelper(Class appClass, Map arguments) thr 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(this.username + Keys.ENTER); + wait.until(presenceOfElementLocated(By.name("loginfmt"))).sendKeys(username + Keys.ENTER); Thread.sleep(10000); - - driver.findElement(By.name("passwd")) - .sendKeys(this.password + Keys.ENTER); + driver.findElement(By.name("passwd")).sendKeys(password + Keys.ENTER); Thread.sleep(10000); - driver.findElement(By.cssSelector("input[type='submit']")).click(); Thread.sleep(10000); } From d58ee31038e0725baf06ca7efb42975cd1a5a935 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 08:08:58 +0800 Subject: [PATCH 08/16] Change the order of static code block. --- .../aad/selenium/AADSeleniumITHelper.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java index bb34d8366431..f705c7268733 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java @@ -31,23 +31,6 @@ public class AADSeleniumITHelper { private final WebDriver driver; private static final Map DEFAULT_ARGUMENTS = new HashMap<>(); - public AADSeleniumITHelper(Class appClass, Map properties) throws InterruptedException { - username = System.getenv(AAD_USER_NAME_1); - password = System.getenv(AAD_USER_PASSWORD_1); - Map appProperties = new HashMap<>(DEFAULT_ARGUMENTS); - appProperties.putAll(properties); - app = new AppRunner(appClass); - appProperties.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(); - } - static { DEFAULT_ARGUMENTS.put("azure.activedirectory.tenant-id", System.getenv(AAD_TENANT_ID_1)); DEFAULT_ARGUMENTS.put("azure.activedirectory.client-id", System.getenv(AAD_MULTI_TENANT_CLIENT_ID)); @@ -85,6 +68,23 @@ public AADSeleniumITHelper(Class appClass, Map properties) th } } + public AADSeleniumITHelper(Class appClass, Map properties) throws InterruptedException { + username = System.getenv(AAD_USER_NAME_1); + password = System.getenv(AAD_USER_PASSWORD_1); + Map appProperties = new HashMap<>(DEFAULT_ARGUMENTS); + appProperties.putAll(properties); + app = new AppRunner(appClass); + appProperties.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"); From a1c35fdfb365c6000cf628832cc1c86cfe2b3ac7 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 08:10:04 +0800 Subject: [PATCH 09/16] No logic change, just change class name. --- .../{AccessTokenScopesIT.java => AADAccessTokenScopesIT.java} | 2 +- .../selenium/login/{AADLoginAndRoleIT.java => AADRoleIT.java} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/{AccessTokenScopesIT.java => AADAccessTokenScopesIT.java} (99%) rename sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/{AADLoginAndRoleIT.java => AADRoleIT.java} (97%) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java similarity index 99% rename from sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java rename to sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java index f7491942cc98..a90c1d28ebfb 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java @@ -19,7 +19,7 @@ import java.util.Optional; import java.util.Set; -public class AccessTokenScopesIT { +public class AADAccessTokenScopesIT { @Test public void testAccessTokenScopes() throws InterruptedException { diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADRoleIT.java similarity index 97% rename from sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java rename to sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADRoleIT.java index c9e5db06510a..47e5b7753e81 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADLoginAndRoleIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADRoleIT.java @@ -19,9 +19,9 @@ import java.security.Principal; import java.util.Collections; -public class AADLoginAndRoleIT { +public class AADRoleIT { - private static final Logger LOGGER = LoggerFactory.getLogger(AADLoginAndRoleIT.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AADRoleIT.class); @Test public void roleTest() throws InterruptedException { From 0d1fc0f42e93fb54a164bee0711c0df5ec067ecf Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:03:14 +0800 Subject: [PATCH 10/16] Update properties. --- .../test/aad/selenium/AADSeleniumITHelper.java | 17 ++++++++--------- .../token/scopes/AADAccessTokenScopesIT.java | 3 +-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java index f705c7268733..5cbc6fa12c34 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/AADSeleniumITHelper.java @@ -29,14 +29,14 @@ public class AADSeleniumITHelper { private final String password; private final AppRunner app; private final WebDriver driver; - private static final Map DEFAULT_ARGUMENTS = new HashMap<>(); + private static final Map DEFAULT_PROPERTIES = new HashMap<>(); static { - DEFAULT_ARGUMENTS.put("azure.activedirectory.tenant-id", System.getenv(AAD_TENANT_ID_1)); - DEFAULT_ARGUMENTS.put("azure.activedirectory.client-id", System.getenv(AAD_MULTI_TENANT_CLIENT_ID)); - DEFAULT_ARGUMENTS.put("azure.activedirectory.client-secret", System.getenv(AAD_MULTI_TENANT_CLIENT_SECRET)); - DEFAULT_ARGUMENTS.put("azure.activedirectory.user-group.allowed-groups", "group1"); - DEFAULT_ARGUMENTS.put("azure.activedirectory.post-logout-redirect-uri", "http://localhost:${server.port}"); + 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"; @@ -71,10 +71,9 @@ public class AADSeleniumITHelper { public AADSeleniumITHelper(Class appClass, Map properties) throws InterruptedException { username = System.getenv(AAD_USER_NAME_1); password = System.getenv(AAD_USER_PASSWORD_1); - Map appProperties = new HashMap<>(DEFAULT_ARGUMENTS); - appProperties.putAll(properties); app = new AppRunner(appClass); - appProperties.forEach(app::property); + DEFAULT_PROPERTIES.forEach(app::property); + properties.forEach(app::property); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java index a90c1d28ebfb..30f8d1f85953 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java @@ -29,8 +29,7 @@ public void testAccessTokenScopes() throws InterruptedException { + "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"); + "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"); From f2cb8f1a940469d7575f511cb23ccc894348a8eb Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:13:19 +0800 Subject: [PATCH 11/16] Change package name for AADRoleIT. --- .../com/azure/test/aad/selenium/{login => role}/AADRoleIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/{login => role}/AADRoleIT.java (98%) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADRoleIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java similarity index 98% rename from sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADRoleIT.java rename to sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java index 47e5b7753e81..853d8bc9a886 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/login/AADRoleIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -package com.azure.test.aad.selenium.login; +package com.azure.test.aad.selenium.role; import com.azure.test.aad.selenium.AADSeleniumITHelper; import org.junit.Assert; From dba4e8f6b92c1f6829c4913c98f497869fa14a88 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:30:58 +0800 Subject: [PATCH 12/16] No logic change, just make property easier to read. --- .../access/token/scopes/AADAccessTokenScopesIT.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java index 30f8d1f85953..924d2282c18e 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java @@ -24,11 +24,12 @@ public class AADAccessTokenScopesIT { @Test public void testAccessTokenScopes() throws InterruptedException { Map arguments = new HashMap<>(); - arguments.put("azure.activedirectory.authorization-clients.office.scopes", - "https://manage.office.com/ActivityFeed.Read, " - + "https://manage.office.com/ActivityFeed.ReadDlp, " + 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", + 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); From e194625285db4b08ad482c3ea02c831186d6eda8 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:34:26 +0800 Subject: [PATCH 13/16] No logic change, just code order. --- .../access/token/scopes/AADAccessTokenScopesIT.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java index 924d2282c18e..aed266781295 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java @@ -58,12 +58,6 @@ public void testAccessTokenScopes() throws InterruptedException { @RestController public static class DumbApp { - @GetMapping(value = "accessTokenScopes/arm") - public String arm( - @RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) { - return "error"; - } - @GetMapping(value = "accessTokenScopes/azure") public Set azure( @RegisteredOAuth2AuthorizedClient("azure") OAuth2AuthorizedClient authorizedClient) { @@ -90,6 +84,12 @@ public Set office( .map(OAuth2AccessToken::getScopes) .orElse(null); } + + @GetMapping(value = "accessTokenScopes/arm") + public String arm( + @RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) { + return "error"; + } } } From 416923bd9ad26b5b2f4bb5fd0a019ce0983f25ea Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:37:31 +0800 Subject: [PATCH 14/16] Fix assert error in AADAccessTokenScopesIT. --- .../selenium/access/token/scopes/AADAccessTokenScopesIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java index aed266781295..60248c60389f 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java @@ -50,7 +50,7 @@ public void testAccessTokenScopes() throws InterruptedException { Assert.assertTrue(httpResponse.contains("https://manage.office.com/ServiceHealth.Read")); httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/arm"); - Assert.assertFalse(httpResponse.contains("error")); + Assert.assertNotEquals(httpResponse, "error"); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) From eabd0119a8f8bb7d1d76a50f9583f255cf637620 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:39:57 +0800 Subject: [PATCH 15/16] Fix assert error in AADRoleIT. --- .../test/java/com/azure/test/aad/selenium/role/AADRoleIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java index 853d8bc9a886..3db9f01679d2 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java @@ -31,7 +31,7 @@ public void roleTest() throws InterruptedException { httpResponse = aadSeleniumITHelper.httpGet("api/group1"); Assert.assertTrue(httpResponse.contains("group1")); httpResponse = aadSeleniumITHelper.httpGet("api/status403"); - Assert.assertFalse(httpResponse.contains("error")); + Assert.assertNotEquals(httpResponse, "error"); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) From f4224f91e381c28ad22d78bf0cc4fd1646fc6224 Mon Sep 17 00:00:00 2001 From: Rujun Chen Date: Tue, 29 Dec 2020 09:43:06 +0800 Subject: [PATCH 16/16] Change the style of forbidden endpoint. --- .../access/token/scopes/AADAccessTokenScopesIT.java | 8 ++++---- .../com/azure/test/aad/selenium/role/AADRoleIT.java | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java index 60248c60389f..c2afc98a9a01 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/access/token/scopes/AADAccessTokenScopesIT.java @@ -49,8 +49,8 @@ public void testAccessTokenScopes() throws InterruptedException { Assert.assertTrue(httpResponse.contains("https://manage.office.com/ActivityFeed.ReadDlp")); Assert.assertTrue(httpResponse.contains("https://manage.office.com/ServiceHealth.Read")); - httpResponse = aadSeleniumITHelper.httpGet("accessTokenScopes/arm"); - Assert.assertNotEquals(httpResponse, "error"); + httpResponse = aadSeleniumITHelper.httpGet("arm"); + Assert.assertNotEquals(httpResponse, "arm"); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) @@ -85,10 +85,10 @@ public Set office( .orElse(null); } - @GetMapping(value = "accessTokenScopes/arm") + @GetMapping(value = "arm") public String arm( @RegisteredOAuth2AuthorizedClient("arm") OAuth2AuthorizedClient authorizedClient) { - return "error"; + return "arm"; } } diff --git a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java index 3db9f01679d2..d21e99758cb9 100644 --- a/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java +++ b/sdk/spring/azure-spring-boot-test-aad/src/test/java/com/azure/test/aad/selenium/role/AADRoleIT.java @@ -30,8 +30,8 @@ public void roleTest() throws InterruptedException { Assert.assertTrue(httpResponse.contains("home")); httpResponse = aadSeleniumITHelper.httpGet("api/group1"); Assert.assertTrue(httpResponse.contains("group1")); - httpResponse = aadSeleniumITHelper.httpGet("api/status403"); - Assert.assertNotEquals(httpResponse, "error"); + httpResponse = aadSeleniumITHelper.httpGet("api/group_fdsaliieammQiovlikIOWssIEURsafjFelasdfe"); + Assert.assertNotEquals(httpResponse, "group_fdsaliieammQiovlikIOWssIEURsafjFelasdfe"); } @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) @@ -52,9 +52,9 @@ public ResponseEntity group1() { } @PreAuthorize("hasRole('ROLE_fdsaliieammQiovlikIOWssIEURsafjFelasdfe')") - @GetMapping(value = "/api/status403") - public ResponseEntity status403() { - return ResponseEntity.ok("error"); + @GetMapping(value = "/api/group_fdsaliieammQiovlikIOWssIEURsafjFelasdfe") + public ResponseEntity nonExistGroup() { + return ResponseEntity.ok("group_fdsaliieammQiovlikIOWssIEURsafjFelasdfe"); } } }