diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java index 01a448a52e..cab58d9762 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java @@ -22,10 +22,10 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Assert; +import org.junit.rules.TestRule; +import org.junit.runners.model.Statement; import org.keycloak.OAuth2Constants; -import org.keycloak.admin.client.resource.RealmResource; import org.keycloak.authentication.authenticators.client.ClientIdAndSecretAuthenticator; -import org.keycloak.common.util.PemUtils; import org.keycloak.events.Details; import org.keycloak.events.EventType; import org.keycloak.representations.idm.ClientRepresentation; @@ -33,11 +33,9 @@ import org.keycloak.representations.idm.EventRepresentation; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserSessionRepresentation; -import org.keycloak.testsuite.client.resources.TestingResource; import org.keycloak.util.TokenUtil; import javax.ws.rs.core.Response; -import java.security.PublicKey; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -45,7 +43,7 @@ import java.util.Map; /** * @author Stian Thorgersen */ -public class AssertEvents { +public class AssertEvents implements TestRule { public static final String DEFAULT_CLIENT_ID = "test-app"; public static final String DEFAULT_IP_ADDRESS = "127.0.0.1"; @@ -54,25 +52,22 @@ public class AssertEvents { String defaultRedirectUri = "http://localhost:8180/auth/realms/master/app/auth"; - private RealmResource realmResource; - private TestingResource testingResource; - private RealmRepresentation realmRep; private AbstractKeycloakTest context; - private PublicKey realmPublicKey; - public AssertEvents(AbstractKeycloakTest ctx) throws Exception { + public AssertEvents(AbstractKeycloakTest ctx) { context = ctx; + } - realmResource = context.adminClient.realms().realm(DEFAULT_REALM); - realmRep = realmResource.toRepresentation(); - String pubKeyString = realmRep.getPublicKey(); - realmPublicKey = PemUtils.decodePublicKey(pubKeyString); - - UserRepresentation defaultUser = getUser(DEFAULT_USERNAME); - if (defaultUser == null) { - throw new RuntimeException("Default user does not exist: " + DEFAULT_USERNAME + ". Make sure to add it to your test realm."); - } - testingResource = context.testingClient.testing(); + @Override + public Statement apply(final Statement base, org.junit.runner.Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + base.evaluate(); + // TODO Test should fail if there are leftover events + context.testingClient.testing().clearQueue(); + } + }; } public EventRepresentation poll() { @@ -83,7 +78,7 @@ public class AssertEvents { } public void clear() { - Response res = testingResource.clearQueue(); + Response res = context.testingClient.testing().clearQueue(); try { Assert.assertEquals("clear-event-queue success", res.getStatus(), 200); } finally { @@ -166,7 +161,7 @@ public class AssertEvents { public ExpectedEvent expect(EventType event) { return new ExpectedEvent() - .realm(realmRep.getId()) + .realm(defaultRealmId()) .client(DEFAULT_CLIENT_ID) .user(defaultUserId()) .ipAddress(DEFAULT_IP_ADDRESS) @@ -174,29 +169,24 @@ public class AssertEvents { .event(event); } - UserRepresentation getUser(String username) { - List result = realmResource.users().search(username, null, null, null, 0, 1); - return result.size() > 0 ? result.get(0) : null; - } - - public PublicKey getRealmPublicKey() { - return realmPublicKey; - } - public class ExpectedEvent { private EventRepresentation expected = new EventRepresentation(); + private Matcher realmId; private Matcher userId; private Matcher sessionId; private HashMap> details; - public ExpectedEvent realm(RealmRepresentation realm) { - expected.setRealmId(realm.getId()); + public ExpectedEvent realm(Matcher realmId) { + this.realmId = realmId; return this; } + public ExpectedEvent realm(RealmRepresentation realm) { + return realm(CoreMatchers.equalTo(realm.getId())); + } + public ExpectedEvent realm(String realmId) { - expected.setRealmId(realmId); - return this; + return realm(CoreMatchers.equalTo(realmId)); } public ExpectedEvent client(ClientRepresentation client) { @@ -283,7 +273,7 @@ public class AssertEvents { expected.setType(expected.getType() + "_ERROR"); } Assert.assertEquals(expected.getType(), actual.getType()); - Assert.assertEquals(expected.getRealmId(), actual.getRealmId()); + Assert.assertThat(actual.getRealmId(), realmId); Assert.assertEquals(expected.getClientId(), actual.getClientId()); Assert.assertEquals(expected.getError(), actual.getError()); Assert.assertEquals(expected.getIpAddress(), actual.getIpAddress()); @@ -333,6 +323,34 @@ public class AssertEvents { }; } + public Matcher defaultRealmId() { + return new TypeSafeMatcher() { + private String realmId; + + @Override + protected boolean matchesSafely(String item) { + return item.equals(getRealmId()); + } + + @Override + public void describeTo(Description description) { + description.appendText(getRealmId()); + } + + private String getRealmId() { + if (realmId == null) { + RealmRepresentation realm = context.adminClient.realm(DEFAULT_REALM).toRepresentation(); + if (realm == null) { + throw new RuntimeException("Default user does not exist: " + DEFAULT_USERNAME + ". Make sure to add it to your test realm."); + } + realmId = realm.getId(); + } + return realmId; + } + + }; + } + public Matcher defaultUserId() { return new TypeSafeMatcher() { private String userId; @@ -361,7 +379,12 @@ public class AssertEvents { }; } + private UserRepresentation getUser(String username) { + List users = context.adminClient.realm(DEFAULT_REALM).users().search(username, null, null, null, 0, 1); + return users.isEmpty() ? null : users.get(0); + } + private EventRepresentation fetchNextEvent() { - return testingResource.pollEvent(); + return context.testingClient.testing().pollEvent(); } } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/federation/AbstractKerberosAdapterTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/federation/AbstractKerberosAdapterTest.java index 0bd5b01771..ccfe5a3a7a 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/federation/AbstractKerberosAdapterTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/federation/AbstractKerberosAdapterTest.java @@ -41,6 +41,7 @@ import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.keycloak.adapters.HttpClientBuilder; import org.keycloak.admin.client.resource.RealmResource; @@ -77,8 +78,9 @@ public abstract class AbstractKerberosAdapterTest extends AbstractServletsAdapte protected ResteasyClient client; protected static LDAPEmbeddedServer ldapEmbeddedServer; - - protected AssertEvents events; + + @Rule + public AssertEvents events = new AssertEvents(this); @Page protected ChangePassword changePasswordPage; @@ -115,7 +117,6 @@ public abstract class AbstractKerberosAdapterTest extends AbstractServletsAdapte String krb5ConfPath = LDAPTestConfiguration.getResource("test-krb5.conf"); log.info("Krb5.conf file location is: " + krb5ConfPath); System.setProperty("java.security.krb5.conf", krb5ConfPath); - events = new AssertEvents(this); UserFederationProviderModel model = new UserFederationProviderModel(); model.setConfig(ldapTestConfiguration.getLDAPConfig()); spnegoSchemeFactory = new KeycloakSPNegoSchemeFactory(getKerberosConfig(model)); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/ImpersonationTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/ImpersonationTest.java index be642b64ac..7ec1da25dc 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/ImpersonationTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/ImpersonationTest.java @@ -18,6 +18,7 @@ package org.keycloak.testsuite.admin; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; import org.keycloak.Config; import org.keycloak.admin.client.resource.ClientResource; @@ -58,14 +59,14 @@ import java.util.Map; */ public class ImpersonationTest extends AbstractKeycloakTest { - private AssertEvents events; + @Rule + public AssertEvents events = new AssertEvents(this); private String impersonatedUserId; @Override public void beforeAbstractKeycloakTest() throws Exception { super.beforeAbstractKeycloakTest(); - events = new AssertEvents(this); } @Override diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/AbstractGroupTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/AbstractGroupTest.java index a9520e05d9..543482d221 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/AbstractGroupTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/group/AbstractGroupTest.java @@ -18,8 +18,10 @@ package org.keycloak.testsuite.admin.group; import org.junit.Before; +import org.junit.Rule; import org.keycloak.OAuth2Constants; import org.keycloak.RSATokenVerifier; +import org.keycloak.common.util.PemUtils; import org.keycloak.events.Details; import org.keycloak.jose.jws.JWSInput; import org.keycloak.jose.jws.crypto.RSAProvider; @@ -31,6 +33,7 @@ import org.keycloak.testsuite.AbstractKeycloakTest; import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.arquillian.AuthServerTestEnricher; +import java.security.PublicKey; import java.util.List; import static org.keycloak.testsuite.util.IOUtil.loadRealm; @@ -40,13 +43,8 @@ import static org.keycloak.testsuite.util.IOUtil.loadRealm; */ public abstract class AbstractGroupTest extends AbstractKeycloakTest { - AssertEvents events; - - @Before - public void initAssertEvents() throws Exception { - events = new AssertEvents(this); - events.clear(); - } + @Rule + public AssertEvents events = new AssertEvents(this); AccessToken login(String login, String clientId, String clientSecret, String userId) throws Exception { @@ -55,10 +53,12 @@ public abstract class AbstractGroupTest extends AbstractKeycloakTest { String accessToken = tokenResponse.getToken(); String refreshToken = tokenResponse.getRefreshToken(); - AccessToken accessTokenRepresentation = RSATokenVerifier.verifyToken(accessToken, events.getRealmPublicKey(), AuthServerTestEnricher.getAuthServerContextRoot() + "/auth/realms/test"); + PublicKey publicKey = PemUtils.decodePublicKey(adminClient.realm("test").toRepresentation().getPublicKey()); + + AccessToken accessTokenRepresentation = RSATokenVerifier.verifyToken(accessToken, publicKey, AuthServerTestEnricher.getAuthServerContextRoot() + "/auth/realms/test"); JWSInput jws = new JWSInput(refreshToken); - if (!RSAProvider.verify(jws, events.getRealmPublicKey())) { + if (!RSAProvider.verify(jws, publicKey)) { throw new RuntimeException("Invalid refresh token"); } RefreshToken refreshTokenRepresentation = jws.readJsonContent(RefreshToken.class); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/TokenIntrospectionTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/TokenIntrospectionTest.java index 54f218e890..081cec566c 100755 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/TokenIntrospectionTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/oauth/TokenIntrospectionTest.java @@ -18,13 +18,10 @@ package org.keycloak.testsuite.oauth; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.ArrayList; -import java.util.List; -import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.keycloak.OAuth2Constants; import org.keycloak.common.util.Time; -import org.keycloak.testsuite.util.KeycloakModelUtils; import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.EventRepresentation; @@ -33,13 +30,13 @@ import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.oidc.TokenMetadataRepresentation; import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.TestRealmKeycloakTest; +import org.keycloak.testsuite.util.KeycloakModelUtils; import org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse; -//import org.adminClient.testsuite.pages.LoginPage; -//import org.adminClient.testsuite.rule.KeycloakRule; -//import org.adminClient.testsuite.rule.WebResource; -//import org.adminClient.testsuite.rule.WebRule; import org.keycloak.util.JsonSerialization; +import java.util.ArrayList; +import java.util.List; + import static org.junit.Assert.*; /** @@ -48,34 +45,8 @@ import static org.junit.Assert.*; */ public class TokenIntrospectionTest extends TestRealmKeycloakTest { - private AssertEvents events; - - /* @ClassRule >>> now implemented in configureTestRealm() - public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakRule.KeycloakSetup() { - - @Override - vvv "test" realm - public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { - vvv Done in TestRealmKeycloakTest vvv - appRealm.getClientByClientId("test-app").setDirectAccessGrantsEnabled(true); - - ClientModel confApp = KeycloakModelUtils.createClient(appRealm, "confidential-cli"); - confApp.setSecret("secret1"); - new ClientManager(manager).enableServiceAccount(confApp); - ClientModel pubApp = KeycloakModelUtils.createClient(appRealm, "public-cli"); - pubApp.setPublicClient(true); - { - UserModel user = manager.getSession().users().addUser(appRealm, KeycloakModelUtils.generateId(), "no-permissions", false, false); - user.updateCredential(UserCredentialModel.password("password")); - user.setEnabled(true); - RoleModel role = appRealm.getRole("user"); - user.grantRole(role); - } - - adminClient = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID); - } - - }); */ + @Rule + public AssertEvents events = new AssertEvents(this); @Override public void configureTestRealm(RealmRepresentation testRealm) { @@ -101,11 +72,6 @@ public class TokenIntrospectionTest extends TestRealmKeycloakTest { testRealm.getUsers().add(user); } - @Before - public void setUpAssertEvents() throws Exception { - events = new AssertEvents(this); - } - @Test public void testConfidentialClientCredentialsBasicAuthentication() throws Exception { oauth.doLogin("test-user@localhost", "password");