Importing organizations failing if there is no broker and members in the representation

Closes #30305

Signed-off-by: Martin Kanis <mkanis@redhat.com>
This commit is contained in:
Martin Kanis 2024-06-18 13:49:07 +02:00 committed by Pedro Igor
parent 57139cbefc
commit 89f83e9788
2 changed files with 45 additions and 21 deletions

View file

@ -1592,12 +1592,12 @@ public class DefaultExportImportManager implements ExportImportManager {
OrganizationModel org = provider.create(orgRep.getName()); OrganizationModel org = provider.create(orgRep.getName());
org.setDomains(orgRep.getDomains().stream().map(r -> new OrganizationDomainModel(r.getName(), r.isVerified())).collect(Collectors.toSet())); org.setDomains(orgRep.getDomains().stream().map(r -> new OrganizationDomainModel(r.getName(), r.isVerified())).collect(Collectors.toSet()));
for (IdentityProviderRepresentation identityProvider : orgRep.getIdentityProviders()) { for (IdentityProviderRepresentation identityProvider : Optional.ofNullable(orgRep.getIdentityProviders()).orElse(Collections.emptyList())) {
IdentityProviderModel idp = newRealm.getIdentityProviderByAlias(identityProvider.getAlias()); IdentityProviderModel idp = newRealm.getIdentityProviderByAlias(identityProvider.getAlias());
provider.addIdentityProvider(org, idp); provider.addIdentityProvider(org, idp);
} }
for (UserRepresentation member : orgRep.getMembers()) { for (UserRepresentation member : Optional.ofNullable(orgRep.getMembers()).orElse(Collections.emptyList())) {
UserModel m = session.users().getUserByUsername(newRealm, member.getUsername()); UserModel m = session.users().getUserByUsername(newRealm, member.getUsername());
provider.addMember(org, m); provider.addMember(org, m);
} }

View file

@ -30,6 +30,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import jakarta.ws.rs.core.Response;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import org.keycloak.admin.client.resource.OrganizationResource; import org.keycloak.admin.client.resource.OrganizationResource;
@ -109,25 +110,7 @@ public class OrganizationExportTest extends AbstractOrganizationTest {
} }
} }
// export RealmRepresentation importedRealm = exportRemoveImportRealm();
TestingExportImportResource exportImport = testingClient.testing().exportImport();
exportImport.setProvider(SingleFileExportProviderFactory.PROVIDER_ID);
exportImport.setAction(ExportImportConfig.ACTION_EXPORT);
exportImport.setRealmName(testRealm().toRepresentation().getRealm());
String targetFilePath = exportImport.getExportImportTestDirectory() + File.separator + "org-export.json";
exportImport.setFile(targetFilePath);
exportImport.runExport();
// remove the realm and import it back
testRealm().remove();
exportImport = testingClient.testing().exportImport();
exportImport.setProvider(SingleFileImportProviderFactory.PROVIDER_ID);
exportImport.setAction(ExportImportConfig.ACTION_IMPORT);
exportImport.setFile(targetFilePath);
exportImport.runImport();
getCleanup().addCleanup(() -> testRealm().remove());
RealmRepresentation importedRealm = testRealm().toRepresentation();
assertTrue(importedRealm.isOrganizationsEnabled()); assertTrue(importedRealm.isOrganizationsEnabled());
@ -158,6 +141,47 @@ public class OrganizationExportTest extends AbstractOrganizationTest {
assertThat(appPage.getRequestType(),is(AppPage.RequestType.AUTH_RESPONSE)); assertThat(appPage.getRequestType(),is(AppPage.RequestType.AUTH_RESPONSE));
} }
@Test
public void testExportImportEmptyOrg() {
OrganizationRepresentation orgRep = createRepresentation("acme", "acme.com");
try (Response response = testRealm().organizations().create(orgRep)) {
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
}
List<OrganizationRepresentation> orgs = testRealm().organizations().getAll();
assertEquals(1, orgs.size());
RealmRepresentation importedRealm = exportRemoveImportRealm();
assertTrue(importedRealm.isOrganizationsEnabled());
orgs = testRealm().organizations().getAll();
assertEquals(1, orgs.size());
assertEquals("acme", orgs.get(0).getName());
}
private RealmRepresentation exportRemoveImportRealm() {
//export
TestingExportImportResource exportImport = testingClient.testing().exportImport();
exportImport.setProvider(SingleFileExportProviderFactory.PROVIDER_ID);
exportImport.setAction(ExportImportConfig.ACTION_EXPORT);
exportImport.setRealmName(testRealm().toRepresentation().getRealm());
String targetFilePath = exportImport.getExportImportTestDirectory() + File.separator + "org-export.json";
exportImport.setFile(targetFilePath);
exportImport.runExport();
// remove the realm and import it back
testRealm().remove();
exportImport = testingClient.testing().exportImport();
exportImport.setProvider(SingleFileImportProviderFactory.PROVIDER_ID);
exportImport.setAction(ExportImportConfig.ACTION_IMPORT);
exportImport.setFile(targetFilePath);
exportImport.runImport();
getCleanup().addCleanup(() -> testRealm().remove());
return testRealm().toRepresentation();
}
@Test @Test
public void testPartialExport() { public void testPartialExport() {
createOrganization(); createOrganization();