-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
116 additions
and
114 deletions.
There are no files selected for viewing
26 changes: 13 additions & 13 deletions
26
cakey-auth/src/test/java/com/cakey/TestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
204
cakey-auth/src/test/java/com/cakey/jwt/auth/JwtGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); // ํค์ ํด๋นํ๋ ๊ฐ์ด ์ญ์ ๋์๋์ง ํ์ธ | ||
// } | ||
//} |