[issue-14134] test partial import user with id

Fix #14134
This commit is contained in:
Oliver 2023-08-21 10:26:14 +02:00 committed by Pedro Igor
parent e05effe62d
commit 563ae104fd
3 changed files with 61 additions and 2 deletions

View file

@ -110,7 +110,9 @@ public class UsersPartialImport extends AbstractPartialImport<UserRepresentation
@Override @Override
public void create(RealmModel realm, KeycloakSession session, UserRepresentation user) { public void create(RealmModel realm, KeycloakSession session, UserRepresentation user) {
if (user.getId() == null) {
user.setId(KeycloakModelUtils.generateId()); user.setId(KeycloakModelUtils.generateId());
}
UserModel userModel = RepresentationToModel.createUser(session, realm, user); UserModel userModel = RepresentationToModel.createUser(session, realm, user);
if (userModel == null) throw new RuntimeException("Unable to create user " + getName(user)); if (userModel == null) throw new RuntimeException("Unable to create user " + getName(user));
createdIds.put(getName(user), userModel.getId()); createdIds.put(getName(user), userModel.getId());

View file

@ -556,8 +556,9 @@ public abstract class AbstractKeycloakTest {
return ApiUtil.createUserWithAdminClient(adminClient.realm(realm), homer); return ApiUtil.createUserWithAdminClient(adminClient.realm(realm), homer);
} }
public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, List<String> groups, boolean enabled) { public static UserRepresentation createUserRepresentation(String id, String username, String email, String firstName, String lastName, List<String> groups, boolean enabled) {
UserRepresentation user = new UserRepresentation(); UserRepresentation user = new UserRepresentation();
user.setId(id);
user.setUsername(username); user.setUsername(username);
user.setEmail(email); user.setEmail(email);
user.setFirstName(firstName); user.setFirstName(firstName);
@ -567,6 +568,10 @@ public abstract class AbstractKeycloakTest {
return user; return user;
} }
public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, List<String> groups, boolean enabled) {
return createUserRepresentation(null, username, email, firstName, lastName, groups, enabled);
}
public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, boolean enabled) { public static UserRepresentation createUserRepresentation(String username, String email, String firstName, String lastName, boolean enabled) {
return createUserRepresentation(username, email, firstName, lastName, null, enabled); return createUserRepresentation(username, email, firstName, lastName, null, enabled);
} }

View file

@ -57,6 +57,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.CoreMatchers.startsWith;
@ -247,6 +248,17 @@ public class PartialImportTest extends AbstractAuthTest {
piRep.setUsers(users); piRep.setUsers(users);
} }
private void addUsersWithIds() {
List<UserRepresentation> users = new ArrayList<>();
for (int i = 0; i < NUM_ENTITIES; i++) {
UserRepresentation user = createUserRepresentation(UUID.randomUUID().toString(), USER_PREFIX + i, USER_PREFIX + i + "@foo.com", "foo", "bar", null, true);
users.add(user);
}
piRep.setUsers(users);
}
private void addUsersWithTermsAndConditions() { private void addUsersWithTermsAndConditions() {
List<UserRepresentation> users = new ArrayList<>(); List<UserRepresentation> users = new ArrayList<>();
List<String> requiredActions = new ArrayList<>(); List<String> requiredActions = new ArrayList<>();
@ -418,6 +430,46 @@ public class PartialImportTest extends AbstractAuthTest {
} }
} }
@Test
public void testAddUsersWithIds() {
assertAdminEvents.clear();
setFail();
addUsersWithIds();
Set<String> userRepIds = new HashSet<>();
for (UserRepresentation userRep : piRep.getUsers()) {
userRepIds.add(userRep.getId());
}
PartialImportResults results = doImport();
assertEquals(NUM_ENTITIES, results.getAdded());
// Need to do this way as admin events from partial import are unsorted
Set<String> userIds = new HashSet<>();
for (int i=0 ; i<NUM_ENTITIES ; i++) {
AdminEventRepresentation adminEvent = assertAdminEvents.poll();
Assert.assertEquals(realmId, adminEvent.getRealmId());
Assert.assertEquals(OperationType.CREATE.name(), adminEvent.getOperationType());
Assert.assertTrue(adminEvent.getResourcePath().startsWith("users/"));
assertThat(adminEvent.getResourceType(), equalTo(org.keycloak.events.admin.ResourceType.REALM.name()));
String userId = adminEvent.getResourcePath().substring(6);
userIds.add(userId);
assertThat(userRepIds, hasItem(userId));
}
assertAdminEvents.assertEmpty();
for (PartialImportResult result : results.getResults()) {
String id = result.getId();
UserResource userRsc = testRealmResource().users().get(id);
UserRepresentation user = userRsc.toRepresentation();
assertThat(user.getUsername(), startsWith(USER_PREFIX));
assertThat(userIds, hasItem(id));
assertThat(userRepIds, hasItem(id));
}
}
@Test @Test
public void testAddUsersWithDuplicateEmailsForbidden() { public void testAddUsersWithDuplicateEmailsForbidden() {
assertAdminEvents.clear(); assertAdminEvents.clear();