Merge pull request #2938 from mhajas/addJSConsoleTests-master

KEYCLOAK-3121 Add JS adapter tests
This commit is contained in:
Stian Thorgersen 2016-06-17 14:01:21 +02:00 committed by GitHub
commit 8ae08f0432
12 changed files with 478 additions and 100 deletions

View file

@ -21,6 +21,20 @@
"clientRoles": { "clientRoles": {
"account": ["view-profile", "manage-account"] "account": ["view-profile", "manage-account"]
} }
},{
"username" : "unauthorized",
"enabled": true,
"email" : "sample-user2@example",
"firstName": "Sample",
"lastName": "User",
"credentials" : [
{ "type" : "password",
"value" : "password" }
],
"realmRoles": [],
"clientRoles": {
"account": ["view-profile", "manage-account"]
}
} }
], ],
"roles" : { "roles" : {
@ -54,6 +68,12 @@
"http://localhost:8280", "http://localhost:8280",
"https://localhost:8643" "https://localhost:8643"
] ]
},{
"clientId": "js-database",
"enabled": true,
"adminUrl": "/database",
"baseUrl": "/database",
"bearerOnly": true
} }
], ],
"clientScopeMappings": { "clientScopeMappings": {

View file

@ -39,6 +39,7 @@
<button onclick="output(keycloak.createLoginUrl())">Show Login URL</button> <button onclick="output(keycloak.createLoginUrl())">Show Login URL</button>
<button onclick="output(keycloak.createLogoutUrl())">Show Logout URL</button> <button onclick="output(keycloak.createLogoutUrl())">Show Logout URL</button>
<button onclick="output(keycloak.createRegisterUrl())">Show Register URL</button> <button onclick="output(keycloak.createRegisterUrl())">Show Register URL</button>
<button onclick="createBearerRequest()">Create Bearer Request</button>
<select id="flowSelect"> <select id="flowSelect">
<option value="standard">standard</option> <option value="standard">standard</option>
<option value="implicit">implicit</option> <option value="implicit">implicit</option>
@ -48,6 +49,10 @@
<option value="fragment">fragment</option> <option value="fragment">fragment</option>
<option value="query">query</option> <option value="query">query</option>
</select> </select>
<select id="onLoad">
<option value="check-sso">check-sso</option>
<option value="login-required">login-required</option>
</select>
</div> </div>
<h2>Result</h2> <h2>Result</h2>
@ -115,6 +120,34 @@
document.getElementById('events').innerHTML = new Date().toLocaleString() + "\t" + event + "\n" + e; document.getElementById('events').innerHTML = new Date().toLocaleString() + "\t" + event + "\n" + e;
} }
function createBearerRequest() {
var url = 'http://localhost:8280/js-database/customers';
if (window.location.href.indexOf("8643") > -1) {
url = url.replace("8280","8643");
url = url.replace("http","https");
}
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Authorization', 'Bearer ' + keycloak.token);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
output(req.responseText);
} else if (req.status == 403) {
output('Forbidden');
} else if (req.status == 401) {
output('Unauthorized');
}
}
};
req.send();
}
var keycloak; var keycloak;
function keycloakInit() { function keycloakInit() {
@ -144,7 +177,7 @@
event('Access token expired.'); event('Access token expired.');
}; };
var initOptions = {flow: document.getElementById("flowSelect").value, responseMode: document.getElementById("responseModeSelect").value} var initOptions = {onLoad: document.getElementById("onLoad").value, flow: document.getElementById("flowSelect").value, responseMode: document.getElementById("responseModeSelect").value};
keycloak.init(initOptions).success(function (authenticated) { keycloak.init(initOptions).success(function (authenticated) {
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')'); output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
}).error(function () { }).error(function () {

View file

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>integration-arquillian-test-apps</artifactId>
<groupId>org.keycloak.testsuite</groupId>
<version>2.0.0.CR1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>integration-arquillian-test-apps-js-database</artifactId>
<name>JAX-RS Database Service Using OAuth Bearer Tokens</name>
<packaging>war</packaging>
<repositories>
<repository>
<id>jboss</id>
<name>jboss repo</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-adapter-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>js-database</finalName>
<plugins>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,61 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.example.oauth;
import org.jboss.resteasy.annotations.cache.NoCache;
import org.jboss.resteasy.spi.HttpRequest;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.AccessToken;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.util.ArrayList;
import java.util.List;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Path("customers")
public class CustomerService {
@Context
private HttpRequest httpRequest;
@GET
@Produces("application/json")
@NoCache
public List<String> getCustomers() {
// Just to show how to user info from access token in REST endpoint
KeycloakSecurityContext securityContext = (KeycloakSecurityContext) httpRequest.getAttribute(KeycloakSecurityContext.class.getName());
AccessToken accessToken = securityContext.getToken();
System.out.println(String.format("User '%s' with email '%s' made request to CustomerService REST endpoint", accessToken.getPreferredUsername(), accessToken.getEmail()));
ArrayList<String> rtn = new ArrayList<String>();
rtn.add("Bill Burke");
rtn.add("Stian Thorgersen");
rtn.add("Stan Silvert");
rtn.add("Gabriel Cardoso");
rtn.add("Viliam Rockai");
rtn.add("Marek Posolda");
rtn.add("Boleslaw Dawidowicz");
return rtn;
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.example.oauth;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@ApplicationPath("/")
public class DataApplication extends Application
{
}

View file

@ -0,0 +1,8 @@
{
"realm" : "example",
"resource" : "js-database",
"realm-public-key" : "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB",
"auth-server-url": "http://localhost:8180/auth",
"bearer-only" : true,
"ssl-required" : "external"
}

View file

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2016 Red Hat, Inc. and/or its affiliates
~ and other contributors as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<module-name>js-database</module-name>
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<!-- <user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint> -->
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>KEYCLOAK</auth-method>
<realm-name>example</realm-name>
</login-config>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>

View file

@ -17,5 +17,6 @@
<modules> <modules>
<module>js-console</module> <module>js-console</module>
<module>test-apps-dist</module> <module>test-apps-dist</module>
<module>js-database</module>
</modules> </modules>
</project> </project>

View file

@ -30,7 +30,7 @@ import java.net.URL;
* *
* @author tkyjovsk * @author tkyjovsk
*/ */
public class JSConsoleExample extends AbstractPageWithInjectedUrl { public class JSConsoleTestApp extends AbstractPageWithInjectedUrl {
public static final String DEPLOYMENT_NAME = "js-console-example"; public static final String DEPLOYMENT_NAME = "js-console-example";
public static final String CLIENT_ID = "integration-arquillian-test-apps-js-console"; public static final String CLIENT_ID = "integration-arquillian-test-apps-js-console";
@ -69,11 +69,16 @@ public class JSConsoleExample extends AbstractPageWithInjectedUrl {
private WebElement showExpiresButton; private WebElement showExpiresButton;
@FindBy(xpath = "//button[text() = 'Show Details']") @FindBy(xpath = "//button[text() = 'Show Details']")
private WebElement showDetailsButton; private WebElement showDetailsButton;
@FindBy(xpath = "//button[text() = 'Create Bearer Request']")
private WebElement createBearerRequest;
@FindBy(id = "flowSelect") @FindBy(id = "flowSelect")
private Select flowSelect; private Select flowSelect;
@FindBy(id = "responseModeSelect") @FindBy(id = "responseModeSelect")
private Select responseModeSelect; private Select responseModeSelect;
@FindBy(id = "onLoad")
private Select onLoad;
@FindBy(id = "output") @FindBy(id = "output")
private WebElement outputArea; private WebElement outputArea;
@ -105,10 +110,18 @@ public class JSConsoleExample extends AbstractPageWithInjectedUrl {
flowSelect.selectByValue(value); flowSelect.selectByValue(value);
} }
public void setOnLoad(String value) {
onLoad.selectByValue(value);
}
public void init() { public void init() {
initButton.click(); initButton.click();
} }
public void createBearerRequest() {
createBearerRequest.click();
}
public void setResponseMode(String value) { public void setResponseMode(String value) {
responseModeSelect.selectByValue(value); responseModeSelect.selectByValue(value);
} }

View file

@ -0,0 +1,46 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.testsuite.adapter.page;
import org.jboss.arquillian.container.test.api.OperateOnDeployment;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.keycloak.testsuite.page.AbstractPageWithInjectedUrl;
import java.net.URL;
/**
*
* @author tkyjovsk
*/
public class JSDatabaseTestApp extends AbstractPageWithInjectedUrl {
public static final String DEPLOYMENT_NAME = "js-database-example";
public static final String CLIENT_ID = "integration-arquillian-test-apps-js-database";
@ArquillianResource
@OperateOnDeployment(DEPLOYMENT_NAME)
private URL url;
@Override
public URL getInjectedUrl() {
//EAP6 URL fix
URL fixedUrl = createInjectedURL("js-database");
return fixedUrl != null ? fixedUrl : url;
}
}

View file

@ -25,7 +25,8 @@ import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.adapter.AbstractExampleAdapterTest; import org.keycloak.testsuite.adapter.AbstractExampleAdapterTest;
import org.keycloak.testsuite.adapter.page.JSConsoleExample; import org.keycloak.testsuite.adapter.page.JSConsoleTestApp;
import org.keycloak.testsuite.adapter.page.JSDatabaseTestApp;
import org.keycloak.testsuite.admin.ApiUtil; import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.auth.page.account.Applications; import org.keycloak.testsuite.auth.page.account.Applications;
import org.keycloak.testsuite.auth.page.login.OAuthGrant; import org.keycloak.testsuite.auth.page.login.OAuthGrant;
@ -51,7 +52,7 @@ import static org.keycloak.testsuite.util.WaitUtils.waitUntilElement;
public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampleAdapterTest { public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampleAdapterTest {
@Page @Page
private JSConsoleExample jsConsoleExamplePage; private JSConsoleTestApp jsConsoleTestAppPage;
@Page @Page
private Config configPage; private Config configPage;
@ -67,9 +68,14 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
public static int TOKEN_LIFESPAN_LEEWAY = 3; // seconds public static int TOKEN_LIFESPAN_LEEWAY = 3; // seconds
@Deployment(name = JSConsoleExample.DEPLOYMENT_NAME) @Deployment(name = JSConsoleTestApp.DEPLOYMENT_NAME)
private static WebArchive jsConsoleExample() throws IOException { private static WebArchive jsConsoleTestApp() throws IOException {
return exampleDeployment(JSConsoleExample.CLIENT_ID); return exampleDeployment(JSConsoleTestApp.CLIENT_ID);
}
@Deployment(name = JSDatabaseTestApp.DEPLOYMENT_NAME)
private static WebArchive jsDbApp() throws IOException {
return exampleDeployment(JSDatabaseTestApp.CLIENT_ID);
} }
@Override @Override
@ -77,7 +83,7 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
RealmRepresentation jsConsoleRealm = loadRealm(new File(TEST_APPS_HOME_DIR + "/js-console/example-realm.json")); RealmRepresentation jsConsoleRealm = loadRealm(new File(TEST_APPS_HOME_DIR + "/js-console/example-realm.json"));
fixClientUrisUsingDeploymentUrl(jsConsoleRealm, fixClientUrisUsingDeploymentUrl(jsConsoleRealm,
JSConsoleExample.CLIENT_ID, jsConsoleExamplePage.buildUri().toASCIIString()); JSConsoleTestApp.CLIENT_ID, jsConsoleTestAppPage.buildUri().toASCIIString());
jsConsoleRealm.setAccessTokenLifespan(30 + TOKEN_LIFESPAN_LEEWAY); // seconds jsConsoleRealm.setAccessTokenLifespan(30 + TOKEN_LIFESPAN_LEEWAY); // seconds
@ -92,92 +98,93 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
@Test @Test
public void testJSConsoleAuth() { public void testJSConsoleAuth() {
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
waitUntilElement(jsConsoleExamplePage.getInitButtonElement()).is().present(); waitUntilElement(jsConsoleTestAppPage.getInitButtonElement()).is().present();
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
testRealmLoginPage.form().login("user", "invalid-password"); testRealmLoginPage.form().login("user", "invalid-password");
assertCurrentUrlDoesntStartWith(jsConsoleExamplePage); assertCurrentUrlDoesntStartWith(jsConsoleTestAppPage);
testRealmLoginPage.form().login("invalid-user", "password"); testRealmLoginPage.form().login("invalid-user", "password");
assertCurrentUrlDoesntStartWith(jsConsoleExamplePage); assertCurrentUrlDoesntStartWith(jsConsoleTestAppPage);
testRealmLoginPage.form().login("user", "password"); testRealmLoginPage.form().login("user", "password");
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Init Success (Authenticated)"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Init Success (Authenticated)");
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Auth Success"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Auth Success");
jsConsoleExamplePage.logOut(); jsConsoleTestAppPage.logOut();
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); waitUntilElement(jsConsoleTestAppPage.getInitButtonElement()).is().present();
jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Init Success (Not Authenticated)"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Init Success (Not Authenticated)");
} }
@Test @Test
public void testRefreshToken() { public void testRefreshToken() {
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.refreshToken(); jsConsoleTestAppPage.refreshToken();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Failed to refresh token"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Failed to refresh token");
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
testRealmLoginPage.form().login("user", "password"); testRealmLoginPage.form().login("user", "password");
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Auth Success"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Auth Success");
jsConsoleExamplePage.refreshToken(); jsConsoleTestAppPage.refreshToken();
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Auth Refresh Success"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Auth Refresh Success");
} }
@Test @Test
public void testRefreshTokenIfUnder30s() { public void testRefreshTokenIfUnder30s() {
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.refreshToken(); jsConsoleTestAppPage.refreshToken();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Failed to refresh token"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Failed to refresh token");
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
testRealmLoginPage.form().login("user", "password"); testRealmLoginPage.form().login("user", "password");
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Auth Success"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Auth Success");
jsConsoleExamplePage.refreshTokenIfUnder30s(); jsConsoleTestAppPage.refreshTokenIfUnder30s();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Token not refreshed, valid for"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Token not refreshed, valid for");
pause((TOKEN_LIFESPAN_LEEWAY + 2) * 1000); pause((TOKEN_LIFESPAN_LEEWAY + 2) * 1000);
jsConsoleExamplePage.refreshTokenIfUnder30s(); jsConsoleTestAppPage.refreshTokenIfUnder30s();
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Auth Refresh Success"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Auth Refresh Success");
} }
@Test @Test
public void testGetProfile() { public void testGetProfile() {
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.getProfile(); jsConsoleTestAppPage.getProfile();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Failed to load profile"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Failed to load profile");
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
testRealmLoginPage.form().login("user", "password"); testRealmLoginPage.form().login("user", "password");
assertCurrentUrlStartsWith(jsConsoleExamplePage); assertCurrentUrlStartsWith(jsConsoleTestAppPage);
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Auth Success"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Auth Success");
jsConsoleExamplePage.getProfile(); jsConsoleTestAppPage.getProfile();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("\"username\": \"user\""); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("\"username\": \"user\"");
} }
@Test @Test
@ -188,7 +195,7 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
loginEventsPage.setConsoleRealm(EXAMPLE); loginEventsPage.setConsoleRealm(EXAMPLE);
applicationsPage.setAuthRealm(EXAMPLE); applicationsPage.setAuthRealm(EXAMPLE);
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
driver.manage().deleteAllCookies(); driver.manage().deleteAllCookies();
ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), "js-console"); ClientResource clientResource = ApiUtil.findClientResourceByClientId(testRealmResource(), "js-console");
@ -201,26 +208,27 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
realm.setEnabledEventTypes(Arrays.asList("REVOKE_GRANT", "LOGIN")); realm.setEnabledEventTypes(Arrays.asList("REVOKE_GRANT", "LOGIN"));
testRealmResource().update(realm); testRealmResource().update(realm);
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
testRealmLoginPage.form().login("user", "password"); testRealmLoginPage.form().login("user", "password");
assertTrue(oAuthGrantPage.isCurrent()); assertTrue(oAuthGrantPage.isCurrent());
oAuthGrantPage.accept(); oAuthGrantPage.accept();
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Init Success (Authenticated)"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Init Success (Authenticated)");
applicationsPage.navigateTo(); applicationsPage.navigateTo();
applicationsPage.revokeGrantForApplication("js-console"); applicationsPage.revokeGrantForApplication("js-console");
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
jsConsoleExamplePage.init(); jsConsoleTestAppPage.setOnLoad("login-required");
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.init();
waitUntilElement(By.tagName("body")).is().visible();
assertTrue(oAuthGrantPage.isCurrent()); assertTrue(oAuthGrantPage.isCurrent());
loginEventsPage.navigateTo(); loginEventsPage.navigateTo();
@ -255,34 +263,34 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
@Test @Test
public void implicitFlowTest() { public void implicitFlowTest() {
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
jsConsoleExamplePage.setFlow("implicit"); jsConsoleTestAppPage.setFlow("implicit");
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
assertTrue(driver.getPageSource().contains("Implicit flow is disabled for the client")); assertTrue(driver.getPageSource().contains("Implicit flow is disabled for the client"));
setImplicitFlowFroClient(); setImplicitFlowFroClient();
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
assertTrue(driver.getPageSource().contains("Standard flow is disabled for the client")); assertTrue(driver.getPageSource().contains("Standard flow is disabled for the client"));
logInAndInit("implicit"); logInAndInit("implicit");
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Init Success (Authenticated)"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Init Success (Authenticated)");
} }
@Test @Test
public void implicitFlowQueryTest() { public void implicitFlowQueryTest() {
setImplicitFlowFroClient(); setImplicitFlowFroClient();
jsConsoleExamplePage.navigateTo(); jsConsoleTestAppPage.navigateTo();
jsConsoleExamplePage.setFlow("implicit"); jsConsoleTestAppPage.setFlow("implicit");
jsConsoleExamplePage.setResponseMode("query"); jsConsoleTestAppPage.setResponseMode("query");
jsConsoleExamplePage.init(); jsConsoleTestAppPage.init();
jsConsoleExamplePage.logIn(); jsConsoleTestAppPage.logIn();
assertTrue(driver.getPageSource().contains("Invalid parameter: response_mode")); assertTrue(driver.getPageSource().contains("Invalid parameter: response_mode"));
} }
@ -292,9 +300,9 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
logInAndInit("implicit"); logInAndInit("implicit");
jsConsoleExamplePage.refreshToken(); jsConsoleTestAppPage.refreshToken();
waitUntilElement(jsConsoleExamplePage.getOutputElement()).text().contains("Failed to refresh token"); waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Failed to refresh token");
} }
@Test @Test
@ -309,7 +317,38 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
pause(6000); pause(6000);
waitUntilElement(jsConsoleExamplePage.getEventsElement()).text().contains("Access token expired"); waitUntilElement(jsConsoleTestAppPage.getEventsElement()).text().contains("Access token expired");
}
@Test
public void testBearerRequest() {
jsConsoleTestAppPage.navigateTo();
jsConsoleTestAppPage.init();
jsConsoleTestAppPage.createBearerRequest();
waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Unauthorized");
logInAndInit("standard", "unauthorized");
jsConsoleTestAppPage.createBearerRequest();
waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Forbidden");
jsConsoleTestAppPage.logOut();
logInAndInit("standard");
jsConsoleTestAppPage.createBearerRequest();
waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("[\"Bill Burke\",\"Stian Thorgersen\",\"Stan Silvert\",\"Gabriel Cardoso\",\"Viliam Rockai\",\"Marek Posolda\",\"Boleslaw Dawidowicz\"]");
}
@Test
public void loginRequiredAction() {
jsConsoleTestAppPage.navigateTo();
jsConsoleTestAppPage.setOnLoad("login-required");
jsConsoleTestAppPage.init();
assertCurrentUrlStartsWith(testRealmLoginPage);
testRealmLoginPage.form().login("user", "password");
waitUntilElement(jsConsoleTestAppPage.getInitButtonElement()).is().present();
jsConsoleTestAppPage.init();
waitUntilElement(jsConsoleTestAppPage.getOutputElement()).text().contains("Init Success (Authenticated)");
} }
private void setImplicitFlowFroClient() { private void setImplicitFlowFroClient() {
@ -320,14 +359,18 @@ public abstract class AbstractJSConsoleExampleAdapterTest extends AbstractExampl
clientResource.update(client); clientResource.update(client);
} }
private void logInAndInit(String flow, String user) {
jsConsoleTestAppPage.navigateTo();
jsConsoleTestAppPage.setFlow(flow);
jsConsoleTestAppPage.init();
jsConsoleTestAppPage.logIn();
testRealmLoginPage.form().login(user, "password");
jsConsoleTestAppPage.setFlow(flow);
jsConsoleTestAppPage.init();
}
private void logInAndInit(String flow) { private void logInAndInit(String flow) {
jsConsoleExamplePage.navigateTo(); logInAndInit(flow, "user");
jsConsoleExamplePage.setFlow(flow);
jsConsoleExamplePage.init();
jsConsoleExamplePage.logIn();
testRealmLoginPage.form().login("user", "password");
jsConsoleExamplePage.setFlow(flow);
jsConsoleExamplePage.init();
} }
} }

