Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 committed Jan 24, 2025
1 parent 8284566 commit b8ba6a3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 114 deletions.
26 changes: 13 additions & 13 deletions cakey-auth/src/test/java/com/cakey/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.cakey;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
@SpringBootApplication
@ComponentScan(basePackages = "com.cakey.jwt") // ์‹ค์ œ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋กœ ์ˆ˜์ •
public class TestConfiguration {
}
//package com.cakey;
//
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//@EnableCaching
//@SpringBootApplication
//@ComponentScan(basePackages = "com.cakey.jwt") // ์‹ค์ œ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋กœ ์ˆ˜์ •
//public class TestConfiguration {
//}
204 changes: 103 additions & 101 deletions cakey-auth/src/test/java/com/cakey/jwt/auth/JwtGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -1,101 +1,103 @@
package com.cakey.jwt.auth;

import com.cakey.TestConfiguration;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;


@SpringBootTest(classes = TestConfiguration.class)
@TestPropertySource(properties = {
"jwt.secret=testasdfasdfasddfsecretfdadfsdfasdfasdfasdfasdf",
"jwt.accessTokenExpirationTime=3600",
"jwt.refreshTokenExpirationTime=604800"
})
@ComponentScan(basePackages = "com.cakey")
class JwtGeneratorTest {

@Autowired
private JwtGenerator jwtGenerator;

@Autowired
private CacheManager cacheManager;

@Autowired
private JwtProvider jwtProvider;

@Test
@DisplayName("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ์— ๋“ฑ๋ก")
void generateRefreshToken() {
// Given
// ์บ์‹œ ์ดˆ๊ธฐํ™”
cacheManager.getCacheNames().forEach(cacheName -> {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.clear();
}
});

long firstId = 123L;
long secondId = 456L;

final String token1 = jwtGenerator.generateRefreshToken(firstId);
System.out.println("token 1 : " + token1);

final String token2 = jwtGenerator.generateRefreshToken(secondId);
System.out.println("token 2 : " + token2);

// When
final Cache cache = cacheManager.getCache("refresh");

// Then
// ์บ์‹œ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
final String firstCachedToken = cache.get(firstId, String.class);
assertThat(firstCachedToken).isNotNull();
System.out.println("token 1: " + firstCachedToken);
assertThat(firstCachedToken).isEqualTo(token1); // ์บ์‹œ๋œ ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ํ† ํฐ๊ณผ ๋™์ผํ•ด์•ผ ํ•จ

final String secondCachedToken = cache.get(secondId, String.class);
assertThat(secondCachedToken).isNotNull();
System.out.println("token 2: " + secondCachedToken);
assertThat(secondCachedToken).isEqualTo(token2); // ์บ์‹œ๋œ ๊ฐ’์ด ๋‘ ๋ฒˆ์งธ ํ† ํฐ๊ณผ ๋™์ผํ•ด์•ผ ํ•จ
}

@Test
@DisplayName("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ ์‚ญ์ œ")
void deleteRefreshToken() {
//Given
final long userId = 123L;

Cache cache = cacheManager.getCache("refresh");
System.out.println(cache);
assertThat(cache).isNotNull();

String cachedValue = cache.get(userId, String.class);
System.out.println(cachedValue);
assertThat(cachedValue).isNotNull();

//When
jwtProvider.deleteRefreshToken(userId);

//Then
cache = cacheManager.getCache("refresh");
System.out.println(cache);

cachedValue = cache.get(userId, String.class);
System.out.println(cachedValue);

Assertions.assertThat(cachedValue).isNull(); // ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์‚ญ์ œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
}
}
//package com.cakey.jwt.auth;
//
//import com.cakey.TestConfiguration;
//import org.assertj.core.api.Assertions;
//import org.junit.jupiter.api.DisplayName;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.cache.Cache;
//import org.springframework.cache.CacheManager;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.test.context.ActiveProfiles;
//import org.springframework.test.context.TestPropertySource;
//
//import static org.assertj.core.api.Assertions.assertThat;
//import static org.assertj.core.api.Assertions.assertThatThrownBy;
//
//
//@SpringBootTest(classes = TestConfiguration.class, properties = {
// "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
//})
//@TestPropertySource(properties = {
// "jwt.secret=testasdfasdfasddfsecretfdadfsdfasdfasdfasdfasdf",
// "jwt.accessTokenExpirationTime=3600",
// "jwt.refreshTokenExpirationTime=604800"
//})
//@ComponentScan(basePackages = "com.cakey.jwt.auth")
//class JwtGeneratorTest {
//
// @Autowired
// private JwtGenerator jwtGenerator;
//
// @Autowired
// private CacheManager cacheManager;
//
// @Autowired
// private JwtProvider jwtProvider;
//
// @Test
// @DisplayName("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ์— ๋“ฑ๋ก")
// void generateRefreshToken() {
// // Given
// // ์บ์‹œ ์ดˆ๊ธฐํ™”
// cacheManager.getCacheNames().forEach(cacheName -> {
// Cache cache = cacheManager.getCache(cacheName);
// if (cache != null) {
// cache.clear();
// }
// });
//
// long firstId = 123L;
// long secondId = 456L;
//
// final String token1 = jwtGenerator.generateRefreshToken(firstId);
// System.out.println("token 1 : " + token1);
//
// final String token2 = jwtGenerator.generateRefreshToken(secondId);
// System.out.println("token 2 : " + token2);
//
// // When
// final Cache cache = cacheManager.getCache("refresh");
//
// // Then
// // ์บ์‹œ์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
// final String firstCachedToken = cache.get(firstId, String.class);
// assertThat(firstCachedToken).isNotNull();
// System.out.println("token 1: " + firstCachedToken);
// assertThat(firstCachedToken).isEqualTo(token1); // ์บ์‹œ๋œ ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ํ† ํฐ๊ณผ ๋™์ผํ•ด์•ผ ํ•จ
//
// final String secondCachedToken = cache.get(secondId, String.class);
// assertThat(secondCachedToken).isNotNull();
// System.out.println("token 2: " + secondCachedToken);
// assertThat(secondCachedToken).isEqualTo(token2); // ์บ์‹œ๋œ ๊ฐ’์ด ๋‘ ๋ฒˆ์งธ ํ† ํฐ๊ณผ ๋™์ผํ•ด์•ผ ํ•จ
// }
//
// @Test
// @DisplayName("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์บ์‹œ ์‚ญ์ œ")
// void deleteRefreshToken() {
// //Given
// final long userId = 123L;
//
// Cache cache = cacheManager.getCache("refresh");
// System.out.println(cache);
// assertThat(cache).isNotNull();
//
// String cachedValue = cache.get(userId, String.class);
// System.out.println(cachedValue);
// assertThat(cachedValue).isNotNull();
//
// //When
// jwtProvider.deleteRefreshToken(userId);
//
// //Then
// cache = cacheManager.getCache("refresh");
// System.out.println(cache);
//
// cachedValue = cache.get(userId, String.class);
// System.out.println(cachedValue);
//
// Assertions.assertThat(cachedValue).isNull(); // ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์‚ญ์ œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
// }
//}

0 comments on commit b8ba6a3

Please sign in to comment.