Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor two SAML tests to use page objects #2543

Merged
merged 7 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.cloudfoundry.identity.uaa.integration.endpoints;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.cloudfoundry.identity.uaa.integration.pageObjects.LoginPage;
import org.cloudfoundry.identity.uaa.integration.pageObjects.Page;
import org.openqa.selenium.WebDriver;

public class LogoutDoEndpoint {
static final private String urlPath = "/logout.do";

static public LoginPage logout_goesToLoginPage(WebDriver driver, String baseUrl, String redirectUrl, String clientId) {
driver.get(buildLogoutDoUrl(baseUrl, redirectUrl, clientId));
return new LoginPage(driver);
}

private static String buildLogoutDoUrl(String baseUrl, String redirectUrl, String clientId) {
return baseUrl
+ urlPath
+ "?redirect=" + URLEncoder.encode(redirectUrl, StandardCharsets.UTF_8)
+ "&client_id=" + clientId;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cloudfoundry.identity.uaa.integration.endpoints;

import org.cloudfoundry.identity.uaa.integration.pageObjects.Page;
import org.cloudfoundry.identity.uaa.integration.pageObjects.SamlWelcomePage;
import org.openqa.selenium.WebDriver;

public class SamlLogoutAuthSourceEndpoint {
static final private String urlPath = "/module.php/core/logout";

static public SamlWelcomePage logoutAuthSource_goesToSamlWelcomePage(WebDriver driver, String baseUrl, String authSource) {
driver.get(baseUrl + urlPath + "/" + authSource);
return new SamlWelcomePage(driver);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.cloudfoundry.identity.uaa.codestore.ExpiringCode;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.extensions.PollutionPreventionExtension;
import org.cloudfoundry.identity.uaa.integration.pageObjects.SamlLogoutAuthSourceEndpoint;
import org.cloudfoundry.identity.uaa.integration.endpoints.SamlLogoutAuthSourceEndpoint;
import org.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils;
import org.cloudfoundry.identity.uaa.integration.util.ScreenshotOnFail;
import org.cloudfoundry.identity.uaa.invitations.InvitationsRequest;
Expand All @@ -34,7 +34,7 @@
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
Expand Down Expand Up @@ -142,7 +142,7 @@ public void logout_and_clear_cookies() {
webDriver.get(baseUrl + "/logout.do");
}
webDriver.get(appUrl + "/j_spring_security_logout");
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goesToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
webDriver.manage().deleteAllCookies();

webDriver.get("http://localhost:8080/app/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.cloudfoundry.identity.uaa.ServerRunning;
import org.cloudfoundry.identity.uaa.account.UserInfoResponse;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.integration.pageObjects.SamlLogoutAuthSourceEndpoint;
import org.cloudfoundry.identity.uaa.integration.endpoints.SamlLogoutAuthSourceEndpoint;
import org.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils;
import org.cloudfoundry.identity.uaa.integration.util.ScreenshotOnFail;
import org.cloudfoundry.identity.uaa.oauth.jwt.Jwt;
Expand Down Expand Up @@ -233,7 +233,7 @@ public void tearDown() throws URISyntaxException {
}

private void doLogout(String zoneUrl) {
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goesToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
webDriver.manage().deleteAllCookies();

for (String url : Arrays.asList(baseUrl + "/logout.do", zoneUrl + "/logout.do")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.cloudfoundry.identity.uaa.account.UserInfoResponse;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.integration.pageObjects.FaviconElement;
import org.cloudfoundry.identity.uaa.integration.pageObjects.HomePage;
import org.cloudfoundry.identity.uaa.integration.pageObjects.LoginPage;
import org.cloudfoundry.identity.uaa.integration.endpoints.LogoutDoEndpoint;
import org.cloudfoundry.identity.uaa.integration.pageObjects.PasscodePage;
import org.cloudfoundry.identity.uaa.integration.pageObjects.SamlLogoutAuthSourceEndpoint;
import org.cloudfoundry.identity.uaa.integration.endpoints.SamlLogoutAuthSourceEndpoint;
import org.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils;
import org.cloudfoundry.identity.uaa.integration.util.ScreenshotOnFail;
import org.cloudfoundry.identity.uaa.mock.util.MockMvcUtils;
Expand Down Expand Up @@ -102,6 +104,7 @@
import static org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition.USER_ATTRIBUTE_PREFIX;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -211,7 +214,7 @@ public void clearWebDriverOfCookies() {
webDriver.get(baseUrl.replace("localhost", domain) + "/logout.do");
webDriver.manage().deleteAllCookies();
}
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goesToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
}

@Test
Expand Down Expand Up @@ -246,9 +249,9 @@ public void testContentTypes() {
public void testSimpleSamlPhpPasscodeRedirect() throws Exception {
createIdentityProvider(SAML_ORIGIN);

PasscodePage.requestPasscode_goToLoginPage(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToPasscodePage(testAccounts.getUserName(), testAccounts.getPassword());
PasscodePage.requestPasscode_goesToLoginPage(webDriver, baseUrl)
.clickSamlLink_goesToSamlLoginPage()
.login_goesToPasscodePage(testAccounts.getUserName(), testAccounts.getPassword());
}

@Test
Expand All @@ -261,16 +264,13 @@ public void testSimpleSamlLoginWithAddShadowUserOnLoginFalse() throws Exception
String clientId = "app-addnew-false"+ new RandomValueStringGenerator().generate();
String redirectUri = "http://nosuchhostname:0/nosuchendpoint";
BaseClientDetails client = createClientAndSpecifyProvider(clientId, provider, redirectUri);

String firstUrl = "/oauth/authorize?"
+ "client_id=" + clientId
+ "&response_type=code"
+ "&redirect_uri=" + URLEncoder.encode(redirectUri, StandardCharsets.UTF_8);

webDriver.get(baseUrl + firstUrl);
webDriver.findElement(By.xpath(SIMPLESAMLPHP_LOGIN_PROMPT_XPATH_EXPR));
sendCredentials(testAccounts.getUserName(), testAccounts.getPassword());

assertThat(webDriver.getCurrentUrl(), containsString(redirectUri + "?error=access_denied&error_description=SAML+user+does+not+exist.+You+can+correct+this+by+creating+a+shadow+user+for+the+SAML+user."));
}

Expand Down Expand Up @@ -317,12 +317,10 @@ public void incorrectResponseFromSamlIDP_showErrorFromSaml() {

IntegrationTestUtils.createOrUpdateProvider(zoneAdminToken, baseUrl, provider);

webDriver.get(zoneUrl);
webDriver.findElement(By.linkText("Login with Simple SAML PHP(simplesamlphp)")).click();
webDriver.findElement(By.xpath(SIMPLESAMLPHP_LOGIN_PROMPT_XPATH_EXPR));
sendCredentials(testAccounts.getUserName(), testAccounts.getPassword());

assertEquals("No local entity found for alias invalid, verify your configuration.", webDriver.findElement(By.cssSelector("h2")).getText());
HomePage.tryToGoHome_redirectsToLoginPage(webDriver, zoneUrl)
.clickSamlLink_goesToSamlLoginPage()
.login_goesToSamlErrorPage(testAccounts.getUserName(), testAccounts.getPassword())
.validatePageSource(containsString("No local entity found for alias invalid, verify your configuration"));
}

@Test
Expand All @@ -331,8 +329,8 @@ public void testSimpleSamlPhpLogin() throws Exception {

Long beforeTest = System.currentTimeMillis();
LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(testAccounts.getUserName(), testAccounts.getPassword());
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword());
Long afterTest = System.currentTimeMillis();

String zoneAdminToken = IntegrationTestUtils.getClientCredentialsToken(serverRunning, "admin", "adminsecret");
Expand All @@ -345,11 +343,11 @@ public void testSimpleSamlPhpLoginDisplaysLastLogin() throws Exception {
Long beforeTest = System.currentTimeMillis();
IdentityProvider<SamlIdentityProviderDefinition> provider = createIdentityProvider(SAML_ORIGIN);
LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goToLoginPage()
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goesToLoginPage()
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.hasLastLoginTime();

Long afterTest = System.currentTimeMillis();
Expand All @@ -363,10 +361,10 @@ public void testSingleLogout() throws Exception {
IdentityProvider<SamlIdentityProviderDefinition> provider = createIdentityProvider(SAML_ORIGIN);

LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goToLoginPage()
.clickSamlLink_goToSamlLoginPage();
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goesToLoginPage()
.clickSamlLink_goesToSamlLoginPage();
}

@Test
Expand Down Expand Up @@ -413,23 +411,21 @@ public void testSingleLogoutWithLogoutRedirect() {
provider.setConfig(providerDefinition);
provider.setOriginKey(providerDefinition.getIdpEntityAlias());
provider.setName("simplesamlphp for uaa");
provider = IntegrationTestUtils.createOrUpdateProvider(zoneAdminToken, baseUrl, provider);
IntegrationTestUtils.createOrUpdateProvider(zoneAdminToken, baseUrl, provider);

webDriver.get(zoneUrl + "/login");
Assert.assertTrue(webDriver.getTitle().contains("testzone2"));
webDriver.findElement(By.xpath("//a[text()='" + provider.getConfig().getLinkText() + "']")).click();
webDriver.findElement(By.xpath(SIMPLESAMLPHP_LOGIN_PROMPT_XPATH_EXPR));
sendCredentials(testAccounts.getUserName(), testAccounts.getPassword());
assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), Matchers.containsString("Where to"));
LoginPage loginPage = LoginPage.go(webDriver, zoneUrl);
loginPage.validateTitle(Matchers.containsString("testzone2"));
loginPage.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword());

String redirectUrl = zoneUrl + "/login?test=test";
BaseClientDetails clientDetails = new BaseClientDetails("test-logout-redirect", null, null, GRANT_TYPE_AUTHORIZATION_CODE, null);
clientDetails.setRegisteredRedirectUri(Collections.singleton(redirectUrl));
clientDetails.setClientSecret("secret");
IntegrationTestUtils.createOrUpdateClient(zoneAdminToken, baseUrl, zoneId, clientDetails);

webDriver.get(zoneUrl + "/logout.do?redirect=" + URLEncoder.encode(redirectUrl, StandardCharsets.UTF_8) + "&client_id=test-logout-redirect");
assertEquals(redirectUrl, webDriver.getCurrentUrl());
LogoutDoEndpoint.logout_goesToLoginPage(webDriver, zoneUrl, redirectUrl, "test-logout-redirect")
.validateUrl(equalTo(redirectUrl));
}

@Test
Expand All @@ -448,27 +444,27 @@ public void testSingleLogoutWithNoLogoutUrlOnIDP() throws Exception {
provider = IntegrationTestUtils.createOrUpdateProvider(zoneAdminToken, baseUrl, provider);

LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goToLoginPage()
.clickSamlLink_goToHomePage();
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(testAccounts.getUserName(), testAccounts.getPassword())
.logout_goesToLoginPage()
.clickSamlLink_goesToHomePage();
}

@Test
public void testGroupIntegration() throws Exception {
createIdentityProvider(SAML_ORIGIN);
LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(MARISSA4_USERNAME, MARISSA4_PASSWORD);
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(MARISSA4_USERNAME, MARISSA4_PASSWORD);
}

@Test
public void testFavicon_Should_Not_Save() throws Exception {
createIdentityProvider(SAML_ORIGIN);
FaviconElement.getDefaultIcon(webDriver, baseUrl);
LoginPage.go(webDriver, baseUrl)
.clickSamlLink_goToSamlLoginPage()
.login_goToHomePage(MARISSA4_USERNAME, MARISSA4_PASSWORD);
.clickSamlLink_goesToSamlLoginPage()
.login_goesToHomePage(MARISSA4_USERNAME, MARISSA4_PASSWORD);
}


Expand Down Expand Up @@ -634,7 +630,7 @@ public void perform_SamlInvitation_Automatic_Redirect_In_Zone2(String username,

webDriver.get(baseUrl + "/logout.do");
webDriver.get(zoneUrl + "/logout.do");
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goesToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
}

@Test
Expand Down Expand Up @@ -1290,7 +1286,7 @@ public void testSimpleSamlPhpLoginInTestZone1Works() {
webDriver.get(testZone1Url + "/logout.do");

//disable the provider
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
SamlLogoutAuthSourceEndpoint.logoutAuthSource_goesToSamlWelcomePage(webDriver, IntegrationTestUtils.SIMPLESAMLPHP_UAA_ACCEPTANCE, SAML_AUTH_SOURCE);
provider.setActive(false);
provider = IntegrationTestUtils.createOrUpdateProvider(zoneAdminToken,baseUrl,provider);
assertNotNull(provider.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cloudfoundry.identity.uaa.integration.pageObjects;

import java.util.Date;

import org.openqa.selenium.WebDriver;

import static org.hamcrest.Matchers.containsString;

public class DnsErrorPage extends Page {
public DnsErrorPage(WebDriver driver) {
super(driver);
validatePageSource(driver, containsString("This site can’t be reached"));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;

// TODO extend LoggedInPage
public class HomePage extends Page {
static final protected String urlPath = "/";
static final private String urlPath = "/";

public HomePage(WebDriver driver) {
super(driver);
validateUrl(driver, endsWith(urlPath));
validatePageSource(driver, containsString("Where to?"));
}

static public LoginPage tryToGoHome_redirectsToLoginPage(WebDriver driver, String baseUrl) {
driver.get(baseUrl + urlPath);
return new LoginPage(driver);
}

public boolean hasLastLoginTime() {
WebElement lastLoginTime = driver.findElement(By.id("last_login_time"));
String loginTime = lastLoginTime.getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.matchesPattern;

public class LoginPage extends Page {

static final protected String urlPath = "/login";
static final private String urlPath = "/login";

public LoginPage(WebDriver driver) {
super(driver);
validateUrl(driver, endsWith(urlPath));
validateUrl(driver, matchesPattern(".*" + urlPath + "(\\?.*)?$"));
swalchemist marked this conversation as resolved.
Show resolved Hide resolved
}

static public LoginPage go(WebDriver driver, String baseUrl) {
Expand All @@ -21,15 +21,15 @@ static public LoginPage go(WebDriver driver, String baseUrl) {

// When there is a SAML integration, there is a link to go to a SAML login page instead. This assumes there is
// only one SAML link.
public SamlLoginPage clickSamlLink_goToSamlLoginPage() {
public SamlLoginPage clickSamlLink_goesToSamlLoginPage() {
clickFirstSamlLoginLink();
return new SamlLoginPage(driver);
}

// If the SAML IDP has no logout URL in the metadata, logging out of UAA will leave
// the IDP still logged in, and when going back to the SAML login page, it will log
// the app back in automatically and immediately redirect to the post-login page.
public HomePage clickSamlLink_goToHomePage() {
public HomePage clickSamlLink_goesToHomePage() {
clickFirstSamlLoginLink();
return new HomePage(driver);
}
Expand Down
Loading