Skip to content

Commit

Permalink
test: UserController 테스트 코드 주석 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
1o18z committed Oct 31, 2023
1 parent ac19aa5 commit 39385f5
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -159,6 +164,7 @@ void findUserProfileTest() throws Exception {
@Test
@DisplayName("마이페이지를 조회할 수 있다.")
void findMyProfileTest() throws Exception {
// given
Long userId = 1L;
RefreshToken refreshToken = RefreshTokenFixture.refreshToken();

Expand All @@ -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)
Expand Down Expand Up @@ -199,6 +209,7 @@ void findMyProfileTest() throws Exception {
@Test
@DisplayName("본인 프로필 사진을 수정할 수 있다.")
void updateProfileImageTest() throws Exception {
// given
Long userId = 1L;
RefreshToken refreshToken = RefreshTokenFixture.refreshToken();

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

0 comments on commit 39385f5

Please sign in to comment.