Merge pull request #554 from stianst/master

Moved audit/tests and model/tests to testsuite
This commit is contained in:
Stian Thorgersen 2014-07-24 13:50:15 +01:00
commit 8a54f133ba
51 changed files with 304 additions and 909 deletions

View file

@ -37,12 +37,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-audit-tests</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
@ -64,16 +58,5 @@
<artifactId>jackson-core-asl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View file

@ -1,17 +0,0 @@
package org.keycloak.audit.jpa;
import org.junit.Ignore;
import org.keycloak.audit.tests.AbstractAuditProviderTest;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@Ignore
public class JpaAuditProviderTest extends AbstractAuditProviderTest {
@Override
protected String getProviderId() {
return JpaAuditProviderFactory.ID;
}
}

View file

@ -37,12 +37,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-audit-tests</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
@ -53,90 +47,5 @@
<artifactId>mongo-java-driver</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<keycloak.audit.mongo.host>localhost</keycloak.audit.mongo.host>
<keycloak.audit.mongo.port>27018</keycloak.audit.mongo.port>
<keycloak.audit.mongo.db>keycloak</keycloak.audit.mongo.db>
<keycloak.audit.mongo.clearOnStartup>true</keycloak.audit.mongo.clearOnStartup>
<keycloak.audit.mongo.bindIp>127.0.0.1</keycloak.audit.mongo.bindIp>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Postpone tests to "integration-test" phase, so that we can bootstrap embedded mongo on 27018 before running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<systemPropertyVariables>
<keycloak.audit.mongo.host>${keycloak.audit.mongo.host}</keycloak.audit.mongo.host>
<keycloak.audit.mongo.port>${keycloak.audit.mongo.port}</keycloak.audit.mongo.port>
<keycloak.audit.mongo.db>${keycloak.audit.mongo.db}</keycloak.audit.mongo.db>
<keycloak.audit.mongo.clearOnStartup>${keycloak.audit.mongo.clearOnStartup}</keycloak.audit.mongo.clearOnStartup>
<keycloak.audit.mongo.bindIp>${keycloak.model.mongo.bindIp}</keycloak.audit.mongo.bindIp>
</systemPropertyVariables>
</configuration>
</execution>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
<!-- Embedded mongo -->
<plugin>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin</artifactId>
<executions>
<execution>
<id>start-mongodb</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<port>${keycloak.audit.mongo.port}</port>
<logging>file</logging>
<logFile>${project.build.directory}/mongodb.log</logFile>
<bindIp>${keycloak.audit.mongo.bindIp}</bindIp>
</configuration>
</execution>
<execution>
<id>stop-mongodb</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,17 +0,0 @@
package org.keycloak.audit.mongo;
import org.junit.Ignore;
import org.keycloak.audit.tests.AbstractAuditProviderTest;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@Ignore
public class MongoAuditProviderTest extends AbstractAuditProviderTest {
@Override
protected String getProviderId() {
return MongoAuditProviderFactory.ID;
}
}

View file

@ -21,6 +21,5 @@
<module>jpa</module>
<module>jboss-logging</module>
<module>mongo</module>
<module>tests</module>
</modules>
</project>

View file

@ -1,53 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>keycloak-audit-parent</artifactId>
<groupId>org.keycloak</groupId>
<version>1.0-beta-4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>keycloak-audit-tests</artifactId>
<name>Keycloak Audit Tests</name>
<description/>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-audit-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -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 <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public abstract class AbstractAuditProviderTest {
private ProviderFactory<AuditProvider> factory;
private AuditProvider provider;
@Before
public void before() {
String providerId = getProviderId();
ServiceLoader<AuditProviderFactory> 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<String, String> details = new HashMap<String, String>();
details.put("key1", "value1");
details.put("key2", "value2");
e.setDetails(details);
return e;
}
}

View file

@ -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);

View file

@ -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<T> implements Mapper<T, BasicDBObject> {
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);

View file

@ -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;

View file

@ -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

View file

@ -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));
}

View file

@ -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;
}

View file

@ -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<String> redirectUris = new HashSet<String>();
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;
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<String> composites = new HashSet<String>();
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;
}

View file

