Skip to content

Commit

Permalink
New module for integration tests using Playwright framework.
Browse files Browse the repository at this point in the history
Signed-off-by: Miquel Simon <[email protected]>
  • Loading branch information
miquelsi committed Mar 11, 2024
1 parent 1e12b15 commit 1a09434
Show file tree
Hide file tree
Showing 22 changed files with 5,247 additions and 22 deletions.
22 changes: 22 additions & 0 deletions authz/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>2.1.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.keycloak.authorization.client;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import java.io.ByteArrayInputStream;

@ExtendWith(SystemStubsExtension.class)
public class AuthzClientTest {

@SystemStub
public final EnvironmentVariables envVars = new EnvironmentVariables();

@Test
public void testCreateWithEnvVars() {
envVars.set("KEYCLOAK_REALM", "test");
envVars.set("KEYCLOAK_AUTH_SERVER", "http://test");

RuntimeException runtimeException = Assertions.assertThrows(RuntimeException.class, () -> {

AuthzClient.create(new ByteArrayInputStream(("{\n"
+ " \"realm\": \"${env.KEYCLOAK_REALM}\",\n"
+ " \"auth-server-url\": \"${env.KEYCLOAK_AUTH_SERVER}\",\n"
+ " \"ssl-required\": \"external\",\n"
+ " \"enable-cors\": true,\n"
+ " \"resource\": \"my-server\",\n"
+ " \"credentials\": {\n"
+ " \"secret\": \"${env.KEYCLOAK_SECRET}\"\n"
+ " },\n"
+ " \"confidential-port\": 0,\n"
+ " \"policy-enforcer\": {\n"
+ " \"enforcement-mode\": \"ENFORCING\"\n"
+ " }\n"
+ "}").getBytes()));
});

MatcherAssert.assertThat(runtimeException.getMessage(), Matchers.containsString("Could not obtain configuration from server"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class AbstractPage {
@ArquillianResource
protected SuiteContext suiteContext;

@ArquillianResource
//@ArquillianResource
protected WebDriver driver;

@ArquillianResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jboss.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.model.TestTimedOutException;
Expand Down Expand Up @@ -61,6 +62,7 @@
import org.keycloak.testsuite.util.OAuthClient;
import org.keycloak.testsuite.util.TestCleanup;
import org.keycloak.testsuite.util.TestEventsLogger;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;

import jakarta.ws.rs.NotFoundException;
Expand Down Expand Up @@ -92,6 +94,9 @@
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import org.keycloak.models.UserModel;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import static org.keycloak.testsuite.admin.Users.setPasswordFor;
import static org.keycloak.testsuite.auth.page.AuthRealm.MASTER;
import static org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_HOST;
Expand Down Expand Up @@ -131,7 +136,7 @@ public abstract class AbstractKeycloakTest {
protected List<RealmRepresentation> testRealmReps;

@Drone
protected WebDriver driver;
protected static WebDriver driver;

@Page
protected AuthServerContextRoot authServerContextRootPage;
Expand All @@ -157,6 +162,20 @@ public abstract class AbstractKeycloakTest {

private boolean resetTimeOffset;

/*@BeforeClass
public static void initDriver() {
ChromeOptions options = new ChromeOptions();
// Waits for webpage setup
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
// Chromium headless browser
options.addArguments("--headless=new");
// Turn on BiDi protocol
//options.setCapability("webSocketUrl", true);
options.addArguments("--ignore-certificate-errors");
driver = new ChromeDriver(options);
}*/

@Before
public void beforeAbstractKeycloakTest() throws Exception {
adminClient = testContext.getAdminClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import org.hamcrest.Matchers;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.After;
import org.junit.Before;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.*;
import org.keycloak.TokenVerifier;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.GroupResource;
Expand Down Expand Up @@ -97,6 +93,7 @@
import org.keycloak.userprofile.validator.UsernameProhibitedCharactersValidator;
import org.keycloak.util.JsonSerialization;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;

import jakarta.mail.internet.MimeMessage;
Expand All @@ -105,6 +102,9 @@
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -152,8 +152,7 @@ public class UserTest extends AbstractAdminTest {
@Rule
public GreenMailRule greenMail = new GreenMailRule();

@Drone
protected WebDriver driver;
protected static WebDriver driver;

@Page
protected LoginPasswordUpdatePage passwordUpdatePage;
Expand Down Expand Up @@ -192,9 +191,29 @@ public class UserTest extends AbstractAdminTest {
}
}

/*@BeforeClass
public static void beforeClass() {
ChromeOptions options = new ChromeOptions();
// Waits for webpage setup
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
// Chromium headless browser
options.addArguments("--headless=new");
// Turn on BiDi protocol
//options.setCapability("webSocketUrl", true);
options.addArguments("--ignore-certificate-errors");
driver = new ChromeDriver(options);
}*/

@Before
public void beforeUserTest() throws IOException {
createAppClientInRealm(REALM_NAME);

/*proceedPage.setDriver(driver);
infoPage.setDriver(driver);
loginPage.setDriver(driver);
errorPage.setDriver(driver);
oauth.setDriver(driver);
createAppClientInRealm(REALM_NAME);*/

VerifyProfileTest.setUserProfileConfiguration(realm, null);
UPConfig upConfig = realm.users().userProfile().getConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.*;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.authentication.authenticators.browser.OTPFormAuthenticator;
Expand Down Expand Up @@ -49,8 +47,11 @@
import org.keycloak.testsuite.util.RoleBuilder;
import org.keycloak.testsuite.util.URLUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -74,8 +75,8 @@ public class BrowserFlowTest extends AbstractTestRealmKeycloakTest {
@ArquillianResource
protected OAuthClient oauth;

@Drone
protected WebDriver driver;
//@Drone
//protected WebDriver driver;

@Page
protected LoginPage loginPage;
Expand All @@ -98,6 +99,17 @@ public class BrowserFlowTest extends AbstractTestRealmKeycloakTest {
@Rule
public AssertEvents events = new AssertEvents(this);

@Before
public void beforeBrowserFlowTest() {
loginPage.setDriver(driver);
loginUsernameOnlyPage.setDriver(driver);
passwordPage.setDriver(driver);
errorPage.setDriver(driver);
loginTotpPage.setDriver(driver);
oneTimeCodePage.setDriver(driver);
oauth.setDriver(driver);
}

@Override
public void configureTestRealm(RealmRepresentation testRealm) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class DirectGrantFlowTest extends AbstractTestRealmKeycloakTest {
@ArquillianResource
protected OAuthClient oauth;

@Drone
protected WebDriver driver;
//@Drone
//protected WebDriver driver;

@Rule
public AssertEvents events = new AssertEvents(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.authentication.AuthenticationFlow;
Expand Down Expand Up @@ -62,8 +63,8 @@ public class MultiFactorAuthenticationTest extends AbstractTestRealmKeycloakTest
@ArquillianResource
protected OAuthClient oauth;

@Drone
protected WebDriver driver;
//@Drone
//protected WebDriver driver;

@Page
protected LoginPage loginPage;
Expand All @@ -90,6 +91,17 @@ public class MultiFactorAuthenticationTest extends AbstractTestRealmKeycloakTest
public void configureTestRealm(RealmRepresentation testRealm) {
}

@Before
public void beforeMultiFactorAuthenticationTest() {
loginPage.setDriver(driver);
loginUsernameOnlyPage.setDriver(driver);
passwordPage.setDriver(driver);
errorPage.setDriver(driver);
loginTotpPage.setDriver(driver);
selectAuthenticatorPage.setDriver(driver);
oauth.setDriver(driver);
}

private RealmRepresentation loadTestRealm() {
RealmRepresentation res = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class);
res.setBrowserFlow("browser");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.authentication.authenticators.browser.PasswordFormFactory;
Expand Down Expand Up @@ -68,8 +69,8 @@ public class ReAuthenticationTest extends AbstractTestRealmKeycloakTest {
@ArquillianResource
protected OAuthClient oauth;

@Drone
protected WebDriver driver;
//@Drone
//protected WebDriver driver;

@Page
protected LoginPage loginPage;
Expand All @@ -92,6 +93,17 @@ public class ReAuthenticationTest extends AbstractTestRealmKeycloakTest {
@Page
protected AppPage appPage;

@Before
public void beforeReAuthenticationTest() {
loginPage.setDriver(driver);
loginUsernameOnlyPage.setDriver(driver);
passwordPage.setDriver(driver);
errorPage.setDriver(driver);
loginTotpPage.setDriver(driver);
appPage.setDriver(driver);
oauth.setDriver(driver);
}

@Override
public void configureTestRealm(RealmRepresentation testRealm) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void addTestRealms(List<RealmRepresentation> testRealms) {
public void beforeAbstractKeycloakTest() throws Exception {
Assume.assumeThat("Test skipped",
suiteContext.getAuthServerInfo().isJBossBased(),
Matchers.is(true));
Matchers.is(false));
DroneUtils.replaceDefaultWebDriver(this, phantomJS);
setDefaultPageUriParameters();
}
Expand Down
Loading

0 comments on commit 1a09434

Please sign in to comment.