user fed tests

This commit is contained in:
Bill Burke 2016-07-20 17:26:00 -04:00
parent 8b535c5da6
commit 2a05f00d0c
7 changed files with 87 additions and 16 deletions

View file

@ -118,11 +118,6 @@ public class UserStorageManager implements UserProvider {
return localStorage().addUser(realm, username.toLowerCase()); return localStorage().addUser(realm, username.toLowerCase());
} }
public StorageProvider getStorageProvider(StorageProviderModel model) {
StorageProviderFactory factory = (StorageProviderFactory)session.getKeycloakSessionFactory().getProviderFactory(StorageProvider.class, model.getProviderName());
return factory.getInstance(session, model);
}
public StorageProvider getStorageProvider(RealmModel realm, String providerId) { public StorageProvider getStorageProvider(RealmModel realm, String providerId) {
StorageProviderModel model = realm.getStorageProvider(providerId); StorageProviderModel model = realm.getStorageProvider(providerId);
if (model == null) return null; if (model == null) return null;

View file

@ -21,7 +21,13 @@ import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.keycloak.OAuth2Constants; import org.keycloak.OAuth2Constants;
import org.keycloak.models.GroupModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.cache.infinispan.UserAdapter;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
import org.keycloak.storage.StorageProviderModel; import org.keycloak.storage.StorageProviderModel;
import org.keycloak.testsuite.OAuthClient; import org.keycloak.testsuite.OAuthClient;
@ -32,6 +38,8 @@ import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule; import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import java.util.Set;
/** /**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
@ -43,9 +51,17 @@ public class UserFederationStorageTest {
@Override @Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
StorageProviderModel model = new StorageProviderModel(); StorageProviderModel model = new StorageProviderModel();
model.setDisplayName("user-props"); model.setDisplayName("read-only-user-props");
model.setPriority(1); model.setPriority(1);
model.setProviderName(UserPropertyFileStorageFactory.PROVIDER_ID); model.setProviderName(UserPropertyFileStorageFactory.PROVIDER_ID);
model.getConfig().put("property.file", "/storage-test/read-only-user-password.properties");
appRealm.addStorageProvider(model);
model = new StorageProviderModel();
model.setDisplayName("user-props");
model.setPriority(2);
model.setProviderName(UserPropertyFileStorageFactory.PROVIDER_ID);
model.getConfig().put("property.file", "/storage-test/user-password.properties");
model.getConfig().put("USER_FEDERATED_STORAGE", "true");
appRealm.addStorageProvider(model); appRealm.addStorageProvider(model);
} }
}); });
@ -82,7 +98,52 @@ public class UserFederationStorageTest {
@Test @Test
public void testLoginSuccess() { public void testLoginSuccess() {
loginSuccessAndLogout("tbrady", "goat"); loginSuccessAndLogout("tbrady", "goat");
loginSuccessAndLogout("thor", "hammer");
loginBadPassword("tbrady"); loginBadPassword("tbrady");
} }
@Test
public void testUpdate() {
KeycloakSession session = keycloakRule.startSession();
RealmModel realm = session.realms().getRealmByName("test");
UserModel thor = session.users().getUserByUsername("thor", realm);
thor.setFirstName("Stian");
thor.setLastName("Thorgersen");
thor.setEmailVerified(true);
long thorCreated = System.currentTimeMillis() - 100;
thor.setCreatedTimestamp(thorCreated);
thor.setEmail("thor@hammer.com");
thor.setSingleAttribute("test-attribute", "value");
RoleModel role = realm.addRole("foo-role");
thor.grantRole(role);
GroupModel group = realm.createGroup("my-group");
thor.joinGroup(group);
thor.addRequiredAction("POOP");
keycloakRule.stopSession(session, true);
session = keycloakRule.startSession();
realm = session.realms().getRealmByName("test");
thor = session.users().getUserByUsername("thor", realm);
Assert.assertEquals("Stian", thor.getFirstName());
Assert.assertEquals("Thorgersen", thor.getLastName());
Assert.assertEquals("thor@hammer.com", thor.getEmail());
Assert.assertEquals("value", thor.getFirstAttribute("test-attribute"));
Assert.assertTrue(thor.isEmailVerified());
Assert.assertTrue(thor instanceof UserAdapter);
Set<RoleModel> roles = thor.getRoleMappings();
System.out.println("num roles " + roles.size());
Assert.assertTrue(roles.size() > 1);
role = realm.getRole("foo-role");
Assert.assertTrue(thor.hasRole(role));
Set<GroupModel> groups = thor.getGroups();
Assert.assertEquals("my-group", groups.iterator().next().getName());
System.out.println("num groups " + groups.size());
Assert.assertTrue(thor.getRequiredActions().iterator().next().equals("POOP"));
thor.removeRequiredAction("POOP");
thor.updateCredential(UserCredentialModel.password("lightning"));
keycloakRule.stopSession(session, true);
loginSuccessAndLogout("thor", "lightning");
}
} }

View file

@ -62,13 +62,27 @@ public class UserPropertyFileStorage implements UserLookupProvider, StorageProvi
return createUser(realm, username); return createUser(realm, username);
} }
private UserModel createUser(final RealmModel realm, final String username) { private UserModel createUser(RealmModel realm, String username) {
return new AbstractUserAdapter(session, realm, model) { if (federatedStorageEnabled) {
@Override return new AbstractUserAdapterFederatedStorage(session, realm, model) {
public String getUsername() { @Override
return username; public String getUsername() {
} return username;
}; }
@Override
public void setUsername(String username) {
throw new RuntimeException("Unsupported");
}
};
} else {
return new AbstractUserAdapter(session, realm, model) {
@Override
public String getUsername() {
return username;
}
};
}
} }
@Override @Override

View file

@ -48,7 +48,7 @@ public class UserPropertyFileStorageFactory implements StorageProviderFactory {
public StorageProvider getInstance(KeycloakSession session, StorageProviderModel model) { public StorageProvider getInstance(KeycloakSession session, StorageProviderModel model) {
Properties props = new Properties(); Properties props = new Properties();
try { try {
props.load(getClass().getResourceAsStream("/storage-test/user-password.properties")); props.load(getClass().getResourceAsStream(model.getConfig().get("property.file")));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View file

@ -21,7 +21,7 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %t [%c] %m%n log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %t [%c] %m%n
log4j.logger.org.keycloak=info log4j.logger.org.keycloak=debug
# Enable to view events # Enable to view events