use of org.keycloak.testsuite.model.RequireProvider in project keycloak by keycloak.
the class UserSessionInitializerTest method testUserSessionPropagationBetweenSites.
@Test
@RequireProvider(value = UserSessionProvider.class, only = InfinispanUserSessionProviderFactory.PROVIDER_ID)
public void testUserSessionPropagationBetweenSites() throws InterruptedException {
AtomicInteger index = new AtomicInteger();
AtomicReference<String> userSessionId = new AtomicReference<>();
AtomicReference<List<Boolean>> containsSession = new AtomicReference<>(new LinkedList<>());
Object lock = new Object();
Optional<HotRodServerRule> hotRodServer = getParameters(HotRodServerRule.class).findFirst();
inIndependentFactories(4, 300, () -> {
synchronized (lock) {
if (index.incrementAndGet() == 1) {
// create a user session in the first node
UserSessionModel userSessionModel = withRealm(realmId, (session, realm) -> {
final UserModel user = session.users().getUserByUsername(realm, "user1");
return session.sessions().createUserSession(realm, user, "un1", "ip1", "auth", false, null, null);
});
userSessionId.set(userSessionModel.getId());
} else {
// try to get the user session at other nodes and also at different sites
inComittedTransaction(session -> {
InfinispanConnectionProvider provider = session.getProvider(InfinispanConnectionProvider.class);
Cache<String, Object> localSessions = provider.getCache(USER_SESSION_CACHE_NAME);
containsSession.get().add(localSessions.containsKey(userSessionId.get()));
if (hotRodServer.isPresent()) {
RemoteCache<String, Object> remoteSessions = provider.getRemoteCache(USER_SESSION_CACHE_NAME);
containsSession.get().add(remoteSessions.containsKey(userSessionId.get()));
}
});
}
}
});
assertThat(containsSession.get(), everyItem(is(true)));
// 3 nodes (first node just creates the session), with Hot Rod server we have local + remote cache, without just local cache
int size = hotRodServer.isPresent() ? 6 : 3;
assertThat(containsSession.get().size(), is(size));
}
use of org.keycloak.testsuite.model.RequireProvider in project keycloak by keycloak.
the class OfflineSessionPersistenceTest method testOfflineSessionLoadingAfterCacheRemoval.
@Test
@RequireProvider(UserSessionPersisterProvider.class)
@RequireProvider(value = UserSessionProvider.class, only = InfinispanUserSessionProviderFactory.PROVIDER_ID)
public void testOfflineSessionLoadingAfterCacheRemoval() {
List<String> offlineSessionIds = createOfflineSessions(realmId, userIds);
assertOfflineSessionsExist(realmId, offlineSessionIds);
// Simulate server restart
reinitializeKeycloakSessionFactory();
assertOfflineSessionsExist(realmId, offlineSessionIds);
// remove sessions from the cache
withRealm(realmId, (session, realm) -> {
// Delete local user cache (persisted sessions are still kept)
UserSessionProvider provider = session.getProvider(UserSessionProvider.class);
// Remove in-memory representation of the offline sessions
((InfinispanUserSessionProvider) provider).removeLocalUserSessions(realm.getId(), true);
return null;
});
// assert sessions are lazily loaded from DB
assertOfflineSessionsExist(realmId, offlineSessionIds);
}
use of org.keycloak.testsuite.model.RequireProvider in project keycloak by keycloak.
the class OfflineSessionPersistenceTest method testLazyClientSessionStatsFetching.
@Test
@RequireProvider(UserSessionPersisterProvider.class)
@RequireProvider(value = UserSessionProvider.class, only = InfinispanUserSessionProviderFactory.PROVIDER_ID)
public void testLazyClientSessionStatsFetching() {
List<String> clientIds = withRealm(realmId, (session, realm) -> IntStream.range(0, 5).mapToObj(cid -> session.clients().addClient(realm, "client-" + cid)).map(ClientModel::getId).collect(Collectors.toList()));
List<String> offlineSessionIds = createOfflineSessions(realmId, userIds);
assertOfflineSessionsExist(realmId, offlineSessionIds);
Random r = new Random();
offlineSessionIds.stream().forEach(offlineSessionId -> createOfflineClientSession(offlineSessionId, clientIds.get(r.nextInt(5))));
// Simulate server restart
reinitializeKeycloakSessionFactory();
// load active client sessions stats from DB
Map<String, Long> sessionStats = withRealm(realmId, (session, realm) -> session.sessions().getActiveClientSessionStats(realm, true));
long client1SessionCount = sessionStats.get(clientIds.get(0));
int clientSessionsCount = sessionStats.values().stream().reduce(0l, Long::sum).intValue();
assertThat(clientSessionsCount, Matchers.is(USER_COUNT * OFFLINE_SESSION_COUNT_PER_USER));
// Simulate server restart
reinitializeKeycloakSessionFactory();
long actualClient1SessionCount = withRealm(realmId, (session, realm) -> {
ClientModel client = realm.getClientById(clientIds.get(0));
return session.sessions().getOfflineSessionsCount(realm, client);
});
assertThat(actualClient1SessionCount, Matchers.is(client1SessionCount));
}
use of org.keycloak.testsuite.model.RequireProvider in project keycloak by keycloak.
the class UserSessionPersisterProviderTest method testPersistenceWithLoadWithExternalClientStorage.
@Test
@RequireProvider(ClientStorageProvider.class)
public void testPersistenceWithLoadWithExternalClientStorage() {
try {
inComittedTransaction(session -> {
setupClientStorageComponents(session, session.realms().getRealm(realmId));
});
int started = Time.currentTime();
UserSessionModel origSession = inComittedTransaction(session -> {
// Create session in infinispan
RealmModel realm = session.realms().getRealm(realmId);
UserSessionModel userSession = session.sessions().createUserSession(realm, session.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", true, null, null);
createClientSession(session, realmId, realm.getClientByClientId("test-app"), userSession, "http://redirect", "state");
createClientSession(session, realmId, realm.getClientByClientId("external-storage-client"), userSession, "http://redirect", "state");
return userSession;
});
inComittedTransaction(session -> {
// Persist created userSession and clientSessions as offline
persistUserSession(session, origSession, true);
});
inComittedTransaction(session -> {
// Assert offline session
RealmModel realm = session.realms().getRealm(realmId);
List<UserSessionModel> loadedSessions = loadPersistedSessionsPaginated(session, true, 1, 1, 1);
assertSessions(loadedSessions, new String[] { origSession.getId() });
assertSessionLoaded(loadedSessions, origSession.getId(), session.users().getUserByUsername(realm, "user1"), "127.0.0.1", started, started, "test-app", "external-storage-client");
});
} finally {
inComittedTransaction(session -> {
cleanClientStorageComponents(session, session.realms().getRealm(realmId));
});
}
}
Aggregations