Search in sources :

Example 6 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method resolvePrincipalIndex.

@Test
public void resolvePrincipalIndex() {
    PrincipalNameResolver resolver = RedisOperationsSessionRepository.PRINCIPAL_NAME_RESOLVER;
    String username = "username";
    RedisSession session = this.redisRepository.createSession();
    session.setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, username);
    assertThat(resolver.resolvePrincipal(session)).isEqualTo(username);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PrincipalNameResolver(org.springframework.session.data.redis.RedisOperationsSessionRepository.PrincipalNameResolver) Test(org.junit.Test)

Example 7 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveLastAccessChanged.

@Test
public void saveLastAccessChanged() {
    RedisSession session = this.redisRepository.new RedisSession(new MapSession(this.cached));
    session.setLastAccessedTime(Instant.ofEpochMilli(12345678L));
    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.save(session);
    assertThat(getDelta()).isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli()));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 8 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method redisSessionGetAttributes.

@Test
public void redisSessionGetAttributes() {
    String attrName = "attrName";
    RedisSession session = this.redisRepository.new RedisSession();
    assertThat(session.getAttributeNames()).isEmpty();
    session.setAttribute(attrName, "attrValue");
    assertThat(session.getAttributeNames()).containsOnly(attrName);
    session.removeAttribute(attrName);
    assertThat(session.getAttributeNames()).isEmpty();
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 9 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveJavadoc.

@Test
public void saveJavadoc() {
    RedisSession session = this.redisRepository.new RedisSession(this.cached);
    session.setLastAccessedTime(session.getLastAccessedTime());
    given(this.redisOperations.boundHashOps("spring:session:sessions:session-id")).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps("spring:session:expirations:1404361860000")).willReturn(this.boundSetOperations);
    given(this.redisOperations.boundValueOps("spring:session:sessions:expires:session-id")).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
    verify(this.boundHashOperations).expire(session.getMaxInactiveInterval().plusMinutes(5).getSeconds(), TimeUnit.SECONDS);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 10 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method changeSessionId.

@Test
public void changeSessionId() {
    RedisSession createSession = this.redisRepository.createSession();
    String originalId = createSession.getId();
    String changeSessionId = createSession.changeSessionId();
    assertThat(originalId).isNotEqualTo(changeSessionId);
    assertThat(createSession.getId()).isEqualTo(createSession.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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