Skip to content

Commit

Permalink
test: 만료된 토큰 요청, 회원가입 테스트(#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
acceptor-gyu committed Jun 15, 2023
1 parent 814fdf6 commit bbb8c6f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions be/src/main/java/com/secondhand/user/login/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public class JwtUtil {
@Value("${JWT_SECRET_KEY}")
private String secret; // 시크릿 키를 설정

public String createToken(LoggedInUser loggedInUser) {
public String createToken(LoggedInUser loggedInUser, Date expiredDate) {
log.info("create Token start");
return Jwts.builder()
.setHeaderParam("typ", "JWT")
.setSubject("login_member")
.claim("userProfile", loggedInUser)
.setExpiration(new Date((new Date()).getTime() + 3600000))
.setExpiration(expiredDate)
.signWith(SignatureAlgorithm.HS256, secret)
.compact();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.secondhand.user.login;

import com.secondhand.user.entity.User;
import com.secondhand.user.login.dto.GithubToken;
import com.secondhand.user.login.dto.JWTResponse;
import com.secondhand.user.login.dto.LoggedInUser;
Expand All @@ -12,6 +11,7 @@
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Slf4j
@RestController
Expand All @@ -29,7 +29,9 @@ public ResponseEntity<JWTResponse> githubLogin(String code, HttpServletResponse
UserProfileResponse userProfile = loginService.getUserProfile(githubToken.getAccessToken());
LoggedInUser loggedInUser = loginService.createUser(userProfile);

String token = jwtUtil.createToken(loggedInUser);
Date expiredDate = new Date(new Date().getTime() + 3600000);

String token = jwtUtil.createToken(loggedInUser, expiredDate);

return ResponseEntity.ok(new JWTResponse("login success", token));
}
Expand Down
49 changes: 45 additions & 4 deletions be/src/test/java/com/secondhand/user/login/JwtUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.secondhand.user.entity.User;
import com.secondhand.user.login.dto.LoggedInUser;
import com.secondhand.user.login.dto.UserProfileResponse;
import com.secondhand.user.repository.UserRepository;
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 javax.transaction.Transactional;
import java.util.Date;

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

Expand All @@ -18,16 +20,21 @@ class JwtUtilTest {
@Autowired
private JwtUtil jwtUtil;

@Autowired
private LoginService loginService;

@Autowired
private UserRepository userRepository;

@DisplayName("유저가 로그인을 하면 해당 유저의 정보가 담긴 JWT 토근을 만들 수 있다.")
@Transactional
@Test
void testCreateToken() {

// given
LoggedInUser loggedInUser = getLoggedInUser();

// when
String token = jwtUtil.createToken(loggedInUser);
String token = jwtUtil.createToken(loggedInUser, new Date(new Date().getTime() + 3600000));
LoggedInUser extractedUserFromToken = jwtUtil.extractedUserFromToken(token);

// then
Expand All @@ -39,13 +46,13 @@ void testCreateToken() {
assertThat(loggedInUser.getSecondRegionId()).isEqualTo(extractedUserFromToken.getSecondRegionId());
}

@DisplayName("조작된 토큰이 들어오면 ManipulatedTokenException이 발생한다.")
@DisplayName("조작된 토큰이 들어오면 false를 반환한다")
@Test
void testManipulatedToken() {

// given
LoggedInUser loggedInUser = getLoggedInUser();
String token = jwtUtil.createToken(loggedInUser);
String token = jwtUtil.createToken(loggedInUser, new Date(new Date().getTime() + 3600000));

// when
boolean validated = jwtUtil.validateTokenIsManipulated(token + "a");
Expand All @@ -54,6 +61,40 @@ void testManipulatedToken() {
assertThat(validated).isFalse();
}

@DisplayName("만료된 토큰이 들어오면 false를 반환한다.")
@Test
void testExpiredToken() {

// given
LoggedInUser loggedInUser = getLoggedInUser();
String token = jwtUtil.createToken(loggedInUser, new Date(new Date().getTime() - 3600000));

// when
boolean validated = jwtUtil.validateTokenIsExpired(token);

// then
assertThat(validated).isFalse();
}

@DisplayName("회원가입을 하면 유저 정보를 저장한다.")
@Transactional
@Test
void testCreateUser() {

// given
UserProfileResponse userProfileResponse = new UserProfileResponse();
userProfileResponse.setId(200L);
userProfileResponse.setName("test");
userProfileResponse.setAvatarUrl("test");

// when
LoggedInUser user = loginService.createUser(userProfileResponse);
User foundUser = userRepository.findById(user.getId()).orElseThrow();

// then
assertThat(user.getId()).isEqualTo(foundUser.getId());
}

private LoggedInUser getLoggedInUser() {
UserProfileResponse userProfileResponse = new UserProfileResponse();
userProfileResponse.setId(200L);
Expand Down

0 comments on commit bbb8c6f

Please sign in to comment.