Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 불필요한 컨텍스트 사용 제거 및 DI를 활용해 테스트 성능 개선 #734

Open
wants to merge 5 commits into
base: be/feature/729-test-refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harustudy.backend.auth.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Clock;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
Expand All @@ -14,6 +15,8 @@
@Component
public class JwtTokenProvider {

private final Clock clock;

@Builder
public String createAccessToken(String subject, Long accessTokenExpireLength, String secretKey) {
Claims claims = generateClaims(subject, accessTokenExpireLength);
Expand All @@ -28,7 +31,7 @@ public String createAccessToken(String subject, Long accessTokenExpireLength, St
}

private Claims generateClaims(String subject, Long accessTokenExpireLength) {
Date now = new Date();
Date now = clock.now();
Date expiredAt = new Date(now.getTime() + accessTokenExpireLength);

return Jwts.claims()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import harustudy.backend.participantcode.domain.CodeGenerationStrategy;
import harustudy.backend.participantcode.domain.GenerationStrategy;
import io.jsonwebtoken.Clock;
import io.jsonwebtoken.impl.DefaultClock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -12,4 +14,9 @@ public class BeanConfig {
public GenerationStrategy generationStrategy() {
return new CodeGenerationStrategy();
}

@Bean
public Clock clock() {
return new DefaultClock();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import harustudy.backend.auth.dto.AuthMember;
import harustudy.backend.auth.service.AuthService;
import harustudy.backend.auth.util.BearerAuthorizationParser;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest;
Expand All @@ -23,21 +24,15 @@

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(ReplaceUnderscores.class)
@SpringBootTest
@ExtendWith(MockitoExtension.class)
class AuthArgumentResolverTest {

@Autowired
@InjectMocks
private AuthArgumentResolver authArgumentResolver;

@MockBean
private AuthService authService;

@Mock
private MethodParameter methodParameter;
@Mock
private ModelAndViewContainer modelAndViewContainer;
private AuthService authService;
@Mock
private WebDataBinderFactory webDataBinderFactory;
private BearerAuthorizationParser bearerAuthorizationParser;

@Test
void 액세스_토큰의_파싱된_아이디에_해당하는_인증_멤버가_반환된다() {
Expand All @@ -48,12 +43,13 @@ class AuthArgumentResolverTest {
request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
NativeWebRequest nativeWebRequest = new ServletWebRequest(request);

given(bearerAuthorizationParser.parse(any(String.class)))
.willReturn(accessToken);
given(authService.parseMemberId(any(String.class)))
.willReturn(mockedAuthMemberId);

// when
AuthMember authMember = authArgumentResolver.resolveArgument(methodParameter,
modelAndViewContainer, nativeWebRequest, webDataBinderFactory);
AuthMember authMember = authArgumentResolver.resolveArgument(null, null, nativeWebRequest, null);

// then
assertThat(authMember.id()).isEqualTo(Long.valueOf(mockedAuthMemberId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
import static org.mockito.Mockito.times;

import harustudy.backend.auth.service.AuthService;
import harustudy.backend.auth.util.BearerAuthorizationParser;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -19,14 +25,16 @@

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(ReplaceUnderscores.class)
@SpringBootTest
@ExtendWith(MockitoExtension.class)
class AuthInterceptorTest {

@Autowired
@InjectMocks
private AuthInterceptor authInterceptor;

@MockBean
@Mock
private AuthService authService;
@Spy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spy위에 개행 한 칸 해주면 좋을 것 같습니다!

private BearerAuthorizationParser bearerAuthorizationParser = new BearerAuthorizationParser();

@Test
void preflight_요청시_예외를_던지지_않는다() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package harustudy.backend.integration;

import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import harustudy.backend.auth.domain.RefreshToken;
import harustudy.backend.auth.dto.OauthLoginRequest;
import harustudy.backend.auth.dto.OauthTokenResponse;
import harustudy.backend.auth.dto.TokenResponse;
import harustudy.backend.member.domain.Member;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.BeforeEach;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SuppressWarnings("NonAsciiCharacters")
class AuthIntegrationTest extends IntegrationTest {
Expand Down Expand Up @@ -63,9 +64,8 @@ class AuthIntegrationTest extends IntegrationTest {
// given
MemberDto memberDto = createMember("member1");

// access token을 재발급 하더라도 Date는 초 단위의 시간 정보를 담은 액세스 토큰을 생성하기 때문에
// 같은 access token이 만들어지는 문제가 있어서 갱신된다는 것을 검증하기 위해 사용
Thread.sleep(1000);
Date now = new Date();
when(customClock.now()).thenReturn(new Date(now.getTime() + 1000L));

// when
MvcResult result = mockMvc.perform(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package harustudy.backend.integration;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import harustudy.backend.auth.config.TokenConfig;
import harustudy.backend.auth.domain.RefreshToken;
import harustudy.backend.auth.domain.oauth.OauthClients;
import harustudy.backend.auth.dto.OauthLoginRequest;
import harustudy.backend.auth.dto.OauthTokenResponse;
import harustudy.backend.auth.dto.TokenResponse;
import harustudy.backend.auth.util.JwtTokenProvider;
import harustudy.backend.member.domain.LoginType;
import harustudy.backend.member.domain.Member;
import harustudy.backend.participantcode.domain.GenerationStrategy;
import io.jsonwebtoken.Clock;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.servlet.http.Cookie;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.util.Date;


@DisplayNameGeneration(ReplaceUnderscores.class)
Expand All @@ -55,14 +47,23 @@ class IntegrationTest {
@Autowired
private TokenConfig tokenConfig;

@MockBean
protected OauthClients oauthClients;

@MockBean
protected Clock customClock;

@Autowired
protected GenerationStrategy generationStrategy;

@MockBean
protected OauthClients oauthClients;
@BeforeEach
void setUp() {
when(customClock.now()).thenReturn(new Date());
}

protected MemberDto createMember(String name) {
Member member = generateAndSaveMemberNamedWith(name);
when(customClock.now()).thenReturn(new Date());
String accessToken = jwtTokenProvider.createAccessToken(String.valueOf(member.getId()),
tokenConfig.accessTokenExpireLength(), tokenConfig.secretKey());
RefreshToken refreshToken = generateAndSaveRefreshTokenOf(member);
Expand Down