Search in sources :

Example 1 with XMLHttpRequest

use of org.keycloak.testsuite.util.javascript.XMLHttpRequest in project keycloak by keycloak.

the class JavascriptAdapterTest method testUpdateToken.

@Test
public void testUpdateToken() {
    XMLHttpRequest request = XMLHttpRequest.create().url(authServerContextRootPage + "/auth/admin/realms/" + REALM_NAME + "/roles").method("GET").addHeader("Accept", "application/json").addHeader("Authorization", "Bearer ' + keycloak.token + '");
    testExecutor.logInAndInit(defaultArguments(), testUser, this::assertInitAuth).addTimeSkew(-33);
    setTimeOffset(33);
    testExecutor.refreshToken(5, assertEventsContains("Auth Refresh Success"));
    setTimeOffset(67);
    testExecutor.addTimeSkew(-34).sendXMLHttpRequest(request, response -> assertThat(response, hasEntry(is("status"), anyOf(is(0L), is(401L))))).refreshToken(5, assertEventsContains("Auth Refresh Success")).sendXMLHttpRequest(request, assertResponseStatus(200));
}
Also used : XMLHttpRequest(org.keycloak.testsuite.util.javascript.XMLHttpRequest) Test(org.junit.Test)

Example 2 with XMLHttpRequest

use of org.keycloak.testsuite.util.javascript.XMLHttpRequest in project keycloak by keycloak.

the class JavascriptAdapterTest method testBearerRequest.

@Test
public void testBearerRequest() {
    XMLHttpRequest request = XMLHttpRequest.create().url(authServerContextRootPage + "/auth/admin/realms/" + REALM_NAME + "/roles").method("GET").addHeader("Accept", "application/json").addHeader("Authorization", "Bearer ' + keycloak.token + '");
    testExecutor.init(defaultArguments()).sendXMLHttpRequest(request, response -> assertThat(response, hasEntry(is("status"), anyOf(is(0L), is(401L))))).refresh();
    if (!"phantomjs".equals(System.getProperty("js.browser"))) {
        // I have no idea why, but this request doesn't work with phantomjs, it works in chrome
        testExecutor.logInAndInit(defaultArguments(), unauthorizedUser, this::assertInitAuth).sendXMLHttpRequest(request, output -> Assert.assertThat(output, hasEntry("status", 403L))).logout(this::assertOnTestAppUrl).refresh();
    }
    testExecutor.logInAndInit(defaultArguments(), testUser, this::assertInitAuth).sendXMLHttpRequest(request, assertResponseStatus(200));
}
Also used : JavascriptBrowser(org.keycloak.testsuite.util.JavascriptBrowser) URL(java.net.URL) JavascriptStateValidator(org.keycloak.testsuite.util.javascript.JavascriptStateValidator) AssertEvents(org.keycloak.testsuite.AssertEvents) WebElement(org.openqa.selenium.WebElement) Page(org.jboss.arquillian.graphene.page.Page) OAuthClient(org.keycloak.testsuite.util.OAuthClient) Assert.assertThat(org.junit.Assert.assertThat) Map(java.util.Map) ClientResource(org.keycloak.admin.client.resource.ClientResource) IsMapContaining.hasEntry(org.hamcrest.collection.IsMapContaining.hasEntry) UriUtils(org.keycloak.common.util.UriUtils) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DisableFeature(org.keycloak.testsuite.arquillian.annotation.DisableFeature) AUTH_SERVER_HOST(org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_HOST) UpdatePassword(org.keycloak.testsuite.auth.page.login.UpdatePassword) IDToken(org.keycloak.representations.IDToken) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) XMLHttpRequest(org.keycloak.testsuite.util.javascript.XMLHttpRequest) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) List(java.util.List) URLAssert.assertCurrentUrlStartsWith(org.keycloak.testsuite.util.URLAssert.assertCurrentUrlStartsWith) TimeoutException(org.openqa.selenium.TimeoutException) Details(org.keycloak.events.Details) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) OAuth2Constants(org.keycloak.OAuth2Constants) WaitUtils.waitUntilElement(org.keycloak.testsuite.util.WaitUtils.waitUntilElement) CoreMatchers.anyOf(org.hamcrest.CoreMatchers.anyOf) Profile(org.keycloak.common.Profile) Assert(org.keycloak.testsuite.Assert) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) WebDriver(org.openqa.selenium.WebDriver) WebDriverException(org.openqa.selenium.WebDriverException) JavascriptTestExecutor(org.keycloak.testsuite.util.javascript.JavascriptTestExecutor) OAuthGrant(org.keycloak.testsuite.auth.page.login.OAuthGrant) RealmBuilder(org.keycloak.testsuite.util.RealmBuilder) UserBuilder(org.keycloak.testsuite.util.UserBuilder) CoreMatchers.both(org.hamcrest.CoreMatchers.both) Matchers.lessThan(org.hamcrest.Matchers.lessThan) URLAssert.assertCurrentUrlDoesntStartWith(org.keycloak.testsuite.util.URLAssert.assertCurrentUrlDoesntStartWith) Assume(org.junit.Assume) Math.toIntExact(java.lang.Math.toIntExact) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) Retry(org.keycloak.common.util.Retry) Before(org.junit.Before) ClaimsRepresentation(org.keycloak.representations.ClaimsRepresentation) ApiUtil(org.keycloak.testsuite.admin.ApiUtil) SuiteContext(org.keycloak.testsuite.arquillian.SuiteContext) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) EventType(org.keycloak.events.EventType) IOException(java.io.IOException) WaitUtils.waitForPageToLoad(org.keycloak.testsuite.util.WaitUtils.waitForPageToLoad) JsonSerialization(org.keycloak.util.JsonSerialization) Rule(org.junit.Rule) JSObjectBuilder(org.keycloak.testsuite.util.javascript.JSObjectBuilder) Applications(org.keycloak.testsuite.auth.page.account.Applications) Assert.assertEquals(org.junit.Assert.assertEquals) XMLHttpRequest(org.keycloak.testsuite.util.javascript.XMLHttpRequest) Test(org.junit.Test)

