Search in sources :

Example 1 with RequireProvider

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));
}
Also used : HotRodServerRule(org.keycloak.testsuite.model.HotRodServerRule) UserSessionModel(org.keycloak.models.UserSessionModel) AtomicReference(java.util.concurrent.atomic.AtomicReference) UserModel(org.keycloak.models.UserModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LinkedList(java.util.LinkedList) List(java.util.List) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) RequireProvider(org.keycloak.testsuite.model.RequireProvider)

Example 2 with RequireProvider

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);
}
Also used : UserSessionProvider(org.keycloak.models.UserSessionProvider) InfinispanUserSessionProvider(org.keycloak.models.sessions.infinispan.InfinispanUserSessionProvider) InfinispanUserSessionProvider(org.keycloak.models.sessions.infinispan.InfinispanUserSessionProvider) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) Test(org.junit.Test) RequireProvider(org.keycloak.testsuite.model.RequireProvider)

Example 3 with RequireProvider

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));
}
Also used : ClientModel(org.keycloak.models.ClientModel) IntStream(java.util.stream.IntStream) UserSessionProvider(org.keycloak.models.UserSessionProvider) InfinispanUserSessionProviderFactory(org.keycloak.models.sessions.infinispan.InfinispanUserSessionProviderFactory) RealmManager(org.keycloak.services.managers.RealmManager) Constants(org.keycloak.models.Constants) Random(java.util.Random) UserModel(org.keycloak.models.UserModel) RealmProvider(org.keycloak.models.RealmProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel) Map(java.util.Map) RequireProvider(org.keycloak.testsuite.model.RequireProvider) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) RealmModel(org.keycloak.models.RealmModel) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeycloakSession(org.keycloak.models.KeycloakSession) Matchers(org.hamcrest.Matchers) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) Test(org.junit.Test) UserSessionModel(org.keycloak.models.UserSessionModel) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) UserProvider(org.keycloak.models.UserProvider) List(java.util.List) Stream(java.util.stream.Stream) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) InfinispanUserSessionProvider(org.keycloak.models.sessions.infinispan.InfinispanUserSessionProvider) ClientModel(org.keycloak.models.ClientModel) Random(java.util.Random) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) Test(org.junit.Test) RequireProvider(org.keycloak.testsuite.model.RequireProvider)

Example 4 with RequireProvider

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));
        });
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserSessionModel(org.keycloak.models.UserSessionModel) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) RequireProvider(org.keycloak.testsuite.model.RequireProvider)

Aggregations

Test (org.junit.Test)4 KeycloakModelTest (org.keycloak.testsuite.model.KeycloakModelTest)4 RequireProvider (org.keycloak.testsuite.model.RequireProvider)4 UserSessionModel (org.keycloak.models.UserSessionModel)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RealmModel (org.keycloak.models.RealmModel)2 UserModel (org.keycloak.models.UserModel)2 UserSessionProvider (org.keycloak.models.UserSessionProvider)2 InfinispanUserSessionProvider (org.keycloak.models.sessions.infinispan.InfinispanUserSessionProvider)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1