diff --git a/audit/jpa/pom.xml b/audit/jpa/pom.xml index 08df5b949b..dd725b86bb 100755 --- a/audit/jpa/pom.xml +++ b/audit/jpa/pom.xml @@ -37,12 +37,6 @@ ${project.version} provided - - org.keycloak - keycloak-audit-tests - ${project.version} - provided - org.hibernate.javax.persistence hibernate-jpa-2.0-api @@ -64,16 +58,5 @@ jackson-core-asl provided - - junit - junit - test - - - com.h2database - h2 - test - - diff --git a/audit/jpa/src/test/java/org/keycloak/audit/jpa/JpaAuditProviderTest.java b/audit/jpa/src/test/java/org/keycloak/audit/jpa/JpaAuditProviderTest.java deleted file mode 100644 index 41f774160c..0000000000 --- a/audit/jpa/src/test/java/org/keycloak/audit/jpa/JpaAuditProviderTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.keycloak.audit.jpa; - -import org.junit.Ignore; -import org.keycloak.audit.tests.AbstractAuditProviderTest; - -/** - * @author Stian Thorgersen - */ -@Ignore -public class JpaAuditProviderTest extends AbstractAuditProviderTest { - - @Override - protected String getProviderId() { - return JpaAuditProviderFactory.ID; - } - -} diff --git a/audit/mongo/pom.xml b/audit/mongo/pom.xml index 3f40488e3f..e9287847f6 100755 --- a/audit/mongo/pom.xml +++ b/audit/mongo/pom.xml @@ -37,12 +37,6 @@ ${project.version} provided - - org.keycloak - keycloak-audit-tests - ${project.version} - test - org.jboss.logging jboss-logging @@ -53,90 +47,5 @@ mongo-java-driver provided - - junit - junit - test - - - - localhost - 27018 - keycloak - true - 127.0.0.1 - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.6 - 1.6 - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - test - integration-test - - test - - - - ${keycloak.audit.mongo.host} - ${keycloak.audit.mongo.port} - ${keycloak.audit.mongo.db} - ${keycloak.audit.mongo.clearOnStartup} - ${keycloak.model.mongo.bindIp} - - - - - default-test - - true - - - - - - - - com.github.joelittlejohn.embedmongo - embedmongo-maven-plugin - - - start-mongodb - pre-integration-test - - start - - - ${keycloak.audit.mongo.port} - file - ${project.build.directory}/mongodb.log - ${keycloak.audit.mongo.bindIp} - - - - stop-mongodb - post-integration-test - - stop - - - - - - - diff --git a/audit/mongo/src/test/java/org/keycloak/audit/mongo/MongoAuditProviderTest.java b/audit/mongo/src/test/java/org/keycloak/audit/mongo/MongoAuditProviderTest.java deleted file mode 100644 index 92aa672602..0000000000 --- a/audit/mongo/src/test/java/org/keycloak/audit/mongo/MongoAuditProviderTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.keycloak.audit.mongo; - -import org.junit.Ignore; -import org.keycloak.audit.tests.AbstractAuditProviderTest; - -/** - * @author Stian Thorgersen - */ -@Ignore -public class MongoAuditProviderTest extends AbstractAuditProviderTest { - - @Override - protected String getProviderId() { - return MongoAuditProviderFactory.ID; - } - -} diff --git a/audit/pom.xml b/audit/pom.xml index b6ae9e3650..0f210d374c 100755 --- a/audit/pom.xml +++ b/audit/pom.xml @@ -21,6 +21,5 @@ jpa jboss-logging mongo - tests diff --git a/audit/tests/pom.xml b/audit/tests/pom.xml deleted file mode 100755 index 7b288c5df9..0000000000 --- a/audit/tests/pom.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - - keycloak-audit-parent - org.keycloak - 1.0-beta-4-SNAPSHOT - - 4.0.0 - - keycloak-audit-tests - Keycloak Audit Tests - - - - - - org.apache.maven.plugins - maven-deploy-plugin - - true - - - - - - - - org.keycloak - keycloak-core - ${project.version} - provided - - - org.keycloak - keycloak-audit-api - ${project.version} - provided - - - org.keycloak - keycloak-model-api - ${project.version} - provided - - - junit - junit - provided - - - diff --git a/audit/tests/src/main/java/org/keycloak/audit/tests/AbstractAuditProviderTest.java b/audit/tests/src/main/java/org/keycloak/audit/tests/AbstractAuditProviderTest.java deleted file mode 100644 index 82ecc28088..0000000000 --- a/audit/tests/src/main/java/org/keycloak/audit/tests/AbstractAuditProviderTest.java +++ /dev/null @@ -1,139 +0,0 @@ -package org.keycloak.audit.tests; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.keycloak.Config; -import org.keycloak.audit.AuditProvider; -import org.keycloak.audit.AuditProviderFactory; -import org.keycloak.audit.Event; -import org.keycloak.audit.EventType; -import org.keycloak.provider.ProviderFactory; - -import java.util.HashMap; -import java.util.Map; -import java.util.ServiceLoader; - -/** - * @author Stian Thorgersen - */ -public abstract class AbstractAuditProviderTest { - - private ProviderFactory factory; - private AuditProvider provider; - - @Before - public void before() { - String providerId = getProviderId(); - ServiceLoader factories = ServiceLoader.load(AuditProviderFactory.class); - for (AuditProviderFactory f : factories) { - if (f.getId().equals(providerId)) { - factory = f; - factory.init(Config.scope("audit", providerId)); - } - } - - provider = factory.create(null); - } - - protected abstract String getProviderId(); - - @After - public void after() { - provider.clear(); - provider.close(); - factory.close(); - } - - @Test - public void save() { - provider.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - } - - @Test - public void query() { - long oldest = System.currentTimeMillis() - 30000; - long newest = System.currentTimeMillis() + 30000; - - provider.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId2", "127.0.0.1", "error")); - provider.onEvent(create(EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(oldest, EventType.LOGIN, "realmId", "clientId2", "userId", "127.0.0.1", "error")); - provider.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId2", "127.0.0.1", "error")); - - provider.close(); - provider = factory.create(null); - - Assert.assertEquals(5, provider.createQuery().client("clientId").getResultList().size()); - Assert.assertEquals(5, provider.createQuery().realm("realmId").getResultList().size()); - Assert.assertEquals(4, provider.createQuery().event(EventType.LOGIN).getResultList().size()); - Assert.assertEquals(6, provider.createQuery().event(EventType.LOGIN, EventType.REGISTER).getResultList().size()); - Assert.assertEquals(4, provider.createQuery().user("userId").getResultList().size()); - - Assert.assertEquals(1, provider.createQuery().user("userId").event(EventType.REGISTER).getResultList().size()); - - Assert.assertEquals(2, provider.createQuery().maxResults(2).getResultList().size()); - Assert.assertEquals(1, provider.createQuery().firstResult(5).getResultList().size()); - - Assert.assertEquals(newest, provider.createQuery().maxResults(1).getResultList().get(0).getTime()); - Assert.assertEquals(oldest, provider.createQuery().firstResult(5).maxResults(1).getResultList().get(0).getTime()); - } - - @Test - public void clear() { - provider.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error")); - - provider.close(); - provider = factory.create(null); - - provider.clear("realmId"); - - Assert.assertEquals(1, provider.createQuery().getResultList().size()); - } - - @Test - public void clearOld() { - provider.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); - provider.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error")); - - provider.close(); - provider = factory.create(null); - - provider.clear("realmId", System.currentTimeMillis() - 10000); - - Assert.assertEquals(3, provider.createQuery().getResultList().size()); - } - - private Event create(EventType event, String realmId, String clientId, String userId, String ipAddress, String error) { - return create(System.currentTimeMillis(), event, realmId, clientId, userId, ipAddress, error); - } - - private Event create(long time, EventType event, String realmId, String clientId, String userId, String ipAddress, String error) { - Event e = new Event(); - e.setTime(time); - e.setEvent(event); - e.setRealmId(realmId); - e.setClientId(clientId); - e.setUserId(userId); - e.setIpAddress(ipAddress); - e.setError(error); - - Map details = new HashMap(); - details.put("key1", "value1"); - details.put("key2", "value2"); - - e.setDetails(details); - - return e; - } - -} diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java index 2b40f801f9..cea49fffd9 100644 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/DefaultMongoConnectionFactoryProvider.java @@ -31,7 +31,8 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro "org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity", "org.keycloak.models.mongo.keycloak.entities.MongoOAuthClientEntity", "org.keycloak.models.sessions.mongo.entities.MongoUsernameLoginFailureEntity", - "org.keycloak.models.sessions.mongo.entities.MongoUserSessionEntity" + "org.keycloak.models.sessions.mongo.entities.MongoUserSessionEntity", + "org.keycloak.models.entities.FederationProviderEntity" }; private static final Logger logger = Logger.getLogger(DefaultMongoConnectionFactoryProvider.class); diff --git a/connections/mongo/src/main/java/org/keycloak/connections/mongo/impl/types/MongoEntityMapper.java b/connections/mongo/src/main/java/org/keycloak/connections/mongo/impl/types/MongoEntityMapper.java index 41dc75cc3d..513a7fc7ab 100755 --- a/connections/mongo/src/main/java/org/keycloak/connections/mongo/impl/types/MongoEntityMapper.java +++ b/connections/mongo/src/main/java/org/keycloak/connections/mongo/impl/types/MongoEntityMapper.java @@ -1,6 +1,7 @@ package org.keycloak.connections.mongo.impl.types; import com.mongodb.BasicDBObject; +import org.keycloak.connections.mongo.api.MongoIdentifiableEntity; import org.keycloak.connections.mongo.api.types.Mapper; import org.keycloak.connections.mongo.api.types.MapperContext; import org.keycloak.connections.mongo.api.types.MapperRegistry; @@ -38,7 +39,7 @@ public class MongoEntityMapper implements Mapper { String propName = property.getName(); // Ignore "id" property - if (!"id".equals(propName)) { + if (!"id".equals(propName) || !(applicationObject instanceof MongoIdentifiableEntity)) { Object propValue = property.getValue(applicationObject); if (propValue != null) { Object dbValue = propValue == null ? null : mapperRegistry.convertApplicationObjectToDBObject(propValue, Object.class); diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java index 57f3f90a61..dfbd165ec4 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheRealmProvider.java @@ -243,14 +243,18 @@ public class DefaultCacheRealmProvider implements CacheRealmProvider { public RoleModel getRoleById(String id, RealmModel realm) { if (!cache.isEnabled()) return getDelegate().getRoleById(id, realm); CachedRole cached = cache.getRole(id); + if (cached != null && !cached.getRealm().equals(realm.getId())) { + cached = null; + } + if (cached == null) { RoleModel model = getDelegate().getRoleById(id, realm); if (model == null) return null; if (roleInvalidations.contains(id)) return model; if (model.getContainer() instanceof ApplicationModel) { - cached = new CachedApplicationRole(((ApplicationModel) model.getContainer()).getId(), model); + cached = new CachedApplicationRole(((ApplicationModel) model.getContainer()).getId(), model, realm); } else { - cached = new CachedRealmRole(model); + cached = new CachedRealmRole(model, realm); } cache.addCachedRole(cached); @@ -268,6 +272,10 @@ public class DefaultCacheRealmProvider implements CacheRealmProvider { public ApplicationModel getApplicationById(String id, RealmModel realm) { if (!cache.isEnabled()) return getDelegate().getApplicationById(id, realm); CachedApplication cached = cache.getApplication(id); + if (cached != null && !cached.getRealm().equals(realm.getId())) { + cached = null; + } + if (cached == null) { ApplicationModel model = getDelegate().getApplicationById(id, realm); if (model == null) return null; @@ -288,6 +296,10 @@ public class DefaultCacheRealmProvider implements CacheRealmProvider { public OAuthClientModel getOAuthClientById(String id, RealmModel realm) { if (!cache.isEnabled()) return getDelegate().getOAuthClientById(id, realm); CachedOAuthClient cached = cache.getOAuthClient(id); + if (cached != null && !cached.getRealm().equals(realm.getId())) { + cached = null; + } + if (cached == null) { OAuthClientModel model = getDelegate().getOAuthClientById(id, realm); if (model == null) return null; diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/RealmAdapter.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/RealmAdapter.java index b4fa0c84a4..deed00b702 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/RealmAdapter.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/RealmAdapter.java @@ -1,5 +1,6 @@ package org.keycloak.models.cache; +import org.keycloak.Config; import org.keycloak.models.ApplicationModel; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.ClientModel; @@ -726,8 +727,7 @@ public class RealmAdapter implements RealmModel { @Override public ApplicationModel getMasterAdminApp() { - if (updated != null) return updated.getMasterAdminApp(); - return getApplicationById(cached.getMasterAdminApp()); + return cacheSession.getRealm(Config.getAdminRealm()).getApplicationById(cached.getMasterAdminApp()); } @Override diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplication.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplication.java index 3ccacbf51a..db0e1c0a4f 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplication.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplication.java @@ -32,7 +32,7 @@ public class CachedApplication extends CachedClient { bearerOnly = model.isBearerOnly(); for (RoleModel role : model.getRoles()) { roles.put(role.getName(), role.getId()); - cache.addCachedRole(new CachedApplicationRole(id, role)); + cache.addCachedRole(new CachedApplicationRole(id, role, realm)); } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplicationRole.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplicationRole.java index 269fba99a7..da0451c788 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplicationRole.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedApplicationRole.java @@ -1,5 +1,6 @@ package org.keycloak.models.cache.entities; +import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; /** @@ -9,8 +10,8 @@ import org.keycloak.models.RoleModel; public class CachedApplicationRole extends CachedRole { private final String appId; - public CachedApplicationRole(String appId, RoleModel model) { - super(model); + public CachedApplicationRole(String appId, RoleModel model, RealmModel realm) { + super(model, realm); this.appId = appId; } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedClient.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedClient.java index feabb452c2..8e4b0081a8 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedClient.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedClient.java @@ -16,6 +16,7 @@ import java.util.Set; public class CachedClient { protected String id; protected String name; + protected String realm; protected long allowedClaimsMask; protected Set redirectUris = new HashSet(); protected boolean enabled; @@ -30,6 +31,7 @@ public class CachedClient { id = model.getId(); secret = model.getSecret(); name = model.getClientId(); + this.realm = realm.getId(); enabled = model.isEnabled(); notBefore = model.getNotBefore(); directGrantsOnly = model.isDirectGrantsOnly(); @@ -51,6 +53,10 @@ public class CachedClient { return name; } + public String getRealm() { + return realm; + } + public long getAllowedClaimsMask() { return allowedClaimsMask; } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealm.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealm.java index 507cef49f9..cfd6018812 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealm.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealm.java @@ -136,7 +136,7 @@ public class CachedRealm { for (RoleModel role : model.getRoles()) { realmRoles.put(role.getName(), role.getId()); - CachedRole cachedRole = new CachedRealmRole(role); + CachedRole cachedRole = new CachedRealmRole(role, model); cache.addCachedRole(cachedRole); } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealmRole.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealmRole.java index f1f34dc704..e34478e645 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealmRole.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRealmRole.java @@ -1,5 +1,6 @@ package org.keycloak.models.cache.entities; +import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; /** @@ -9,8 +10,8 @@ import org.keycloak.models.RoleModel; public class CachedRealmRole extends CachedRole { - public CachedRealmRole(RoleModel model) { - super(model); + public CachedRealmRole(RoleModel model, RealmModel realm) { + super(model, realm); } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRole.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRole.java index ed451e2846..d52d0ffb97 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRole.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedRole.java @@ -1,5 +1,6 @@ package org.keycloak.models.cache.entities; +import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import java.util.HashSet; @@ -12,15 +13,17 @@ import java.util.Set; public class CachedRole { final protected String id; final protected String name; + final protected String realm; final protected String description; final protected boolean composite; final protected Set composites = new HashSet(); - public CachedRole(RoleModel model) { + public CachedRole(RoleModel model, RealmModel realm) { composite = model.isComposite(); description = model.getDescription(); id = model.getId(); name = model.getName(); + this.realm = realm.getId(); if (composite) { for (RoleModel child : model.getComposites()) { composites.add(child.getId()); @@ -37,6 +40,10 @@ public class CachedRole { return name; } + public String getRealm() { + return realm; + } + public String getDescription() { return description; } diff --git a/model/jpa/pom.xml b/model/jpa/pom.xml index 9a3e026108..52c7db511e 100755 --- a/model/jpa/pom.xml +++ b/model/jpa/pom.xml @@ -83,26 +83,6 @@ - - - - - - - - - - - - - - - - com.h2database - h2 - test - - @@ -114,37 +94,6 @@ ${maven.compiler.target} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java index 24e6797ee3..b93fdff0f3 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java @@ -123,6 +123,8 @@ public class JpaUserProvider implements UserProvider { public void preRemove(RealmModel realm) { int num = em.createNamedQuery("deleteUserRoleMappingsByRealm") .setParameter("realmId", realm.getId()).executeUpdate(); + num = em.createNamedQuery("deleteUserRequiredActionsByRealm") + .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteSocialLinkByRealm") .setParameter("realmId", realm.getId()).executeUpdate(); num = em.createNamedQuery("deleteCredentialsByRealm") diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRequiredActionEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRequiredActionEntity.java index 2d3ddbde5f..d2161e905e 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRequiredActionEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRequiredActionEntity.java @@ -20,7 +20,7 @@ import java.io.Serializable; * @version $Revision: 1 $ */ @NamedQueries({ - @NamedQuery(name="deleteUserRequiredActionsByRealm", query="delete from UserRequiredActionEntity action where action.user IN (select u from UserEntity u where realm=:realm)") + @NamedQuery(name="deleteUserRequiredActionsByRealm", query="delete from UserRequiredActionEntity action where action.user IN (select u from UserEntity u where u.realmId=:realmId)") }) @Entity @Table(name="USER_REQUIRED_ACTION") diff --git a/model/mongo/pom.xml b/model/mongo/pom.xml index cdf80b3a1a..c239f07443 100755 --- a/model/mongo/pom.xml +++ b/model/mongo/pom.xml @@ -43,12 +43,6 @@ ${project.version} provided - - org.keycloak - keycloak-invalidation-cache-model - ${project.version} - test - org.jboss.logging jboss-logging @@ -59,128 +53,5 @@ mongo-java-driver provided - - org.keycloak - keycloak-model-sessions-mem - ${project.version} - test - - - - org.keycloak - keycloak-model-tests - ${project.version} - test - - - org.jboss.resteasy - resteasy-jaxrs - test - - - log4j - log4j - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-simple - - - - - - - - - - - - - - localhost - 27018 - keycloak - true - 127.0.0.1 - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/model/pom.xml b/model/pom.xml index cc61acbff5..bfb162dcb6 100755 --- a/model/pom.xml +++ b/model/pom.xml @@ -29,15 +29,8 @@ invalidation-cache jpa mongo - tests - sessions-jpa sessions-mem sessions-mongo - - - - - diff --git a/model/tests/pom.xml b/model/tests/pom.xml deleted file mode 100755 index 4f6b938f65..0000000000 --- a/model/tests/pom.xml +++ /dev/null @@ -1,208 +0,0 @@ - - - - keycloak-parent - org.keycloak - 1.0-beta-4-SNAPSHOT - ../../pom.xml - - 4.0.0 - - keycloak-model-tests - Keycloak Model Tests - - - - - org.keycloak - keycloak-core - ${project.version} - compile - - - org.keycloak - keycloak-services - ${project.version} - compile - - - org.keycloak - keycloak-model-api - ${project.version} - compile - - - org.keycloak - keycloak-invalidation-cache-model - ${project.version} - test - - - org.keycloak - keycloak-authentication-api - ${project.version} - compile - - - org.keycloak - keycloak-authentication-model - ${project.version} - compile - - - org.keycloak - keycloak-authentication-picketlink - ${project.version} - compile - - - org.keycloak - keycloak-picketlink-api - ${project.version} - compile - - - org.keycloak - keycloak-picketlink-realm - ${project.version} - compile - - - org.keycloak - keycloak-timer-api - ${project.version} - compile - - - org.keycloak - keycloak-timer-basic - ${project.version} - compile - - - junit - junit - compile - - - org.easymock - easymock - compile - - - org.jboss.resteasy - resteasy-jaxrs - provided - - - log4j - log4j - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-simple - - - - - org.jboss.resteasy - jaxrs-api - provided - - - org.codehaus.jackson - jackson-core-asl - compile - - - org.codehaus.jackson - jackson-mapper-asl - compile - - - - - org.picketlink - picketlink-common - compile - - - org.picketlink - picketlink-idm-api - compile - - - org.picketlink - picketlink-idm-impl - compile - - - org.picketlink - picketlink-idm-simple-schema - compile - - - org.picketbox - picketbox-ldap - compile - - - org.picketbox - picketbox-ldap - compile - test-jar - - - org.slf4j - slf4j-api - compile - - - org.slf4j - slf4j-simple - compile - - - - - - - - org.apache.maven.plugins - maven-deploy-plugin - - true - - - - org.apache.maven.plugins - maven-jar-plugin - - - package-tests-jar - package - - test-jar - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - true - - - - - - - diff --git a/model/tests/src/test/resources/ldap/ldap-connection.properties b/model/tests/src/test/resources/ldap/ldap-connection.properties deleted file mode 100644 index 5ccc6ad815..0000000000 --- a/model/tests/src/test/resources/ldap/ldap-connection.properties +++ /dev/null @@ -1,9 +0,0 @@ -idm.test.ldap.connection.url=ldap\://localhost\:10389 -idm.test.ldap.base.dn=dc\=keycloak,dc\=org -idm.test.ldap.roles.dn.suffix=ou\=Roles,dc\=keycloak,dc\=org -idm.test.ldap.group.dn.suffix=ou\=Groups,dc\=keycloak,dc\=org -idm.test.ldap.user.dn.suffix=ou\=People,dc\=keycloak,dc\=org -idm.test.ldap.agent.dn.suffix=ou\=Agent,dc\=keycloak,dc\=org -idm.test.ldap.start.embedded.ldap.server=true -idm.test.ldap.bind.dn=uid\=admin,ou\=system -idm.test.ldap.bind.credential=secret \ No newline at end of file diff --git a/model/tests/src/test/resources/ldap/users.ldif b/model/tests/src/test/resources/ldap/users.ldif deleted file mode 100644 index cd6bac8240..0000000000 --- a/model/tests/src/test/resources/ldap/users.ldif +++ /dev/null @@ -1,30 +0,0 @@ -dn: dc=keycloak,dc=org -objectclass: dcObject -objectclass: organization -o: Keycloak -dc: Keycloak - -dn: ou=People,dc=keycloak,dc=org -objectclass: top -objectclass: organizationalUnit -ou: People - -dn: uid=johnkeycloak,ou=People,dc=keycloak,dc=org -objectclass: top -objectclass: uidObject -objectclass: person -objectclass: inetOrgPerson -uid: johnkeycloak -cn: John -sn: Doe -mail: john@email.org - -dn: ou=Roles,dc=keycloak,dc=org -objectclass: top -objectclass: organizationalUnit -ou: Roles - -dn: ou=Groups,dc=keycloak,dc=org -objectclass: top -objectclass: organizationalUnit -ou: Groups \ No newline at end of file diff --git a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java index 6b972ceaba..7df23f236a 100755 --- a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java +++ b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java @@ -301,16 +301,16 @@ public class AuthenticationManager { if (!session.users().validCredentials(realm, user, UserCredentialModel.totp(token))) { return AuthenticationStatus.INVALID_CREDENTIALS; } - } else { - logger.debug("validating password for user: " + username); + } - AuthProviderStatus authStatus = AuthenticationProviderManager.getManager(realm, session).validatePassword(user, password); - if (authStatus == AuthProviderStatus.INVALID_CREDENTIALS) { - logger.debug("invalid password for user: " + username); - return AuthenticationStatus.INVALID_CREDENTIALS; - } else if (authStatus == AuthProviderStatus.FAILED) { - return AuthenticationStatus.FAILED; - } + logger.debug("validating password for user: " + username); + + AuthProviderStatus authStatus = AuthenticationProviderManager.getManager(realm, session).validatePassword(user, password); + if (authStatus == AuthProviderStatus.INVALID_CREDENTIALS) { + logger.debug("invalid password for user: " + username); + return AuthenticationStatus.INVALID_CREDENTIALS; + } else if (authStatus == AuthProviderStatus.FAILED) { + return AuthenticationStatus.FAILED; } if (!user.getRequiredActions().isEmpty()) { diff --git a/testsuite/integration/pom.xml b/testsuite/integration/pom.xml index 3aa1ffdaeb..55fe1a005f 100755 --- a/testsuite/integration/pom.xml +++ b/testsuite/integration/pom.xml @@ -30,7 +30,19 @@ ${project.version} pom - + + log4j + log4j + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + org.jboss.spec.javax.servlet jboss-servlet-api_3.0_spec @@ -177,12 +189,16 @@ org.seleniumhq.selenium selenium-chrome-driver - - - org.keycloak - keycloak-model-tests - ${project.version} + org.picketbox + picketbox-ldap + compile + test-jar + + + org.picketbox + picketbox-ldap + compile @@ -256,14 +272,29 @@ + + jpa + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + jpa + jpa + jpa + jpa + + + + + + + mongo - - - keycloak.realm.provider - mongo - - localhost @@ -292,7 +323,7 @@ mongo mongo mongo - + mongo ${keycloak.connectionsMongo.host} ${keycloak.connectionsMongo.port} ${keycloak.connectionsMongo.db} diff --git a/testsuite/integration/src/main/resources/META-INF/keycloak-server.json b/testsuite/integration/src/main/resources/META-INF/keycloak-server.json index 9c23dad55c..8e1ef4c5f2 100755 --- a/testsuite/integration/src/main/resources/META-INF/keycloak-server.json +++ b/testsuite/integration/src/main/resources/META-INF/keycloak-server.json @@ -77,7 +77,7 @@ "host": "${keycloak.connectionsMongo.host:127.0.0.1}", "port": "${keycloak.connectionsMongo.port:27017}", "db": "${keycloak.connectionsMongo.db:keycloak}", - "clearOnStartup": "${keycloak.connectionsMongo.clearOnStartup:false}" + "clearOnStartup": "${keycloak.connectionsMongo.clearOnStartup:true}" } } } \ No newline at end of file diff --git a/model/tests/src/main/java/org/keycloak/model/test/LDAPEmbeddedServer.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/LDAPEmbeddedServer.java similarity index 99% rename from model/tests/src/main/java/org/keycloak/model/test/LDAPEmbeddedServer.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/LDAPEmbeddedServer.java index 4101191c40..e2955171c3 100755 --- a/model/tests/src/main/java/org/keycloak/model/test/LDAPEmbeddedServer.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/LDAPEmbeddedServer.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite; import org.keycloak.models.LDAPConstants; import org.keycloak.models.RealmModel; diff --git a/model/tests/src/main/java/org/keycloak/model/test/LDAPTestUtils.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/LDAPTestUtils.java similarity index 97% rename from model/tests/src/main/java/org/keycloak/model/test/LDAPTestUtils.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/LDAPTestUtils.java index 55f3d88495..b7cdeaf099 100644 --- a/model/tests/src/main/java/org/keycloak/model/test/LDAPTestUtils.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/LDAPTestUtils.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite; import org.keycloak.authentication.picketlink.PicketlinkAuthenticationProvider; import org.keycloak.models.KeycloakSession; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/audit/AuditProviderTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/audit/AuditProviderTest.java new file mode 100644 index 0000000000..6c0c538785 --- /dev/null +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/audit/AuditProviderTest.java @@ -0,0 +1,134 @@ +package org.keycloak.testsuite.audit; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.keycloak.audit.AuditProvider; +import org.keycloak.audit.Event; +import org.keycloak.audit.EventType; +import org.keycloak.models.KeycloakSession; +import org.keycloak.testsuite.rule.KeycloakRule; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Stian Thorgersen + */ +public class AuditProviderTest { + + @ClassRule + public static KeycloakRule kc = new KeycloakRule(); + + private KeycloakSession session; + + private AuditProvider audit; + + @Before + public void before() { + session = kc.startSession(); + audit = session.getProvider(AuditProvider.class); + } + + @After + public void after() { + audit.clear(); + kc.stopSession(session, true); + } + + @Test + public void save() { + audit.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + } + + @Test + public void query() { + long oldest = System.currentTimeMillis() - 30000; + long newest = System.currentTimeMillis() + 30000; + + audit.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId2", "127.0.0.1", "error")); + audit.onEvent(create(EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(oldest, EventType.LOGIN, "realmId", "clientId2", "userId", "127.0.0.1", "error")); + audit.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId2", "127.0.0.1", "error")); + + resetSession(); + + Assert.assertEquals(5, audit.createQuery().client("clientId").getResultList().size()); + Assert.assertEquals(5, audit.createQuery().realm("realmId").getResultList().size()); + Assert.assertEquals(4, audit.createQuery().event(EventType.LOGIN).getResultList().size()); + Assert.assertEquals(6, audit.createQuery().event(EventType.LOGIN, EventType.REGISTER).getResultList().size()); + Assert.assertEquals(4, audit.createQuery().user("userId").getResultList().size()); + + Assert.assertEquals(1, audit.createQuery().user("userId").event(EventType.REGISTER).getResultList().size()); + + Assert.assertEquals(2, audit.createQuery().maxResults(2).getResultList().size()); + Assert.assertEquals(1, audit.createQuery().firstResult(5).getResultList().size()); + + Assert.assertEquals(newest, audit.createQuery().maxResults(1).getResultList().get(0).getTime()); + Assert.assertEquals(oldest, audit.createQuery().firstResult(5).maxResults(1).getResultList().get(0).getTime()); + } + + @Test + public void clear() { + audit.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error")); + + resetSession(); + + audit.clear("realmId"); + + Assert.assertEquals(1, audit.createQuery().getResultList().size()); + } + + @Test + public void clearOld() { + audit.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error")); + audit.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error")); + + resetSession(); + + audit.clear("realmId", System.currentTimeMillis() - 10000); + + Assert.assertEquals(3, audit.createQuery().getResultList().size()); + } + + private Event create(EventType event, String realmId, String clientId, String userId, String ipAddress, String error) { + return create(System.currentTimeMillis(), event, realmId, clientId, userId, ipAddress, error); + } + + private Event create(long time, EventType event, String realmId, String clientId, String userId, String ipAddress, String error) { + Event e = new Event(); + e.setTime(time); + e.setEvent(event); + e.setRealmId(realmId); + e.setClientId(clientId); + e.setUserId(userId); + e.setIpAddress(ipAddress); + e.setError(error); + + Map details = new HashMap(); + details.put("key1", "value1"); + details.put("key2", "value2"); + + e.setDetails(details); + + return e; + } + + private void resetSession() { + kc.stopSession(session, true); + session = kc.startSession(); + audit = session.getProvider(AuditProvider.class); + } + +} diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/AuthProvidersIntegrationTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/AuthProvidersIntegrationTest.java index ede84bf74a..49ac620e44 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/AuthProvidersIntegrationTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/AuthProvidersIntegrationTest.java @@ -9,7 +9,7 @@ import org.junit.rules.RuleChain; import org.junit.rules.TestRule; import org.junit.runners.MethodSorters; import org.keycloak.OAuth2Constants; -import org.keycloak.model.test.LDAPTestUtils; +import org.keycloak.testsuite.LDAPTestUtils; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.PasswordPolicy; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java index a2d03050f5..56221f822c 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/forms/FederationProvidersIntegrationTest.java @@ -11,8 +11,8 @@ import org.junit.runners.MethodSorters; import org.keycloak.OAuth2Constants; import org.keycloak.authentication.AuthProviderConstants; import org.keycloak.federation.ldap.LDAPFederationProviderFactory; -import org.keycloak.model.test.LDAPEmbeddedServer; -import org.keycloak.model.test.LDAPTestUtils; +import org.keycloak.testsuite.LDAPEmbeddedServer; +import org.keycloak.testsuite.LDAPTestUtils; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.FederationProviderModel; import org.keycloak.models.KeycloakSession; @@ -36,7 +36,6 @@ import org.keycloak.testsuite.rule.WebRule; import org.openqa.selenium.WebDriver; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.Map; diff --git a/model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AbstractModelTest.java similarity index 78% rename from model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/AbstractModelTest.java index 0779fe54ef..db72c0d5c0 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AbstractModelTest.java @@ -1,20 +1,22 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; -import org.keycloak.models.RealmProvider; import org.keycloak.models.RealmModel; +import org.keycloak.models.RealmProvider; import org.keycloak.models.RoleModel; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.RealmManager; import org.keycloak.services.resources.KeycloakApplication; +import org.keycloak.testsuite.rule.KeycloakRule; import org.keycloak.util.JsonSerialization; import java.io.ByteArrayOutputStream; @@ -27,47 +29,27 @@ import java.util.Set; */ public class AbstractModelTest { - protected static KeycloakSessionFactory sessionFactory; + @ClassRule + public static KeycloakRule kc = new KeycloakRule(); protected KeycloakSession session; + protected RealmManager realmManager; protected RealmProvider model; - @BeforeClass - public static void beforeClass() { - sessionFactory = KeycloakApplication.createSessionFactory(); - - KeycloakSession session = sessionFactory.create(); - try { - session.getTransaction().begin(); - new ApplianceBootstrap().bootstrap(session, "/auth"); - session.getTransaction().commit(); - } finally { - session.close(); - } - } - - @AfterClass - public static void afterClass() { - sessionFactory.close(); - } - @Before public void before() throws Exception { - session = sessionFactory.create(); - session.getTransaction().begin(); + session = kc.startSession(); model = session.realms(); realmManager = new RealmManager(session); } @After public void after() throws Exception { - session.getTransaction().commit(); - session.close(); + kc.stopSession(session, true); - session = sessionFactory.create(); + session = kc.startSession(); try { - session.getTransaction().begin(); model = session.realms(); RealmManager rm = new RealmManager(session); @@ -76,12 +58,9 @@ public class AbstractModelTest { rm.removeRealm(realm); } } - - session.getTransaction().commit(); } finally { - session.close(); + kc.stopSession(session, true); } - } protected void commit() { @@ -101,11 +80,8 @@ public class AbstractModelTest { if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } - - session.close(); - - session = sessionFactory.create(); - session.getTransaction().begin(); + kc.stopSession(session, false); + session = kc.startSession(); model = session.realms(); realmManager = new RealmManager(session); } diff --git a/model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java similarity index 97% rename from model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java index ab40eb88d1..b72d5ec3cb 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AdapterTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.FixMethodOrder; diff --git a/model/tests/src/test/java/org/keycloak/model/test/ApplicationModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ApplicationModelTest.java similarity index 93% rename from model/tests/src/test/java/org/keycloak/model/test/ApplicationModelTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/ApplicationModelTest.java index 66e36e2db3..6a9cb02ca8 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/ApplicationModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ApplicationModelTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.Before; @@ -6,6 +6,7 @@ import org.junit.Test; import org.keycloak.models.ApplicationModel; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; +import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.idm.ApplicationRepresentation; @@ -59,6 +60,7 @@ public class ApplicationModelTest extends AbstractModelTest { @Test public void json() { ApplicationRepresentation representation = ModelToRepresentation.toRepresentation(application); + representation.setId(null); RealmModel realm = realmManager.createRealm("copy"); ApplicationModel copy = RepresentationToModel.createApplication(realm, representation, true); diff --git a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersConfigTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersConfigTest.java similarity index 96% rename from model/tests/src/test/java/org/keycloak/model/test/AuthProvidersConfigTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersConfigTest.java index 308c8f7d82..85441109c3 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersConfigTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersConfigTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.Test; @@ -20,7 +20,7 @@ public class AuthProvidersConfigTest extends AbstractModelTest { @Test public void testConfiguration() { // Create realm and add some providers and ldap config. Then commit - RealmModel realm = realmManager.createRealm("test"); + RealmModel realm = realmManager.createRealm("auth-providers-config-test"); Map ldapConfig = new HashMap(); ldapConfig.put("connectionUrl", "ldap://localhost:10389"); diff --git a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersExternalModelTest.java similarity index 99% rename from model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersExternalModelTest.java index 2d30404baf..0b4b972481 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersExternalModelTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.jboss.resteasy.specimpl.MultivaluedMapImpl; import org.jboss.resteasy.spi.ResteasyProviderFactory; diff --git a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersLDAPTest.java similarity index 98% rename from model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersLDAPTest.java index 0ebecd61ca..56bbb91b47 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthProvidersLDAPTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.AfterClass; import org.junit.Assert; @@ -10,6 +10,8 @@ import org.junit.runners.MethodSorters; import org.keycloak.authentication.AuthProviderConstants; import org.keycloak.authentication.AuthenticationProviderException; import org.keycloak.authentication.AuthenticationProviderManager; +import org.keycloak.testsuite.LDAPEmbeddedServer; +import org.keycloak.testsuite.LDAPTestUtils; import org.keycloak.models.AuthenticationLinkModel; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.RealmModel; @@ -35,8 +37,6 @@ public class AuthProvidersLDAPTest extends AbstractModelTest { @BeforeClass public static void beforeClass() { - AbstractModelTest.beforeClass(); - try { embeddedServer = new LDAPEmbeddedServer(); embeddedServer.setup(); @@ -48,8 +48,6 @@ public class AuthProvidersLDAPTest extends AbstractModelTest { @AfterClass public static void afterClass() { - AbstractModelTest.afterClass(); - try { embeddedServer.tearDown(); } catch (Exception e) { diff --git a/model/tests/src/test/java/org/keycloak/model/test/AuthenticationManagerTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthenticationManagerTest.java similarity index 91% rename from model/tests/src/test/java/org/keycloak/model/test/AuthenticationManagerTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthenticationManagerTest.java index a8491c3caf..583035687d 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AuthenticationManagerTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/AuthenticationManagerTest.java @@ -1,9 +1,11 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.jboss.resteasy.specimpl.MultivaluedMapImpl; +import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.RealmModel; @@ -99,7 +101,7 @@ public class AuthenticationManagerTest extends AbstractModelTest { @Test public void authFormWithTotp() { realm.addRequiredCredential(CredentialRepresentation.TOTP); - + String totpSecret = UUID.randomUUID().toString(); UserCredentialModel credential = new UserCredentialModel(); @@ -119,7 +121,7 @@ public class AuthenticationManagerTest extends AbstractModelTest { } @Test - public void authFormWithTotpInvalidPassword() { + public void authFormWithToltpInvalidPassword() { authFormWithTotp(); formData.remove(CredentialRepresentation.PASSWORD); @@ -154,17 +156,18 @@ public class AuthenticationManagerTest extends AbstractModelTest { @Override public void before() throws Exception { super.before(); - realm = realmManager.createRealm("Test"); + + realm = realmManager.createRealm("TestAuth"); realm.setAccessCodeLifespan(100); realm.setEnabled(true); - realm.setName("Test"); + realm.setName("TestAuth"); realm.setPrivateKeyPem("0234234"); realm.setPublicKeyPem("0234234"); realm.setAccessTokenLifespan(1000); realm.addRequiredCredential(CredentialRepresentation.PASSWORD); realm.setAuthenticationProviders(Arrays.asList(AuthenticationProviderModel.DEFAULT_PROVIDER)); - protector = new BruteForceProtector(sessionFactory); - protector.start(); + + protector = ResteasyProviderFactory.getContextData(BruteForceProtector.class); am = new AuthenticationManager(protector); user = realmManager.getSession().users().addUser(realm, "test"); @@ -183,12 +186,4 @@ public class AuthenticationManagerTest extends AbstractModelTest { otp = new TimeBasedOTP(); } - @After - public void after() throws Exception { - protector.shutdown(); - super.after(); - - - } - } diff --git a/model/tests/src/test/java/org/keycloak/model/test/CompositeRolesModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/CompositeRolesModelTest.java similarity index 90% rename from model/tests/src/test/java/org/keycloak/model/test/CompositeRolesModelTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/CompositeRolesModelTest.java index 768e9d02c0..25be765342 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/CompositeRolesModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/CompositeRolesModelTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.Before; @@ -23,8 +23,8 @@ public class CompositeRolesModelTest extends AbstractModelTest { public void before() throws Exception { super.before(); RealmManager manager = realmManager; - RealmRepresentation rep = AbstractModelTest.loadJson("testcomposites.json"); - RealmModel realm = manager.createRealm("Test", rep.getRealm()); + RealmRepresentation rep = AbstractModelTest.loadJson("model/testcomposites.json"); + RealmModel realm = manager.createRealm("TestComposites", rep.getRealm()); manager.importRealm(rep, realm); } @@ -56,7 +56,7 @@ public class CompositeRolesModelTest extends AbstractModelTest { private Set getRequestedRoles(String applicationName, String username) { Set requestedRoles = new HashSet(); - RealmModel realm = realmManager.getRealm("Test"); + RealmModel realm = realmManager.getRealm("TestComposites"); UserModel user = realmManager.getSession().users().getUserByUsername(username, realm); ApplicationModel application = realm.getApplicationByName(applicationName); @@ -91,7 +91,7 @@ public class CompositeRolesModelTest extends AbstractModelTest { } private RoleModel getRole(String appName, String roleName) { - RealmModel realm = realmManager.getRealm("Test"); + RealmModel realm = realmManager.getRealm("TestComposites"); if ("realm".equals(appName)) { return realm.getRole(roleName); } else { diff --git a/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java similarity index 95% rename from model/tests/src/test/java/org/keycloak/model/test/ImportTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java index c0131557c9..f512c761d2 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ImportTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.FixMethodOrder; @@ -33,7 +33,7 @@ public class ImportTest extends AbstractModelTest { @Test public void demoDelete() throws Exception { // was having trouble deleting this realm from admin console - RealmRepresentation rep = AbstractModelTest.loadJson("testrealm2.json"); + RealmRepresentation rep = AbstractModelTest.loadJson("model/testrealm2.json"); RealmModel realm = realmManager.importRealm(rep); commit(); realm = realmManager.getRealmByName("demo-delete"); @@ -42,7 +42,7 @@ public class ImportTest extends AbstractModelTest { @Test public void install() throws Exception { - RealmRepresentation rep = AbstractModelTest.loadJson("testrealm.json"); + RealmRepresentation rep = AbstractModelTest.loadJson("model/testrealm.json"); RealmModel realm = realmManager.createRealm("demo", rep.getRealm()); realmManager.importRealm(rep, realm); @@ -228,7 +228,7 @@ public class ImportTest extends AbstractModelTest { @Test public void install2() throws Exception { RealmManager manager = realmManager; - RealmRepresentation rep = AbstractModelTest.loadJson("testrealm-demo.json"); + RealmRepresentation rep = AbstractModelTest.loadJson("model/testrealm-demo.json"); RealmModel realm = manager.createRealm("demo", rep.getRealm()); manager.importRealm(rep, realm); diff --git a/model/tests/src/test/java/org/keycloak/model/test/ModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ModelTest.java similarity index 96% rename from model/tests/src/test/java/org/keycloak/model/test/ModelTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/ModelTest.java index 14a315ef53..56d9afe351 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/ModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/ModelTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.Test; diff --git a/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/MultipleRealmsTest.java similarity index 74% rename from model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/MultipleRealmsTest.java index 73cc6ad121..4c60766b7d 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/MultipleRealmsTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.Before; @@ -9,7 +9,6 @@ import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserCredentialModel; import org.keycloak.models.UserModel; -import org.keycloak.models.UserProvider; /** * @author Marek Posolda @@ -32,9 +31,8 @@ public class MultipleRealmsTest extends AbstractModelTest { @Test public void testUsers() { - UserProvider userProvider = realmManager.getSession().users(); - UserModel r1user1 = userProvider.getUserByUsername("user1", realm1); - UserModel r2user1 = userProvider.getUserByUsername("user1", realm2); + UserModel r1user1 = session.users().getUserByUsername("user1", realm1); + UserModel r2user1 = session.users().getUserByUsername("user1", realm2); Assert.assertEquals(r1user1.getUsername(), r2user1.getUsername()); Assert.assertNotEquals(r1user1.getId(), r2user1.getId()); @@ -42,22 +40,22 @@ public class MultipleRealmsTest extends AbstractModelTest { r1user1.updateCredential(UserCredentialModel.password("pass1")); r2user1.updateCredential(UserCredentialModel.password("pass2")); - Assert.assertTrue(userProvider.validCredentials(realm1, r1user1, UserCredentialModel.password("pass1"))); - Assert.assertFalse(userProvider.validCredentials(realm1, r1user1, UserCredentialModel.password("pass2"))); - Assert.assertFalse(userProvider.validCredentials(realm2, r2user1, UserCredentialModel.password("pass1"))); - Assert.assertTrue(userProvider.validCredentials(realm2, r2user1, UserCredentialModel.password("pass2"))); + Assert.assertTrue(session.users().validCredentials(realm1, r1user1, UserCredentialModel.password("pass1"))); + Assert.assertFalse(session.users().validCredentials(realm1, r1user1, UserCredentialModel.password("pass2"))); + Assert.assertFalse(session.users().validCredentials(realm2, r2user1, UserCredentialModel.password("pass1"))); + Assert.assertTrue(session.users().validCredentials(realm2, r2user1, UserCredentialModel.password("pass2"))); // Test searching - Assert.assertEquals(2, userProvider.searchForUser("user", realm1).size()); + Assert.assertEquals(2, session.users().searchForUser("user", realm1).size()); commit(); realm1 = model.getRealm("id1"); realm2 = model.getRealm("id2"); - userProvider.removeUser(realm1, "user1"); - userProvider.removeUser(realm1, "user2"); - Assert.assertEquals(0, userProvider.searchForUser("user", realm1).size()); - Assert.assertEquals(2, userProvider.searchForUser("user", realm2).size()); + session.users().removeUser(realm1, "user1"); + session.users().removeUser(realm1, "user2"); + Assert.assertEquals(0, session.users().searchForUser("user", realm1).size()); + Assert.assertEquals(2, session.users().searchForUser("user", realm2).size()); } @Test @@ -76,8 +74,8 @@ public class MultipleRealmsTest extends AbstractModelTest { Assert.assertNull(realm2.getApplicationById(r1app1.getId())); OAuthClientModel r2cl1 = realm2.getOAuthClient("cl1"); - Assert.assertNull(realm1.getOAuthClientById(r2cl1.getId())); Assert.assertEquals(r2cl1.getId(), realm2.getOAuthClientById(r2cl1.getId()).getId()); + Assert.assertNull(realm1.getOAuthClientById(r2cl1.getId())); RoleModel r1App1Role = r1app1.getRole("app1Role1"); Assert.assertEquals(r1App1Role, realm1.getRoleById(r1App1Role.getId())); diff --git a/model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java similarity index 88% rename from model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java rename to testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java index 15ce4235fb..3adce35793 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/model/UserModelTest.java @@ -1,4 +1,4 @@ -package org.keycloak.model.test; +package org.keycloak.testsuite.model; import org.junit.Assert; import org.junit.Test; @@ -75,12 +75,11 @@ public class UserModelTest extends AbstractModelTest { @Test public void testUserRequiredActions() throws Exception { RealmModel realm = realmManager.createRealm("original"); - KeycloakSession session = realmManager.getSession(); UserModel user = session.users().addUser(realm, "user"); Assert.assertTrue(user.getRequiredActions().isEmpty()); - user.addRequiredAction(UserModel.RequiredAction.CONFIGURE_TOTP); + user.addRequiredAction(RequiredAction.CONFIGURE_TOTP); String id = realm.getId(); commit(); realm = realmManager.getRealm(id); @@ -89,26 +88,26 @@ public class UserModelTest extends AbstractModelTest { Assert.assertEquals(1, user.getRequiredActions().size()); Assert.assertTrue(user.getRequiredActions().contains(RequiredAction.CONFIGURE_TOTP)); - user.addRequiredAction(UserModel.RequiredAction.CONFIGURE_TOTP); + user.addRequiredAction(RequiredAction.CONFIGURE_TOTP); user = session.users().getUserByUsername("user", realm); Assert.assertEquals(1, user.getRequiredActions().size()); Assert.assertTrue(user.getRequiredActions().contains(RequiredAction.CONFIGURE_TOTP)); - user.addRequiredAction(UserModel.RequiredAction.VERIFY_EMAIL); + user.addRequiredAction(RequiredAction.VERIFY_EMAIL); user = session.users().getUserByUsername("user", realm); Assert.assertEquals(2, user.getRequiredActions().size()); Assert.assertTrue(user.getRequiredActions().contains(RequiredAction.CONFIGURE_TOTP)); Assert.assertTrue(user.getRequiredActions().contains(RequiredAction.VERIFY_EMAIL)); - user.removeRequiredAction(UserModel.RequiredAction.CONFIGURE_TOTP); + user.removeRequiredAction(RequiredAction.CONFIGURE_TOTP); user = session.users().getUserByUsername("user", realm); Assert.assertEquals(1, user.getRequiredActions().size()); Assert.assertTrue(user.getRequiredActions().contains(RequiredAction.VERIFY_EMAIL)); - user.removeRequiredAction(UserModel.RequiredAction.VERIFY_EMAIL); + user.removeRequiredAction(RequiredAction.VERIFY_EMAIL); user = session.users().getUserByUsername("user", realm); Assert.assertTrue(user.getRequiredActions().isEmpty()); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java index 6ff74367eb..da334fa172 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java @@ -8,6 +8,7 @@ import io.undertow.servlet.api.WebResourceCollection; import org.junit.rules.ExternalResource; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; import org.keycloak.models.utils.ModelToRepresentation; diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/LDAPRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/LDAPRule.java index 3409b0ea84..b2b4aae1ef 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/LDAPRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/LDAPRule.java @@ -1,7 +1,7 @@ package org.keycloak.testsuite.rule; import org.junit.rules.ExternalResource; -import org.keycloak.model.test.LDAPEmbeddedServer; +import org.keycloak.testsuite.LDAPEmbeddedServer; /** * @author Marek Posolda diff --git a/model/tests/src/test/resources/testcomposites.json b/testsuite/integration/src/test/resources/model/testcomposites.json similarity index 95% rename from model/tests/src/test/resources/testcomposites.json rename to testsuite/integration/src/test/resources/model/testcomposites.json index 4a851b514f..ce128c1617 100755 --- a/model/tests/src/test/resources/testcomposites.json +++ b/testsuite/integration/src/test/resources/model/testcomposites.json @@ -1,6 +1,6 @@ { - "id": "Test", - "realm": "Test", + "id": "TestComposites", + "realm": "TestComposites", "enabled": true, "accessTokenLifespan": 600, "accessCodeLifespan": 600, diff --git a/model/tests/src/test/resources/testrealm-demo.json b/testsuite/integration/src/test/resources/model/testrealm-demo.json similarity index 100% rename from model/tests/src/test/resources/testrealm-demo.json rename to testsuite/integration/src/test/resources/model/testrealm-demo.json diff --git a/model/tests/src/test/resources/testrealm.json b/testsuite/integration/src/test/resources/model/testrealm.json similarity index 100% rename from model/tests/src/test/resources/testrealm.json rename to testsuite/integration/src/test/resources/model/testrealm.json diff --git a/model/tests/src/test/resources/testrealm2.json b/testsuite/integration/src/test/resources/model/testrealm2.json similarity index 100% rename from model/tests/src/test/resources/testrealm2.json rename to testsuite/integration/src/test/resources/model/testrealm2.json