From 39385f50d63d34b7cdbb92c76b7181178465c9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=87=E1=85=A1=E1=86=A8=E1=84=8B=E1=85=B3=E1=86=AB?= =?UTF-8?q?=E1=84=8C=E1=85=B5?= Date: Tue, 31 Oct 2023 17:25:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?test:=20UserController=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=A3=BC=EC=84=9D=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserControllerTest.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java b/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java index 6223eaa4..a54e4563 100644 --- a/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java +++ b/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java @@ -7,7 +7,6 @@ import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willDoNothing; -import static org.mockito.Mockito.when; import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; @@ -61,11 +60,13 @@ class UserControllerTest extends ControllerTestConfig { @Test @DisplayName("회원가입을 할 수 있다.") void signupTest() throws Exception { + // given SignupHttpDto.Request request = SignupDtoFixture.signupDto(); AuthTokens authTokens = new AuthTokens("accessToken", "refreshToken"); given(userService.signup(any(), any(), any(), any())).willReturn(authTokens); + // when, then mockMvc.perform(post("/api/v1/users/sign-up") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) @@ -94,10 +95,12 @@ void signupTest() throws Exception { @Test @DisplayName("로그인을 할 수 있다.") void loginTest() throws Exception { + // given AuthTokens authTokens = new AuthTokens("accessToken", "refreshToken"); given(userService.login(any(), any())).willReturn(authTokens); + // when, then mockMvc.perform(get("/api/v1/users/login/{oAuthProvider}", OAuthProvider.KAKAO) .contentType(MediaType.APPLICATION_JSON) .param("authCode", "authCode")) @@ -125,11 +128,13 @@ void loginTest() throws Exception { @Test @DisplayName("사용자 프로필을 조회할 수 있다.") void findUserProfileTest() throws Exception { - Long userId = 1L; + // given + long userId = 1L; UserProfileDto.Response response = UserProfileDtoFixture.userProfileDtoResponse(); given(userService.findUserProfile(userId)).willReturn(response); + // when, then mockMvc.perform(get("/api/v1/users/{id}", userId) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) @@ -159,6 +164,7 @@ void findUserProfileTest() throws Exception { @Test @DisplayName("마이페이지를 조회할 수 있다.") void findMyProfileTest() throws Exception { + // given Long userId = 1L; RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); @@ -167,7 +173,11 @@ void findMyProfileTest() throws Exception { when(jwtTokenProvider.extractUserId(TOKEN)).thenReturn(userId); when(userService.findMyProfile(anyLong())).thenReturn(response); + given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); + given(jwtTokenProvider.extractUserId(TOKEN)).willReturn(userId); + given(userService.findMyProfile(anyLong())).willReturn(response); + // when, then mockMvc.perform(get("/api/v1/users/me") .header("Authorization", TOKEN) .contentType(MediaType.APPLICATION_JSON) @@ -199,6 +209,7 @@ void findMyProfileTest() throws Exception { @Test @DisplayName("본인 프로필 사진을 수정할 수 있다.") void updateProfileImageTest() throws Exception { + // given Long userId = 1L; RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); @@ -210,6 +221,7 @@ void updateProfileImageTest() throws Exception { "image/png", "testImage".getBytes()); + // when, given mockMvc.perform((multipart("/api/v1/users/me/profile-image") .file("profileImage", file.getBytes()) .header("Authorization", TOKEN) @@ -232,6 +244,7 @@ void updateProfileImageTest() throws Exception { @Test @DisplayName("본인 프로필 정보를 수정할 수 있다.") void updateProfileInfoTest() throws Exception { + // given Long userId = 1L; Request request = UpdateProfileDtoFixture.updateProfileDtoRequest(); RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); @@ -241,6 +254,7 @@ void updateProfileInfoTest() throws Exception { willDoNothing().given( userService).updateProfileInfo(any(), any(), any()); + // when, given mockMvc.perform(patch("/api/v1/users/me") .header("Authorization", TOKEN) .contentType(MediaType.APPLICATION_JSON) @@ -265,12 +279,14 @@ void updateProfileInfoTest() throws Exception { @Test @DisplayName("닉네임 중복을 확인할 수 있다.") void checkNicknameDuplicationTest() throws Exception { + // given User user = user(); String nickname = user.getProfile().getNickname(); RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); given(refreshTokenQuery.getRefreshToken(anyLong())).willReturn(refreshToken); + // when, given mockMvc.perform(get("/api/v1/users/duplicate") .param("nickname", nickname) ) From 7c18d8735eafee801194666d23f1b0022adcac16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=87=E1=85=A1=E1=86=A8=E1=84=8B=E1=85=B3=E1=86=AB?= =?UTF-8?q?=E1=84=8C=E1=85=B5?= Date: Tue, 31 Oct 2023 18:06:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?test:=20UserController=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=82=A0=EC=A7=9C=20=ED=98=95=EC=8B=9D=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserControllerTest.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java b/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java index a54e4563..56e70b22 100644 --- a/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java +++ b/src/test/java/coffeemeet/server/user/controller/UserControllerTest.java @@ -43,6 +43,7 @@ import coffeemeet.server.user.service.dto.MyProfileDto.Response; import coffeemeet.server.user.service.dto.UserProfileDto; import com.epages.restdocs.apispec.Schema; +import java.time.format.DateTimeFormatter; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -167,13 +168,9 @@ void findMyProfileTest() throws Exception { // given Long userId = 1L; RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); - - given(refreshTokenQuery.getRefreshToken(anyLong())).willReturn(refreshToken); Response response = MyProfileDtoFixture.myProfileDtoResponse(); - when(jwtTokenProvider.extractUserId(TOKEN)).thenReturn(userId); - when(userService.findMyProfile(anyLong())).thenReturn(response); - given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); + given(refreshTokenQuery.getRefreshToken(anyLong())).willReturn(refreshToken); given(jwtTokenProvider.extractUserId(TOKEN)).willReturn(userId); given(userService.findMyProfile(anyLong())).willReturn(response); @@ -203,7 +200,19 @@ void findMyProfileTest() throws Exception { ) ) ) - .andExpect(status().isOk()); + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name").value(response.name())) + .andExpect(jsonPath("$.nickname").value(response.nickname())) + .andExpect(jsonPath("$.email").value(response.email())) + .andExpect(jsonPath("$.profileImageUrl").value(response.profileImageUrl())) + .andExpect(jsonPath("$.birthYear").value(response.birthYear())) + .andExpect(jsonPath("$.birthDay").value(response.birthDay())) + .andExpect(jsonPath("$.reportedCount").value(response.reportedCount())) + .andExpect( + jsonPath("$.sanctionPeriod").value(response.sanctionPeriod() + .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS")))) + .andExpect(jsonPath("$.department").value(String.valueOf(response.department()))) + .andExpect(jsonPath("$.interests[0]").value(response.interests().get(0).name())); } @Test