@ -83,26 +83,6 @@
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>org.keycloak</groupId>-->
<!--<artifactId>keycloak-model-tests</artifactId>-->
<!--<version>${project.version}</version>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.keycloak</groupId>-->
<!--<artifactId>keycloak-model-tests</artifactId>-->
<!--<version>${project.version}</version>-->
<!--<classifier>tests</classifier>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
@ -114,37 +94,6 @@
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<!-- Test jar used in export-import -->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-jar-plugin</artifactId>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>package-tests-jar</id>-->
<!--<phase>package</phase>-->
<!--<goals>-->
<!--<goal>test-jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-surefire-plugin</artifactId>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>default-test</id>-->
<!--<configuration>-->
<!--<dependenciesToScan>-->
<!--<dependency>org.keycloak:keycloak-model-tests</dependency>-->
<!--</dependenciesToScan>-->
<!--</configuration>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
</build>

View file

@ -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")

View file

@ -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")

View file

@ -43,12 +43,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-invalidation-cache-model</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
@ -59,128 +53,5 @@
<artifactId>mongo-java-driver</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-sessions-mem</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-tests</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>org.keycloak</groupId>-->
<!--<artifactId>keycloak-model-tests</artifactId>-->
<!--<version>${project.version}</version>-->
<!--<classifier>tests</classifier>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
</dependencies>
<properties>
<keycloak.model.mongo.host>localhost</keycloak.model.mongo.host>
<keycloak.model.mongo.port>27018</keycloak.model.mongo.port>
<keycloak.model.mongo.db>keycloak</keycloak.model.mongo.db>
<keycloak.model.mongo.clearOnStartup>true</keycloak.model.mongo.clearOnStartup>
<keycloak.model.mongo.bindIp>127.0.0.1</keycloak.model.mongo.bindIp>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<!-- Postpone tests to "integration-test" phase, so that we can bootstrap embedded mongo on 27018 before running tests -->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-surefire-plugin</artifactId>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>test</id>-->
<!--<phase>integration-test</phase>-->
<!--<goals>-->
<!--<goal>test</goal>-->
<!--</goals>-->
<!--<configuration>-->
<!--<systemPropertyVariables>-->
<!--<keycloak.model.mongo.host>${keycloak.model.mongo.host}</keycloak.model.mongo.host>-->
<!--<keycloak.model.mongo.port>${keycloak.model.mongo.port}</keycloak.model.mongo.port>-->
<!--<keycloak.model.mongo.db>${keycloak.model.mongo.db}</keycloak.model.mongo.db>-->
<!--<keycloak.model.mongo.clearOnStartup>${keycloak.model.mongo.clearOnStartup}</keycloak.model.mongo.clearOnStartup>-->
<!--<keycloak.model.mongo.bindIp>${keycloak.model.mongo.bindIp}</keycloak.model.mongo.bindIp>-->
<!--</systemPropertyVariables>-->
<!--<dependenciesToScan>-->
<!--<dependency>org.keycloak:keycloak-model-tests</dependency>-->
<!--</dependenciesToScan>-->
<!--</configuration>-->
<!--</execution>-->
<!--<execution>-->
<!--<id>default-test</id>-->
<!--<configuration>-->
<!--<skip>true</skip>-->
<!--</configuration>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<!-- Embedded mongo -->
<!--<plugin>-->
<!--<groupId>com.github.joelittlejohn.embedmongo</groupId>-->
<!--<artifactId>embedmongo-maven-plugin</artifactId>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>start-mongodb</id>-->
<!--<phase>pre-integration-test</phase>-->
<!--<goals>-->
<!--<goal>start</goal>-->
<!--</goals>-->
<!--<configuration>-->
<!--<port>${keycloak.model.mongo.port}</port>-->
<!--<logging>file</logging>-->
<!--<logFile>${project.build.directory}/mongodb.log</logFile>-->
<!--<bindIp>${keycloak.model.mongo.bindIp}</bindIp>-->
<!--</configuration>-->
<!--</execution>-->
<!--<execution>-->
<!--<id>stop-mongodb</id>-->
<!--<phase>post-integration-test</phase>-->
<!--<goals>-->
<!--<goal>stop</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
</build>
</project>

View file

@ -29,15 +29,8 @@
<module>invalidation-cache</module>
<module>jpa</module>
<module>mongo</module>
<module>tests</module>
<module>sessions-jpa</module>
<module>sessions-mem</module>
<module>sessions-mongo</module>
<!--<module>hybrid</module>-->
<!--<module>realms-jpa</module>-->
<!--<module>users-jpa</module>-->
<!--<module>tests-hybrid</module>-->
</modules>
</project>

View file

