Search in sources :

Example 1 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveJavadocSummary.

@Test
public void saveJavadocSummary() {
    RedisSession session = this.redisRepository.createSession();
    String sessionKey = "spring:session:sessions:" + session.getId();
    String backgroundExpireKey = "spring:session:expirations:" + RedisSessionExpirationPolicy.roundUpToNextMinute(RedisSessionExpirationPolicy.expiresInMillis(session));
    String destroyedTriggerKey = "spring:session:sessions:expires:" + session.getId();
    given(this.redisOperations.boundHashOps(sessionKey)).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(backgroundExpireKey)).willReturn(this.boundSetOperations);
    given(this.redisOperations.boundValueOps(destroyedTriggerKey)).willReturn(this.boundValueOperations);
    this.redisRepository.save(session);
    // the actual data in the session expires 5 minutes after expiration so the data
    // can be accessed in expiration events
    // if the session is retrieved and expired it will not be returned since
    // findById checks if it is expired
    long fiveMinutesAfterExpires = session.getMaxInactiveInterval().plusMinutes(5).getSeconds();
    verify(this.boundHashOperations).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
    verify(this.boundSetOperations).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
    verify(this.boundSetOperations).add("expires:" + session.getId());
    verify(this.boundValueOperations).expire(1800L, TimeUnit.SECONDS);
    verify(this.boundValueOperations).append("");
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method flushModeImmediateRemoveAttribute.

@Test
public void flushModeImmediateRemoveAttribute() {
    given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
    this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE);
    RedisSession session = this.redisRepository.createSession();
    String attrName = "someAttribute";
    session.removeAttribute(attrName);
    Map<String, Object> delta = getDelta(2);
    assertThat(delta.size()).isEqualTo(1);
    assertThat(delta).isEqualTo(map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName)));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 3 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveExpired.

@Test
public void saveExpired() {
    RedisSession session = this.redisRepository.new RedisSession(new MapSession());
    session.setMaxInactiveInterval(Duration.ZERO);
    given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    this.redisRepository.save(session);
    String id = session.getId();
    verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id));
    verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) MapSession(org.springframework.session.MapSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 4 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method flushModeOnSaveSetAttribute.

@Test
public void flushModeOnSaveSetAttribute() {
    RedisSession session = this.redisRepository.createSession();
    session.setAttribute("something", "here");
    verifyZeroInteractions(this.boundHashOperations);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 5 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method findByPrincipalName.

@Test
public void findByPrincipalName() {
    Instant lastAccessed = Instant.now().minusMillis(10);
    Instant createdTime = lastAccessed.minusMillis(10);
    Duration maxInactive = Duration.ofHours(1);
    String sessionId = "some-id";
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    given(this.boundSetOperations.members()).willReturn(Collections.singleton(sessionId));
    given(this.redisOperations.boundHashOps(getKey(sessionId))).willReturn(this.boundHashOperations);
    Map map = map(RedisOperationsSessionRepository.CREATION_TIME_ATTR, createdTime.toEpochMilli(), RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, (int) maxInactive.getSeconds(), RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, lastAccessed.toEpochMilli());
    given(this.boundHashOperations.entries()).willReturn(map);
    Map<String, RedisSession> sessionIdToSessions = this.redisRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, "principal");
    assertThat(sessionIdToSessions).hasSize(1);
    RedisSession session = sessionIdToSessions.get(sessionId);
    assertThat(session).isNotNull();
    assertThat(session.getId()).isEqualTo(sessionId);
    assertThat(session.getLastAccessedTime()).isEqualTo(lastAccessed);
    assertThat(session.getMaxInactiveInterval()).isEqualTo(maxInactive);
    assertThat(session.getCreationTime()).isEqualTo(createdTime);
}
Also used : Instant(java.time.Instant) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Duration(java.time.Duration) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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