KEYCLOAK-15158 Javascript adapter init() is throwing a promise error after upgrade to 11

This commit is contained in:
vmuzikar 2020-09-17 17:34:36 +02:00 committed by Bruno Oliveira da Silva
parent 0a0b7da53e
commit bca73fd04a
5 changed files with 128 additions and 11 deletions

View file

@ -362,8 +362,24 @@
}
}
function domReady() {
var promise = createPromise();
var checkReadyState = function () {
if (document.readyState === 'interactive' || document.readyState === 'complete') {
document.removeEventListener('readystatechange', checkReadyState);
promise.setSuccess();
}
}
document.addEventListener('readystatechange', checkReadyState);
checkReadyState(); // just in case the event was already fired and we missed it (in case the init is done later than at the load time, i.e. it's done from code)
return promise.promise;
}
configPromise.then(function () {
check3pCookiesSupported().then(processInit)
domReady().then(check3pCookiesSupported).then(processInit)
.catch(function() {
promise.setError();
});

View file

@ -41,6 +41,14 @@ public class TestJavascriptResource {
return resourceToString("/javascript/index.html");
}
@GET
@Path("/init-in-head.html")
@Produces(MediaType.TEXT_HTML)
public String getJavascriptTestingEnvironmentWithInitInHead() throws IOException {
session.getProvider(SecurityHeadersProvider.class).options().skipHeaders();
return resourceToString("/javascript/init-in-head.html");
}
@GET
@Path("/silent-check-sso.html")
@Produces(MediaType.TEXT_HTML)

View file

@ -0,0 +1,65 @@
<!--
~ 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.
-->
<html>
<head>
<script src="js/keycloak.js"></script>
<script>
var keycloak = new Keycloak({
url: '${js-adapter.auth-server-url}',
realm: 'test',
clientId: 'js-console'
});
keycloak.init({}).then(function(authenticated) {
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
}).catch(function() {
output('Init Error');
});
keycloak.onAuthSuccess = function () {event('Auth Success')};
keycloak.onAuthError = function () {event('Auth Error')};
keycloak.onAuthRefreshSuccess = function () {event('Auth Refresh Success')};
keycloak.onAuthRefreshError = function () {event('Auth Refresh Error')};
keycloak.onAuthLogout = function () {event('Auth Logout')};
keycloak.onTokenExpired = function () {event('Access token expired.')};
keycloak.onActionUpdate = function (status) {event('AIA status: ' + status)};
</script>
</head>
<body>
<h2>Result</h2>
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px;" id="output"></pre>
<h2>Events</h2>
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px;" id="events"></pre>
<script>
function output(data) {
if (typeof data === 'object') {
data = JSON.stringify(data, null, ' ');
}
document.getElementById('output').innerHTML = data;
}
function event(event) {
var e = document.getElementById('events').innerHTML;
document.getElementById('events').innerHTML = new Date().toLocaleString() + "\t" + event + "\n" + e;
}
</script>
</body>
</html>

View file

@ -322,4 +322,9 @@ public class JavascriptTestExecutor {
return this;
}
public JavascriptTestExecutor validateOutputField(JavascriptStateValidator validator) {
validator.validate(jsDriver, output, events);
return this;
}
}

View file

@ -18,6 +18,8 @@ import org.keycloak.testsuite.Assert;
import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.arquillian.SuiteContext;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
import org.keycloak.testsuite.auth.page.account.Applications;
import org.keycloak.testsuite.auth.page.login.OAuthGrant;
import org.keycloak.testsuite.auth.page.login.UpdatePassword;
@ -47,16 +49,11 @@ import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
import static org.junit.Assume.assumeFalse;
import static org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_HOST;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlDoesntStartWith;
import static org.keycloak.testsuite.util.URLAssert.assertCurrentUrlStartsWith;
import static org.keycloak.testsuite.util.WaitUtils.waitForPageToLoad;
import static org.keycloak.testsuite.util.WaitUtils.waitUntilElement;
import static org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_HOST;
/**
* @author mhajas
@ -65,6 +62,7 @@ import static org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_HOST;
public class JavascriptAdapterTest extends AbstractJavascriptTest {
private String testAppUrl;
private String testAppWithInitInHeadUrl;
protected JavascriptTestExecutor testExecutor;
private static int TIME_SKEW_TOLERANCE = 3;
@ -90,7 +88,9 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
@Before
public void setDefaultEnvironment() {
testAppUrl = authServerContextRootPage.toString().replace(AUTH_SERVER_HOST, JS_APP_HOST) + JAVASCRIPT_URL + "/index.html";
String testAppRootUrl = authServerContextRootPage.toString().replace(AUTH_SERVER_HOST, JS_APP_HOST) + JAVASCRIPT_URL;
testAppUrl = testAppRootUrl + "/index.html";
testAppWithInitInHeadUrl = testAppRootUrl + "/init-in-head.html";
jsDriverTestRealmLoginPage.setAuthRealm(REALM_NAME);
oAuthGrantPage.setAuthRealm(REALM_NAME);
@ -101,10 +101,8 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
events.poll();
jsDriver.manage().deleteAllCookies();
jsDriver.navigate().to(testAppUrl);
navigateToTestApp(testAppUrl);
waitUntilElement(outputArea).is().present();
assertCurrentUrlStartsWith(testAppUrl, jsDriver);
testExecutor = JavascriptTestExecutor.create(jsDriver, jsDriverTestRealmLoginPage);
jsDriver.manage().deleteAllCookies();
@ -121,6 +119,14 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
}
private void assertOnTestAppUrl(WebDriver jsDriver, Object output, WebElement events) {
assertOnTestAppUrl(jsDriver, output, events, testAppUrl);
}
private void assertOnTestAppWithInitInHeadUrl(WebDriver jsDriver, Object output, WebElement events) {
assertOnTestAppUrl(jsDriver, output, events, testAppWithInitInHeadUrl);
}
private void assertOnTestAppUrl(WebDriver jsDriver, Object output, WebElement events, String testAppUrl) {
waitForPageToLoad();
assertCurrentUrlStartsWith(testAppUrl, jsDriver);
}
@ -748,7 +754,24 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
});
}
@Test
// KEYCLOAK-15158
public void testInitInHead() {
navigateToTestApp(testAppWithInitInHeadUrl);
testExecutor.validateOutputField(this::assertInitNotAuth)
.login(this::assertOnLoginPage)
.loginForm(testUser, this::assertOnTestAppWithInitInHeadUrl)
.validateOutputField(this::assertInitAuth);
}
protected void assertAdapterIsLoggedIn(WebDriver driver1, Object output, WebElement events) {
assertTrue(testExecutor.isLoggedIn());
}
protected void navigateToTestApp(final String testAppUrl) {
jsDriver.navigate().to(testAppUrl);
waitUntilElement(outputArea).is().present();
assertCurrentUrlStartsWith(testAppUrl, jsDriver);
}
}