From b5aa9efefb740fa1aaef6aee45c59def5a00fff3 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 19 May 2014 18:53:28 -0400 Subject: [PATCH] fix remove expired sessions --- .../testsuite/adapter/AdapterTest.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java index bebe763e7f..215443d9a3 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java @@ -25,6 +25,7 @@ import org.junit.Assert; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; +import org.keycloak.Config; import org.keycloak.OAuth2Constants; import org.keycloak.models.ApplicationModel; import org.keycloak.models.Constants; @@ -40,7 +41,6 @@ import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.TokenManager; import org.keycloak.services.resources.TokenService; import org.keycloak.services.resources.admin.AdminRoot; -import org.keycloak.services.resources.admin.RealmAdminResource; import org.keycloak.testsuite.OAuthClient; import org.keycloak.testsuite.pages.LoginPage; import org.keycloak.testsuite.rule.AbstractKeycloakRule; @@ -69,7 +69,7 @@ public class AdapterTest { public static final String LOGIN_URL = TokenService.loginPageUrl(UriBuilder.fromUri("http://localhost:8081/auth")).build("demo").toString(); public static PublicKey realmPublicKey; @ClassRule - public static AbstractKeycloakRule keycloakRule = new AbstractKeycloakRule(){ + public static AbstractKeycloakRule keycloakRule = new AbstractKeycloakRule() { @Override protected void configure(RealmManager manager, RealmModel adminRealm) { RealmRepresentation representation = KeycloakServer.loadJson(getClass().getResourceAsStream("/adapter-test/demorealm.json"), RealmRepresentation.class); @@ -83,17 +83,28 @@ public class AdapterTest { deployApplication("customer-db", "/customer-db", CustomerDatabaseServlet.class, url.getPath(), "user"); url = getClass().getResource("/adapter-test/product-keycloak.json"); deployApplication("product-portal", "/product-portal", ProductServlet.class, url.getPath(), "user"); - ApplicationModel adminConsole = adminRealm.getApplicationByName(Constants.ADMIN_CONSOLE_APPLICATION); - TokenManager tm = new TokenManager(); - UserModel admin = adminRealm.getUser("admin"); - UserSessionModel session = adminRealm.createUserSession(admin, null); - AccessToken token = tm.createClientAccessToken(null, adminRealm, adminConsole, admin, session); - adminToken = tm.encodeToken(adminRealm, token); } }; - public static String adminToken; + private static String createToken() { + ProviderSession providerSession = keycloakRule.startSession(); + try { + RealmManager manager = new RealmManager(providerSession.getProvider(KeycloakSession.class)); + + RealmModel adminRealm = manager.getRealm(Config.getAdminRealm()); + ApplicationModel adminConsole = adminRealm.getApplicationByName(Constants.ADMIN_CONSOLE_APPLICATION); + TokenManager tm = new TokenManager(); + UserModel admin = adminRealm.getUser("admin"); + UserSessionModel userSession = adminRealm.createUserSession(admin, null); + AccessToken token = tm.createClientAccessToken(null, adminRealm, adminConsole, admin, userSession); + return tm.encodeToken(adminRealm, token); + } finally { + keycloakRule.stopSession(providerSession, true); + + } + } + @Rule public WebRule webRule = new WebRule(this); @@ -128,12 +139,15 @@ public class AdapterTest { Assert.assertTrue(pageSource.contains("iPhone") && pageSource.contains("iPad")); // View stats + String adminToken = createToken(); + Client client = ClientBuilder.newClient(); UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth"); WebTarget adminTarget = client.target(AdminRoot.realmsUrl(authBase)).path("demo"); Map stats = adminTarget.path("session-stats").request() .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminToken) - .get(new GenericType>(){}); + .get(new GenericType>() { + }); SessionStats custStats = stats.get("customer-portal"); Assert.assertNotNull(custStats);