Testsuite: ensure realm is set in session context

Closes #31636

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>
This commit is contained in:
Stefan Guilhen 2024-07-24 09:23:52 -03:00 committed by Pedro Igor
parent 3818f8f575
commit c9f5a0aa32
41 changed files with 340 additions and 165 deletions

View file

@ -100,7 +100,7 @@ public final class KcOidcBrokerTokenExchangeTest extends AbstractInitializedBase
OAuthClient.AccessTokenResponse tokenResponse = oauth.doGrantAccessTokenRequest(bc.providerRealmName(), bc.getUserLogin(), bc.getUserPassword(), null, brokerApp.getClientId(), brokerApp.getSecret());
assertThat(tokenResponse.getIdToken(), notNullValue());
testingClient.server().run(KcOidcBrokerTokenExchangeTest::setupRealm);
testingClient.server(BrokerTestConstants.REALM_CONS_NAME).run(KcOidcBrokerTokenExchangeTest::setupRealm);
ClientRepresentation client = consumerRealm.clients().findByClientId("test-app").get(0);
@ -135,7 +135,7 @@ public final class KcOidcBrokerTokenExchangeTest extends AbstractInitializedBase
@Test
public void testSupportedTokenTypesWhenValidatingSubjectToken() throws Exception {
testingClient.server().run(KcOidcBrokerTokenExchangeTest::setupRealm);
testingClient.server(BrokerTestConstants.REALM_CONS_NAME).run(KcOidcBrokerTokenExchangeTest::setupRealm);
RealmResource providerRealm = realmsResouce().realm(bc.providerRealmName());
ClientsResource clients = providerRealm.clients();
ClientRepresentation brokerApp = clients.findByClientId("brokerapp").get(0);
@ -189,7 +189,7 @@ public final class KcOidcBrokerTokenExchangeTest extends AbstractInitializedBase
}
private static void setupRealm(KeycloakSession session) {
RealmModel realm = session.realms().getRealmByName(BrokerTestConstants.REALM_CONS_NAME);
RealmModel realm = session.getContext().getRealm();
IdentityProviderModel idp = realm.getIdentityProviderByAlias(IDP_OIDC_ALIAS);
org.junit.Assert.assertNotNull(idp);

View file

@ -266,9 +266,9 @@ public class ExportImportUtil {
Assert.assertEquals(1, socialLowercaseLinks.size());
Assert.assertEquals("lowercasesocialuser@gmail.com", socialLowercaseLinks.get(0).getUserName());
UserRepresentation foundSocialUser = testingClient.testing().getUserByFederatedIdentity(realm.getRealm(), "facebook1", "facebook1", "fbuser1");
UserRepresentation foundSocialUser = testingClient.testing(realm.getRealm()).getUserByFederatedIdentity(realm.getRealm(), "facebook1", "facebook1", "fbuser1");
Assert.assertEquals(foundSocialUser.getUsername(), socialUser.toRepresentation().getUsername());
Assert.assertNull(testingClient.testing().getUserByFederatedIdentity(realm.getRealm(), "facebook", "not-existing", "not-existing"));
Assert.assertNull(testingClient.testing(realm.getRealm()).getUserByFederatedIdentity(realm.getRealm(), "facebook", "not-existing", "not-existing"));
Assert.assertEquals("facebook1", facebookIdentityRep.getUserId());
Assert.assertEquals("fbuser1", facebookIdentityRep.getUserName());
@ -331,15 +331,15 @@ public class ExportImportUtil {
/////////////////
// Assert that federation link wasn't created during import
Assert.assertNull(testingClient.testing().getUserByUsernameFromFedProviderFactory(realm.getRealm(), "wburke"));
Assert.assertNull(testingClient.testing(realm.getRealm()).getUserByUsernameFromFedProviderFactory(realm.getRealm(), "wburke"));
// Test builtin authentication flows
AuthenticationFlowRepresentation clientFlow = testingClient.testing().getClientAuthFlow(realm.getRealm());
AuthenticationFlowRepresentation clientFlow = testingClient.testing(realm.getRealm()).getClientAuthFlow(realm.getRealm());
Assert.assertEquals(DefaultAuthenticationFlows.CLIENT_AUTHENTICATION_FLOW, clientFlow.getAlias());
Assert.assertNotNull(realmRsc.flows().getFlow(clientFlow.getId()));
Assert.assertTrue(realmRsc.flows().getExecutions(clientFlow.getAlias()).size() > 0);
AuthenticationFlowRepresentation resetFlow = testingClient.testing().getResetCredFlow(realm.getRealm());
AuthenticationFlowRepresentation resetFlow = testingClient.testing(realm.getRealm()).getResetCredFlow(realm.getRealm());
Assert.assertEquals(DefaultAuthenticationFlows.RESET_CREDENTIALS_FLOW, resetFlow.getAlias());
Assert.assertNotNull(realmRsc.flows().getFlow(resetFlow.getId()));
Assert.assertTrue(realmRsc.flows().getExecutions(resetFlow.getAlias()).size() > 0);
@ -416,13 +416,13 @@ public class ExportImportUtil {
Assert.assertFalse(application.isServiceAccountsEnabled());
Assert.assertTrue(otherApp.isServiceAccountsEnabled());
if (ProfileAssume.isFeatureEnabled(Profile.Feature.AUTHORIZATION)) {
if (ProfileAssume.isFeatureEnabled(Profile.Feature.AUTHORIZATION)) {
Assert.assertTrue(testAppAuthzApp.isServiceAccountsEnabled());
Assert.assertNull(testingClient.testing().getUserByServiceAccountClient(realm.getRealm(), application.getClientId()));//session.users().getUserByServiceAccountClient(application));
UserRepresentation otherAppSA = testingClient.testing().getUserByServiceAccountClient(realm.getRealm(), otherApp.getClientId());//session.users().getUserByServiceAccountClient(otherApp);
Assert.assertNull(testingClient.testing(realm.getRealm()).getUserByServiceAccountClient(realm.getRealm(), application.getClientId()));//session.users().getUserByServiceAccountClient(application));
UserRepresentation otherAppSA = testingClient.testing(realm.getRealm()).getUserByServiceAccountClient(realm.getRealm(), otherApp.getClientId());//session.users().getUserByServiceAccountClient(otherApp);
Assert.assertNotNull(otherAppSA);
Assert.assertEquals("service-account-otherapp", otherAppSA.getUsername());
UserRepresentation testAppAuthzSA = testingClient.testing().getUserByServiceAccountClient(realm.getRealm(), testAppAuthzApp.getClientId());
UserRepresentation testAppAuthzSA = testingClient.testing(realm.getRealm()).getUserByServiceAccountClient(realm.getRealm(), testAppAuthzApp.getClientId());
Assert.assertNotNull(testAppAuthzSA);
Assert.assertEquals("service-account-test-app-authz", testAppAuthzSA.getUsername());
@ -471,7 +471,7 @@ public class ExportImportUtil {
if (mappers == null) {
return null;
}
for (ProtocolMapperRepresentation mapper : mappers) {
if (mapper.getProtocol().equals(type) &&
mapper.getName().equals(name)) {

View file

@ -268,8 +268,9 @@ public class AuthenticationSessionProviderTest extends AbstractTestRealmKeycloak
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesRealmRemoved2) -> {
KeycloakSession currentSession = sesRealmRemoved2;
new RealmManager(currentSession).removeRealm(currentSession.realms().getRealmByName("foo-realm"));
RealmModel fooRealm = currentSession.realms().getRealmByName("foo-realm");
currentSession.getContext().setRealm(fooRealm);
new RealmManager(currentSession).removeRealm(fooRealm);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesRealmRemoved3) -> {

View file

@ -104,7 +104,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
// Client "from" has a role. Assign this role to a scope to client "scoped". Delete the role and make sure
// cache gets cleared
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemove1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientRoleRemove1) -> {
currentSession = sessionClientRoleRemove1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -120,7 +120,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemove2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientRoleRemove2) -> {
currentSession = sessionClientRoleRemove2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -133,7 +133,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemove3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientRoleRemove3) -> {
currentSession = sessionClientRoleRemove3;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -153,7 +153,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
// Client "from" has a role. Assign this role to a scope to client "scoped". Delete the role and make sure
// cache gets cleared
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemoveTx1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientRoleRemoveTx1) -> {
currentSession = sessionClientRoleRemoveTx1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -166,7 +166,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
scoped.addScopeMapping(role);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientRoleRemoveTx2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientRoleRemoveTx2) -> {
currentSession = sessionClientRoleRemoveTx2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -190,7 +190,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
// Client "from" has a role. Assign this role to a scope to client "scoped". Delete the role and make sure
// cache gets cleared
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRealmRoleRemove1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionRealmRoleRemove1) -> {
currentSession = sessionRealmRoleRemove1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
RoleModel role = realm.addRole("clientRole");
@ -200,14 +200,14 @@ public class ClientModelTest extends AbstractKeycloakTest {
scoped.addScopeMapping(role);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRealmRoleRemove2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionRealmRoleRemove2) -> {
currentSession = sessionRealmRoleRemove2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
RoleModel role = currentSession.roles().getRoleById(realm, roleId);
realm.removeRole(role);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRealmRoleRemove3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionRealmRoleRemove3) -> {
currentSession = sessionRealmRoleRemove3;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
ClientModel scoped = realm.getClientByClientId("scoped");
@ -221,7 +221,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
@ModelTest
public void testCircularClientScopes(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCircuilarClient1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCircuilarClient1) -> {
currentSession = sessionCircuilarClient1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
ClientModel scoped1 = realm.addClient("scoped1");
@ -232,7 +232,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
scoped2.addScopeMapping(role1);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCircuilarClient2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCircuilarClient2) -> {
currentSession = sessionCircuilarClient2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -245,7 +245,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
@Test
@ModelTest
public void persist(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionPersist) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionPersist) -> {
currentSession = sessionPersist;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = setUpClient(realm);
@ -263,7 +263,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
@Test
@ModelTest
public void json(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionJson) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionJson) -> {
currentSession = sessionJson;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -275,6 +275,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
}
realm = currentSession.realms().createRealm("copy");
currentSession.getContext().setRealm(realm);
ClientModel copyClient = RepresentationToModel.createClient(currentSession, realm, representation);
assertEquals(client, copyClient);
@ -292,7 +293,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
@ModelTest
public void testAddApplicationWithId(KeycloakSession session) {
final String id = KeycloakModelUtils.generateId();
String newClientId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), (KeycloakSession sessionAppWithId1) -> {
String newClientId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionAppWithId1) -> {
currentSession = sessionAppWithId1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -300,7 +301,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
return client.getId();
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionAppWithId2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionAppWithId2) -> {
currentSession = sessionAppWithId2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -318,7 +319,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
AtomicReference<ClientScopeModel> scope2Atomic = new AtomicReference<>();
AtomicReference<ClientScopeModel> scope3Atomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientScopeBind1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientScopeBind1) -> {
currentSession = sessionClientScopeBind1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.addClient("templatized");
@ -338,7 +339,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientScopeBind2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientScopeBind2) -> {
currentSession = sessionClientScopeBind2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("templatized");
@ -356,7 +357,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
client.addClientScope(scope3, false);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientScopeBind3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientScopeBind3) -> {
currentSession = sessionClientScopeBind3;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("templatized");
@ -379,7 +380,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
client.removeClientScope(scope2);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionClientScopeBind3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionClientScopeBind3) -> {
currentSession = sessionClientScopeBind3;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("templatized");
@ -410,7 +411,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
AtomicReference<ClientScopeModel> scope2Atomic = new AtomicReference<>();
AtomicReference<ClientScopeModel> scope3Atomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionDefaultClientScope1) -> {
currentSession = sessionDefaultClientScope1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -427,7 +428,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
scope3Atomic.set(scope3);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionDefaultClientScope2) -> {
currentSession = sessionDefaultClientScope2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
@ -444,7 +445,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
realm.addDefaultClientScope(scope3, false);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionDefaultClientScope3) -> {
currentSession = sessionDefaultClientScope3;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.addClient("foo");
@ -452,7 +453,7 @@ public class ClientModelTest extends AbstractKeycloakTest {
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope4) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionDefaultClientScope4) -> {
currentSession = sessionDefaultClientScope4;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("foo");
@ -478,14 +479,14 @@ public class ClientModelTest extends AbstractKeycloakTest {
realm.removeDefaultClientScope(scope2);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope5) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionDefaultClientScope5) -> {
currentSession = sessionDefaultClientScope5;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.addClient("foo2");
client.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope5) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionDefaultClientScope5) -> {
currentSession = sessionDefaultClientScope5;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("foo2");

View file

@ -53,7 +53,7 @@ public class ConcurrentTransactionsTest extends AbstractTestRealmKeycloakTest {
private static final Logger logger = Logger.getLogger(ConcurrentTransactionsTest.class);
@Test
@ModelTest
@ModelTest(realmName = "test")
public void persistClient(KeycloakSession session) {
final ClientModel[] client = {null};
@ -61,7 +61,7 @@ public class ConcurrentTransactionsTest extends AbstractTestRealmKeycloakTest {
AtomicReference<Exception> exceptionHolder = new AtomicReference<>();
try {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionSetup) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionSetup) -> {
RealmModel realm = sessionSetup.realms().getRealmByName("test");
sessionSetup.users().addUser(realm, "user1").setEmail("user1@localhost");
@ -201,8 +201,9 @@ public class ConcurrentTransactionsTest extends AbstractTestRealmKeycloakTest {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionSet) -> {
RealmModel realm = sessionSet.realms().createRealm("original");
sessionSet.getContext().setRealm(realm);
realm.setDefaultRole(sessionSet.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
UserModel john = sessionSet.users().addUser(realm, "john");
john.setSingleAttribute("foo", "val1");
@ -290,6 +291,7 @@ public class ConcurrentTransactionsTest extends AbstractTestRealmKeycloakTest {
um.removeUser(realm, realmUser2);
}
currentSession.getContext().setRealm(realm);
Assert.assertTrue(currentSession.realms().removeRealm(realm.getId()));
assertThat(currentSession.realms().getRealm(realm.getId()), is(nullValue()));
}

View file

@ -50,6 +50,8 @@ public class MultipleRealmsTest extends AbstractTestRealmKeycloakTest {
private static final String REALM_ATTRIBUTE = "test-realm";
public static void createObjects(KeycloakSession session, RealmModel realm) {
RealmModel sessionRealm = session.getContext().getRealm();
session.getContext().setRealm(realm);
final List<String> realmNameList = Collections.singletonList(realm.getName());
ClientModel app1 = realm.addClient("app1");
@ -66,6 +68,7 @@ public class MultipleRealmsTest extends AbstractTestRealmKeycloakTest {
app1.addScopeMapping(realm.getRole("role1"));
realm.addClient("cl1").setAttribute(REALM_ATTRIBUTE, realm.getName());
session.getContext().setRealm(sessionRealm);
}
@Test
@ -108,7 +111,7 @@ public class MultipleRealmsTest extends AbstractTestRealmKeycloakTest {
// Test searching
Assert.assertEquals(2, currentSession.users().searchForUserStream(realm1, Map.of(UserModel.SEARCH, "user")).count());
return new String[] { id1, id2 };
return new String[]{id1, id2};
});
String id1 = res[0];
@ -118,34 +121,38 @@ public class MultipleRealmsTest extends AbstractTestRealmKeycloakTest {
KeycloakSession currentSession = sessionTestUser2;
RealmModel realm1 = currentSession.realms().getRealm(id1);
RealmModel realm2 = currentSession.realms().getRealm(id2);
currentSession.getContext().setRealm(realm1);
UserModel r1user1 = r1user1Atomic.get();
currentSession.users().removeUser(realm1, r1user1);
UserModel user2 = currentSession.users().getUserByUsername(realm1, "user2");
currentSession.users().removeUser(realm1, user2);
Assert.assertEquals(0, currentSession.users().searchForUserStream(realm1, Map.of(UserModel.SEARCH, "user")).count());
Assert.assertEquals(2, currentSession.users().searchForUserStream(realm2, Map.of(UserModel.SEARCH, "user")).count());
UserModel user1 = currentSession.users().getUserByUsername(realm1, "user1");
UserModel user1a = currentSession.users().getUserByUsername(realm2, "user1");
UserManager um = new UserManager(currentSession);
if (user1 != null) {
um.removeUser(realm1, user1);
}
if (user1a != null) {
um.removeUser(realm2, user1a);
}
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionTestUser3) -> {
KeycloakSession currentSession = sessionTestUser3;
currentSession.realms().removeRealm(id1);
currentSession.realms().removeRealm(id2);
RealmModel realm2 = currentSession.realms().getRealm(id2);
currentSession.getContext().setRealm(realm2);
Assert.assertEquals(2, currentSession.users().searchForUserStream(realm2, Map.of(UserModel.SEARCH, "user")).count());
UserModel user1a = currentSession.users().getUserByUsername(realm2, "user1");
UserManager um = new UserManager(currentSession);
if (user1a != null) {
um.removeUser(realm2, user1a);
}
});
removeRealm(session, id1);
removeRealm(session, id2);
}
@Test
@ -197,9 +204,11 @@ public class MultipleRealmsTest extends AbstractTestRealmKeycloakTest {
UserManager um = new UserManager(currentSession);
if (user1 != null) {
currentSession.getContext().setRealm(realm1);
um.removeUser(realm1, user1);
}
if (user1a != null) {
currentSession.getContext().setRealm(realm2);
um.removeUser(realm2, user1a);
}
@ -209,17 +218,23 @@ public class MultipleRealmsTest extends AbstractTestRealmKeycloakTest {
String id1 = res[0];
String id2 = res[1];
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionTestUser3) -> {
KeycloakSession currentSession = sessionTestUser3;
currentSession.realms().removeRealm(id1);
currentSession.realms().removeRealm(id2);
});
removeRealm(session, id1);
removeRealm(session, id2);
}
private void assertAttrRealm(RealmModel realm, Stream<String> attr) {
assertThat(attr.collect(Collectors.toList()), containsInAnyOrder(realm.getName()));
}
private void removeRealm(KeycloakSession session, String realmId) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionTestUser3) -> {
RealmModel realm = sessionTestUser3.realms().getRealm(realmId);
sessionTestUser3.getContext().setRealm(realm);
sessionTestUser3.realms().removeRealm(realmId);
});
}
@Override
public void configureTestRealm(RealmRepresentation testRealm) {
}

View file

@ -102,9 +102,10 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try to update some component in realm1 through the realm2
((session, realm1, realm2, realm1ComponentId) -> {
session.getContext().setRealm(realm1);
ComponentModel component = realm1.getComponent(realm1ComponentId);
component.put("key1", "Val1");
session.getContext().setRealm(realm2);
realm2.updateComponent(component);
}),
@ -117,8 +118,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove component from realm1 in the context of realm2
((session, realm1, realm2, realm1ComponentId) -> {
session.getContext().setRealm(realm1);
ComponentModel component = realm1.getComponent(realm1ComponentId);
session.getContext().setRealm(realm2);
realm2.removeComponent(component);
}),
@ -147,9 +149,10 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try to update some object in realm1 through the realm2
((session, realm1, realm2, realm1ReqActionId) -> {
session.getContext().setRealm(realm1);
RequiredActionProviderModel reqAction = realm1.getRequiredActionProviderById(realm1ReqActionId);
reqAction.getConfig().put("key1", "Val1");
session.getContext().setRealm(realm2);
realm2.updateRequiredActionProvider(reqAction);
}),
@ -162,8 +165,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove object from realm1 in the context of realm2
((session, realm1, realm2, realm1ReqActionId) -> {
session.getContext().setRealm(realm1);
RequiredActionProviderModel reqAction = realm1.getRequiredActionProviderById(realm1ReqActionId);
session.getContext().setRealm(realm2);
realm2.removeRequiredActionProvider(reqAction);
}),
@ -198,9 +202,10 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try to update some object in realm1 through the realm2
((session, realm1, realm2, realm1FlowId) -> {
session.getContext().setRealm(realm1);
AuthenticationFlowModel flow = realm1.getAuthenticationFlowById(realm1FlowId);
flow.setDescription("foo");
session.getContext().setRealm(realm2);
realm2.updateAuthenticationFlow(flow);
}),
@ -213,8 +218,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove object from realm1 in the context of realm2
((session, realm1, realm2, realm1FlowId) -> {
session.getContext().setRealm(realm1);
AuthenticationFlowModel flow = realm1.getAuthenticationFlowById(realm1FlowId);
session.getContext().setRealm(realm2);
realm2.removeAuthenticationFlow(flow);
}),
@ -249,9 +255,10 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try to update some object in realm1 through the realm2
((session, realm1, realm2, realm1ExecutionId) -> {
session.getContext().setRealm(realm1);
AuthenticationExecutionModel execution = realm1.getAuthenticationExecutionById(realm1ExecutionId);
execution.setPriority(1234);
session.getContext().setRealm(realm2);
realm2.updateAuthenticatorExecution(execution);
}),
@ -264,8 +271,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove object from realm1 in the context of realm2
((session, realm1, realm2, realm1ExecutionId) -> {
session.getContext().setRealm(realm1);
AuthenticationExecutionModel execution = realm1.getAuthenticationExecutionById(realm1ExecutionId);
session.getContext().setRealm(realm2);
realm2.removeAuthenticatorExecution(execution);
}),
@ -295,9 +303,10 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try to update some object in realm1 through the realm2
((session, realm1, realm2, realm1AuthConfigId) -> {
session.getContext().setRealm(realm1);
AuthenticatorConfigModel config = realm1.getAuthenticatorConfigById(realm1AuthConfigId);
config.getConfig().put("key1", "val1");
session.getContext().setRealm(realm2);
realm2.updateAuthenticatorConfig(config);
}),
@ -310,8 +319,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove object from realm1 in the context of realm2
((session, realm1, realm2, realm1AuthConfigId) -> {
session.getContext().setRealm(realm1);
AuthenticatorConfigModel config = realm1.getAuthenticatorConfigById(realm1AuthConfigId);
session.getContext().setRealm(realm2);
realm2.removeAuthenticatorConfig(config);
}),
@ -358,7 +368,7 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove object from realm1 in the context of realm2
((session, realm1, realm2, realm1ClientInitialAccessId) -> {
session.getContext().setRealm(realm2);
session.getProvider(RealmProvider.class).removeClientInitialAccessModel(realm2, realm1ClientInitialAccessId);
}),
@ -450,8 +460,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
}),
// Try remove object from realm1 in the context of realm2
((session, realm1, realm2, realm1SessionId) -> {
session.getContext().setRealm(realm1);
UserSessionModel userSession = session.sessions().getUserSession(realm1, realm1SessionId);
session.getContext().setRealm(realm2);
session.sessions().removeUserSession(realm2, userSession);
}),
@ -481,6 +492,7 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
// can't use getRealmByName as that returns the infinispan realm adapter version, meaning the tests will query
// the cache instead of the actual provider.
RealmModel realm1 = session.getProvider(RealmProvider.class).getRealm(testRealmId);
session.getContext().setRealm(realm1);
realm1ObjectId.set(realm1ObjectIdProducer.apply(session, realm1));
});
@ -490,7 +502,9 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
RealmModel realm1 = session.getProvider(RealmProvider.class).getRealm(testRealmId);
RealmModel realm2 = session.getProvider(RealmProvider.class).getRealm(fooRealmId);
session.getContext().setRealm(realm2);
testLookupRealm1ObjectInRealm2.accept(session, realm2, realm1ObjectId.get());
// each implementation of updater should set the realm in context according to the operations executed
updaterRealm1ObjectInRealm2.accept(session, realm1, realm2, realm1ObjectId.get());
});
@ -498,7 +512,7 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
// Transaction 3
KeycloakModelUtils.runJobInTransaction(session1.getKeycloakSessionFactory(), (KeycloakSession session) -> {
RealmModel realm1 = session.getProvider(RealmProvider.class).getRealm(testRealmId);
session.getContext().setRealm(realm1);
testUpdateFailed.accept(session, realm1, realm1ObjectId.get());
});
@ -507,6 +521,7 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
KeycloakModelUtils.runJobInTransaction(session1.getKeycloakSessionFactory(), (KeycloakSession session) -> {
RealmModel realm1 = session.getProvider(RealmProvider.class).getRealm(testRealmId);
RealmModel realm2 = session.getProvider(RealmProvider.class).getRealm(fooRealmId);
// each implementation of remover should set the realm in context according to the operations executed
removeRealm1ObjectInRealm2.accept(session, realm1, realm2, realm1ObjectId.get());
});
@ -517,6 +532,7 @@ public class OwnerReplacementTest extends AbstractKeycloakTest {
// Transaction 5
KeycloakModelUtils.runJobInTransaction(session1.getKeycloakSessionFactory(), (KeycloakSession session) -> {
RealmModel realm1 = session.getProvider(RealmProvider.class).getRealm(testRealmId);
session.getContext().setRealm(realm1);
testRemoveFailed.accept(session, realm1, realm1ObjectId.get());
});
}

View file

@ -69,6 +69,7 @@ public class SimpleModelTest extends AbstractKeycloakTest {
RealmModel realm = session2.realms().getRealmByName("foo");
Assert.assertNotNull(realm);
session2.getContext().setRealm(realm);
realm.setAttribute("bar", "baz");
@ -79,6 +80,7 @@ public class SimpleModelTest extends AbstractKeycloakTest {
RealmModel realm = session3.realms().getRealmByName("foo");
Assert.assertNotNull(realm);
session3.getContext().setRealm(realm);
String attrValue = realm.getAttribute("bar");
Assert.assertEquals("baz", attrValue);
@ -93,6 +95,7 @@ public class SimpleModelTest extends AbstractKeycloakTest {
RealmModel realm = session4.realms().getRealmByName("foo");
Assert.assertNotNull(realm);
session4.getContext().setRealm(realm);
String attrValue = realm.getAttribute("bar");
Assert.assertEquals("baz", attrValue);

View file

@ -65,6 +65,7 @@ public class UserConsentModelTest extends AbstractTestRealmKeycloakTest {
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.getRealm(realmId);
session.getContext().setRealm(realm);
if (realm != null) {
@ -99,6 +100,7 @@ public class UserConsentModelTest extends AbstractTestRealmKeycloakTest {
RealmManager realmManager = new RealmManager(currentSession);
RealmModel realm = realmManager.createRealm("original");
currentSession.getContext().setRealm(realm);
ClientModel fooClient = realm.addClient("foo-client");
ClientModel barClient = realm.addClient("bar-client");
@ -302,6 +304,7 @@ public class UserConsentModelTest extends AbstractTestRealmKeycloakTest {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUT) -> {
KeycloakSession currentSession = sessionUT;
RealmModel realm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(realm);
UserModel john = currentSession.users().getUserByUsername(realm, "john");
currentSession.users().removeUser(realm, john);

View file

@ -66,6 +66,7 @@ public class UserConsentWithUserStorageModelTest extends AbstractTestRealmKeyclo
RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.getRealmByName("original");
session.getContext().setRealm(realm);
if (realm != null) {
@ -100,6 +101,7 @@ public class UserConsentWithUserStorageModelTest extends AbstractTestRealmKeyclo
RealmManager realmManager = new RealmManager(currentSession);
RealmModel realm = realmManager.createRealm("original");
currentSession.getContext().setRealm(realm);
UserStorageProviderModel model = new UserStorageProviderModel();
model.setName("memory");
@ -311,6 +313,7 @@ public class UserConsentWithUserStorageModelTest extends AbstractTestRealmKeyclo
// Validate user deleted without any referential constraint errors
KeycloakSession currentSession = sessionDelete;
RealmModel realm = currentSession.realms().getRealmByName("original");
currentSession.getContext().setRealm(realm);
UserModel john = currentSession.users().getUserByUsername(realm, "john");
currentSession.users().removeUser(realm, john);

View file

@ -70,9 +70,9 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void persistUser(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesPersistUser) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesPersistUser) -> {
KeycloakSession currentSession = sesPersistUser;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -120,10 +120,10 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void webOriginSetTest(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesWebOrigin) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesWebOrigin) -> {
KeycloakSession currentSession = sesWebOrigin;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -162,10 +162,10 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testUserRequiredActions(KeycloakSession session) throws Exception {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserReqActions) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUserReqActions) -> {
KeycloakSession currentSession = sesUserReqActions;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -182,7 +182,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
assertThat(requiredActions, hasSize(1));
assertThat(requiredActions, contains(RequiredAction.CONFIGURE_TOTP.name()));
user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
user = currentSession.users().getUserByUsername(realm, "user");
@ -196,7 +196,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
requiredActions = user.getRequiredActionsStream().collect(Collectors.toList());
assertThat(requiredActions, hasSize(2));
assertThat(requiredActions, containsInAnyOrder(
RequiredAction.CONFIGURE_TOTP.name(),
RequiredAction.CONFIGURE_TOTP.name(),
RequiredAction.VERIFY_EMAIL.name())
);
@ -216,11 +216,11 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testUserMultipleAttributes(KeycloakSession session) throws Exception {
AtomicReference<List<String>> attrValsAtomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesMultipleAtr1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesMultipleAtr1) -> {
KeycloakSession currentSession = sesMultipleAtr1;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -235,7 +235,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
user.setAttribute("key2", attrVals);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesMultipleAtr2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesMultipleAtr2) -> {
KeycloakSession currentSession = sesMultipleAtr2;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -266,7 +266,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
user.setSingleAttribute("key2", "val23");
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesMultipleAtr3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesMultipleAtr3) -> {
KeycloakSession currentSession = sesMultipleAtr3;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -282,10 +282,10 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
// KEYCLOAK-3494
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testUpdateUserAttribute(KeycloakSession session) throws Exception {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateAtr1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUpdateAtr1) -> {
KeycloakSession currentSession = sesUpdateAtr1;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -294,7 +294,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
user.setSingleAttribute("key1", "value1");
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateAtr2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUpdateAtr2) -> {
KeycloakSession currentSession = sesUpdateAtr2;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -314,12 +314,12 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
// KEYCLOAK-3608
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testUpdateUserSingleAttribute(KeycloakSession session) {
AtomicReference<Map<String, List<String>>> expectedAtomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateUserSingleAtr) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUpdateUserSingleAtr) -> {
KeycloakSession currentSession = sesUpdateUserSingleAtr;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -345,7 +345,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
expectedAtomic.set(expected);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUpdateUserSingleAtr2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUpdateUserSingleAtr2) -> {
KeycloakSession currentSession = sesUpdateUserSingleAtr2;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -355,17 +355,17 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testSearchByString(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesSearchString1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesSearchString1) -> {
KeycloakSession currentSession = sesSearchString1;
RealmModel realm = currentSession.realms().getRealmByName("original");
currentSession.users().addUser(realm, "user1");
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesSearchString1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesSearchString1) -> {
KeycloakSession currentSession = sesSearchString1;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -379,18 +379,16 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testSearchByUserAttribute(KeycloakSession session) throws Exception {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesSearchAtr1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesSearchAtr1) -> {
KeycloakSession currentSession = sesSearchAtr1;
RealmModel realm = currentSession.realms().getRealmByName("original");
UserModel user1 = currentSession.users().addUser(realm, "user1");
UserModel user2 = currentSession.users().addUser(realm, "user2");
UserModel user3 = currentSession.users().addUser(realm, "user3");
RealmModel otherRealm = currentSession.realms().getRealmByName("other");
UserModel otherRealmUser = currentSession.users().addUser(otherRealm, "user1");
user1.setSingleAttribute("key1", "value1");
user1.setSingleAttribute("key2", "value21");
@ -400,10 +398,13 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
user3.setSingleAttribute("key2", "value21");
RealmModel otherRealm = currentSession.realms().getRealmByName("other");
currentSession.getContext().setRealm(otherRealm);
UserModel otherRealmUser = currentSession.users().addUser(otherRealm, "user1");
otherRealmUser.setSingleAttribute("key2", "value21");
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesSearchAtr2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesSearchAtr2) -> {
KeycloakSession currentSession = sesSearchAtr2;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -433,10 +434,10 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testServiceAccountLink(KeycloakSession session) throws Exception {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesServiceLink1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesServiceLink1) -> {
KeycloakSession currentSession = sesServiceLink1;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -461,7 +462,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
user1.setServiceAccountClientLink(client.getId());
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesServiceLink2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesServiceLink2) -> {
KeycloakSession currentSession = sesServiceLink2;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -495,7 +496,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
clientMgr.removeClient(realm, client);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesServiceLink3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesServiceLink3) -> {
KeycloakSession currentSession = sesServiceLink3;
RealmModel realm = currentSession.realms().getRealmByName("original");
// Assert service account removed as well
@ -511,6 +512,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
KeycloakSession currentSession = sesGrantToAll1;
RealmModel realm1 = currentSession.realms().getRealmByName("realm1");
currentSession.getContext().setRealm(realm1);
realm1.addRole("role1");
currentSession.users().addUser(realm1, "user1");
@ -523,6 +525,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesGrantToAll2) -> {
KeycloakSession currentSession = sesGrantToAll2;
RealmModel realm1 = currentSession.realms().getRealmByName("realm1");
currentSession.getContext().setRealm(realm1);
RoleModel role1 = realm1.getRole("role1");
currentSession.users().grantToAllUsers(realm1, role1);
@ -531,6 +534,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesGrantToAll2) -> {
KeycloakSession currentSession = sesGrantToAll2;
RealmModel realm1 = currentSession.realms().getRealmByName("realm1");
currentSession.getContext().setRealm(realm1);
RoleModel role1 = realm1.getRole("role1");
UserModel user1 = currentSession.users().getUserByUsername(realm1, "user1");
@ -539,19 +543,21 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
Assert.assertTrue(user2.hasRole(role1));
RealmModel realm2 = currentSession.realms().getRealmByName("realm2");
currentSession.getContext().setRealm(realm2);
UserModel realm2User1 = currentSession.users().getUserByUsername(realm2, "user1");
Assert.assertFalse(realm2User1.hasRole(role1));
currentSession.realms().removeRealm(realm1.getId());
currentSession.realms().removeRealm(realm2.getId());
currentSession.getContext().setRealm(realm1);
currentSession.realms().removeRealm(realm1.getId());
});
}
@Test
@ModelTest
@ModelTest(realmName = "original")
public void testUserNotBefore(KeycloakSession session) throws Exception {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserNotBefore1) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUserNotBefore1) -> {
KeycloakSession currentSession = sesUserNotBefore1;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -559,7 +565,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
currentSession.users().setNotBeforeForUser(realm, user1, 10);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserNotBefore2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUserNotBefore2) -> {
KeycloakSession currentSession = sesUserNotBefore2;
RealmModel realm = currentSession.realms().getRealmByName("original");
@ -571,7 +577,7 @@ public class UserModelTest extends AbstractTestRealmKeycloakTest {
currentSession.users().setNotBeforeForUser(realm, user1, 20);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesUserNotBefore3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sesUserNotBefore3) -> {
KeycloakSession currentSession = sesUserNotBefore3;
RealmModel realm = currentSession.realms().getRealmByName("original");

View file

@ -61,16 +61,14 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
@Before
public void before() {
testingClient.server().run(session -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionBefore) -> {
reloadState(sessionBefore, true);
});
});
testingClient.server("test").run(session ->
reloadState(session, true));
}
@After
public void after() {
testingClient.server().run(session -> {
testingClient.server("test").run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
session.sessions().removeUserSessions(realm);
UserModel user1 = session.users().getUserByUsername(realm, "user1");
@ -87,17 +85,17 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
}
@Test
@ModelTest
@ModelTest(realmName = "test")
public void testOfflineSessionsCrud(KeycloakSession session) {
Map<String, Set<String>> offlineSessions = new HashMap<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCrud) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud) -> {
// Create some online sessions in infinispan
reloadState(sessionCrud);
createSessions(sessionCrud);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCrud2) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud2) -> {
currentSession = sessionCrud2;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
@ -109,7 +107,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
.forEach(userSession -> offlineSessions.put(userSession.getId(), createOfflineSessionIncludeClientSessions(currentSession, userSession)));
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCrud3) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud3) -> {
currentSession = sessionCrud3;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
@ -145,7 +143,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
sessionManager.revokeOfflineToken(user1, testApp);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCrud4) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud4) -> {
currentSession = sessionCrud4;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
@ -174,7 +172,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionCrud5) -> {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), session.getContext(), (KeycloakSession sessionCrud5) -> {
currentSession = sessionCrud5;
realm = currentSession.realms().getRealmByName("test");
sessionManager = new UserSessionManager(currentSession);
@ -208,6 +206,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR1) -> {
currentSession = sessionRR1;
RealmModel fooRealm = currentSession.realms().createRealm("foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
fooRealm.setSsoSessionIdleTimeout(1800);
fooRealm.setSsoSessionMaxLifespan(36000);
@ -230,6 +229,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
// Persist offline session
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserSessionModel userSession = currentSession.sessions().getUserSession(fooRealm, userSessionID.get());
createOfflineSessionIncludeClientSessions(currentSession, userSession);
@ -247,6 +247,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR3) -> {
currentSession = sessionRR3;
RealmModel fooRealm = currentSession.realms().createRealm(realmId, "foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
fooRealm.addClient("foo-app");
@ -256,7 +257,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionRR4) -> {
currentSession = sessionRR4;
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
Assert.assertEquals(0, currentSession.sessions().getOfflineSessionsCount(fooRealm, fooRealm.getClientByClientId("foo-app")));
// Cleanup
@ -273,6 +274,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
currentSession = sessionCR1;
sessionManager = new UserSessionManager(currentSession);
RealmModel fooRealm = currentSession.realms().createRealm("foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
fooRealm.setSsoSessionIdleTimeout(1800);
fooRealm.setSsoSessionMaxLifespan(36000);
@ -299,6 +301,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
currentSession = sessionCR2;
// Create offline currentSession
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserSessionModel userSession = currentSession.sessions().getUserSession(fooRealm, userSessionID.get());
createOfflineSessionIncludeClientSessions(currentSession, userSession);
});
@ -308,6 +311,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
RealmManager realmMgr = new RealmManager(currentSession);
ClientManager clientMgr = new ClientManager(realmMgr);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
// Assert currentSession was persisted with both clientSessions
UserSessionModel offlineSession = currentSession.sessions().getOfflineUserSession(fooRealm, userSessionID.get());
@ -323,6 +327,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
RealmManager realmMgr = new RealmManager(currentSession);
ClientManager clientMgr = new ClientManager(realmMgr);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
// Assert just one bar-app clientSession persisted now
UserSessionModel offlineSession = currentSession.sessions().getOfflineUserSession(fooRealm, userSessionID.get());
@ -339,6 +344,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
// Assert nothing loaded - userSession was removed as well because it was last userSession
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserSessionModel offlineSession = currentSession.sessions().getOfflineUserSession(fooRealm, userSessionID.get());
Assert.assertEquals(0, offlineSession.getAuthenticatedClientSessions().size());
});
@ -350,6 +356,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
currentSession = sessionTearDown;
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserModel user3 = currentSession.users().getUserByUsername(fooRealm, "user3");
// Remove user3
@ -369,6 +376,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
String realmId = KeycloakModelUtils.runJobInTransactionWithResult(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR1) -> {
currentSession = sessionUR1;
RealmModel fooRealm = currentSession.realms().createRealm("foo");
currentSession.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
fooRealm.setSsoSessionIdleTimeout(1800);
fooRealm.setSsoSessionMaxLifespan(36000);
@ -393,6 +401,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
// Create offline session
RealmModel fooRealm = currentSession.realms().getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserSessionModel userSession = currentSession.sessions().getUserSession(fooRealm, userSessionID.get());
createOfflineSessionIncludeClientSessions(currentSession, userSession);
});
@ -402,6 +411,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserModel user3 = currentSession.users().getUserByUsername(fooRealm, "user3");
// Assert session was persisted with both clientSessions
@ -417,6 +427,7 @@ public class UserSessionProviderOfflineTest extends AbstractTestRealmKeycloakTes
RealmManager realmMgr = new RealmManager(currentSession);
RealmModel fooRealm = realmMgr.getRealm(realmId);
currentSession.getContext().setRealm(fooRealm);
UserModel user3 = currentSession.users().getUserByUsername(fooRealm, "user3");
// Remove user3

View file

@ -243,19 +243,25 @@ public class OrganizationIdentityProviderTest extends AbstractOrganizationTest {
IdentityProviderRepresentation idpRepresentation = createRep("master-identity-provider", "oidc");
adminClient.realm("master").identityProviders().create(idpRepresentation).close();
getTestingClient().server(TEST_REALM_NAME).run(session -> {
OrganizationProvider provider = session.getProvider(OrganizationProvider.class);
OrganizationModel organization = provider.getById(orgId);
try {
getTestingClient().server(TEST_REALM_NAME).run(session -> {
OrganizationProvider provider = session.getProvider(OrganizationProvider.class);
OrganizationModel organization = provider.getById(orgId);
RealmModel realm = session.realms().getRealmByName("master");
IdentityProviderModel idp = realm.getIdentityProviderByAlias("master-identity-provider");
// adjust the session context to use the master realm to be able to retrieve the idp.
RealmModel realm = session.realms().getRealmByName("master");
RealmModel current = session.getContext().getRealm();
session.getContext().setRealm(realm);
IdentityProviderModel idp = realm.getIdentityProviderByAlias("master-identity-provider");
try {
// restore the context and try to add the idp.
session.getContext().setRealm(current);
assertFalse(provider.addIdentityProvider(organization, idp));
} finally {
realm.removeIdentityProviderByAlias("master-identity-provider");
}
});
});
} finally {
adminClient.realm("master").identityProviders().get("master-identity-provider").remove();
}
}
@Test

View file

@ -45,6 +45,7 @@ public abstract class AbstractUserSessionLimitsBrokerTest extends AbstractInitia
String idpAlias = bc.getIDPAlias();
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName(realmName);
session.getContext().setRealm(realm);
AuthenticationFlowModel postBrokerFlow = new AuthenticationFlowModel();
postBrokerFlow.setAlias("post-broker");
postBrokerFlow.setDescription("post-broker flow with session limits");

View file

@ -48,6 +48,7 @@ public class FederatedIdentityModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
@ -64,6 +65,8 @@ public class FederatedIdentityModelTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -617,8 +617,11 @@ public abstract class KeycloakModelTest {
protected static RealmModel createRealm(KeycloakSession s, String name) {
RealmModel realm = s.realms().getRealmByName(name);
if (realm != null) {
RealmModel current = s.getContext().getRealm();
s.getContext().setRealm(realm);
// The previous test didn't clean up the realm for some reason, cleanup now
s.realms().removeRealm(realm.getId());
s.getContext().setRealm(current);
}
realm = s.realms().createRealm(name);
return realm;

View file

@ -44,12 +44,15 @@ public class MigrationModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -51,15 +51,22 @@ public class RealmModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
this.removeRealm(s, realmId);
if (realm1Id != null) this.removeRealm(s, realm1Id);
if (realm2Id != null) this.removeRealm(s, realm2Id);
}
private void removeRealm(KeycloakSession s, String realmId) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
if (realm1Id != null) s.realms().removeRealm(realm1Id);
if (realm2Id != null) s.realms().removeRealm(realm2Id);
}
@Test
@ -134,8 +141,7 @@ public class RealmModelTest extends KeycloakModelTest {
});
// Remove realm 2
inComittedTransaction( (Consumer<KeycloakSession>) keycloakSession -> keycloakSession.realms().removeRealm(realm2Id));
inComittedTransaction( (Consumer<KeycloakSession>) keycloakSession -> this.removeRealm(keycloakSession, realm2Id));
// ResourceServer in realm1 must still exist
ResourceServer resourceServer = withRealm(realm1Id, (keycloakSession, realmModel) -> {

View file

@ -36,6 +36,7 @@ public class TimeOffsetTest extends KeycloakModelTest {
@Override
protected void createEnvironment(KeycloakSession s) {
RealmModel r = s.realms().createRealm("realm");
s.getContext().setRealm(r);
r.setDefaultRole(s.roles().addRealmRole(r, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + r.getName()));
r.setEventsExpiration(5);
realmId = r.getId();
@ -43,6 +44,8 @@ public class TimeOffsetTest extends KeycloakModelTest {
@Override
protected void cleanEnvironment(KeycloakSession s) {
RealmModel r = s.realms().getRealm(realmId);
s.getContext().setRealm(r);
s.realms().removeRealm(realmId);
}

View file

@ -62,6 +62,7 @@ public class ConcurrentAuthzTest extends KeycloakModelTest {
@Override
protected void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realmId = realm.getId();
@ -81,6 +82,8 @@ public class ConcurrentAuthzTest extends KeycloakModelTest {
@Override
protected void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -53,12 +53,15 @@ public class ClientModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -1,13 +1,13 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -53,12 +53,15 @@ public class ClientScopeModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -1,13 +1,13 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -43,12 +43,15 @@ public class ClientScopeStorageTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -1,13 +1,13 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -52,12 +52,15 @@ public class AdminEventQueryTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
EventStoreProvider eventStore = s.getProvider(EventStoreProvider.class);
eventStore.clearAdmin(s.realms().getRealm(realmId));
s.realms().removeRealm(realmId);
@ -122,7 +125,7 @@ public class AdminEventQueryTest extends KeycloakModelTest {
String longValue = RandomStringUtils.random(30000, true, true);
withRealm(realmId, (session, realm) -> {
AdminEvent event = createClientEvent(realm, OperationType.CREATE);
event.setRepresentation(longValue);

View file

@ -1,13 +1,13 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -48,12 +48,15 @@ public class EventQueryTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -45,12 +45,15 @@ public class GroupModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -50,6 +50,7 @@ public class RemoteLoginFailureTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession session) {
RealmModel realm = createRealm(session, "remote-login-failure-test");
session.getContext().setRealm(realm);
realm.setDefaultRole(session.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realmId = realm.getId();
@ -66,6 +67,8 @@ public class RemoteLoginFailureTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -46,6 +46,7 @@ public class RoleModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
@ -54,6 +55,8 @@ public class RoleModelTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -49,6 +49,7 @@ public class AuthenticationSessionTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realm.setAccessCodeLifespanLogin(1800);
@ -59,6 +60,8 @@ public class AuthenticationSessionTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -1,13 +1,13 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -69,13 +69,14 @@ public class OfflineSessionPersistenceTest extends KeycloakModelTest {
private static final int USER_COUNT = 50;
private static final int OFFLINE_SESSION_COUNT_PER_USER = 10;
private String realmId;
private List<String> userIds;
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = prepareRealm(s, "realm");
s.getContext().setRealm(realm);
this.realmId = realm.getId();
userIds = IntStream.range(0, USER_COUNT)
@ -96,7 +97,9 @@ public class OfflineSessionPersistenceTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
new RealmManager(s).removeRealm(s.realms().getRealm(realmId)); // See https://issues.redhat.com/browse/KEYCLOAK-17876
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
new RealmManager(s).removeRealm(realm); // See https://issues.redhat.com/browse/KEYCLOAK-17876
}
@Test
@ -222,7 +225,7 @@ public class OfflineSessionPersistenceTest extends KeycloakModelTest {
// IllegalLifecycleStateException: ISPN000324: Cache 'clientSessions' is in 'STOPPING' state and this is an invocation not belonging to an
// on-going transaction, so it does not accept new invocations."
// also: org.infinispan.commons.CacheException: java.lang.IllegalStateException: Read commands must ignore leavers
if ((ex.getCause() != null && ex.getCause().getMessage().contains("ISPN000324")) ||
if ((ex.getCause() != null && ex.getCause().getMessage().contains("ISPN000324")) ||
(ex.getMessage() != null && ex.getMessage().contains("ISPN000217")) ||
(ex instanceof CacheException && ex.getMessage().contains("Read commands must ignore leavers"))) {
log.warn("invocation failed, skipping. Retrying might lead to a 'Unique index or primary key violation' when the offline session has already been stored in the DB in the current session", ex);

View file

@ -66,6 +66,7 @@ public class SessionTimeoutsTest extends KeycloakModelTest {
super.createEnvironment(s);
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
@ -79,6 +80,7 @@ public class SessionTimeoutsTest extends KeycloakModelTest {
public void cleanEnvironment(KeycloakSession s) {
InfinispanTestUtil.revertTimeService(s);
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
UserModel user1 = s.users().getUserByUsername(realm, "user1");
s.sessions().removeUserSessions(realm);
s.sessions().getOfflineUserSessionsStream(realm, user1).forEach(us -> s.sessions().removeOfflineUserSession(realm, us));

View file

@ -49,6 +49,7 @@ public class UserSessionConcurrencyTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realm.setSsoSessionIdleTimeout(1800);
realm.setSsoSessionMaxLifespan(36000);
@ -65,6 +66,8 @@ public class UserSessionConcurrencyTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}
@ -114,6 +117,10 @@ public class UserSessionConcurrencyTest extends KeycloakModelTest {
return null;
});
inComittedTransaction((Consumer<KeycloakSession>) session -> session.realms().removeRealm(realmId));
inComittedTransaction((Consumer<KeycloakSession>) session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
session.realms().removeRealm(realmId);
});
}
}

View file

@ -66,6 +66,7 @@ public class UserSessionInitializerTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realm.setSsoSessionIdleTimeout(1800);
@ -81,6 +82,7 @@ public class UserSessionInitializerTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.sessions().removeUserSessions(realm);
UserModel user1 = s.users().getUserByUsername(realm, "user1");

View file

@ -82,6 +82,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT);
realm.setOfflineSessionMaxLifespan(Constants.DEFAULT_OFFLINE_SESSION_MAX_LIFESPAN);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
@ -115,6 +116,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.sessions().removeUserSessions(realm);
UserModel user1 = s.users().getUserByUsername(realm, "user1");
@ -260,6 +262,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel fooRealm = session.realms().createRealm("foo");
session.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(session.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
fooRealm.addClient("foo-app");
@ -274,6 +277,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
// Persist offline session
RealmModel fooRealm = session.realms().getRealmByName("foo");
session.getContext().setRealm(fooRealm);
UserSessionModel userSession = session.sessions().getUserSession(fooRealm, userSessionID.get());
persistUserSession(session, userSession, true);
});
@ -284,7 +288,9 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
// Remove realm
RealmManager realmMgr = new RealmManager(session);
realmMgr.removeRealm(realmMgr.getRealmByName("foo"));
RealmModel fooRealm = realmMgr.getRealmByName("foo");
session.getContext().setRealm(fooRealm);
realmMgr.removeRealm(fooRealm);
});
inComittedTransaction(session -> {
@ -300,6 +306,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel fooRealm = session.realms().createRealm("foo");
session.getContext().setRealm(fooRealm);
fooRealm.setDefaultRole(session.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX));
fooRealm.addClient("foo-app");
@ -315,6 +322,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel fooRealm = session.realms().getRealmByName("foo");
session.getContext().setRealm(fooRealm);
// Persist offline session
UserSessionModel userSession = session.sessions().getUserSession(fooRealm, userSessionID.get());
@ -325,6 +333,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
RealmManager realmMgr = new RealmManager(session);
ClientManager clientMgr = new ClientManager(realmMgr);
RealmModel fooRealm = realmMgr.getRealmByName("foo");
session.getContext().setRealm(fooRealm);
// Assert session was persisted with both clientSessions
UserSessionModel persistedSession = loadPersistedSessionsPaginated(session, true, 10, 1, 1).get(0);
@ -339,6 +348,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
RealmManager realmMgr = new RealmManager(session);
ClientManager clientMgr = new ClientManager(realmMgr);
RealmModel fooRealm = realmMgr.getRealmByName("foo");
session.getContext().setRealm(fooRealm);
// Assert just one bar-app clientSession persisted now
UserSessionModel persistedSession = loadPersistedSessionsPaginated(session, true, 10, 1, 1).get(0);
@ -355,7 +365,9 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
// Cleanup
RealmManager realmMgr = new RealmManager(session);
realmMgr.removeRealm(realmMgr.getRealmByName("foo"));
RealmModel fooRealm = realmMgr.getRealmByName("foo");
session.getContext().setRealm(fooRealm);
realmMgr.removeRealm(fooRealm);
});
}
@ -372,6 +384,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
UserSessionModel[] origSessions = origSessionsAt.get();
@ -384,6 +397,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
// Load offline sessions
loadPersistedSessionsPaginated(session, true, 10, 1, 2);
@ -395,6 +409,7 @@ public class UserSessionPersisterProviderTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
if (InfinispanUtils.isEmbeddedInfinispan()) {

View file

@ -66,6 +66,7 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realm.setSsoSessionIdleTimeout(1800);
@ -82,6 +83,8 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}
@ -91,6 +94,7 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
UserSessionModel userSession = session.sessions().getUserSession(realm, origSessions[0].getId());
Assert.assertEquals(origSessions[0], userSession);
@ -101,6 +105,7 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
session.sessions().removeUserSession(realm, session.sessions().getUserSession(realm, origSessions[0].getId()));
session.sessions().removeUserSession(realm, session.sessions().getUserSession(realm, origSessions[1].getId()));
@ -108,6 +113,7 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
inComittedTransaction(session -> {
RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
UserSessionModel userSession = session.sessions().getUserSession(realm, origSessions[0].getId());
Assert.assertNull(userSession);
@ -191,7 +197,7 @@ public class UserSessionProviderModelTest extends KeycloakModelTest {
ClientModel testApp = realm.getClientByClientId("test-app");
AuthenticatedClientSessionModel clientSession = session.sessions().createClientSession(realm, testApp, userSession);
// assert the client sessions are present
assertThat(session.sessions().getClientSession(userSession, testApp, clientSession.getId(), false), notNullValue());
return userSession.getId();

View file

@ -85,6 +85,7 @@ public class UserSessionProviderOfflineModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "test");
s.getContext().setRealm(realm);
realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realm.setOfflineSessionMaxLifespanEnabled(true);
@ -102,6 +103,7 @@ public class UserSessionProviderOfflineModelTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.sessions().removeUserSessions(realm);
UserModel user1 = s.users().getUserByUsername(realm, "user1");

View file

@ -51,6 +51,7 @@ public class SingleUseObjectModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
realmId = realm.getId();
UserModel user = s.users().addUser(realm, "user");
@ -59,6 +60,8 @@ public class SingleUseObjectModelTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -37,6 +37,7 @@ public class StorageTransactionTest extends KeycloakModelTest {
@Override
protected void createEnvironment(KeycloakSession s) {
RealmModel r = s.realms().createRealm("1");
s.getContext().setRealm(r);
r.setDefaultRole(s.roles().addRealmRole(r, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + r.getName()));
r.setAttribute("k1", "v1");
r.setSsoSessionIdleTimeout(1000);
@ -47,6 +48,8 @@ public class StorageTransactionTest extends KeycloakModelTest {
@Override
protected void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}

View file

@ -1,13 +1,13 @@
/*
* Copyright 2020 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -72,6 +72,7 @@ public class UserModelTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
@ -82,6 +83,8 @@ public class UserModelTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}
@ -92,6 +95,7 @@ public class UserModelTest extends KeycloakModelTest {
private Void addRemoveUser(KeycloakSession session, int i) {
RealmModel realm = session.realms().getRealmByName("realm");
session.getContext().setRealm(realm);
UserModel user = session.users().addUser(realm, "user-" + i);
@ -133,6 +137,7 @@ public class UserModelTest extends KeycloakModelTest {
// Create users and let them join first group
IntStream.range(0, 100).parallel().forEach(index -> inComittedTransaction(index, (session, i) -> {
final RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
final UserModel user = session.users().addUser(realm, "user-" + i);
user.joinGroup(session.groups().getGroupById(realm, groupId));
userIds.add(user.getId());
@ -141,6 +146,7 @@ public class UserModelTest extends KeycloakModelTest {
inComittedTransaction(session -> {
final RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
final GroupModel group = session.groups().getGroupById(realm, groupId);
assertThat(session.users().getGroupMembersStream(realm, group).count(), is(100L));
});
@ -150,6 +156,7 @@ public class UserModelTest extends KeycloakModelTest {
do {
userIds.stream().parallel().forEach(index -> inComittedTransaction(index, (session, userId) -> {
final RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
final UserModel user = session.users().getUserById(realm, userId);
log.debugf("Remove user %s: %s", userId, session.users().removeUser(realm, user));
return null;
@ -162,6 +169,7 @@ public class UserModelTest extends KeycloakModelTest {
inComittedTransaction(session -> {
final RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
final GroupModel group = session.groups().getGroupById(realm, groupId);
assertThat(session.users().getGroupMembersStream(realm, group).collect(Collectors.toList()), Matchers.empty());
});
@ -204,6 +212,7 @@ public class UserModelTest extends KeycloakModelTest {
// Create users and let them join first group
IntStream.range(0, 100).parallel().forEach(index -> inComittedTransaction(index, (session, i) -> {
final RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
final UserModel user = session.users().addUser(realm, "user-" + i);
user.joinGroup(session.groups().getGroupById(realm, groupId));
log.infof("Created user with id: %s", user.getId());
@ -230,7 +239,7 @@ public class UserModelTest extends KeycloakModelTest {
inComittedTransaction(session -> {
// If we are using cache, we need to invalidate all users because after removing users from external
// provider cache may not be cleared and it may be the case, that cache is the only place that is having
// provider cache may not be cleared and it may be the case, that cache is the only place that is having
// a reference to removed users. Our importValidation method won't be called at all for removed users
// because they are not present in any storage. However, when we get users by id cache may still be hit
// since it is not alerted in any way when users are removed from external provider. Hence we need to clear
@ -249,6 +258,7 @@ public class UserModelTest extends KeycloakModelTest {
do {
userIds.stream().parallel().forEach(index -> inComittedTransaction(index, (session, userId) -> {
final RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm);
final UserModel user = session.users().getUserById(realm, userId);
if (user != null) {
log.debugf("Deleting user: %s", userId);

View file

@ -43,6 +43,7 @@ public class UserPaginationTest extends KeycloakModelTest {
@Override
public void createEnvironment(KeycloakSession s) {
RealmModel realm = createRealm(s, "realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(s.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
@ -57,13 +58,15 @@ public class UserPaginationTest extends KeycloakModelTest {
} else {
userFederationId2 = res.getId();
}
log.infof("Added %s user federation provider: %s", fs.getName(), res.getId());
}));
}
@Override
public void cleanEnvironment(KeycloakSession s) {
RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
s.realms().removeRealm(realmId);
}
@ -72,7 +75,7 @@ public class UserPaginationTest extends KeycloakModelTest {
List<UserModel> list = withRealm(realmId, (session, realm) ->
session.users().searchForUserStream(realm, Map.of(UserModel.SEARCH, ""), 0, Constants.DEFAULT_MAX_RESULTS) // Default values used in UsersResource
.collect(Collectors.toList()));
assertThat(list, hasSize(8));
expectedStorageCalls(
@ -86,7 +89,7 @@ public class UserPaginationTest extends KeycloakModelTest {
List<UserModel> list = withRealm(realmId, (session, realm) ->
session.users().searchForUserStream(realm, Map.of(UserModel.SEARCH, ""), 0, 6)
.collect(Collectors.toList()));
assertThat(list, hasSize(6));
@ -127,7 +130,7 @@ public class UserPaginationTest extends KeycloakModelTest {
List<UserModel> list = withRealm(realmId, (session, realm) ->
session.users().searchForUserStream(realm, Map.of(UserModel.SEARCH, ""), 5, 6)
.collect(Collectors.toList()));
assertThat(list, hasSize(3));
expectedStorageCalls(

View file

@ -73,6 +73,7 @@ public class UserSyncTest extends KeycloakModelTest {
public void createEnvironment(KeycloakSession s) {
inComittedTransaction(session -> {
RealmModel realm = session.realms().createRealm("realm");
s.getContext().setRealm(realm);
realm.setDefaultRole(session.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
this.realmId = realm.getId();
});
@ -80,6 +81,7 @@ public class UserSyncTest extends KeycloakModelTest {
getParameters(UserStorageProviderModel.class).forEach(fs -> inComittedTransaction(session -> {
if (userFederationId != null || !fs.isImportEnabled()) return;
RealmModel realm = session.realms().getRealm(realmId);
s.getContext().setRealm(realm);
fs.setParentId(realmId);
@ -101,6 +103,7 @@ public class UserSyncTest extends KeycloakModelTest {
@Override
public void cleanEnvironment(KeycloakSession s) {
final RealmModel realm = s.realms().getRealm(realmId);
s.getContext().setRealm(realm);
ComponentModel ldapModel = LDAPTestUtils.getLdapProviderModel(realm);
LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(s, ldapModel);