Example 3 with XMLHttpRequest

use of org.keycloak.testsuite.util.javascript.XMLHttpRequest in project keycloak by keycloak.

the class JavascriptAdapterTest method testLocationHeaderInResponse.

@Test
public void testLocationHeaderInResponse() {
    XMLHttpRequest request = XMLHttpRequest.create().url(authServerContextRootPage + "/auth/admin/realms/" + REALM_NAME + "/users").method("POST").content("JSON.stringify(JSON.parse('{\"emailVerified\" : false, \"enabled\" : true, \"username\": \"mhajas\", \"firstName\" :\"First\", \"lastName\":\"Last\",\"email\":\"email@redhat.com\", \"attributes\": {}}'))").addHeader("Accept", "application/json").addHeader("Authorization", "Bearer ' + keycloak.token + '").addHeader("Content-Type", "application/json; charset=UTF-8");
    testExecutor.logInAndInit(defaultArguments(), testUser, this::assertInitAuth).sendXMLHttpRequest(request, response -> {
        List<UserRepresentation> users = adminClient.realm(REALM_NAME).users().search("mhajas", 0, 1);
        assertEquals("There should be created user mhajas", 1, users.size());
        assertThat(((String) response.get("responseHeaders")).toLowerCase(), containsString("location: " + authServerContextRootPage.toString() + "/auth/admin/realms/" + REALM_NAME + "/users/" + users.get(0).getId()));
    });
}
Also used : XMLHttpRequest(org.keycloak.testsuite.util.javascript.XMLHttpRequest) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 XMLHttpRequest (org.keycloak.testsuite.util.javascript.XMLHttpRequest)3 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)2 IOException (java.io.IOException)1 Math.toIntExact (java.lang.Math.toIntExact)1 URL (java.net.URL)1 List (java.util.List)1 Map (java.util.Map)1 CoreMatchers.anyOf (org.hamcrest.CoreMatchers.anyOf)1 CoreMatchers.both (org.hamcrest.CoreMatchers.both)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.lessThan (org.hamcrest.Matchers.lessThan)1 IsMapContaining.hasEntry (org.hamcrest.collection.IsMapContaining.hasEntry)1 Page (org.jboss.arquillian.graphene.page.Page)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assume (org.junit.Assume)1