Skip to content

Commit

Permalink
Hotfix: 테스트 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
tioon committed Apr 30, 2024
1 parent 1567afd commit dcb74fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.web.baebaeBE.infra.member.repository.MemberRepository;
import com.web.baebaeBE.presentation.kakao.dto.KakaoUserInfoDto;
import com.web.baebaeBE.presentation.member.dto.MemberRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -28,6 +29,7 @@
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.Optional;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -60,7 +62,7 @@ public class MemberIntegrationTest {
@BeforeEach
void setup() {
Member testMember = memberRepository.save(Member.builder()
.email("user@gmail.com")
.email("test@gmail.com")
.nickname("김예찬")
.memberType(MemberType.KAKAO)
.refreshToken("null")
Expand All @@ -73,6 +75,14 @@ void setup() {
memberRepository.save(testMember);
}

//각 테스트 후마다 실행
@AfterEach
void tearDown() {
Optional<Member> member = memberRepository.findByEmail("[email protected]");
if(member.isPresent())
memberRepository.delete(member.get());
}


@Test
@DisplayName("oAuth2 기반 로그인 테스트(): 가짜 카카오 토큰을 통해 로그인 및 회원가입 테스트한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -26,6 +27,7 @@
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
Expand All @@ -39,40 +41,49 @@ class TokenProviderTest {
private JwtTokenProvider tokenProvider;

@Autowired
private MemberRepository userRepository;
private MemberRepository memberRepository;

@Autowired
private JwtProperties jwtProperties;


//각 테스트 후마다 실행
@AfterEach
void tearDown() {
Optional<Member> member = memberRepository.findByEmail("[email protected]");
if(member.isPresent())
memberRepository.delete(member.get());
}

@Test
@DisplayName("토큰 생성 테스트(): 테스트용 유저정보와 만료기간을 전달해 새로운 토큰을 생성할 수 있다.")
void generateTokenTest() {
// given
Member testMember = userRepository.save(Member.builder()
.email("user@gmail.com")
Member testMember = memberRepository.save(Member.builder()
.email("test@gmail.com")
.memberType(MemberType.KAKAO)
.refreshToken("null")
.build());

// when
String token = tokenProvider.generateToken(testMember, Duration.ofDays(14));
Long userId = Long.valueOf(Jwts.parserBuilder()
String email = Jwts.parserBuilder()
.setSigningKey(jwtProperties.getSecretKey())
.build()
.parseClaimsJws(token)
.getBody()
.getId());
.getId();

// then
assertThat(userId).isEqualTo(testMember.getId());
assertThat(email).isEqualTo(testMember.getEmail());
}

@Test
@DisplayName("토큰 검증 실패 테스트(): 만료된 토큰인 경우에 유효성 검증에 실패한다.")
void invalidTokenTest1() {
// given
Member testMember = userRepository.save(Member.builder()
.email("user@gmail.com")
Member testMember = memberRepository.save(Member.builder()
.email("test@gmail.com")
.memberType(MemberType.KAKAO)
.refreshToken("null")
.build());
Expand Down Expand Up @@ -103,8 +114,8 @@ void invalidTokenTest2() {
@DisplayName("토큰 검증 성공 테스트(): 유효한 토큰인 경우에 유효성 검증에 성공한다.")
void validToken_validToken() {
// given
Member testMember = userRepository.save(Member.builder()
.email("user@gmail.com")
Member testMember = memberRepository.save(Member.builder()
.email("test@gmail.com")
.memberType(MemberType.KAKAO)
.refreshToken("null")
.build());
Expand All @@ -122,8 +133,8 @@ void validToken_validToken() {
@DisplayName("토큰 인증정보 테스트(): 토큰 기반으로 인증정보를 가져올 수 있다.")
void getAuthentication() {
// given
Member testMember = userRepository.save(Member.builder()
.email("user@gmail.com")
Member testMember = memberRepository.save(Member.builder()
.email("test@gmail.com")
.memberType(MemberType.KAKAO)
.refreshToken("null")
.build());
Expand Down

0 comments on commit dcb74fb

Please sign in to comment.