From 1b0171036246b8c7ba1cb407f6607fe2a708258b Mon Sep 17 00:00:00 2001 From: EunChanNam Date: Sun, 29 Oct 2023 16:28:40 +0900 Subject: [PATCH] =?UTF-8?q?test:=20User=20API=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/presentation/UserController.java | 19 ++- .../user/presentation/UserInfoController.java | 6 +- .../auth/presentation/AuthControllerTest.java | 4 - .../presentation/MailAuthControllerTest.java | 4 - .../TokenReissueControllerTest.java | 4 - .../common/support/ApiTestSupport.java | 5 + .../user/fixture/UserFixture.java | 30 ++++ .../user/presentation/UserControllerTest.java | 146 ++++++++++++++++++ .../presentation/UserInfoControllerTest.java | 107 +++++++++++++ 9 files changed, 302 insertions(+), 23 deletions(-) create mode 100644 src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserControllerTest.java create mode 100644 src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoControllerTest.java diff --git a/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserController.java b/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserController.java index 5532392..57eb20b 100644 --- a/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserController.java +++ b/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserController.java @@ -41,14 +41,16 @@ public ResponseEntity signUp(@RequestBody SignUpRequest createRequest) } @DeleteMapping - public ResponseEntity deleteUser(final @FetchAuthInfo AuthInfo authInfo) { + public ResponseEntity deleteUser(@FetchAuthInfo AuthInfo authInfo) { userService.deleteUser(authInfo.userId()); return ResponseEntity.ok(new Success()); } @PatchMapping("/refresh/password") - public ResponseEntity refreshPassword(@RequestBody PasswordRefreshRequest request) { + public ResponseEntity refreshPassword( + @RequestBody PasswordRefreshRequest request + ) { userService.refreshPassword(request); @@ -56,8 +58,9 @@ public ResponseEntity refreshPassword(@RequestBody PasswordRefreshReque } @PatchMapping - public ResponseEntity updateUser(@RequestBody UserUpdateRequest request, - final @FetchAuthInfo AuthInfo authInfo + public ResponseEntity updateUser( + @RequestBody UserUpdateRequest request, + @FetchAuthInfo AuthInfo authInfo ) { userService.updateUser(authInfo.userId(), request); @@ -67,8 +70,8 @@ public ResponseEntity updateUser(@RequestBody UserUpdateRequest request @PatchMapping("/password") public ResponseEntity updatePassword( - final @RequestBody PasswordUpdateRequest request, - final @FetchAuthInfo AuthInfo authInfo + @RequestBody PasswordUpdateRequest request, + @FetchAuthInfo AuthInfo authInfo ) { userService.updatePassword(authInfo.userId(), request); @@ -77,8 +80,8 @@ public ResponseEntity updatePassword( @PatchMapping("/face_shape") public ResponseEntity> updateFaceShape( - final @ModelAttribute FaceShapeUpdateRequest request, - final @FetchAuthInfo AuthInfo authInfo + @ModelAttribute FaceShapeUpdateRequest request, + @FetchAuthInfo AuthInfo authInfo ) { SimpleResponseWrapper result = userService.updateFaceShape(authInfo.userId(), request.file()); diff --git a/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoController.java b/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoController.java index 0c06a99..e5e46ff 100644 --- a/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoController.java +++ b/src/main/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoController.java @@ -22,7 +22,7 @@ public class UserInfoController { private final UserInfoService userInfoService; @GetMapping("/my_page") - public ResponseEntity getMyPageInfo(final @FetchAuthInfo AuthInfo authInfo) { + public ResponseEntity getMyPageInfo(@FetchAuthInfo AuthInfo authInfo) { MyPageResponse result = userInfoService.getMyPageInfo(authInfo.userId()); @@ -31,7 +31,7 @@ public ResponseEntity getMyPageInfo(final @FetchAuthInfo AuthInf @GetMapping("/info") public ResponseEntity getUserInformation( - final @FetchAuthInfo AuthInfo authInfo + @FetchAuthInfo AuthInfo authInfo ) { UserInformation result = userInfoService.getUserInformation(authInfo.userId()); @@ -40,7 +40,7 @@ public ResponseEntity getUserInformation( } @GetMapping("/home_info") - public ResponseEntity getUserInfo(final @FetchAuthInfo AuthInfo authInfo) { + public ResponseEntity getUserInfo(@FetchAuthInfo AuthInfo authInfo) { UserInfo result = userInfoService.getUserInfo(authInfo.userId()); diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/AuthControllerTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/AuthControllerTest.java index 12fb1fb..5bf94e4 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/AuthControllerTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/AuthControllerTest.java @@ -6,11 +6,9 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 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.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -27,8 +25,6 @@ class AuthControllerTest extends ApiTestSupport { private static final String LOGIN_URL = "/api/auth/login"; private static final String LOGOUT_URL = "/api/auth/logout"; - @Autowired - private MockMvc mockMvc; @MockBean private AuthService authService; diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/MailAuthControllerTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/MailAuthControllerTest.java index 048055d..a121670 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/MailAuthControllerTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/MailAuthControllerTest.java @@ -4,11 +4,9 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 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.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -28,8 +26,6 @@ class MailAuthControllerTest extends ApiTestSupport { private static final String AUTHORIZE_KEY_URL = "/api/email/validate"; private static final String EMAIL = "hello@naver.com"; - @Autowired - private MockMvc mockMvc; @MockBean private UserValidator userValidator; @MockBean diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/TokenReissueControllerTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/TokenReissueControllerTest.java index e79aead..fce4a19 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/TokenReissueControllerTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/TokenReissueControllerTest.java @@ -6,10 +6,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -24,8 +22,6 @@ class TokenReissueControllerTest extends ApiTestSupport { private static final String REISSUE_TOKEN_URL = "/api/token/reissue"; - @Autowired - private MockMvc mockMvc; @MockBean private TokenReissueService tokenReissueService; diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java b/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java index 5e350e7..a4b1132 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java @@ -4,7 +4,9 @@ import static org.mockito.BDDMockito.*; import org.junit.jupiter.api.BeforeEach; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -15,6 +17,9 @@ public abstract class ApiTestSupport { private final ObjectMapper objectMapper = new ObjectMapper(); + @Autowired + protected MockMvc mockMvc; + @MockBean protected AuthTokenManager authTokenManager; diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/user/fixture/UserFixture.java b/src/test/java/com/inq/wishhair/wesharewishhair/user/fixture/UserFixture.java index 68c1952..22ff69b 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/user/fixture/UserFixture.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/user/fixture/UserFixture.java @@ -1,5 +1,11 @@ package com.inq.wishhair.wesharewishhair.user.fixture; +import java.util.ArrayList; + +import com.inq.wishhair.wesharewishhair.hairstyle.domain.hashtag.Tag; +import com.inq.wishhair.wesharewishhair.user.application.dto.response.MyPageResponse; +import com.inq.wishhair.wesharewishhair.user.application.dto.response.UserInfo; +import com.inq.wishhair.wesharewishhair.user.application.dto.response.UserInformation; import com.inq.wishhair.wesharewishhair.user.domain.entity.Sex; import com.inq.wishhair.wesharewishhair.user.domain.entity.User; import com.inq.wishhair.wesharewishhair.user.presentation.dto.request.PasswordRefreshRequest; @@ -68,4 +74,28 @@ public static PasswordUpdateRequest getPasswordUpdateRequest() { "newPassword1234!" ); } + + public static MyPageResponse getMyPageResponse() { + return new MyPageResponse( + getFixedManUser(), + new ArrayList<>(), + 1000 + ); + } + + public static UserInformation getUserInformation() { + return new UserInformation( + EMAIL, + NAME, + NICKNAME, + Sex.MAN + ); + } + + public static UserInfo getUserInfo() { + User user = getFixedManUser(); + user.updateFaceShape(Tag.ROUND); + + return new UserInfo(user); + } } diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserControllerTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserControllerTest.java new file mode 100644 index 0000000..f68acc6 --- /dev/null +++ b/src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserControllerTest.java @@ -0,0 +1,146 @@ +package com.inq.wishhair.wesharewishhair.user.presentation; + +import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*; +import static org.mockito.BDDMockito.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.web.multipart.MultipartFile; + +import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; +import com.inq.wishhair.wesharewishhair.common.utils.FileMockingUtils; +import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig; +import com.inq.wishhair.wesharewishhair.user.application.UserService; +import com.inq.wishhair.wesharewishhair.user.fixture.UserFixture; +import com.inq.wishhair.wesharewishhair.user.presentation.dto.request.PasswordRefreshRequest; +import com.inq.wishhair.wesharewishhair.user.presentation.dto.request.PasswordUpdateRequest; +import com.inq.wishhair.wesharewishhair.user.presentation.dto.request.SignUpRequest; +import com.inq.wishhair.wesharewishhair.user.presentation.dto.request.UserUpdateRequest; + +@WebMvcTest(value = {UserController.class, SecurityConfig.class}) +@DisplayName("[UserController 테스트] - API") +class UserControllerTest extends ApiTestSupport { + + private static final String BASE_URL = "/api/users"; + private static final String PASSWORD_REFRESH_URL = BASE_URL + "/refresh/password"; + private static final String PASSWORD_UPDATE_URL = BASE_URL + "/password"; + private static final String UPDATE_FACE_SHAPE_URL = BASE_URL + "/face_shape"; + + @MockBean + private UserService userService; + + @Test + @DisplayName("[회원가입 API 를 호출한다]") + void signUp() throws Exception { + //given + SignUpRequest request = UserFixture.getSignUpRequest(); + given(userService.createUser(request)).willReturn(1L); + + //then + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .post(BASE_URL) + .contentType(MediaType.APPLICATION_JSON) + .content(toJson(request)) + ); + + //then + actual.andExpect(status().isCreated()); + } + + @Test + @DisplayName("[회원탈퇴 API 를 호출한다]") + void deleteUser() throws Exception { + //then + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .delete(BASE_URL) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpect(status().isOk()); + } + + @Test + @DisplayName("[비밀번호 갱신(비밀번호 찾기) API 를 호출한다]") + void refreshPassword() throws Exception { + //given + PasswordRefreshRequest request = UserFixture.getPasswordRefreshRequest(); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .patch(PASSWORD_REFRESH_URL) + .contentType(MediaType.APPLICATION_JSON) + .content(toJson(request)) + ); + + //then + actual.andExpect(status().isOk()); + } + + @Test + @DisplayName("[회원정보 수정 API 를 호출한다]") + void updateUser() throws Exception { + //given + UserUpdateRequest request = UserFixture.getUserUpdateRequest(); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .patch(BASE_URL) + .contentType(MediaType.APPLICATION_JSON) + .content(toJson(request)) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpect(status().isOk()); + } + + @Test + @DisplayName("[비밀번호 변경 API 를 호출한다]") + void updatePassword() throws Exception { + //given + PasswordUpdateRequest request = UserFixture.getPasswordUpdateRequest(); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .patch(PASSWORD_UPDATE_URL) + .contentType(MediaType.APPLICATION_JSON) + .content(toJson(request)) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpect(status().isOk()); + } + + @Test + @DisplayName("[얼굴형 정보 업데이트 API 를 호출한다]") + void updateFaceShape() throws Exception { + //given + MultipartFile file = FileMockingUtils.createMockMultipartFile("hello1.jpg"); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .multipart(HttpMethod.PATCH, UPDATE_FACE_SHAPE_URL) + .file((MockMultipartFile)file) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpect(status().isOk()); + } +} \ No newline at end of file diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoControllerTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoControllerTest.java new file mode 100644 index 0000000..90c47cb --- /dev/null +++ b/src/test/java/com/inq/wishhair/wesharewishhair/user/presentation/UserInfoControllerTest.java @@ -0,0 +1,107 @@ +package com.inq.wishhair.wesharewishhair.user.presentation; + +import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*; +import static org.mockito.BDDMockito.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; +import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig; +import com.inq.wishhair.wesharewishhair.user.application.UserInfoService; +import com.inq.wishhair.wesharewishhair.user.application.dto.response.MyPageResponse; +import com.inq.wishhair.wesharewishhair.user.application.dto.response.UserInfo; +import com.inq.wishhair.wesharewishhair.user.application.dto.response.UserInformation; +import com.inq.wishhair.wesharewishhair.user.fixture.UserFixture; + +@WebMvcTest(value = {UserInfoController.class, SecurityConfig.class}) +@DisplayName("[UserInfoController 테스트] - API") +class UserInfoControllerTest extends ApiTestSupport { + + private static final String BASE_URL = "/api/users"; + private static final String MY_PAGE_URL = BASE_URL + "/my_page"; + private static final String USER_INFORMATION = BASE_URL + "/info"; + private static final String USER_HOME_INFO = BASE_URL + "/home_info"; + + @MockBean + private UserInfoService userInfoService; + + @Test + @DisplayName("[마이페이지 정보 조회 API 를 호출한다]") + void getMyPageInfo() throws Exception { + //given + MyPageResponse response = UserFixture.getMyPageResponse(); + given(userInfoService.getMyPageInfo(1L)) + .willReturn(response); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .get(MY_PAGE_URL) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpectAll( + status().isOk(), + jsonPath("$.nickname").value(response.nickname()), + jsonPath("$.sex").value(response.sex().name()), + jsonPath("$.point").value(response.point()), + jsonPath("$.reviews.size()").value(0) + ); + } + + @Test + @DisplayName("[회원정보 조회 API 를 호출한다]") + void getUserInformation() throws Exception { + //given + UserInformation response = UserFixture.getUserInformation(); + given(userInfoService.getUserInformation(1L)) + .willReturn(response); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .get(USER_INFORMATION) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpectAll( + status().isOk(), + jsonPath("$.email").value(response.email()), + jsonPath("$.name").value(response.name()), + jsonPath("$.nickname").value(response.nickname()), + jsonPath("$.sex").value(response.sex().name()) + ); + } + + @Test + @DisplayName("[홈페이지 회원정보 조회 API 를 호출한다]") + void getUserInfo() throws Exception { + //given + UserInfo response = UserFixture.getUserInfo(); + given(userInfoService.getUserInfo(1L)) + .willReturn(response); + + //when + ResultActions actual = mockMvc.perform( + MockMvcRequestBuilders + .get(USER_HOME_INFO) + .header(AUTHORIZATION, ACCESS_TOKEN) + ); + + //then + actual.andExpectAll( + status().isOk(), + jsonPath("$.nickname").value(response.nickname()), + jsonPath("$.faceShapeTag").value(response.faceShapeTag()), + jsonPath("$.hasFaceShape").value(response.hasFaceShape()) + ); + } +} \ No newline at end of file