Skip to content

Commit

Permalink
test: User API 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
EunChanNam committed Oct 29, 2023
1 parent 58c42a0 commit 1b01710
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,26 @@ public ResponseEntity<Success> signUp(@RequestBody SignUpRequest createRequest)
}

@DeleteMapping
public ResponseEntity<Success> deleteUser(final @FetchAuthInfo AuthInfo authInfo) {
public ResponseEntity<Success> deleteUser(@FetchAuthInfo AuthInfo authInfo) {
userService.deleteUser(authInfo.userId());

return ResponseEntity.ok(new Success());
}

@PatchMapping("/refresh/password")
public ResponseEntity<Success> refreshPassword(@RequestBody PasswordRefreshRequest request) {
public ResponseEntity<Success> refreshPassword(
@RequestBody PasswordRefreshRequest request
) {

userService.refreshPassword(request);

return ResponseEntity.ok(new Success());
}

@PatchMapping
public ResponseEntity<Success> updateUser(@RequestBody UserUpdateRequest request,
final @FetchAuthInfo AuthInfo authInfo
public ResponseEntity<Success> updateUser(
@RequestBody UserUpdateRequest request,
@FetchAuthInfo AuthInfo authInfo
) {

userService.updateUser(authInfo.userId(), request);
Expand All @@ -67,8 +70,8 @@ public ResponseEntity<Success> updateUser(@RequestBody UserUpdateRequest request

@PatchMapping("/password")
public ResponseEntity<Success> updatePassword(
final @RequestBody PasswordUpdateRequest request,
final @FetchAuthInfo AuthInfo authInfo
@RequestBody PasswordUpdateRequest request,
@FetchAuthInfo AuthInfo authInfo
) {
userService.updatePassword(authInfo.userId(), request);

Expand All @@ -77,8 +80,8 @@ public ResponseEntity<Success> updatePassword(

@PatchMapping("/face_shape")
public ResponseEntity<SimpleResponseWrapper<String>> updateFaceShape(
final @ModelAttribute FaceShapeUpdateRequest request,
final @FetchAuthInfo AuthInfo authInfo
@ModelAttribute FaceShapeUpdateRequest request,
@FetchAuthInfo AuthInfo authInfo
) {

SimpleResponseWrapper<String> result = userService.updateFaceShape(authInfo.userId(), request.file());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class UserInfoController {
private final UserInfoService userInfoService;

@GetMapping("/my_page")
public ResponseEntity<MyPageResponse> getMyPageInfo(final @FetchAuthInfo AuthInfo authInfo) {
public ResponseEntity<MyPageResponse> getMyPageInfo(@FetchAuthInfo AuthInfo authInfo) {

MyPageResponse result = userInfoService.getMyPageInfo(authInfo.userId());

Expand All @@ -31,7 +31,7 @@ public ResponseEntity<MyPageResponse> getMyPageInfo(final @FetchAuthInfo AuthInf

@GetMapping("/info")
public ResponseEntity<UserInformation> getUserInformation(
final @FetchAuthInfo AuthInfo authInfo
@FetchAuthInfo AuthInfo authInfo
) {

UserInformation result = userInfoService.getUserInformation(authInfo.userId());
Expand All @@ -40,7 +40,7 @@ public ResponseEntity<UserInformation> getUserInformation(
}

@GetMapping("/home_info")
public ResponseEntity<UserInfo> getUserInfo(final @FetchAuthInfo AuthInfo authInfo) {
public ResponseEntity<UserInfo> getUserInfo(@FetchAuthInfo AuthInfo authInfo) {

UserInfo result = userInfoService.getUserInfo(authInfo.userId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,8 +26,6 @@ class MailAuthControllerTest extends ApiTestSupport {
private static final String AUTHORIZE_KEY_URL = "/api/email/validate";
private static final String EMAIL = "[email protected]";

@Autowired
private MockMvc mockMvc;
@MockBean
private UserValidator userValidator;
@MockBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +17,9 @@ public abstract class ApiTestSupport {

private final ObjectMapper objectMapper = new ObjectMapper();

@Autowired
protected MockMvc mockMvc;

@MockBean
protected AuthTokenManager authTokenManager;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading

0 comments on commit 1b01710

Please sign in to comment.