Added new unit tests. Change get***ById() to find just entities from this realm/application

This commit is contained in:
mposolda
2014-02-21 19:11:38 +01:00
parent e85c2c9826
commit 8a4ef40e9e
10 changed files with 244 additions and 29 deletions

View File

@@ -3,12 +3,15 @@ package org.keycloak.model.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import org.jboss.resteasy.logging.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.RoleModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.KeycloakApplication;
@@ -60,4 +63,33 @@ public class AbstractModelTest {
return JsonSerialization.readValue(bytes, RealmRepresentation.class);
}
// Helper methods for role equality
public static void assertRolesEquals(Set<RoleModel> expected, Set<RoleModel> actual) {
Assert.assertEquals(expected.size(), actual.size());
for (RoleModel current : actual) {
assertRolesContains(current, expected);
}
}
public static void assertRolesContains(RoleModel expected, Set<RoleModel> actual) {
for (RoleModel current : actual) {
if (current.getId().equals(expected.getId())) {
assertRolesEquals(current, expected);
return;
}
}
Assert.fail("Role with id=" + expected.getId() + " name=" + expected.getName() + " not in set " + actual);
}
public static void assertRolesEquals(RoleModel expected, RoleModel actual) {
Assert.assertEquals(expected.getId(), actual.getId());
Assert.assertEquals(expected.getName(), actual.getName());
Assert.assertEquals(expected.getDescription(), actual.getDescription());
Assert.assertEquals(expected.getContainer(), actual.getContainer());
Assert.assertEquals(expected.getComposites().size(), actual.getComposites().size());
}
}

View File

@@ -430,13 +430,60 @@ public class AdapterTest extends AbstractModelTest {
Set<RoleModel> roles = realmModel.getRoles();
Assert.assertEquals(5, roles.size());
UserModel user = realmModel.addUser("bburke");
RoleModel role = realmModel.getRole("user");
realmModel.grantRole(user, role);
Assert.assertTrue(realmModel.hasRole(user, role));
System.out.println("Role id: " + role.getId());
role = realmModel.getRoleById(role.getId());
Assert.assertNotNull(role);
Assert.assertEquals("user", role.getName());
RoleModel realmUserRole = realmModel.getRole("user");
realmModel.grantRole(user, realmUserRole);
Assert.assertTrue(realmModel.hasRole(user, realmUserRole));
RoleModel found = realmModel.getRoleById(realmUserRole.getId());
Assert.assertNotNull(found);
assertRolesEquals(found, realmUserRole);
// Test app roles
ApplicationModel application = realmModel.addApplication("app1");
application.addRole("user");
application.addRole("bar");
Set<RoleModel> appRoles = application.getRoles();
Assert.assertEquals(2, appRoles.size());
RoleModel appBarRole = application.getRole("bar");
// This should return null because it's realmRole
Assert.assertNull(application.getRoleById(realmUserRole.getId()));
// This should return null because appBarRole is application role
Assert.assertNull(realmModel.getRoleById(appBarRole.getId()));
found = application.getRoleById(appBarRole.getId());
Assert.assertNotNull(found);
assertRolesEquals(found, appBarRole);
realmModel.grantRole(user, appBarRole);
realmModel.grantRole(user, application.getRole("user"));
roles = realmModel.getRealmRoleMappings(user);
Assert.assertEquals(roles.size(), 2);
assertRolesContains(realmUserRole, roles);
Assert.assertTrue(realmModel.hasRole(user, realmUserRole));
// Role "foo" is default realm role
Assert.assertTrue(realmModel.hasRole(user, realmModel.getRole("foo")));
roles = application.getApplicationRoleMappings(user);
Assert.assertEquals(roles.size(), 2);
assertRolesContains(application.getRole("user"), roles);
assertRolesContains(appBarRole, roles);
Assert.assertTrue(realmModel.hasRole(user, appBarRole));
// Test that application role 'user' don't clash with realm role 'user'
Assert.assertNotEquals(realmModel.getRole("user").getId(), application.getRole("user").getId());
Assert.assertEquals(6, realmModel.getRoleMappings(user).size());
// Revoke some roles
realmModel.deleteRoleMapping(user, realmModel.getRole("foo"));
realmModel.deleteRoleMapping(user, appBarRole);
roles = realmModel.getRoleMappings(user);
Assert.assertEquals(4, roles.size());
assertRolesContains(realmUserRole, roles);
assertRolesContains(application.getRole("user"), roles);
Assert.assertFalse(realmModel.hasRole(user, appBarRole));
}
// TODO: test scopes
}

View File

