KEYCLOAK-8533 Add tests for native promises
This commit is contained in:
parent
1237986fd0
commit
26c8af5369
4 changed files with 111 additions and 19 deletions
|
@ -55,6 +55,10 @@ public class JSObjectBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean contains(String key, Object value) {
|
||||||
|
return arguments.containsKey(key) && arguments.get(key).equals(value);
|
||||||
|
}
|
||||||
|
|
||||||
public JSObjectBuilder add(String key, Object value) {
|
public JSObjectBuilder add(String key, Object value) {
|
||||||
arguments.put(key, value);
|
arguments.put(key, value);
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -130,13 +130,27 @@ public class JavascriptTestExecutor {
|
||||||
|
|
||||||
String arguments = argumentsBuilder.build();
|
String arguments = argumentsBuilder.build();
|
||||||
|
|
||||||
Object output = jsExecutor.executeAsyncScript(
|
String script;
|
||||||
"var callback = arguments[arguments.length - 1];" +
|
|
||||||
" window.keycloak.init(" + arguments + ").success(function (authenticated) {" +
|
// phantomjs do not support Native promises
|
||||||
" callback(\"Init Success (\" + (authenticated ? \"Authenticated\" : \"Not Authenticated\") + \")\");" +
|
if (argumentsBuilder.contains("promiseType", "native") && !"phantomjs".equals(System.getProperty("js.browser"))) {
|
||||||
" }).error(function () {" +
|
script = "var callback = arguments[arguments.length - 1];" +
|
||||||
" callback(\"Init Error\");" +
|
" window.keycloak.init(" + arguments + ").then(function (authenticated) {" +
|
||||||
" });");
|
" callback(\"Init Success (\" + (authenticated ? \"Authenticated\" : \"Not Authenticated\") + \")\");" +
|
||||||
|
" }, function () {" +
|
||||||
|
" callback(\"Init Error\");" +
|
||||||
|
" });";
|
||||||
|
} else {
|
||||||
|
script = "var callback = arguments[arguments.length - 1];" +
|
||||||
|
" window.keycloak.init(" + arguments + ").success(function (authenticated) {" +
|
||||||
|
" callback(\"Init Success (\" + (authenticated ? \"Authenticated\" : \"Not Authenticated\") + \")\");" +
|
||||||
|
" }).error(function () {" +
|
||||||
|
" callback(\"Init Error\");" +
|
||||||
|
" });";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Object output = jsExecutor.executeAsyncScript(script);
|
||||||
|
|
||||||
if (validator != null) {
|
if (validator != null) {
|
||||||
validator.validate(jsDriver, output, events);
|
validator.validate(jsDriver, output, events);
|
||||||
|
@ -159,8 +173,20 @@ public class JavascriptTestExecutor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public JavascriptTestExecutor refreshToken(int value, JavascriptStateValidator validator) {
|
public JavascriptTestExecutor refreshToken(int value, JavascriptStateValidator validator) {
|
||||||
Object output = jsExecutor.executeAsyncScript(
|
String script;
|
||||||
"var callback = arguments[arguments.length - 1];" +
|
if (useNativePromises()) {
|
||||||
|
script = "var callback = arguments[arguments.length - 1];" +
|
||||||
|
" window.keycloak.updateToken(" + Integer.toString(value) + ").then(function (refreshed) {" +
|
||||||
|
" if (refreshed) {" +
|
||||||
|
" callback(window.keycloak.tokenParsed);" +
|
||||||
|
" } else {" +
|
||||||
|
" callback('Token not refreshed, valid for ' + Math.round(window.keycloak.tokenParsed.exp + window.keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');" +
|
||||||
|
" }" +
|
||||||
|
" }, function () {" +
|
||||||
|
" callback('Failed to refresh token');" +
|
||||||
|
" });";
|
||||||
|
} else {
|
||||||
|
script = "var callback = arguments[arguments.length - 1];" +
|
||||||
" window.keycloak.updateToken(" + Integer.toString(value) + ").success(function (refreshed) {" +
|
" window.keycloak.updateToken(" + Integer.toString(value) + ").success(function (refreshed) {" +
|
||||||
" if (refreshed) {" +
|
" if (refreshed) {" +
|
||||||
" callback(window.keycloak.tokenParsed);" +
|
" callback(window.keycloak.tokenParsed);" +
|
||||||
|
@ -169,7 +195,10 @@ public class JavascriptTestExecutor {
|
||||||
" }" +
|
" }" +
|
||||||
" }).error(function () {" +
|
" }).error(function () {" +
|
||||||
" callback('Failed to refresh token');" +
|
" callback('Failed to refresh token');" +
|
||||||
" });");
|
" });";
|
||||||
|
}
|
||||||
|
|
||||||
|
Object output = jsExecutor.executeAsyncScript(script);
|
||||||
|
|
||||||
if(validator != null) {
|
if(validator != null) {
|
||||||
validator.validate(jsDriver, output, events);
|
validator.validate(jsDriver, output, events);
|
||||||
|
@ -178,6 +207,12 @@ public class JavascriptTestExecutor {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean useNativePromises() {
|
||||||
|
return (boolean) jsExecutor.executeScript("if (typeof window.keycloak !== 'undefined') {" +
|
||||||
|
"return window.keycloak.useNativePromise" +
|
||||||
|
"} else { return false}");
|
||||||
|
}
|
||||||
|
|
||||||
public JavascriptTestExecutor openAccountPage(JavascriptStateValidator validator) {
|
public JavascriptTestExecutor openAccountPage(JavascriptStateValidator validator) {
|
||||||
jsExecutor.executeScript("window.keycloak.accountManagement()");
|
jsExecutor.executeScript("window.keycloak.accountManagement()");
|
||||||
waitForPageToLoad();
|
waitForPageToLoad();
|
||||||
|
@ -198,13 +233,24 @@ public class JavascriptTestExecutor {
|
||||||
|
|
||||||
public JavascriptTestExecutor getProfile(JavascriptStateValidator validator) {
|
public JavascriptTestExecutor getProfile(JavascriptStateValidator validator) {
|
||||||
|
|
||||||
Object output = jsExecutor.executeAsyncScript(
|
String script;
|
||||||
"var callback = arguments[arguments.length - 1];" +
|
if (useNativePromises()) {
|
||||||
" window.keycloak.loadUserProfile().success(function (profile) {" +
|
script = "var callback = arguments[arguments.length - 1];" +
|
||||||
" callback(profile);" +
|
" window.keycloak.loadUserProfile().then(function (profile) {" +
|
||||||
" }).error(function () {" +
|
" callback(profile);" +
|
||||||
" callback('Failed to load profile');" +
|
" }, function () {" +
|
||||||
" });");
|
" callback('Failed to load profile');" +
|
||||||
|
" });";
|
||||||
|
} else {
|
||||||
|
script = "var callback = arguments[arguments.length - 1];" +
|
||||||
|
" window.keycloak.loadUserProfile().success(function (profile) {" +
|
||||||
|
" callback(profile);" +
|
||||||
|
" }).error(function () {" +
|
||||||
|
" callback('Failed to load profile');" +
|
||||||
|
" });";
|
||||||
|
}
|
||||||
|
|
||||||
|
Object output = jsExecutor.executeAsyncScript(script);
|
||||||
|
|
||||||
if(validator != null) {
|
if(validator != null) {
|
||||||
validator.validate(jsDriver, output, events);
|
validator.validate(jsDriver, output, events);
|
||||||
|
|
|
@ -58,7 +58,7 @@ import static org.keycloak.testsuite.util.WaitUtils.waitUntilElement;
|
||||||
public class JavascriptAdapterTest extends AbstractJavascriptTest {
|
public class JavascriptAdapterTest extends AbstractJavascriptTest {
|
||||||
|
|
||||||
private String testAppUrl;
|
private String testAppUrl;
|
||||||
private JavascriptTestExecutor testExecutor;
|
protected JavascriptTestExecutor testExecutor;
|
||||||
private static int TIME_SKEW_TOLERANCE = 3;
|
private static int TIME_SKEW_TOLERANCE = 3;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
|
@ -96,7 +96,7 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
|
||||||
setStandardFlowForClient();
|
setStandardFlowForClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSObjectBuilder defaultArguments() {
|
protected JSObjectBuilder defaultArguments() {
|
||||||
return JSObjectBuilder.create().defaultSettings();
|
return JSObjectBuilder.create().defaultSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package org.keycloak.testsuite.adapter.javascript;
|
||||||
|
|
||||||
|
import org.junit.Assume;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.keycloak.testsuite.util.javascript.JSObjectBuilder;
|
||||||
|
|
||||||
|
import static org.keycloak.testsuite.util.WaitUtils.waitUntilElement;
|
||||||
|
|
||||||
|
public class JavascriptAdapterWithNativePromisesTest extends JavascriptAdapterTest {
|
||||||
|
@Override
|
||||||
|
protected JSObjectBuilder defaultArguments() {
|
||||||
|
return super.defaultArguments().add("promiseType", "native");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void skipOnPhantomJS() {
|
||||||
|
Assume.assumeTrue("Native promises are not supported on PhantomJS", !"phantomjs".equals(System.getProperty("js.browser")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Override
|
||||||
|
public void reentrancyCallbackTest() {
|
||||||
|
testExecutor.logInAndInit(defaultArguments(), testUser, this::assertSuccessfullyLoggedIn)
|
||||||
|
.executeAsyncScript(
|
||||||
|
"var callback = arguments[arguments.length - 1];" +
|
||||||
|
"keycloak.updateToken(60).then(function () {" +
|
||||||
|
" event(\"First callback\");" +
|
||||||
|
" keycloak.updateToken(60).then(function () {" +
|
||||||
|
" event(\"Second callback\");" +
|
||||||
|
" callback(\"Success\");" +
|
||||||
|
" });" +
|
||||||
|
" }" +
|
||||||
|
");"
|
||||||
|
, (driver1, output, events) -> {
|
||||||
|
waitUntilElement(events).text().contains("First callback");
|
||||||
|
waitUntilElement(events).text().contains("Second callback");
|
||||||
|
waitUntilElement(events).text().not().contains("Auth Logout");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue