Skip to content
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
*/
public class OAuthClient extends AbstractOAuthClient<OAuthClient> {

public static String SERVER_ROOT;
public static String AUTH_SERVER_ROOT;
public static String APP_ROOT;
public static String APP_AUTH_ROOT;

static {
updateURLs(defaultServerRoot());
}

private final ManagedWebDriver managedWebDriver;
private final ClientResource clientResource;

Expand All @@ -27,14 +36,61 @@ public OAuthClient(String baseUrl, CloseableHttpClient httpClient, ManagedWebDri
this.managedWebDriver = managedWebDriver;
this.clientResource = clientResource;

config = new OAuthClientConfig()
.responseType(OAuth2Constants.CODE);
init();
}

public OAuthClient(String baseUrl, CloseableHttpClient httpClient, ManagedWebDriver managedWebDriver) {
this(baseUrl, httpClient, managedWebDriver, null);
}

private static String defaultServerRoot() {
boolean ssl = Boolean.parseBoolean(System.getProperty("auth.server.ssl.required", "true"));
String scheme = ssl ? "https" : "http";
String host = System.getProperty("auth.server.host", "localhost");
String port = ssl ? System.getProperty("auth.server.https.port", "8543") : System.getProperty("auth.server.http.port", "8180");
return removeDefaultPorts(String.format("%s://%s:%s", scheme, host, port));
}

private static String removeDefaultPorts(String url) {
return url != null
? url.replaceFirst("(.*)(:80)(\\/.*)?$", "$1$3").replaceFirst("(.*)(:443)(\\/.*)?$", "$1$3")
: null;
}

public static void updateURLs(String serverRoot) {
String normalizedRoot = removeDefaultPorts(serverRoot);
if (normalizedRoot != null) {
normalizedRoot = normalizedRoot.replaceAll("/+$", "");
}

if (normalizedRoot != null && normalizedRoot.endsWith("/auth")) {
AUTH_SERVER_ROOT = normalizedRoot;
SERVER_ROOT = normalizedRoot.substring(0, normalizedRoot.length() - "/auth".length());
} else {
SERVER_ROOT = normalizedRoot;
AUTH_SERVER_ROOT = normalizedRoot;
}
updateAppRootRealm("master");
}

public static void updateAppRootRealm(String realm) {
APP_ROOT = AUTH_SERVER_ROOT + "/realms/" + realm + "/app";
APP_AUTH_ROOT = APP_ROOT + "/auth";
}

public static void resetAppRootRealm() {
updateAppRootRealm("master");
}

public void init() {
config = new OAuthClientConfig()
.realm("test")
.client("test-app", "password")
.redirectUri(APP_ROOT + "/auth")
.postLogoutRedirectUri(APP_ROOT + "/auth")
.responseType(OAuth2Constants.CODE);
}

@Override
public void fillLoginForm(String username, String password) {
LoginPage loginPage = new LoginPage(managedWebDriver);
Expand Down Expand Up @@ -63,6 +119,10 @@ public ClientResource clientResource() {
return clientResource;
}

public org.openqa.selenium.WebDriver getDriver() {
return managedWebDriver.driver();
}

public void close() {
if (clientResource != null) {
clientResource.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,18 @@ public String getAttemptedUsername() {

public Optional<String> getInfoMessage() {
try {
return Optional.of(loginInfoMessage.getText());
String text = loginInfoMessage.getText();
if (text != null && !text.isBlank()) {
return Optional.of(text);
}
} catch (NoSuchElementException e) {
// fall through to legacy/base theme selectors
}

try {
WebElement instruction = driver.driver().findElement(By.cssSelector("#instruction1, p.instruction"));
String text = instruction.getText();
return (text == null || text.isBlank()) ? Optional.empty() : Optional.of(text);
} catch (NoSuchElementException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class LoginConfigTotpPage extends AbstractLoginPage {
@FindBy(id = "userLabel")
private WebElement totpLabelInput;

@FindBy(css = "input[type=\"submit\"]")
@FindBy(css = "input[type=\"submit\"], button[type=\"submit\"]")
private WebElement submitButton;

@FindBy(name = "cancel-aia")
Expand All @@ -61,10 +61,74 @@ public LoginConfigTotpPage(ManagedWebDriver driver) {
super(driver);
}

public void configure(String totp) {
totpInput.clear();
if (totp != null) {
totpInput.sendKeys(totp);
}
submit();
}

public void configure(String totp, String userLabel) {
totpInput.clear();
if (totp != null) {
totpInput.sendKeys(totp);
}
totpLabelInput.clear();
if (userLabel != null) {
totpLabelInput.sendKeys(userLabel);
}
submit();
}

public void submit() {
try {
submitButton.click();
} catch (NoSuchElementException e) {
submitButton.submit();
}
}

public void cancel() {
cancelAIAButton.click();
}

public String getTotpSecret() {
return totpSecret.getAttribute("value");
}

public void clickManual() {
manualLink.click();
}

public void clickBarcode() {
barcodeLink.click();
}

public String getInputCodeError() {
try {
return totpInputCodeError.getText();
} catch (NoSuchElementException e) {
return null;
}
}

public String getInputLabelError() {
try {
return totpInputLabelError.getText();
} catch (NoSuchElementException e) {
return null;
}
}

public String getAlertError() {
try {
return loginAlertErrorMessage.getText();
} catch (NoSuchElementException e) {
return null;
}
}

public boolean isCancelDisplayed() {
try {
return cancelAIAButton.isDisplayed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public void fillPassword(String password) {
passwordInput.sendKeys(password);
}

public void login(String username, String password) {
fillLogin(username, password);
submit();
}

public void login(String password) {
fillPassword(password);
submit();
}

public void submit() {
submitButton.click();
}
Expand Down Expand Up @@ -145,6 +155,30 @@ public boolean isRememberMeCheckboxPresent() {
}
}

public boolean isRegisterLinkPresent() {
return !driver.driver().findElements(By.linkText("Register")).isEmpty();
}

public boolean isUsernameInputEnabled() {
return isUsernameInputPresent() && usernameInput.isEnabled();
}

public String getInputError() {
String usernameError = getUsernameInputError();
if (usernameError != null && !usernameError.isBlank()) {
return usernameError;
}
return getPasswordInputError().orElse(null);
}

public String getError() {
return getErrorMessage().orElse(null);
}

public String getInstruction() {
return getInfoMessage().orElse(null);
}

public boolean isSwitchOrganizationPresent() {
return !driver.driver().findElements(By.id("switch-organization")).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public void changePassword(String username) {
submitButton.click();
}

public void changePassword() {
submitButton.click();
}

public String getUsername() {
return usernameInput.getAttribute("value");
}

public void backToLogin() {
backToLogin.click();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public void changePassword(String newPassword, String passwordConfirm) {
submitButton.click();
}

public void updatePasswords(String newPassword, String passwordConfirm) {
changePassword(newPassword, passwordConfirm);
}

public void cancel() {
cancelAIAButton.click();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ public void submit() {
}
}

page.submitButton.submit();
try {
page.submitButton.click();
} catch (RuntimeException e) {
page.submitButton.submit();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.keycloak.testframework.ui.webdriver.ManagedWebDriver;

import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
Expand Down Expand Up @@ -44,6 +45,11 @@ public void submit() {
submitButton.click();
}

public void login(String password) {
fillPassword(password);
submit();
}

public String getPassword() {
return passwordInput.getAttribute("value");
}
Expand All @@ -68,6 +74,14 @@ public void clickTryAnotherWayLink() {
tryAnotherWayLink.click();
}

public void assertTryAnotherWayLinkAvailability(boolean expectedAvailability) {
try {
Assertions.assertEquals(expectedAvailability, tryAnotherWayLink.isDisplayed());
} catch (NoSuchElementException e) {
Assertions.assertFalse(expectedAvailability);
}
}

@Override
public String getExpectedPageId() {
return "login-login-password";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SelectAuthenticatorPage extends AbstractLoginPage {

// Corresponds to the PasswordForm
public static final String PASSWORD = "Password";
public static final String AUTHENTICATOR_APPLICATION = "Authenticator Application";

// Corresponds to the WebAuthn authenticators
public static final String SECURITY_KEY = "Passkey";
Expand Down Expand Up @@ -46,6 +47,10 @@ public String getLoginMethodHelpText(String loginMethodName) {
return getLoginMethodRowByName(loginMethodName).findElement(By.className("select-auth-box-desc")).getText();
}

public List<String> getAvailableLoginMethods() {
return getLoginMethodsRows().stream().map(this::getLoginMethodNameFromRow).toList();
}


private List<WebElement> getLoginMethodsRows() {
return driver.driver().findElements(By.className("select-auth-box-parent"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public class ManagedWebDriver extends ManagedTestResource {

private static final ThreadLocal<WebDriver> CURRENT_DRIVER = new ThreadLocal<>();

private WebDriver driver;

private AssertionUtils assertionUtils = new AssertionUtils(this);
Expand All @@ -24,13 +26,18 @@ public class ManagedWebDriver extends ManagedTestResource {

public ManagedWebDriver(WebDriver driver) {
this.driver = driver;
CURRENT_DRIVER.set(driver);
this.tabUtils = new BrowserTabUtils(this);
}

public WebDriver driver() {
return driver;
}

public static WebDriver currentDriver() {
return CURRENT_DRIVER.get();
}

public BrowserType getBrowserType() {
if (driver instanceof HtmlUnitDriver) {
return BrowserType.HTML_UNIT;
Expand All @@ -46,10 +53,26 @@ public String getCurrentUrl() {
return driver.getCurrentUrl();
}

public String getTitle() {
return driver.getTitle();
}

public String getPageSource() {
return driver.getPageSource();
}

public WebDriver.Options manage() {
return driver.manage();
}

public WebElement findElement(By by) {
return driver.findElement(by);
}

public java.util.List<WebElement> findElements(By by) {
return driver.findElements(by);
}

public void open(String url) {
driver.navigate().to(url);
}
Expand Down
Loading