@ -1,208 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>keycloak-parent</artifactId>
<groupId>org.keycloak</groupId>
<version>1.0-beta-4-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>keycloak-model-tests</artifactId>
<name>Keycloak Model Tests</name>
<description/>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-invalidation-cache-model</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authentication-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authentication-model</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authentication-picketlink</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-picketlink-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-picketlink-realm</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-timer-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-timer-basic</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>compile</scope>
</dependency>
<!-- picketlink dependencies -->
<dependency>
<groupId>org.picketlink</groupId>
<artifactId>picketlink-common</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.picketlink</groupId>
<artifactId>picketlink-idm-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.picketlink</groupId>
<artifactId>picketlink-idm-impl</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.picketlink</groupId>
<artifactId>picketlink-idm-simple-schema</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.picketbox</groupId>
<artifactId>picketbox-ldap</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.picketbox</groupId>
<artifactId>picketbox-ldap</artifactId>
<scope>compile</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>package-tests-jar</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -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

View file

@ -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

View file

@ -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()) {

View file

@ -30,7 +30,19 @@
<version>${project.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
@ -177,12 +189,16 @@
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
</dependency>
<!-- This adds couple of other dependencies (like picketlink) -->
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-tests</artifactId>
<version>${project.version}</version>
<groupId>org.picketbox</groupId>
<artifactId>picketbox-ldap</artifactId>
<scope>compile</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.picketbox</groupId>
<artifactId>picketbox-ldap</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@ -256,14 +272,29 @@
</build>
</profile>
<profile>
<id>jpa</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<keycloak.realm.provider>jpa</keycloak.realm.provider>
<keycloak.user.provider>jpa</keycloak.user.provider>
<keycloak.audit.provider>jpa</keycloak.audit.provider>
<keycloak.userSessions.provider>jpa</keycloak.userSessions.provider>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>mongo</id>
<activation>
<property>
<name>keycloak.realm.provider</name>
<value>mongo</value>
</property>
</activation>
<properties>
<keycloak.connectionsMongo.host>localhost</keycloak.connectionsMongo.host>
@ -292,7 +323,7 @@
<keycloak.realm.provider>mongo</keycloak.realm.provider>
<keycloak.user.provider>mongo</keycloak.user.provider>
<keycloak.audit.provider>mongo</keycloak.audit.provider>
<!--<keycloak.userSessions.provider>mongo</keycloak.userSessions.provider>-->
<keycloak.userSessions.provider>mongo</keycloak.userSessions.provider>
<keycloak.connectionsMongo.host>${keycloak.connectionsMongo.host}</keycloak.connectionsMongo.host>
<keycloak.connectionsMongo.port>${keycloak.connectionsMongo.port}</keycloak.connectionsMongo.port>
<keycloak.connectionsMongo.db>${keycloak.connectionsMongo.db}</keycloak.connectionsMongo.db>

View file

@ -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}"
}
}
}

View file

@ -1,4 +1,4 @@
package org.keycloak.model.test;
package org.keycloak.testsuite;
import org.keycloak.models.LDAPConstants;
import org.keycloak.models.RealmModel;

View file

@ -1,4 +1,4 @@
package org.keycloak.model.test;
package org.keycloak.testsuite;
import org.keycloak.authentication.picketlink.PicketlinkAuthenticationProvider;
import org.keycloak.models.KeycloakSession;

View file

@ -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 <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
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<String, String> details = new HashMap<String, String>();
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);
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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);
}

View file

@ -1,4 +1,4 @@
package org.keycloak.model.test;
package org.keycloak.testsuite.model;
import org.junit.Assert;
import org.junit.FixMethodOrder;

View file

@ -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);

View file

@ -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<String, String> ldapConfig = new HashMap<String,String>();
ldapConfig.put("connectionUrl", "ldap://localhost:10389");

View file

@ -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;

View file

@ -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) {

View file

@ -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();
}
}

View file

@ -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<RoleModel> getRequestedRoles(String applicationName, String username) {
Set<RoleModel> requestedRoles = new HashSet<RoleModel>();
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 {

View file

@ -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);

View file

@ -1,4 +1,4 @@
package org.keycloak.model.test;
package org.keycloak.testsuite.model;
import org.junit.Assert;
import org.junit.Test;

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
@ -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()));

View file

@ -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());

View file

@ -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;

View file

@ -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 <a href="mailto:mposolda@redhat.com">Marek Posolda</a>

View file

@ -1,6 +1,6 @@
{
"id": "Test",
"realm": "Test",
"id": "TestComposites",
"realm": "TestComposites",
"enabled": true,
"accessTokenLifespan": 600,
"accessCodeLifespan": 600,