From 4664bb01d8c55ff19265ec87b9f596586c7b9b61 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 9 May 2016 13:40:31 -0300 Subject: [PATCH] Migration of AuthorizationCodeTest --- .../rest/TestingResourceProvider.java | 19 ++++ .../client/resources/TestingResource.java | 6 +- .../oauth/AuthorizationCodeTest.java | 93 ++++++++----------- .../testsuite/util/ClientManager.java | 19 ++++ 4 files changed, 80 insertions(+), 57 deletions(-) rename testsuite/{integration => integration-arquillian/tests/base}/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java (63%) diff --git a/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestingResourceProvider.java b/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestingResourceProvider.java index a1c1c4d38f..61169ec32c 100644 --- a/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestingResourceProvider.java +++ b/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestingResourceProvider.java @@ -26,6 +26,7 @@ import org.keycloak.models.RealmModel; import org.keycloak.models.UserSessionModel; import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.representations.idm.EventRepresentation; +import org.keycloak.services.managers.ClientSessionCode; import org.keycloak.services.managers.RealmManager; import org.keycloak.services.resource.RealmResourceProvider; import org.keycloak.testsuite.events.EventsListenerProvider; @@ -165,4 +166,22 @@ public class TestingResourceProvider implements RealmResourceProvider { public void close() { } + /* + * Migration from KeycloakRule#verifyCode + */ + @GET + @Path("/verify-code") + @Produces(MediaType.APPLICATION_JSON) + public String verifyCode(@QueryParam("realm") String realmName, @QueryParam("code") String code) { + RealmModel realm = session.realms().getRealm(realmName); + try { + ClientSessionCode accessCode = ClientSessionCode.parse(code, session, realm); + if (accessCode == null) { + throw new AssertionError("Invalid code"); + } + return accessCode.getClientSession().getId(); + } catch (Throwable t) { + throw new AssertionError("Failed to parse code", t); + } + } } diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java index 6c950eed43..58d45d79f2 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java @@ -42,7 +42,7 @@ public interface TestingResource { @GET @Path("/time-offset") @Produces(MediaType.APPLICATION_JSON) - Map getTimeOffset() ; + Map getTimeOffset(); @PUT @Path("/time-offset") @@ -80,4 +80,8 @@ public interface TestingResource { @Produces(MediaType.APPLICATION_JSON) boolean isCached(@PathParam("cache") String cacheName, @PathParam("id") String id); + @GET + @Path("/verify-code") + @Produces(MediaType.APPLICATION_JSON) + String verifyCode(@QueryParam("realm") String realmName, @QueryParam("code") String code); } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java similarity index 63% rename from testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java rename to testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java index 9a71c6497d..3fc08b4640 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/AuthorizationCodeTest.java @@ -16,71 +16,67 @@ */ package org.keycloak.testsuite.oauth; +import org.jboss.arquillian.graphene.page.Page; import org.junit.Assert; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.keycloak.OAuth2Constants; import org.keycloak.events.Details; import org.keycloak.events.Errors; import org.keycloak.models.Constants; -import org.keycloak.models.RealmModel; -import org.keycloak.services.managers.ClientSessionCode; -import org.keycloak.services.managers.RealmManager; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.testsuite.AbstractKeycloakTest; import org.keycloak.testsuite.AssertEvents; -import org.keycloak.testsuite.OAuthClient; -import org.keycloak.testsuite.OAuthClient.AuthorizationCodeResponse; import org.keycloak.testsuite.pages.ErrorPage; -import org.keycloak.testsuite.pages.LoginPage; -import org.keycloak.testsuite.rule.KeycloakRule; -import org.keycloak.testsuite.rule.WebResource; -import org.keycloak.testsuite.rule.WebRule; +import org.keycloak.testsuite.util.ClientManager; +import org.keycloak.testsuite.util.OAuthClient; import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; import javax.ws.rs.core.UriBuilder; import java.io.IOException; +import java.util.List; import static org.junit.Assert.assertEquals; +import static org.keycloak.testsuite.admin.AbstractAdminTest.loadJson; /** * @author Stian Thorgersen */ -public class AuthorizationCodeTest { - - @ClassRule - public static KeycloakRule keycloakRule = new KeycloakRule(); +public class AuthorizationCodeTest extends AbstractKeycloakTest { @Rule - public WebRule webRule = new WebRule(this); + public AssertEvents events = new AssertEvents(this); - @WebResource - protected WebDriver driver; - - @WebResource - protected OAuthClient oauth; - - @WebResource - protected LoginPage loginPage; - - @WebResource + @Page protected ErrorPage errorPage; - @Rule - public AssertEvents events = new AssertEvents(keycloakRule); + + @Override + public void beforeAbstractKeycloakTest() throws Exception { + super.beforeAbstractKeycloakTest(); + } + + @Override + public void addTestRealms(List testRealms) { + + RealmRepresentation realmRepresentation = loadJson(getClass().getResourceAsStream("/testrealm.json"), RealmRepresentation.class); + + testRealms.add(realmRepresentation); + + } @Test public void authorizationRequest() throws IOException { oauth.state("mystate"); - AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); + OAuthClient.AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); Assert.assertTrue(response.isRedirected()); Assert.assertNotNull(response.getCode()); assertEquals("mystate", response.getState()); Assert.assertNull(response.getError()); - keycloakRule.verifyCode(response.getCode()); + testingClient.testing().verifyCode("test", response.getCode()); String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID); assertCode(codeId, response.getCode()); @@ -88,12 +84,7 @@ public class AuthorizationCodeTest { @Test public void authorizationRequestInstalledApp() throws IOException { - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - appRealm.getClientByClientId("test-app").addRedirectUri(Constants.INSTALLED_APP_URN); - } - }); + ClientManager.realm(adminClient.realm("test")).clientId("test-app").addRedirectUris(Constants.INSTALLED_APP_URN); oauth.redirectUri(Constants.INSTALLED_APP_URN); oauth.doLogin("test-user@localhost", "password"); @@ -102,36 +93,26 @@ public class AuthorizationCodeTest { Assert.assertEquals("Success code", title); String code = driver.findElement(By.id(OAuth2Constants.CODE)).getAttribute("value"); - keycloakRule.verifyCode(code); + testingClient.testing().verifyCode("test", code); - String codeId = events.expectLogin().detail(Details.REDIRECT_URI, "http://localhost:8081/auth/realms/test/protocol/openid-connect/oauth/oob").assertEvent().getDetails().get(Details.CODE_ID); + String codeId = events.expectLogin().detail(Details.REDIRECT_URI, "http://localhost:8180/auth/realms/test/protocol/openid-connect/oauth/oob").assertEvent().getDetails().get(Details.CODE_ID); assertCode(codeId, code); - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - appRealm.getClientByClientId("test-app").removeRedirectUri(Constants.INSTALLED_APP_URN); - } - }); + ClientManager.realm(adminClient.realm("test")).clientId("test-app").removeRedirectUris(Constants.INSTALLED_APP_URN); } @Test public void authorizationValidRedirectUri() throws IOException { - keycloakRule.update(new KeycloakRule.KeycloakSetup() { - @Override - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - appRealm.getClientByClientId("test-app").addRedirectUri(oauth.getRedirectUri()); - } - }); + ClientManager.realm(adminClient.realm("test")).clientId("test-app").addRedirectUris(oauth.getRedirectUri()); oauth.state("mystate"); - AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); + OAuthClient.AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); Assert.assertTrue(response.isRedirected()); Assert.assertNotNull(response.getCode()); - keycloakRule.verifyCode(response.getCode()); + testingClient.testing().verifyCode("test", response.getCode()); String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID); assertCode(codeId, response.getCode()); @@ -141,14 +122,14 @@ public class AuthorizationCodeTest { public void authorizationRequestNoState() throws IOException { oauth.state(null); - AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); + OAuthClient.AuthorizationCodeResponse response = oauth.doLogin("test-user@localhost", "password"); Assert.assertTrue(response.isRedirected()); Assert.assertNotNull(response.getCode()); Assert.assertNull(response.getState()); Assert.assertNull(response.getError()); - keycloakRule.verifyCode(response.getCode()); + testingClient.testing().verifyCode("test", response.getCode()); String codeId = events.expectLogin().assertEvent().getDetails().get(Details.CODE_ID); assertCode(codeId, response.getCode()); @@ -173,8 +154,8 @@ public class AuthorizationCodeTest { } private void assertCode(String expectedCodeId, String actualCode) { - ClientSessionCode code = keycloakRule.verifyCode(actualCode); - assertEquals(expectedCodeId, code.getClientSession().getId()); + String code = testingClient.testing().verifyCode("test", actualCode); + assertEquals(expectedCodeId, code); } } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/ClientManager.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/ClientManager.java index 597376ce71..02bd19a5cd 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/ClientManager.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/ClientManager.java @@ -6,8 +6,10 @@ import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ProtocolMapperRepresentation; import org.keycloak.representations.idm.RoleRepresentation; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.LinkedList; import static org.keycloak.testsuite.admin.ApiUtil.findClientByClientId; import static org.keycloak.testsuite.admin.ApiUtil.findProtocolMapperByName; @@ -96,5 +98,22 @@ public class ClientManager { public void removeScopeMapping(RoleRepresentation newRole) { clientResource.getScopeMappings().realmLevel().remove(Collections.singletonList(newRole)); } + + public void addRedirectUris(String... redirectUris) { + ClientRepresentation app = clientResource.toRepresentation(); + if (app.getRedirectUris() == null) { + app.setRedirectUris(new LinkedList()); + } + app.setRedirectUris(Arrays.asList(redirectUris)); + clientResource.update(app); + } + + public void removeRedirectUris(String... redirectUris) { + ClientRepresentation app = clientResource.toRepresentation(); + for (String redirectUri : redirectUris) { + app.getRedirectUris().remove(redirectUri); + } + clientResource.update(app); + } } }