@@ -33,6 +33,7 @@ public class ApplicationModelTest extends AbstractModelTest {
application.setName("app-name");
application.addRole("role-1");
application.addRole("role-2");
application.addRole("role-3");
application.addDefaultRole("role-1");
application.addDefaultRole("role-2");

View File

@@ -68,15 +68,6 @@ public class ModelTest extends AbstractModelTest {
Assert.assertEquals(expected.getSocialConfig(), actual.getSocialConfig());
}
public static void assertEquals(List<RoleModel> expected, List<RoleModel> actual) {
Assert.assertEquals(expected.size(), actual.size());
Iterator<RoleModel> exp = expected.iterator();
Iterator<RoleModel> act = actual.iterator();
while (exp.hasNext()) {
Assert.assertEquals(exp.next().getName(), act.next().getName());
}
}
private RealmModel importExport(RealmModel src, String copyName) {
RealmRepresentation representation = ModelToRepresentation.toRepresentation(src);
RealmModel copy = realmManager.createRealm(copyName);

View File

@@ -0,0 +1,110 @@
package org.keycloak.model.test;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.OAuthClientModel;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserModel;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class MultipleRealmsTest extends AbstractModelTest {
private RealmModel realm1;
private RealmModel realm2;
@Before
public void before() throws Exception {
super.before();
realm1 = identitySession.createRealm("id1", "realm1");
realm2 = identitySession.createRealm("id2", "realm2");
createObjects(realm1);
createObjects(realm2);
}
@Test
public void testUsers() {
UserModel r1user1 = realm1.getUser("user1");
UserModel r2user1 = realm2.getUser("user1");
Assert.assertEquals(r1user1.getLoginName(), r2user1.getLoginName());
Assert.assertNotEquals(r1user1.getId(), r2user1.getId());
// Test password
realm1.updateCredential(r1user1, UserCredentialModel.password("pass1"));
realm2.updateCredential(r2user1, UserCredentialModel.password("pass2"));
Assert.assertTrue(realm1.validatePassword(r1user1, "pass1"));
Assert.assertFalse(realm1.validatePassword(r1user1, "pass2"));
Assert.assertFalse(realm2.validatePassword(r2user1, "pass1"));
Assert.assertTrue(realm2.validatePassword(r2user1, "pass2"));
// Test searching
Assert.assertEquals(2, realm1.searchForUser("user").size());
realm1.removeUser("user1");
realm1.removeUser("user2");
Assert.assertEquals(0, realm1.searchForUser("user").size());
Assert.assertEquals(2, realm2.searchForUser("user").size());
}
@Test
public void testGetById() {
Assert.assertEquals(realm1, identitySession.getRealm("id1"));
Assert.assertEquals(realm1, identitySession.getRealmByName("realm1"));
Assert.assertEquals(realm2, identitySession.getRealm("id2"));
Assert.assertEquals(realm2, identitySession.getRealmByName("realm2"));
ApplicationModel r1app1 = realm1.getApplicationByName("app1");
ApplicationModel r1app2 = realm1.getApplicationByName("app2");
ApplicationModel r2app1 = realm2.getApplicationByName("app1");
ApplicationModel r2app2 = realm2.getApplicationByName("app2");
Assert.assertEquals(r1app1, realm1.getApplicationById(r1app1.getId()));
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());
RoleModel r1App1Role = r1app1.getRole("app1Role1");
Assert.assertNull(realm1.getRoleById(r1App1Role.getId()));
Assert.assertNull(realm2.getRoleById(r1App1Role.getId()));
Assert.assertEquals(r1App1Role, r1app1.getRoleById(r1App1Role.getId()));
Assert.assertNull(r1app2.getRoleById(r1App1Role.getId()));
Assert.assertNull(r2app1.getRoleById(r1App1Role.getId()));
Assert.assertNull(r2app2.getRoleById(r1App1Role.getId()));
RoleModel r2Role1 = realm2.getRole("role2");
Assert.assertNull(realm1.getRoleById(r2Role1.getId()));
Assert.assertEquals(r2Role1, realm2.getRoleById(r2Role1.getId()));
Assert.assertNull(r1app1.getRoleById(r2Role1.getId()));
Assert.assertNull(r1app2.getRoleById(r2Role1.getId()));
Assert.assertNull(r2app1.getRoleById(r2Role1.getId()));
Assert.assertNull(r2app2.getRoleById(r2Role1.getId()));
}
private void createObjects(RealmModel realm) {
ApplicationModel app1 = realm.addApplication("app1");
realm.addApplication("app2");
realm.addUser("user1");
realm.addUser("user2");
realm.addRole("role1");
realm.addRole("role2");
app1.addRole("app1Role1");
app1.addScope(realm.getRole("role1"));
realm.addOAuthClient("cl1");
}
}