-
Notifications
You must be signed in to change notification settings - Fork 1
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
test: 인증 단위 테스트 구현 #202
Merged
Merged
test: 인증 단위 테스트 구현 #202
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
40fd56b
refactor: JwtAuthenticationFilter 불필요한 로직 제거 (#198)
choidongkuen 5c2b417
refactor: SecurityValue 패키지 이동 (#198)
choidongkuen d4633eb
test: 인증 관련 단위 테스트 구현 (#198)
choidongkuen d35c3ed
test: 인증 관련 단위 테스트 구현 (#198)
choidongkuen b23ec53
test: JwtAuthenticationFilterTest 리팩토링 (#198)
choidongkuen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions
8
src/test/java/net/teumteum/unit/auth/controller/AuthControllerTest.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
4 changes: 2 additions & 2 deletions
4
src/test/java/net/teumteum/unit/auth/service/AuthServiceTest.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
2 changes: 1 addition & 1 deletion
2
...mteum/unit/auth/common/SecurityValue.java → ...t/teumteum/unit/common/SecurityValue.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package net.teumteum.unit.auth.common; | ||
package net.teumteum.unit.common; | ||
|
||
public final class SecurityValue { | ||
|
||
|
64 changes: 64 additions & 0 deletions
64
src/test/java/net/teumteum/unit/core/security/JwtAuthenticationEntryPointTest.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,64 @@ | ||
package net.teumteum.unit.core.security; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import net.teumteum.core.error.ErrorResponse; | ||
import net.teumteum.core.security.filter.JwtAuthenticationEntryPoint; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.mock.web.DelegatingServletOutputStream; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@DisplayName("JwtAuthenticationEntryPoint 단위 테스트의") | ||
public class JwtAuthenticationEntryPointTest { | ||
|
||
private static final String ATTRIBUTE_NAME = "exception"; | ||
@Mock | ||
private ObjectMapper objectMapper; | ||
@Mock | ||
private AuthenticationException authenticationException; | ||
@Mock | ||
private HttpServletRequest request; | ||
@Mock | ||
private HttpServletResponse response; | ||
@InjectMocks | ||
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; | ||
|
||
@Nested | ||
@DisplayName("JwtAuthenticationFilter 에서 인증 예외가 발생시") | ||
class When_authentication_error_occurs_from_filter { | ||
|
||
@Test | ||
@DisplayName("알맞은 예외 메시지와 관련 응답을 반환한다.") | ||
void Return_error_response_with_message() throws IOException { | ||
// given | ||
var errorMessage = "Authentication Exception Occurred"; | ||
var outputStream = new ByteArrayOutputStream(); | ||
|
||
given(request.getAttribute(ATTRIBUTE_NAME)).willReturn(errorMessage); | ||
given(response.getOutputStream()).willReturn(new DelegatingServletOutputStream(outputStream)); | ||
|
||
// when | ||
jwtAuthenticationEntryPoint.commence(request, response, authenticationException); | ||
|
||
// then | ||
verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
verify(objectMapper, times(1)).writeValue(any(OutputStream.class), any(ErrorResponse.class)); | ||
} | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
src/test/java/net/teumteum/unit/core/security/JwtAuthenticationFilterTest.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,136 @@ | ||
package net.teumteum.unit.core.security; | ||
|
||
import static net.teumteum.unit.common.SecurityValue.INVALID_ACCESS_TOKEN; | ||
import static net.teumteum.unit.common.SecurityValue.VALID_ACCESS_TOKEN; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import java.io.IOException; | ||
import net.teumteum.auth.service.AuthService; | ||
import net.teumteum.core.property.JwtProperty; | ||
import net.teumteum.core.security.UserAuthentication; | ||
import net.teumteum.core.security.filter.JwtAuthenticationFilter; | ||
import net.teumteum.core.security.service.JwtService; | ||
import net.teumteum.user.domain.UserFixture; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@DisplayName("JwtAuthenticationFilter 단위 테스트의") | ||
public class JwtAuthenticationFilterTest { | ||
|
||
private static final String ATTRIBUTE_NAME = "exception"; | ||
@Mock | ||
private JwtService jwtService; | ||
@Mock | ||
private AuthService authService; | ||
@Mock | ||
private JwtProperty jwtProperty; | ||
@Mock | ||
private JwtProperty.Access access; | ||
@Mock | ||
private FilterChain filterChain; | ||
@InjectMocks | ||
private JwtAuthenticationFilter jwtAuthenticationFilter; | ||
|
||
@Nested | ||
@DisplayName("API 요청시 JWT 파싱 및 회원 조회 로직은") | ||
class Api_request_with_valid_jwt_unit { | ||
|
||
@BeforeEach | ||
@AfterEach | ||
void clearSecurityContextHolder() { | ||
SecurityContextHolder.clearContext(); | ||
} | ||
|
||
@Test | ||
@DisplayName("유효한 JWT 인 경우, JWT 을 파싱하고 성공적으로 UserAuthentication 을 SecurityContext 에 저장한다.") | ||
void Parsing_jwt_and_save_user_in_security_context() throws ServletException, IOException { | ||
// given | ||
var request = new MockHttpServletRequest(); | ||
var response = new MockHttpServletResponse(); | ||
|
||
given(jwtProperty.getAccess()).willReturn(access); | ||
given(jwtProperty.getAccess().getHeader()).willReturn("Authorization"); | ||
given(jwtProperty.getBearer()).willReturn("Bearer"); | ||
|
||
request.addHeader(jwtProperty.getAccess().getHeader(), | ||
jwtProperty.getBearer() + " " + VALID_ACCESS_TOKEN); | ||
|
||
var user = UserFixture.getIdUser(); | ||
|
||
given(jwtService.validateToken(anyString())).willReturn(true); | ||
given(authService.findUserByAccessToken(anyString())).willReturn(user); | ||
|
||
// when | ||
jwtAuthenticationFilter.doFilterInternal(request, response, filterChain); | ||
|
||
// then | ||
var authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
assertThat(authentication).isInstanceOf(UserAuthentication.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("유효하지 않은 JWT 와 함께 요청이 들어오면, 요청 처리를 중단하고 에러 메세지를 반환한다.") | ||
void Return_error_when_jwt_is_invalid() throws ServletException, IOException { | ||
// given | ||
var request = new MockHttpServletRequest(); | ||
var response = new MockHttpServletResponse(); | ||
|
||
given(jwtProperty.getAccess()).willReturn(access); | ||
given(jwtProperty.getAccess().getHeader()).willReturn("Authorization"); | ||
given(jwtProperty.getBearer()).willReturn("Bearer"); | ||
|
||
request.addHeader(jwtProperty.getAccess().getHeader(), | ||
jwtProperty.getBearer() + " " + INVALID_ACCESS_TOKEN); | ||
|
||
given(jwtService.validateToken(anyString())).willReturn(false); | ||
|
||
// when | ||
jwtAuthenticationFilter.doFilterInternal(request, response, filterChain); | ||
|
||
// then | ||
assertThat(request.getAttribute(ATTRIBUTE_NAME)).isEqualTo("요청에 대한 JWT 가 유효하지 않습니다."); | ||
var authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
assertThat(authentication).isNull(); | ||
verify(filterChain, times(1)).doFilter(request, response); | ||
} | ||
|
||
@Test | ||
@DisplayName("JWT 가 존재하지 않는 경우, 요청 처리를 중단하고 에러 메세지를 반환한다.") | ||
void Return_error_when_jwt_is_not_exist() throws ServletException, IOException { | ||
// given | ||
var request = new MockHttpServletRequest(); | ||
var response = new MockHttpServletResponse(); | ||
|
||
given(jwtProperty.getAccess()).willReturn(access); | ||
given(jwtProperty.getAccess().getHeader()).willReturn("Authorization"); | ||
given(jwtProperty.getBearer()).willReturn("Bearer"); | ||
|
||
request.addHeader(jwtProperty.getAccess().getHeader(), | ||
jwtProperty.getBearer() + " "); | ||
|
||
// when | ||
jwtAuthenticationFilter.doFilterInternal(request, response, filterChain); | ||
|
||
// then | ||
assertThat(request.getAttribute(ATTRIBUTE_NAME)).isEqualTo("요청에 대한 JWT 정보가 존재하지 않습니다."); | ||
verify(jwtService, times(0)).validateToken(anyString()); | ||
} | ||
} | ||
} |
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
5 changes: 3 additions & 2 deletions
5
src/test/java/net/teumteum/unit/user/service/UserServiceTest.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package net.teumteum.unit.user.service; | ||
|
||
import static net.teumteum.unit.auth.common.SecurityValue.VALID_ACCESS_TOKEN; | ||
import static net.teumteum.unit.auth.common.SecurityValue.VALID_REFRESH_TOKEN; | ||
|
||
import static net.teumteum.unit.common.SecurityValue.VALID_ACCESS_TOKEN; | ||
import static net.teumteum.unit.common.SecurityValue.VALID_REFRESH_TOKEN; | ||
Comment on lines
1
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 라인 맞춰주세용 |
||
import static net.teumteum.user.domain.Review.별로에요; | ||
import static net.teumteum.user.domain.Review.최고에요; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테스트 때문에 열어둔거면 @VisibleForTesting 붙여주면 좋을것 같습니당
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋아요~