View file

@ -214,22 +214,22 @@
<artifactItems> <artifactItems>
<artifactItem> <artifactItem>
<groupId>org.keycloak.example.demo</groupId> <groupId>org.keycloak.example.demo</groupId>
<artifactId>product-portal-example</artifactId> <artifactId>product-portal-example</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<type>war</type> <type>war</type>
</artifactItem> </artifactItem>
<artifactItem> <artifactItem>
<groupId>org.keycloak.example.demo</groupId> <groupId>org.keycloak.example.demo</groupId>
<artifactId>customer-portal-example</artifactId> <artifactId>customer-portal-example</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<type>war</type> <type>war</type>
</artifactItem> </artifactItem>
<artifactItem> <artifactItem>
<groupId>org.keycloak.example.demo</groupId> <groupId>org.keycloak.example.demo</groupId>
<artifactId>database-service</artifactId> <artifactId>database-service</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<type>war</type> <type>war</type>
</artifactItem> </artifactItem>
<artifactItem> <artifactItem>
<groupId>org.keycloak.testsuite</groupId> <groupId>org.keycloak.testsuite</groupId>
<artifactId>integration-arquillian-test-apps-js-console</artifactId> <artifactId>integration-arquillian-test-apps-js-console</artifactId>
@ -237,14 +237,20 @@
<type>war</type> <type>war</type>
</artifactItem> </artifactItem>
<artifactItem> <artifactItem>
<groupId>org.keycloak</groupId> <groupId>org.keycloak.testsuite</groupId>
<artifactId>examples-multitenant</artifactId> <artifactId>integration-arquillian-test-apps-js-database</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<type>war</type> <type>war</type>
</artifactItem> </artifactItem>
<artifactItem> <artifactItem>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>examples-basicauth</artifactId> <artifactId>examples-multitenant</artifactId>
<version>${project.version}</version>
<type>war</type>
</artifactItem>
<artifactItem>
<groupId>org.keycloak</groupId>
<artifactId>examples-basicauth</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<type>war</type> <type>war</type>
</artifactItem> </artifactItem>
@ -293,7 +299,7 @@
<artifactItems> <artifactItems>
<artifactItem> <artifactItem>
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-examples-dist</artifactId> <artifactId>keycloak-examples-dist</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<type>zip</type> <type>zip</type>
<includes>**/*realm.json,**/testsaml.json</includes> <includes>**/*realm.json,**/testsaml.json</includes>