-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
401 additions
and
6 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
src/main/java/corecord/dev/domain/auth/handler/OAuthLoginSuccessHandler.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
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
3 changes: 2 additions & 1 deletion
3
...a/corecord/dev/common/util/JwtFilter.java → ...ecord/dev/domain/auth/util/JwtFilter.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
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
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
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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package corecord.dev.auth.util; | ||
|
||
import corecord.dev.domain.auth.util.JwtUtil; | ||
import corecord.dev.domain.auth.exception.enums.TokenErrorStatus; | ||
import corecord.dev.domain.auth.exception.model.TokenException; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.security.Keys; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import java.util.Date; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class JwtUtilTest { | ||
|
||
private JwtUtil jwtUtil; | ||
private final String SECRET_KEY = "testsecretkeytestsecretkeytestsecretkeytestsecretkeytestsecretkeytestsecretkeytestsecretkeytestsecretkey"; | ||
private final long REGISTER_TOKEN_EXPIRE_TIME = 1000 * 60 * 60; // 1 hour | ||
private final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24; // 24 hours | ||
private final long REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7 days | ||
private SecretKey key; | ||
private Long userId; | ||
private String providerId; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
userId = 1L; | ||
providerId = "testProvider"; | ||
jwtUtil = new JwtUtil(); | ||
ReflectionTestUtils.setField(jwtUtil, "SECRET_KEY", SECRET_KEY); | ||
ReflectionTestUtils.setField(jwtUtil, "REGISTER_TOKEN_EXPIRATION_TIME", REGISTER_TOKEN_EXPIRE_TIME); | ||
ReflectionTestUtils.setField(jwtUtil, "ACCESS_TOKEN_EXPIRATION_TIME", ACCESS_TOKEN_EXPIRE_TIME); | ||
ReflectionTestUtils.setField(jwtUtil, "REFRESH_TOKEN_EXPIRATION_TIME", REFRESH_TOKEN_EXPIRE_TIME); | ||
key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET_KEY)); | ||
} | ||
|
||
@Test | ||
@DisplayName("액세스 토큰 생성 및 유효성 검사") | ||
void generateAndValidateAccessToken() { | ||
// when | ||
String accessToken = jwtUtil.generateAccessToken(userId); | ||
|
||
// then | ||
assertThat(accessToken).isNotNull().isNotEmpty(); | ||
assertThat(accessToken.split("\\.")).hasSize(3); | ||
|
||
Claims payload = Jwts.parser() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(accessToken) | ||
.getBody(); | ||
|
||
assertThat(payload.get("userId", String.class)).isEqualTo(userId.toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("리프레쉬 토큰 생성 및 유효성 검사") | ||
void generateAndValidateRefreshToken() { | ||
// when | ||
String refreshToken = jwtUtil.generateRefreshToken(userId); | ||
|
||
// then | ||
assertThat(refreshToken).isNotNull().isNotEmpty(); | ||
assertThat(refreshToken.split("\\.")).hasSize(3); | ||
|
||
Claims payload = Jwts.parser() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(refreshToken) | ||
.getBody(); | ||
|
||
assertThat(payload.get("userId", String.class)).isEqualTo(userId.toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("레지스터 토큰 생성 및 유효성 검사") | ||
void generateAndValidateRegisterToken() { | ||
// when | ||
String registerToken = jwtUtil.generateRegisterToken(providerId); | ||
|
||
// then | ||
assertThat(registerToken).isNotNull().isNotEmpty(); | ||
assertThat(registerToken.split("\\.")).hasSize(3); | ||
|
||
Claims payload = Jwts.parser() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(registerToken) | ||
.getBody(); | ||
|
||
assertThat(payload.get("providerId", String.class)).isEqualTo(providerId); | ||
} | ||
|
||
@Test | ||
@DisplayName("임시 토큰 생성 및 유효성 검사") | ||
void generateAndValidateTmpToken() { | ||
// when | ||
String tmpToken = jwtUtil.generateTmpToken(userId); | ||
|
||
// then | ||
assertThat(tmpToken).isNotNull().isNotEmpty(); | ||
assertThat(tmpToken.split("\\.")).hasSize(3); | ||
|
||
Claims payload = Jwts.parser() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(tmpToken) | ||
.getBody(); | ||
|
||
assertThat(payload.get("userId", String.class)).isEqualTo(userId.toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("만료된 액세스 토큰 예외 발생") | ||
void expiredAccessTokenThrowsException() { | ||
// given | ||
String expiredAccessToken = Jwts.builder() | ||
.setSubject(userId.toString()) | ||
.setExpiration(new Date(System.currentTimeMillis() - 1000)) // 이미 만료된 시간 설정 | ||
.signWith(SignatureAlgorithm.HS256, key) | ||
.compact(); | ||
|
||
// then | ||
TokenException exception = assertThrows(TokenException.class, () -> jwtUtil.isAccessTokenValid(expiredAccessToken)); | ||
assertThat(exception.getTokenErrorStatus()).isEqualTo(TokenErrorStatus.INVALID_ACCESS_TOKEN); | ||
} | ||
|
||
|
||
@Test | ||
@DisplayName("유효하지 않은 토큰 예외 발생") | ||
void invalidTokenThrowsException() { | ||
// given | ||
String invalidToken = "invalid.token"; | ||
|
||
// then | ||
TokenException exception = assertThrows(TokenException.class, () -> jwtUtil.isAccessTokenValid(invalidToken)); | ||
assertThat(exception.getTokenErrorStatus()).isEqualTo(TokenErrorStatus.INVALID_ACCESS_TOKEN); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/corecord/dev/user/repository/UserRepositoryTest.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package corecord.dev.user.repository; | ||
|
||
import corecord.dev.domain.user.entity.Status; | ||
import corecord.dev.domain.user.entity.User; | ||
import corecord.dev.domain.user.repository.UserRepository; | ||
import jakarta.persistence.EntityManager; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@DataJpaTest | ||
@Transactional | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
public class UserRepositoryTest { | ||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Test | ||
@DisplayName("UserId로 회원 삭제") | ||
void deleteUserByUserId() { | ||
// Given | ||
User user = createTestUser(); | ||
userRepository.save(user); | ||
|
||
// When | ||
userRepository.deleteUserByUserId(user.getUserId()); | ||
entityManager.flush(); | ||
entityManager.clear(); | ||
|
||
// Then | ||
Optional<User> deletedUser = userRepository.findById(user.getUserId()); | ||
assertThat(deletedUser).isEmpty(); | ||
} | ||
|
||
|
||
private User createTestUser() { | ||
return User.builder() | ||
.userId(1L) | ||
.providerId("providerId") | ||
.nickName("testUser") | ||
.status(Status.UNIVERSITY_STUDENT) | ||
.abilities(new ArrayList<>()) | ||
.chatRooms(new ArrayList<>()) | ||
.folders(new ArrayList<>()) | ||
.records(new ArrayList<>()) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.