Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat, test : redis 전체 삭제 기능 구현 및 테스트 코드 추가 #175

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public ResponseEntity<LoginResponse> kakaoCallback(@RequestParam("code") String
return ResponseEntity.ok().body(loginResponse);
}

@Operation(summary = "Redis안의 모든 데이터 제거", description = "발급된 refreshToken을 사용하지 못하게 Redis 안의 모든 데이터를 제거합니다.")
@DeleteMapping("/redis")
public ResponseEntity<Void> deleteAllDataFromRedis(){
tokenService.deleteAllDataFromRedis();
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,11 @@ public TokenResponse refreshAccessToken(String refreshToken) {

return new TokenResponse(newAccessToken, newRefreshToken);
}

public void deleteAllDataFromRedis(){
redisTemplate.getConnectionFactory()
.getConnection()
.serverCommands()
.flushAll();
}
}
3 changes: 0 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
logging.level.org.hibernate.orm.jdbc.bind=TRACE
spring.profiles.active=dev
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=sinitto
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.h2.console.enabled=false
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoSettings;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

Expand All @@ -23,6 +26,15 @@ public class TokenServiceTest {
@Mock
private HashOperations<String, Object, Object> hashOperations;

@Mock
private RedisConnectionFactory redisConnectionFactory;

@Mock
private RedisConnection redisConnection;

@Mock
private RedisServerCommands redisServerCommands;

private TokenService tokenService;

@BeforeEach
Expand Down Expand Up @@ -65,4 +77,31 @@ void generateRefreshTokenTest() {
assertTrue(token.startsWith("ey"));
assertEquals(email, resultEmail);
}

@Test
@DisplayName("deleteAllDataFromRedis 메소드 테스트")
void deleteAllDataFromRedisTest(){
//given
String email = "[email protected]";
String refreshToken = "test.Refresh.Token";

when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(redisTemplate.getConnectionFactory()).thenReturn(redisConnectionFactory);
when(redisConnectionFactory.getConnection()).thenReturn(redisConnection);
when(redisConnection.serverCommands()).thenReturn(redisServerCommands);

redisTemplate.opsForHash().put(email, "refreshToken1", refreshToken);
redisTemplate.opsForHash().put(email, "refreshToken2", refreshToken);
redisTemplate.opsForHash().put(email, "refreshToken3", refreshToken);
redisTemplate.opsForHash().put(email, "refreshToken4", refreshToken);

//when
tokenService.deleteAllDataFromRedis();

//then
assertNull(redisTemplate.opsForHash().get(email, "refreshToken1"));
assertNull(redisTemplate.opsForHash().get(email, "refreshToken2"));
assertNull(redisTemplate.opsForHash().get(email, "refreshToken3"));
assertNull(redisTemplate.opsForHash().get(email, "refreshToken4"));
}
}