Skip to content

Commit

Permalink
Merge pull request #45 from KUSITMS-30th-TEAM-A/feature/#43/user-test
Browse files Browse the repository at this point in the history
[feat] User, Auth 도메인의 테스트코드를 작성한다.
  • Loading branch information
juuuunny authored Nov 4, 2024
2 parents 538d7de + a70d2bb commit c1fed80
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 85 deletions.
Binary file modified dump.rdb
Binary file not shown.
16 changes: 8 additions & 8 deletions src/main/java/kusitms/backend/auth/jwt/JWTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public JWTUtil(@Value("${spring.jwt.secret}") String secret) {
public String generateToken(Long userId, long expirationMillis) {
return Jwts.builder()
.claim("userId", userId)
.setIssuedAt(new Date(System.currentTimeMillis())) // setIssuedAt 사용
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + expirationMillis))
.signWith(secretKey, SignatureAlgorithm.HS256) // secretKey와 알고리즘을 함께 명시
.signWith(secretKey, SignatureAlgorithm.HS256)
.compact();
}

Expand All @@ -41,20 +41,20 @@ public String generateRegisterToken(String provider, String providerId, String e
.claim("provider", provider)
.claim("providerId", providerId)
.claim("email", email)
.setIssuedAt(new Date(System.currentTimeMillis())) // setIssuedAt 사용
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + expirationMillis))
.signWith(secretKey, SignatureAlgorithm.HS256) // secretKey와 알고리즘을 함께 명시
.signWith(secretKey, SignatureAlgorithm.HS256)
.compact();
}

// 토큰 파싱 메소드
private Claims tokenParser(String token) {
try {
return Jwts.parserBuilder()
.setSigningKey(secretKey) // setSigningKey 사용
.setSigningKey(secretKey)
.build()
.parseClaimsJws(token)
.getBody(); // getBody()로 Claims 가져옴
.getBody();
} catch (ExpiredJwtException e) {
log.error("Expired JWT : {}", token);
throw new CustomException(AuthErrorStatus._EXPIRED_TOKEN);
Expand All @@ -68,10 +68,10 @@ private Claims tokenParser(String token) {
private Claims refreshTokenParser(String token) {
try {
return Jwts.parserBuilder()
.setSigningKey(secretKey) // setSigningKey 사용
.setSigningKey(secretKey)
.build()
.parseClaimsJws(token)
.getBody(); // getBody()로 Claims 가져옴
.getBody();
} catch (ExpiredJwtException e) {
log.error("Expired Refresh Token : {}", token);
throw new CustomException(AuthErrorStatus._EXPIRED_REFRESH_TOKEN);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kusitms.backend.user.application;

import kusitms.backend.auth.jwt.JWTUtil;
import kusitms.backend.auth.jwt.SecurityContextProvider;
import kusitms.backend.auth.status.AuthErrorStatus;
import kusitms.backend.global.exception.CustomException;
import kusitms.backend.global.redis.RedisManager;
Expand Down Expand Up @@ -61,8 +60,8 @@ public void signupUser(String registerToken, SignUpRequestDto request) {
}

@Transactional(readOnly = true)
public UserInfoResponseDto getUserInfo() {
Long userId = SecurityContextProvider.getAuthenticatedUserId();
public UserInfoResponseDto getUserInfo(String accessToken) {
Long userId = jwtUtil.getUserIdFromToken(accessToken);
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(UserErrorStatus._NOT_FOUND_USER));
log.info("유저 정보 조회 성공");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public ResponseEntity<ApiResponse<Void>> signupUser(
* @return 닉네임, 이메일
*/
@GetMapping("/user-info")
public ResponseEntity<ApiResponse<UserInfoResponseDto>> getUserInfo() {
return ApiResponse.onSuccess(UserSuccessStatus._OK_GET_USER_INFO, userService.getUserInfo());
public ResponseEntity<ApiResponse<UserInfoResponseDto>> getUserInfo(
@CookieValue String accessToken
) {
return ApiResponse.onSuccess(UserSuccessStatus._OK_GET_USER_INFO, userService.getUserInfo(accessToken));
}

/**
Expand Down
Loading

0 comments on commit c1fed80

Please sign in to comment.