Fix adding social link in mongo. Fixed export/import of federationProviders. Enhanced ExportImportTest
This commit is contained in:
parent
5214fe2a55
commit
5673fb34b1
8 changed files with 31 additions and 18 deletions
|
@ -71,6 +71,7 @@ public class ExportUtils {
|
||||||
List<OAuthClientRepresentation> oauthClientReps = new ArrayList<OAuthClientRepresentation>();
|
List<OAuthClientRepresentation> oauthClientReps = new ArrayList<OAuthClientRepresentation>();
|
||||||
for (OAuthClientModel oauthClient : oauthClients) {
|
for (OAuthClientModel oauthClient : oauthClients) {
|
||||||
OAuthClientRepresentation clientRep = ModelToRepresentation.toRepresentation(oauthClient);
|
OAuthClientRepresentation clientRep = ModelToRepresentation.toRepresentation(oauthClient);
|
||||||
|
clientRep.setSecret(oauthClient.getSecret());
|
||||||
oauthClientReps.add(clientRep);
|
oauthClientReps.add(clientRep);
|
||||||
}
|
}
|
||||||
rep.setOauthClients(oauthClientReps);
|
rep.setOauthClients(oauthClientReps);
|
||||||
|
|
|
@ -182,7 +182,7 @@ public class ImportUtils {
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
String currRealmName = parser.getText();
|
String currRealmName = parser.getText();
|
||||||
if (!currRealmName.equals(realmName)) {
|
if (!currRealmName.equals(realmName)) {
|
||||||
throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + realmName);
|
throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
|
||||||
}
|
}
|
||||||
} else if ("users".equals(parser.getText())) {
|
} else if ("users".equals(parser.getText())) {
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
|
|
|
@ -5,9 +5,6 @@ import java.io.FileInputStream;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
@ -86,7 +83,7 @@ public class DirImportProvider implements ImportProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File dir, String name) {
|
public boolean accept(File dir, String name) {
|
||||||
return (name.startsWith(realmName)) && (name.endsWith(".json")) && (name.substring(realmName.length()).contains("-users-") );
|
return name.matches(realmName + "-users-[0-9]+\\.json");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,7 @@ public class ZipImportProvider implements ImportProvider {
|
||||||
// Import users
|
// Import users
|
||||||
for (ExtZipEntry entry : this.decrypter.getEntryList()) {
|
for (ExtZipEntry entry : this.decrypter.getEntryList()) {
|
||||||
String name = entry.getName();
|
String name = entry.getName();
|
||||||
if ( (name.startsWith(realmName)) && (name.endsWith(".json")) && (name.substring(realmName.length()).contains("-users-")) ) {
|
if (name.matches(realmName + "-users-[0-9]+\\.json")) {
|
||||||
bos = new ByteArrayOutputStream();
|
bos = new ByteArrayOutputStream();
|
||||||
this.decrypter.extractEntry(entry, bos, this.password);
|
this.decrypter.extractEntry(entry, bos, this.password);
|
||||||
final ByteArrayInputStream bis2 = new ByteArrayInputStream(bos.toByteArray());
|
final ByteArrayInputStream bis2 = new ByteArrayInputStream(bos.toByteArray());
|
||||||
|
|
|
@ -134,10 +134,7 @@ public class ModelToRepresentation {
|
||||||
if (fedProviderModels.size() > 0) {
|
if (fedProviderModels.size() > 0) {
|
||||||
List<UserFederationProviderRepresentation> fedProviderReps = new ArrayList<UserFederationProviderRepresentation>();
|
List<UserFederationProviderRepresentation> fedProviderReps = new ArrayList<UserFederationProviderRepresentation>();
|
||||||
for (UserFederationProviderModel model : fedProviderModels) {
|
for (UserFederationProviderModel model : fedProviderModels) {
|
||||||
UserFederationProviderRepresentation fedProvRep = new UserFederationProviderRepresentation();
|
UserFederationProviderRepresentation fedProvRep = toRepresentation(model);
|
||||||
fedProvRep.setId(model.getId());
|
|
||||||
fedProvRep.setProviderName(model.getProviderName());
|
|
||||||
fedProvRep.setConfig(model.getConfig());
|
|
||||||
fedProviderReps.add(fedProvRep);
|
fedProviderReps.add(fedProvRep);
|
||||||
}
|
}
|
||||||
rep.setUserFederationProviders(fedProviderReps);
|
rep.setUserFederationProviders(fedProviderReps);
|
||||||
|
|
|
@ -297,6 +297,7 @@ public class MongoUserProvider implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addSocialLink(RealmModel realm, UserModel user, SocialLinkModel socialLink) {
|
public void addSocialLink(RealmModel realm, UserModel user, SocialLinkModel socialLink) {
|
||||||
|
user = getUserById(user.getId(), realm);
|
||||||
MongoUserEntity userEntity = ((UserAdapter) user).getUser();
|
MongoUserEntity userEntity = ((UserAdapter) user).getUser();
|
||||||
SocialLinkEntity socialLinkEntity = new SocialLinkEntity();
|
SocialLinkEntity socialLinkEntity = new SocialLinkEntity();
|
||||||
socialLinkEntity.setSocialProvider(socialLink.getSocialProvider());
|
socialLinkEntity.setSocialProvider(socialLink.getSocialProvider());
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package org.keycloak.testsuite.exportimport;
|
package org.keycloak.testsuite.exportimport;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -26,10 +28,14 @@ import org.keycloak.models.KeycloakSession;
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
import org.keycloak.models.RealmProvider;
|
import org.keycloak.models.RealmProvider;
|
||||||
import org.keycloak.models.UserCredentialModel;
|
import org.keycloak.models.UserCredentialModel;
|
||||||
|
import org.keycloak.models.UserFederationProviderModel;
|
||||||
import org.keycloak.models.UserModel;
|
import org.keycloak.models.UserModel;
|
||||||
import org.keycloak.models.UserProvider;
|
import org.keycloak.models.UserProvider;
|
||||||
import org.keycloak.representations.idm.CredentialRepresentation;
|
import org.keycloak.representations.idm.CredentialRepresentation;
|
||||||
|
import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.services.managers.RealmManager;
|
import org.keycloak.services.managers.RealmManager;
|
||||||
|
import org.keycloak.testsuite.model.AbstractModelTest;
|
||||||
|
import org.keycloak.testsuite.model.ImportTest;
|
||||||
import org.keycloak.testsuite.rule.KeycloakRule;
|
import org.keycloak.testsuite.rule.KeycloakRule;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -92,14 +98,22 @@ public class ExportImportTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
||||||
|
// Create some users in "test" and "master" realms
|
||||||
addUser(manager.getSession().users(), appRealm, "user1", "password");
|
addUser(manager.getSession().users(), appRealm, "user1", "password");
|
||||||
addUser(manager.getSession().users(), appRealm, "user2", "password");
|
addUser(manager.getSession().users(), appRealm, "user2", "password");
|
||||||
addUser(manager.getSession().users(), appRealm, "user3", "password");
|
addUser(manager.getSession().users(), appRealm, "user3", "password");
|
||||||
addUser(manager.getSession().users(), adminstrationRealm, "admin2", "admin2");
|
addUser(manager.getSession().users(), adminstrationRealm, "admin2", "admin2");
|
||||||
|
|
||||||
|
// Import "test-realm" realm
|
||||||
|
try {
|
||||||
|
RealmRepresentation rep = AbstractModelTest.loadJson("model/testrealm.json");
|
||||||
|
RealmModel demoRealm = manager.createRealm("test-realm", rep.getRealm());
|
||||||
|
manager.importRealm(rep, demoRealm);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
throw new RuntimeException(ioe);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}) {
|
}) {
|
||||||
@Override
|
@Override
|
||||||
protected void after() {
|
protected void after() {
|
||||||
|
@ -137,8 +151,8 @@ public class ExportImportTest {
|
||||||
|
|
||||||
testFullExportImport();
|
testFullExportImport();
|
||||||
|
|
||||||
// There should be 4 files in target directory (2 realm, 2 user)
|
// There should be 6 files in target directory (3 realm, 3 user)
|
||||||
Assert.assertEquals(4, new File(targetDirPath).listFiles().length);
|
Assert.assertEquals(6, new File(targetDirPath).listFiles().length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -210,7 +224,7 @@ public class ExportImportTest {
|
||||||
RealmProvider realmProvider = session.realms();
|
RealmProvider realmProvider = session.realms();
|
||||||
UserProvider userProvider = session.users();
|
UserProvider userProvider = session.users();
|
||||||
new RealmManager(session).removeRealm(realmProvider.getRealmByName("test"));
|
new RealmManager(session).removeRealm(realmProvider.getRealmByName("test"));
|
||||||
Assert.assertEquals(1, realmProvider.getRealms().size());
|
Assert.assertEquals(2, realmProvider.getRealms().size());
|
||||||
|
|
||||||
RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm());
|
RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm());
|
||||||
UserModel admin2 = session.users().getUserByUsername("admin2", master);
|
UserModel admin2 = session.users().getUserByUsername("admin2", master);
|
||||||
|
@ -235,7 +249,7 @@ public class ExportImportTest {
|
||||||
try {
|
try {
|
||||||
RealmProvider model = session.realms();
|
RealmProvider model = session.realms();
|
||||||
UserProvider userProvider = session.users();
|
UserProvider userProvider = session.users();
|
||||||
Assert.assertEquals(2, model.getRealms().size());
|
Assert.assertEquals(3, model.getRealms().size());
|
||||||
|
|
||||||
assertAuthenticated(userProvider, model, Config.getAdminRealm(), "admin2", "admin2");
|
assertAuthenticated(userProvider, model, Config.getAdminRealm(), "admin2", "admin2");
|
||||||
assertAuthenticated(userProvider, model, "test", "test-user@localhost", "password");
|
assertAuthenticated(userProvider, model, "test", "test-user@localhost", "password");
|
||||||
|
@ -243,6 +257,8 @@ public class ExportImportTest {
|
||||||
assertAuthenticated(userProvider, model, "test", "user2", "password");
|
assertAuthenticated(userProvider, model, "test", "user2", "password");
|
||||||
assertAuthenticated(userProvider, model, "test", "user3", "password");
|
assertAuthenticated(userProvider, model, "test", "user3", "password");
|
||||||
|
|
||||||
|
RealmModel testRealmRealm = model.getRealm("test-realm");
|
||||||
|
ImportTest.assertDataImportedInRealm(session, testRealmRealm);
|
||||||
} finally {
|
} finally {
|
||||||
keycloakRule.stopSession(session, true);
|
keycloakRule.stopSession(session, true);
|
||||||
}
|
}
|
||||||
|
@ -261,7 +277,7 @@ public class ExportImportTest {
|
||||||
RealmProvider realmProvider = session.realms();
|
RealmProvider realmProvider = session.realms();
|
||||||
UserProvider userProvider = session.users();
|
UserProvider userProvider = session.users();
|
||||||
new RealmManager(session).removeRealm(realmProvider.getRealmByName("test"));
|
new RealmManager(session).removeRealm(realmProvider.getRealmByName("test"));
|
||||||
Assert.assertEquals(1, realmProvider.getRealms().size());
|
Assert.assertEquals(2, realmProvider.getRealms().size());
|
||||||
|
|
||||||
RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm());
|
RealmModel master = realmProvider.getRealmByName(Config.getAdminRealm());
|
||||||
UserModel admin2 = session.users().getUserByUsername("admin2", master);
|
UserModel admin2 = session.users().getUserByUsername("admin2", master);
|
||||||
|
@ -287,7 +303,7 @@ public class ExportImportTest {
|
||||||
try {
|
try {
|
||||||
RealmProvider realmProvider = session.realms();
|
RealmProvider realmProvider = session.realms();
|
||||||
UserProvider userProvider = session.users();
|
UserProvider userProvider = session.users();
|
||||||
Assert.assertEquals(2, realmProvider.getRealms().size());
|
Assert.assertEquals(3, realmProvider.getRealms().size());
|
||||||
|
|
||||||
assertNotAuthenticated(userProvider, realmProvider, Config.getAdminRealm(), "admin2", "admin2");
|
assertNotAuthenticated(userProvider, realmProvider, Config.getAdminRealm(), "admin2", "admin2");
|
||||||
assertAuthenticated(userProvider, realmProvider, "test", "test-user@localhost", "password");
|
assertAuthenticated(userProvider, realmProvider, "test", "test-user@localhost", "password");
|
||||||
|
|
|
@ -185,6 +185,7 @@ public class ImportTest extends AbstractModelTest {
|
||||||
Assert.assertTrue(session.users().removeSocialLink(realm, socialUser, "facebook"));
|
Assert.assertTrue(session.users().removeSocialLink(realm, socialUser, "facebook"));
|
||||||
Assert.assertNull(session.users().getSocialLink(socialUser, "facebook", realm));
|
Assert.assertNull(session.users().getSocialLink(socialUser, "facebook", realm));
|
||||||
Assert.assertFalse(session.users().removeSocialLink(realm, socialUser, "facebook"));
|
Assert.assertFalse(session.users().removeSocialLink(realm, socialUser, "facebook"));
|
||||||
|
session.users().addSocialLink(realm, socialUser, new SocialLinkModel("facebook", "facebook1", "fbuser1"));
|
||||||
|
|
||||||
// Test smtp config
|
// Test smtp config
|
||||||
Map<String, String> smtpConfig = realm.getSmtpConfig();
|
Map<String, String> smtpConfig = realm.getSmtpConfig();
|
||||||
|
|
Loading…
Reference in a new issue