Merge branch '3.0.x' into 3.1.x

This commit is contained in:
Marcus Da Coregio
2023-10-18 14:38:38 -03:00
2 changed files with 103 additions and 34 deletions

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.redis;
import java.time.Instant;
import io.lettuce.core.RedisCommandExecutionException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.session.data.redis.RedisIndexedSessionRepository.RedisSession;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisIndexedHttpSession;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.spy;
/**
* Key miss error tests for {@link RedisIndexedSessionRepository}
*
* @author Marcus da Coregio
* @see <a href="https://github.com/spring-projects/spring-session/issues/2021">Related
* GitHub Issue</a>
*/
@ExtendWith(SpringExtension.class)
class RedisIndexedSessionRepositoryDynamicITests extends AbstractRedisITests {
private RedisIndexedSessionRepository sessionRepository;
private RedisOperations<String, Object> spyOperations;
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
// gh-1743
@Test
@SuppressWarnings("unchecked")
void saveChangeSessionIdWhenFailedRenameOperationExceptionContainsMoreDetailsThenIgnoreError() {
this.context.register(Config.class);
refreshAndPrepareFields();
RedisSession toSave = this.sessionRepository.createSession();
String sessionId = toSave.getId();
this.sessionRepository.save(toSave);
RedisSession session = this.sessionRepository.findById(sessionId);
this.sessionRepository.deleteById(sessionId);
String newSessionId = session.changeSessionId();
RedisSystemException redisSystemException = new RedisSystemException(null,
new RedisCommandExecutionException("ERR no such key. channel: [id: 0xec125091,..."));
willThrow(redisSystemException).given(this.spyOperations).rename(any(), any());
this.sessionRepository.save(session);
assertThat(this.sessionRepository.findById(sessionId)).isNull();
assertThat(this.sessionRepository.findById(newSessionId)).isNull();
}
@SuppressWarnings("unchecked")
private void refreshAndPrepareFields() {
this.context.refresh();
this.sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
RedisOperations<String, Object> redisOperations = (RedisOperations<String, Object>) ReflectionTestUtils
.getField(this.sessionRepository, "sessionRedisOperations");
this.spyOperations = spy(redisOperations);
ReflectionTestUtils.setField(this.sessionRepository, "sessionRedisOperations", this.spyOperations);
}
private RedisSession createAndSaveSession(Instant lastAccessedTime) {
RedisSession session = this.sessionRepository.createSession();
session.setLastAccessedTime(lastAccessedTime);
session.setAttribute("attribute1", "value1");
this.sessionRepository.save(session);
return this.sessionRepository.findById(session.getId());
}
@Configuration
@EnableRedisIndexedHttpSession
static class Config extends BaseConfig {
}
}

View File

@@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import io.lettuce.core.RedisCommandExecutionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -28,7 +27,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.DefaultMessage;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -47,13 +45,8 @@ import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
/**
* Integration tests for {@link RedisIndexedSessionRepository}.
@@ -689,33 +682,6 @@ class RedisIndexedSessionRepositoryITests extends AbstractRedisITests {
assertThat(this.repository.findById(copy2.getId())).isNull();
}
// gh-1743
@Test
@SuppressWarnings("unchecked")
void saveChangeSessionIdWhenFailedRenameOperationExceptionContainsMoreDetailsThenIgnoreError() {
RedisOperations<Object, Object> sessionRedisOperations = (RedisOperations<Object, Object>) ReflectionTestUtils
.getField(this.repository, "sessionRedisOperations");
RedisOperations<Object, Object> spyOperations = spy(sessionRedisOperations);
ReflectionTestUtils.setField(this.repository, "sessionRedisOperations", spyOperations);
RedisSession toSave = this.repository.createSession();
String sessionId = toSave.getId();
this.repository.save(toSave);
RedisIndexedSessionRepository.RedisSession session = this.repository.findById(sessionId);
this.repository.deleteById(sessionId);
String newSessionId = session.changeSessionId();
RedisSystemException redisSystemException = new RedisSystemException(null,
new RedisCommandExecutionException("ERR no such key. channel: [id: 0xec125091,..."));
willThrow(redisSystemException).given(spyOperations).rename(any(), any());
this.repository.save(session);
assertThat(this.repository.findById(sessionId)).isNull();
assertThat(this.repository.findById(newSessionId)).isNull();
reset(spyOperations);
}
private String getSecurityName() {
return this.context.getAuthentication().getName();
}