Search in sources :

Example 26 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByPrincipalNameExpireRemovesIndex.

@Test
public void findByPrincipalNameExpireRemovesIndex() throws Exception {
    String principalName = "findByPrincipalNameExpireRemovesIndex" + UUID.randomUUID();
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(INDEX_NAME, principalName);
    this.repository.save(toSave);
    String body = "RedisOperationsSessionRepositoryITests:sessions:expires:" + toSave.getId();
    String channel = ":expired";
    DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), body.getBytes("UTF-8"));
    byte[] pattern = new byte[] {};
    this.repository.onMessage(message, pattern);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).hasSize(0);
    assertThat(findByPrincipalName.keySet()).doesNotContain(toSave.getId());
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 27 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByDeletedPrincipalName.

@Test
public void findByDeletedPrincipalName() throws Exception {
    String principalName = "findByDeletedPrincipalName" + UUID.randomUUID();
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(INDEX_NAME, principalName);
    this.repository.save(toSave);
    toSave.setAttribute(INDEX_NAME, null);
    this.repository.save(toSave);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).isEmpty();
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 28 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method flushModeOnSaveSetMaxInactiveIntervalInSeconds.

@Test
public void flushModeOnSaveSetMaxInactiveIntervalInSeconds() {
    RedisSession session = this.redisRepository.createSession();
    session.setMaxInactiveInterval(Duration.ofSeconds(1));
    verifyZeroInteractions(this.boundHashOperations);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 29 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method flushModeOnSaveSetLastAccessedTime.

@Test
public void flushModeOnSaveSetLastAccessedTime() {
    RedisSession session = this.redisRepository.createSession();
    session.setLastAccessedTime(Instant.ofEpochMilli(1L));
    verifyZeroInteractions(this.boundHashOperations);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 30 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryTests method getSessionFound.

@Test
public void getSessionFound() {
    String attrName = "attrName";
    MapSession expected = new MapSession();
    expected.setLastAccessedTime(Instant.now().minusSeconds(60));
    expected.setAttribute(attrName, "attrValue");
    given(this.redisOperations.boundHashOps(getKey(expected.getId()))).willReturn(this.boundHashOperations);
    Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), expected.getAttribute(attrName), RedisOperationsSessionRepository.CREATION_TIME_ATTR, expected.getCreationTime().toEpochMilli(), RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, (int) expected.getMaxInactiveInterval().getSeconds(), RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, expected.getLastAccessedTime().toEpochMilli());
    given(this.boundHashOperations.entries()).willReturn(map);
    RedisSession session = this.redisRepository.findById(expected.getId());
    assertThat(session.getId()).isEqualTo(expected.getId());
    assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
    assertThat(session.<String>getAttribute(attrName)).isEqualTo(expected.getAttribute(attrName));
    assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
    assertThat(session.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval());
    assertThat(session.getLastAccessedTime()).isEqualTo(expected.getLastAccessedTime());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)47 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)47 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MapSession (org.springframework.session.MapSession)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 Session (org.springframework.session.Session)2 PrincipalNameResolver (org.springframework.session.data.redis.RedisOperationsSessionRepository.PrincipalNameResolver)2 EnableRedisHttpSession (org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 TimeUnit (java.util.concurrent.TimeUnit)1 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)1 SessionCreatedEvent (org.springframework.session.events.SessionCreatedEvent)1 SessionDestroyedEvent (org.springframework.session.events.SessionDestroyedEvent)1