From 4fbe1ebdd012952348dbd9788c473772c07e96a7 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Sat, 16 Mar 2024 14:12:23 +0900 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20#72=20=EC=97=B0=EB=9D=BD=EC=88=98?= =?UTF-8?q?=EB=8B=A8=20=EB=B3=80=EA=B2=BD=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 18 +++++++++ .../ajou/hertz/domain/user/entity/User.java | 3 ++ .../user/service/UserCommandService.java | 15 ++++++++ .../user/controller/UserControllerTest.java | 23 ++++++++++++ .../user/service/UserCommandServiceTest.java | 37 ++++++++++++++++++- 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index d3c4ed7..24bdfa5 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -9,6 +9,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -114,4 +115,21 @@ public ResponseEntity signUpV1( .created(URI.create("/users/" + userCreated.getId())) .body(UserResponse.from(userCreated)); } + + @Operation( + summary = "연락 수단 변경", + description = "연락 수단을 변경합니다." + ) + @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) + public ResponseEntity updateContactLinkV1( + @Parameter( + description = "변경하고자 하는 연락 수단 링크를 입력합니다.", + example = "https://example.com/1234" + ) @RequestParam @NotBlank String contactLink, + @AuthenticationPrincipal UserPrincipal userPrincipal + ) { + userCommandService.updateContactLink(userPrincipal.getUserId(), contactLink); + return ResponseEntity.ok().build(); + } + } diff --git a/src/main/java/com/ajou/hertz/domain/user/entity/User.java b/src/main/java/com/ajou/hertz/domain/user/entity/User.java index f900e55..75ba901 100644 --- a/src/main/java/com/ajou/hertz/domain/user/entity/User.java +++ b/src/main/java/com/ajou/hertz/domain/user/entity/User.java @@ -100,4 +100,7 @@ public static User create( profileImageUrl, birth, gender, phone, null ); } + + public void setContactLink(String contactLink) { + } } diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 59bb57d..6d166ed 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -15,6 +15,7 @@ import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; +import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; import com.ajou.hertz.domain.user.repository.UserRepository; @@ -129,4 +130,18 @@ private String generateRandom16CharString() { .toString() .substring(0, 16); } + + /** + * 연락 수단을 변경합니다. + * + * @param userId 유저 ID + * @param contactLink 변경할 연락 수단 + * @throws UserNotFoundByIdException 해당하는 유저를 찾을 수 없는 경우 + */ + public void updateContactLink(Long userId, String contactLink) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new UserNotFoundByIdException(userId)); + user.setContactLink(contactLink); + } + } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index 281631b..caff0e3 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -35,6 +35,7 @@ import com.ajou.hertz.domain.user.controller.UserController; import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; +import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; import com.ajou.hertz.domain.user.service.UserCommandService; import com.ajou.hertz.domain.user.service.UserQueryService; import com.ajou.hertz.util.ReflectionUtils; @@ -212,6 +213,28 @@ public void securitySetUp() throws Exception { verifyEveryMocksShouldHaveNoMoreInteractions(); } + @Test + void 주어진_연락수단을_새로운_연락수단으로_변경한다() throws Exception { + // given + long userId = 1L; + String newContactLink = "https://new-contact-link.com"; + UpdateContactLinkRequest updateContactLinkRequest = new UpdateContactLinkRequest(newContactLink); + UserDetails userDetails = createTestUser(userId); + + // when & then + mvc.perform( + put("/api/users/me/contact-link") + .header(API_VERSION_HEADER_NAME, 1) + //.contentType(MediaType.APPLICATION_JSON) + .param("contactLink", newContactLink) + .with(user(userDetails)) + ) + .andExpect(status().isOk()); + + then(userCommandService).should().updateContactLink(userId, newContactLink); + verifyEveryMocksShouldHaveNoMoreInteractions(); + } + private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userCommandService).shouldHaveNoMoreInteractions(); then(userQueryService).shouldHaveNoMoreInteractions(); diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index 1e1dedd..a5db66c 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -5,6 +5,7 @@ import static org.mockito.BDDMockito.*; import java.time.LocalDate; +import java.util.Optional; import java.util.Set; import java.util.stream.Stream; @@ -29,6 +30,7 @@ import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; +import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; import com.ajou.hertz.domain.user.repository.UserRepository; import com.ajou.hertz.domain.user.service.UserCommandService; @@ -171,6 +173,39 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { assertThat(t).isInstanceOf(UserKakaoUidDuplicationException.class); } + @Test + void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다() throws Exception { + // given + Long userId = 1L; + String contactLink = "https://contackLink"; + User user = createUser(userId, "$2a$abc123", "12345"); + given(userRepository.findById(userId)).willReturn(Optional.of(user)); + + // when + sut.updateContactLink(userId, contactLink); + + // then + then(userRepository).should().findById(userId); + verifyEveryMocksShouldHaveNoMoreInteractions(); + assertThat(user.getContactLink()).isEqualTo(contactLink); + } + + @Test + void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception { + // given + Long userId = 1L; + String contactLink = "https://contackLink"; + given(userRepository.findById(userId)).willReturn(Optional.empty()); + + // when + Throwable t = catchThrowable(() -> sut.updateContactLink(userId, contactLink)); + + // then + then(userRepository).should().findById(userId); + verifyEveryMocksShouldHaveNoMoreInteractions(); + assertThat(t).isInstanceOf(UserNotFoundByIdException.class); + } + private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userQueryService).shouldHaveNoMoreInteractions(); then(userRepository).shouldHaveNoMoreInteractions(); @@ -188,7 +223,7 @@ private static User createUser(Long id, String password, String kakaoUid, Gender LocalDate.of(2024, 1, 1), gender, "010-1234-5678", - null + "https://contackLink" ); } From a5185baf24aa15de3eeb1470be5c9103b526ff8b Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Sat, 16 Mar 2024 23:40:14 +0900 Subject: [PATCH 02/11] =?UTF-8?q?refactor:=20#72=20=EC=97=B0=EB=9D=BD?= =?UTF-8?q?=EC=88=98=EB=8B=A8=20=EB=B3=80=EA=B2=BD=20API=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/controller/UserController.java | 13 ++++++++----- .../com/ajou/hertz/domain/user/entity/User.java | 2 ++ .../domain/user/service/UserCommandService.java | 5 +---- .../domain/user/controller/UserControllerTest.java | 9 +++++---- .../domain/user/service/UserCommandServiceTest.java | 9 ++++----- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index 24bdfa5..db6106f 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -19,6 +19,7 @@ import com.ajou.hertz.common.validator.PhoneNumber; import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; +import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; import com.ajou.hertz.domain.user.dto.response.UserEmailResponse; import com.ajou.hertz.domain.user.dto.response.UserExistenceResponse; import com.ajou.hertz.domain.user.dto.response.UserResponse; @@ -118,18 +119,20 @@ public ResponseEntity signUpV1( @Operation( summary = "연락 수단 변경", - description = "연락 수단을 변경합니다." + description = "연락 수단을 변경합니다.", + security = @SecurityRequirement(name = "access-token") ) @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) public ResponseEntity updateContactLinkV1( @Parameter( description = "변경하고자 하는 연락 수단 링크를 입력합니다.", example = "https://example.com/1234" - ) @RequestParam @NotBlank String contactLink, + ) + @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, @AuthenticationPrincipal UserPrincipal userPrincipal + ) { - userCommandService.updateContactLink(userPrincipal.getUserId(), contactLink); + userCommandService.updateContactLink(userPrincipal.getUserId(), updateContactLinkRequest.getContactLink()); return ResponseEntity.ok().build(); } - -} +} \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/user/entity/User.java b/src/main/java/com/ajou/hertz/domain/user/entity/User.java index 75ba901..3ad17b8 100644 --- a/src/main/java/com/ajou/hertz/domain/user/entity/User.java +++ b/src/main/java/com/ajou/hertz/domain/user/entity/User.java @@ -102,5 +102,7 @@ public static User create( } public void setContactLink(String contactLink) { + this.contactLink = contactLink; } + } diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 6d166ed..14fe8da 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -15,7 +15,6 @@ import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; -import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; import com.ajou.hertz.domain.user.repository.UserRepository; @@ -136,11 +135,9 @@ private String generateRandom16CharString() { * * @param userId 유저 ID * @param contactLink 변경할 연락 수단 - * @throws UserNotFoundByIdException 해당하는 유저를 찾을 수 없는 경우 */ public void updateContactLink(Long userId, String contactLink) { - User user = userRepository.findById(userId) - .orElseThrow(() -> new UserNotFoundByIdException(userId)); + User user = userQueryService.getById(userId); user.setContactLink(contactLink); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index caff0e3..67c1b39 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -225,13 +225,13 @@ public void securitySetUp() throws Exception { mvc.perform( put("/api/users/me/contact-link") .header(API_VERSION_HEADER_NAME, 1) - //.contentType(MediaType.APPLICATION_JSON) - .param("contactLink", newContactLink) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(updateContactLinkRequest)) .with(user(userDetails)) ) .andExpect(status().isOk()); - then(userCommandService).should().updateContactLink(userId, newContactLink); + then(userCommandService).should().updateContactLink(eq(userId), eq(newContactLink)); verifyEveryMocksShouldHaveNoMoreInteractions(); } @@ -269,7 +269,7 @@ private UserDto createUserDto(long id) throws Exception { LocalDate.of(2024, 1, 1), Gender.ETC, "01012345678", - "https://contack-link", + "https://contact-link", LocalDateTime.of(2024, 1, 1, 0, 0) ); } @@ -281,4 +281,5 @@ private UserDto createUserDto() throws Exception { private UserDetails createTestUser(Long userId) throws Exception { return new UserPrincipal(createUserDto(userId)); } + } \ No newline at end of file diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index a5db66c..75044f6 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -5,7 +5,6 @@ import static org.mockito.BDDMockito.*; import java.time.LocalDate; -import java.util.Optional; import java.util.Set; import java.util.stream.Stream; @@ -179,13 +178,13 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { Long userId = 1L; String contactLink = "https://contackLink"; User user = createUser(userId, "$2a$abc123", "12345"); - given(userRepository.findById(userId)).willReturn(Optional.of(user)); + given(userQueryService.getById(userId)).willReturn(user); // when sut.updateContactLink(userId, contactLink); // then - then(userRepository).should().findById(userId); + then(userQueryService).should().getById(userId); verifyEveryMocksShouldHaveNoMoreInteractions(); assertThat(user.getContactLink()).isEqualTo(contactLink); } @@ -195,13 +194,13 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { // given Long userId = 1L; String contactLink = "https://contackLink"; - given(userRepository.findById(userId)).willReturn(Optional.empty()); + given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class); // when Throwable t = catchThrowable(() -> sut.updateContactLink(userId, contactLink)); // then - then(userRepository).should().findById(userId); + then(userQueryService).should().getById(userId); verifyEveryMocksShouldHaveNoMoreInteractions(); assertThat(t).isInstanceOf(UserNotFoundByIdException.class); } From dc72858b1d8f272e8e7529334fc2a3c9ff2b0bc5 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Sat, 16 Mar 2024 23:47:52 +0900 Subject: [PATCH 03/11] =?UTF-8?q?feat:=20#72=20UpdateContactLinkRequest=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/request/UpdateContactLinkRequest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java diff --git a/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java new file mode 100644 index 0000000..4e83df5 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java @@ -0,0 +1,19 @@ +package com.ajou.hertz.domain.user.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public class UpdateContactLinkRequest { + + @Schema(description = "연락 수단", example = "https://new-contack-link") + @NotBlank + private String contactLink; + +} From 888042eed8b8d80205a65d98769db6da3f6481f5 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Sun, 17 Mar 2024 21:55:52 +0900 Subject: [PATCH 04/11] =?UTF-8?q?feat:=20#72=20UserContactLinkResponse=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/UserContactLinkResponse.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java diff --git a/src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java b/src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java new file mode 100644 index 0000000..bf766a4 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java @@ -0,0 +1,17 @@ +package com.ajou.hertz.domain.user.dto.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public class UserContactLinkResponse { + + @Schema(description = "연락 수단", example = "https://new-contactlink.com") + private String contactLink; + +} From 3d8c2270f2c785de3bf0cb849309d99b22fa4230 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Sun, 17 Mar 2024 21:56:38 +0900 Subject: [PATCH 05/11] =?UTF-8?q?refactor:=20#72=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hertz/domain/user/controller/UserController.java | 11 ++++------- .../java/com/ajou/hertz/domain/user/entity/User.java | 2 +- .../hertz/domain/user/service/UserCommandService.java | 2 +- .../domain/user/controller/UserControllerTest.java | 7 +++++-- .../domain/user/service/UserCommandServiceTest.java | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index db6106f..14ba926 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -20,6 +20,7 @@ import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; +import com.ajou.hertz.domain.user.dto.response.UserContactLinkResponse; import com.ajou.hertz.domain.user.dto.response.UserEmailResponse; import com.ajou.hertz.domain.user.dto.response.UserExistenceResponse; import com.ajou.hertz.domain.user.dto.response.UserResponse; @@ -123,16 +124,12 @@ public ResponseEntity signUpV1( security = @SecurityRequirement(name = "access-token") ) @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) - public ResponseEntity updateContactLinkV1( - @Parameter( - description = "변경하고자 하는 연락 수단 링크를 입력합니다.", - example = "https://example.com/1234" - ) + public ResponseEntity updateContactLinkV1( @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, @AuthenticationPrincipal UserPrincipal userPrincipal - ) { userCommandService.updateContactLink(userPrincipal.getUserId(), updateContactLinkRequest.getContactLink()); - return ResponseEntity.ok().build(); + UserContactLinkResponse response = new UserContactLinkResponse(updateContactLinkRequest.getContactLink()); + return ResponseEntity.ok(response); } } \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/user/entity/User.java b/src/main/java/com/ajou/hertz/domain/user/entity/User.java index 3ad17b8..0be60e1 100644 --- a/src/main/java/com/ajou/hertz/domain/user/entity/User.java +++ b/src/main/java/com/ajou/hertz/domain/user/entity/User.java @@ -101,7 +101,7 @@ public static User create( ); } - public void setContactLink(String contactLink) { + public void changeContactLink(String contactLink) { this.contactLink = contactLink; } diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 14fe8da..26039fb 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -138,7 +138,7 @@ private String generateRandom16CharString() { */ public void updateContactLink(Long userId, String contactLink) { User user = userQueryService.getById(userId); - user.setContactLink(contactLink); + user.changeContactLink(contactLink); } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index 67c1b39..dec683c 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -220,6 +220,7 @@ public void securitySetUp() throws Exception { String newContactLink = "https://new-contact-link.com"; UpdateContactLinkRequest updateContactLinkRequest = new UpdateContactLinkRequest(newContactLink); UserDetails userDetails = createTestUser(userId); + willDoNothing().given(userCommandService).updateContactLink(userId, newContactLink); // when & then mvc.perform( @@ -229,9 +230,11 @@ public void securitySetUp() throws Exception { .content(objectMapper.writeValueAsString(updateContactLinkRequest)) .with(user(userDetails)) ) - .andExpect(status().isOk()); + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.contactLink").value(newContactLink)); - then(userCommandService).should().updateContactLink(eq(userId), eq(newContactLink)); + then(userCommandService).should().updateContactLink(userId, newContactLink); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index 75044f6..07ad760 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -176,7 +176,7 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다() throws Exception { // given Long userId = 1L; - String contactLink = "https://contackLink"; + String contactLink = "https://new-contactLink"; User user = createUser(userId, "$2a$abc123", "12345"); given(userQueryService.getById(userId)).willReturn(user); @@ -193,7 +193,7 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception { // given Long userId = 1L; - String contactLink = "https://contackLink"; + String contactLink = "https://new-contactLink"; given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class); // when @@ -222,7 +222,7 @@ private static User createUser(Long id, String password, String kakaoUid, Gender LocalDate.of(2024, 1, 1), gender, "010-1234-5678", - "https://contackLink" + "https://contactLink" ); } From 7b0d6f51e48d91373627a3a6ac3b7a9bee3005b6 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Mon, 18 Mar 2024 20:37:56 +0900 Subject: [PATCH 06/11] =?UTF-8?q?refactor:=20#72=20Userresponse=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/controller/UserController.java | 10 +++++----- .../dto/response/UserContactLinkResponse.java | 17 ----------------- .../domain/user/service/UserCommandService.java | 11 +++++++---- .../user/controller/UserControllerTest.java | 7 +++++-- 4 files changed, 17 insertions(+), 28 deletions(-) delete mode 100644 src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index 14ba926..3834c05 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -20,7 +20,6 @@ import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; -import com.ajou.hertz.domain.user.dto.response.UserContactLinkResponse; import com.ajou.hertz.domain.user.dto.response.UserEmailResponse; import com.ajou.hertz.domain.user.dto.response.UserExistenceResponse; import com.ajou.hertz.domain.user.dto.response.UserResponse; @@ -124,12 +123,13 @@ public ResponseEntity signUpV1( security = @SecurityRequirement(name = "access-token") ) @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) - public ResponseEntity updateContactLinkV1( + public UserResponse updateContactLinkV1( @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, @AuthenticationPrincipal UserPrincipal userPrincipal ) { - userCommandService.updateContactLink(userPrincipal.getUserId(), updateContactLinkRequest.getContactLink()); - UserContactLinkResponse response = new UserContactLinkResponse(updateContactLinkRequest.getContactLink()); - return ResponseEntity.ok(response); + UserDto userUpdated = userCommandService.updateContactLink(userPrincipal.getUserId(), + updateContactLinkRequest.getContactLink()); + UserDto userDto = userQueryService.getDtoById(userPrincipal.getUserId()); + return UserResponse.from(userUpdated); } } \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java b/src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java deleted file mode 100644 index bf766a4..0000000 --- a/src/main/java/com/ajou/hertz/domain/user/dto/response/UserContactLinkResponse.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.ajou.hertz.domain.user.dto.response; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@AllArgsConstructor -@NoArgsConstructor(access = AccessLevel.PRIVATE) -@Getter -public class UserContactLinkResponse { - - @Schema(description = "연락 수단", example = "https://new-contactlink.com") - private String contactLink; - -} diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 26039fb..5916c4e 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -131,14 +131,17 @@ private String generateRandom16CharString() { } /** - * 연락 수단을 변경합니다. + *연락 수단을 변경합니다. * - * @param userId 유저 ID - * @param contactLink 변경할 연락 수단 + *@param userId 유저ID + *@param contactLink 변경할 연락 수단 + * + *@return 변경된 유저 정보 */ - public void updateContactLink(Long userId, String contactLink) { + public UserDto updateContactLink(Long userId, String contactLink) { User user = userQueryService.getById(userId); user.changeContactLink(contactLink); + return UserDto.from(user); } } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index dec683c..fd37789 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -220,7 +220,9 @@ public void securitySetUp() throws Exception { String newContactLink = "https://new-contact-link.com"; UpdateContactLinkRequest updateContactLinkRequest = new UpdateContactLinkRequest(newContactLink); UserDetails userDetails = createTestUser(userId); - willDoNothing().given(userCommandService).updateContactLink(userId, newContactLink); + UserDto updatedUserDto = createUserDto(userId); + given(userCommandService.updateContactLink(userId, newContactLink)).willReturn(updatedUserDto); + given(userQueryService.getDtoById(userId)).willReturn(updatedUserDto); // when & then mvc.perform( @@ -232,9 +234,10 @@ public void securitySetUp() throws Exception { ) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.contactLink").value(newContactLink)); + .andExpect(jsonPath("$.contactLink").value(updatedUserDto.getContactLink())); then(userCommandService).should().updateContactLink(userId, newContactLink); + then(userQueryService).should().getDtoById(userId); verifyEveryMocksShouldHaveNoMoreInteractions(); } From ab9f56fc96030d52a0ab8db947337c779852a052 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Tue, 19 Mar 2024 22:00:02 +0900 Subject: [PATCH 07/11] =?UTF-8?q?refactor:=20#72=20=ED=95=84=EC=9A=94?= =?UTF-8?q?=EC=97=86=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 11 ++++++----- .../dto/request/UpdateContactLinkRequest.java | 19 ------------------- .../user/controller/UserControllerTest.java | 19 ++++++------------- 3 files changed, 12 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index 3834c05..8889d49 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -19,7 +19,6 @@ import com.ajou.hertz.common.validator.PhoneNumber; import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; -import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; import com.ajou.hertz.domain.user.dto.response.UserEmailResponse; import com.ajou.hertz.domain.user.dto.response.UserExistenceResponse; import com.ajou.hertz.domain.user.dto.response.UserResponse; @@ -124,12 +123,14 @@ public ResponseEntity signUpV1( ) @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) public UserResponse updateContactLinkV1( - @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, + @Parameter( + description = "변경하고자 하는 연락 수단", + example = "https://example.com/1234" + ) @RequestParam String contactLink, @AuthenticationPrincipal UserPrincipal userPrincipal ) { - UserDto userUpdated = userCommandService.updateContactLink(userPrincipal.getUserId(), - updateContactLinkRequest.getContactLink()); - UserDto userDto = userQueryService.getDtoById(userPrincipal.getUserId()); + Long userId = userPrincipal.getUserId(); + UserDto userUpdated = userCommandService.updateContactLink(userId, contactLink); return UserResponse.from(userUpdated); } } \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java deleted file mode 100644 index 4e83df5..0000000 --- a/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.ajou.hertz.domain.user.dto.request; - -import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.NotBlank; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@AllArgsConstructor -@NoArgsConstructor(access = AccessLevel.PRIVATE) -@Getter -public class UpdateContactLinkRequest { - - @Schema(description = "연락 수단", example = "https://new-contack-link") - @NotBlank - private String contactLink; - -} diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index fd37789..7d58be9 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -35,7 +35,6 @@ import com.ajou.hertz.domain.user.controller.UserController; import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; -import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; import com.ajou.hertz.domain.user.service.UserCommandService; import com.ajou.hertz.domain.user.service.UserQueryService; import com.ajou.hertz.util.ReflectionUtils; @@ -218,26 +217,20 @@ public void securitySetUp() throws Exception { // given long userId = 1L; String newContactLink = "https://new-contact-link.com"; - UpdateContactLinkRequest updateContactLinkRequest = new UpdateContactLinkRequest(newContactLink); - UserDetails userDetails = createTestUser(userId); - UserDto updatedUserDto = createUserDto(userId); - given(userCommandService.updateContactLink(userId, newContactLink)).willReturn(updatedUserDto); - given(userQueryService.getDtoById(userId)).willReturn(updatedUserDto); + UserDto expectedResult = createUserDto(userId); + UserDetails testUser = createTestUser(userId); + given(userCommandService.updateContactLink(anyLong(), anyString())).willReturn(expectedResult); // when & then mvc.perform( put("/api/users/me/contact-link") .header(API_VERSION_HEADER_NAME, 1) - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(updateContactLinkRequest)) - .with(user(userDetails)) + .param("contactLink", newContactLink) + .with(user(testUser)) ) .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.contactLink").value(updatedUserDto.getContactLink())); - + .andExpect(jsonPath("$.contactLink").value(expectedResult.getContactLink())); then(userCommandService).should().updateContactLink(userId, newContactLink); - then(userQueryService).should().getDtoById(userId); verifyEveryMocksShouldHaveNoMoreInteractions(); } From ac41304da32345de8b96eef3deb12ff24abb76b0 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Tue, 19 Mar 2024 22:07:26 +0900 Subject: [PATCH 08/11] =?UTF-8?q?refactor:=20#72=20UserCommandServiceTest?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/domain/user/service/UserCommandServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index 07ad760..148f3b3 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -181,12 +181,12 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { given(userQueryService.getById(userId)).willReturn(user); // when - sut.updateContactLink(userId, contactLink); + UserDto updatedUserDto = sut.updateContactLink(userId, contactLink); // then then(userQueryService).should().getById(userId); verifyEveryMocksShouldHaveNoMoreInteractions(); - assertThat(user.getContactLink()).isEqualTo(contactLink); + assertThat(updatedUserDto.getContactLink()).isEqualTo(contactLink); } @Test From 951456fdbb8bd12f3bcb05eb8e9698a481bea91e Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Wed, 20 Mar 2024 23:16:26 +0900 Subject: [PATCH 09/11] =?UTF-8?q?refactor:=20#72=20403=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constant/CustomExceptionType.java | 4 ++-- .../user/controller/UserController.java | 10 ++++---- .../dto/request/UpdateContactLinkRequest.java | 19 +++++++++++++++ .../exception/UserIdForbiddenException.java | 11 +++++++++ .../user/service/UserCommandService.java | 13 ++++++++--- .../user/controller/UserControllerTest.java | 7 +++--- .../user/service/UserCommandServiceTest.java | 23 +++++++++++++++++-- 7 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java create mode 100644 src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java diff --git a/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java b/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java index 08e7143..a8e0f58 100644 --- a/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java +++ b/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java @@ -40,6 +40,7 @@ public enum CustomExceptionType { USER_KAKAO_UID_DUPLICATION(2204, "이미 가입한 계정입니다."), USER_NOT_FOUND_BY_KAKAO_UID(2205, "일치하는 회원을 찾을 수 없습니다."), USER_NOT_FOUND_BY_PHONE(2206, "일치하는 회원을 찾을 수 없습니다."), + USER_ID_FORBIDDEN(2207, "해당 유저는 접근이 금지되었습니다."), KAKAO_CLIENT(10000, "카카오 서버와의 통신 중 오류가 발생했습니다."), @@ -51,8 +52,7 @@ public enum CustomExceptionType { /** * 악기 관련 예외 */ - INSTRUMENT_NOT_FOUND_BY_ID(2600, "일치하는 매물 정보를 찾을 수 없습니다.") - ; + INSTRUMENT_NOT_FOUND_BY_ID(2600, "일치하는 매물 정보를 찾을 수 없습니다."); private final Integer code; private final String message; diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index 8889d49..da0eb61 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -19,6 +19,7 @@ import com.ajou.hertz.common.validator.PhoneNumber; import com.ajou.hertz.domain.user.dto.UserDto; import com.ajou.hertz.domain.user.dto.request.SignUpRequest; +import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest; import com.ajou.hertz.domain.user.dto.response.UserEmailResponse; import com.ajou.hertz.domain.user.dto.response.UserExistenceResponse; import com.ajou.hertz.domain.user.dto.response.UserResponse; @@ -123,14 +124,11 @@ public ResponseEntity signUpV1( ) @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) public UserResponse updateContactLinkV1( - @Parameter( - description = "변경하고자 하는 연락 수단", - example = "https://example.com/1234" - ) @RequestParam String contactLink, + @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, @AuthenticationPrincipal UserPrincipal userPrincipal ) { - Long userId = userPrincipal.getUserId(); - UserDto userUpdated = userCommandService.updateContactLink(userId, contactLink); + UserDto userUpdated = userCommandService.updateContactLink(userPrincipal.getUserId(), userPrincipal.getUserId(), + updateContactLinkRequest.getContactLink()); return UserResponse.from(userUpdated); } } \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java new file mode 100644 index 0000000..d0c69f3 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java @@ -0,0 +1,19 @@ +package com.ajou.hertz.domain.user.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public class UpdateContactLinkRequest { + + @Schema(description = "연락 수단", example = "https://new-contack-link") + @NotBlank + private String contactLink; + +} diff --git a/src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java b/src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java new file mode 100644 index 0000000..2eb423a --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java @@ -0,0 +1,11 @@ +package com.ajou.hertz.domain.user.exception; + +import com.ajou.hertz.common.exception.ForbiddenException; +import com.ajou.hertz.common.exception.constant.CustomExceptionType; + +public class UserIdForbiddenException extends ForbiddenException { + + public UserIdForbiddenException(Long userId, Long loginUserId) { + super(CustomExceptionType.USER_ID_FORBIDDEN, "userid와" + userId + "userid가" + loginUserId + "다릅니다"); + } +} diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 5916c4e..13bdd1c 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -14,6 +14,7 @@ import com.ajou.hertz.domain.user.dto.request.SignUpRequest; import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; +import com.ajou.hertz.domain.user.exception.UserIdForbiddenException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; import com.ajou.hertz.domain.user.repository.UserRepository; @@ -133,12 +134,18 @@ private String generateRandom16CharString() { /** *연락 수단을 변경합니다. * - *@param userId 유저ID - *@param contactLink 변경할 연락 수단 + * @param loginUserId API를 호출한 유저의 ID + * @param userId 변경하고자 하는 유저의 ID + * @param contactLink 변경할 연락 수단 + *@throws UserIdForbiddenException 유저ID가 일치하지 않는 경우 * *@return 변경된 유저 정보 */ - public UserDto updateContactLink(Long userId, String contactLink) { + public UserDto updateContactLink(Long loginUserId, Long userId, String contactLink) { + if (!loginUserId.equals(userId)) { + User user = userQueryService.getById(userId); + throw new UserIdForbiddenException(userId, loginUserId); + } User user = userQueryService.getById(userId); user.changeContactLink(contactLink); return UserDto.from(user); diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index 7d58be9..e58a72e 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -219,18 +219,19 @@ public void securitySetUp() throws Exception { String newContactLink = "https://new-contact-link.com"; UserDto expectedResult = createUserDto(userId); UserDetails testUser = createTestUser(userId); - given(userCommandService.updateContactLink(anyLong(), anyString())).willReturn(expectedResult); + given(userCommandService.updateContactLink(anyLong(), anyLong(), anyString())).willReturn(expectedResult); // when & then mvc.perform( put("/api/users/me/contact-link") .header(API_VERSION_HEADER_NAME, 1) - .param("contactLink", newContactLink) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(newContactLink)) .with(user(testUser)) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.contactLink").value(expectedResult.getContactLink())); - then(userCommandService).should().updateContactLink(userId, newContactLink); + then(userCommandService).should().updateContactLink(userId, userId, newContactLink); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index 148f3b3..eb6d8cc 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -28,6 +28,7 @@ import com.ajou.hertz.domain.user.dto.request.SignUpRequest; import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; +import com.ajou.hertz.domain.user.exception.UserIdForbiddenException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; @@ -181,7 +182,7 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { given(userQueryService.getById(userId)).willReturn(user); // when - UserDto updatedUserDto = sut.updateContactLink(userId, contactLink); + UserDto updatedUserDto = sut.updateContactLink(userId, userId, contactLink); // then then(userQueryService).should().getById(userId); @@ -197,7 +198,7 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class); // when - Throwable t = catchThrowable(() -> sut.updateContactLink(userId, contactLink)); + Throwable t = catchThrowable(() -> sut.updateContactLink(userId, userId, contactLink)); // then then(userQueryService).should().getById(userId); @@ -205,6 +206,24 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { assertThat(t).isInstanceOf(UserNotFoundByIdException.class); } + @Test + void 주어진_유저_ID가_API를_호출한_유저의_ID와_일치하지_않는다면_에러가_발생한다() throws Exception { + // given + Long loginUserId = 1L; + Long userId = 2L; + String contactLink = "https://new-contactLink"; + User user = createUser(userId, "$2a$abc123", "12345"); + given(userQueryService.getById(userId)).willReturn(user); + + // when + Throwable t = catchThrowable(() -> sut.updateContactLink(loginUserId, userId, contactLink)); + + // then + assertThat(t).isInstanceOf(UserIdForbiddenException.class); + then(userQueryService).should().getById(userId); + verifyEveryMocksShouldHaveNoMoreInteractions(); + } + private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userQueryService).shouldHaveNoMoreInteractions(); then(userRepository).shouldHaveNoMoreInteractions(); From 75822128eef6c1877d68b232528a6105976ea31a Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Thu, 21 Mar 2024 10:32:08 +0900 Subject: [PATCH 10/11] =?UTF-8?q?refactor:=20#72=20403=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 2 +- .../exception/UserIdForbiddenException.java | 11 --------- .../user/service/UserCommandService.java | 11 ++------- .../user/controller/UserControllerTest.java | 4 ++-- .../user/service/UserCommandServiceTest.java | 23 ++----------------- 5 files changed, 7 insertions(+), 44 deletions(-) delete mode 100644 src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index da0eb61..b9d4db3 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -127,7 +127,7 @@ public UserResponse updateContactLinkV1( @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, @AuthenticationPrincipal UserPrincipal userPrincipal ) { - UserDto userUpdated = userCommandService.updateContactLink(userPrincipal.getUserId(), userPrincipal.getUserId(), + UserDto userUpdated = userCommandService.updateContactLink(userPrincipal.getUserId(), updateContactLinkRequest.getContactLink()); return UserResponse.from(userUpdated); } diff --git a/src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java b/src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java deleted file mode 100644 index 2eb423a..0000000 --- a/src/main/java/com/ajou/hertz/domain/user/exception/UserIdForbiddenException.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.ajou.hertz.domain.user.exception; - -import com.ajou.hertz.common.exception.ForbiddenException; -import com.ajou.hertz.common.exception.constant.CustomExceptionType; - -public class UserIdForbiddenException extends ForbiddenException { - - public UserIdForbiddenException(Long userId, Long loginUserId) { - super(CustomExceptionType.USER_ID_FORBIDDEN, "userid와" + userId + "userid가" + loginUserId + "다릅니다"); - } -} diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 13bdd1c..ff64dcf 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -14,7 +14,6 @@ import com.ajou.hertz.domain.user.dto.request.SignUpRequest; import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; -import com.ajou.hertz.domain.user.exception.UserIdForbiddenException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; import com.ajou.hertz.domain.user.repository.UserRepository; @@ -134,18 +133,12 @@ private String generateRandom16CharString() { /** *연락 수단을 변경합니다. * - * @param loginUserId API를 호출한 유저의 ID - * @param userId 변경하고자 하는 유저의 ID + * @param userId 유저의 ID * @param contactLink 변경할 연락 수단 - *@throws UserIdForbiddenException 유저ID가 일치하지 않는 경우 * *@return 변경된 유저 정보 */ - public UserDto updateContactLink(Long loginUserId, Long userId, String contactLink) { - if (!loginUserId.equals(userId)) { - User user = userQueryService.getById(userId); - throw new UserIdForbiddenException(userId, loginUserId); - } + public UserDto updateContactLink(Long userId, String contactLink) { User user = userQueryService.getById(userId); user.changeContactLink(contactLink); return UserDto.from(user); diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index e58a72e..ced0d36 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -219,7 +219,7 @@ public void securitySetUp() throws Exception { String newContactLink = "https://new-contact-link.com"; UserDto expectedResult = createUserDto(userId); UserDetails testUser = createTestUser(userId); - given(userCommandService.updateContactLink(anyLong(), anyLong(), anyString())).willReturn(expectedResult); + given(userCommandService.updateContactLink(anyLong(), anyString())).willReturn(expectedResult); // when & then mvc.perform( @@ -231,7 +231,7 @@ public void securitySetUp() throws Exception { ) .andExpect(status().isOk()) .andExpect(jsonPath("$.contactLink").value(expectedResult.getContactLink())); - then(userCommandService).should().updateContactLink(userId, userId, newContactLink); + then(userCommandService).should().updateContactLink(userId, newContactLink); verifyEveryMocksShouldHaveNoMoreInteractions(); } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index eb6d8cc..148f3b3 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -28,7 +28,6 @@ import com.ajou.hertz.domain.user.dto.request.SignUpRequest; import com.ajou.hertz.domain.user.entity.User; import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException; -import com.ajou.hertz.domain.user.exception.UserIdForbiddenException; import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException; import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException; import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException; @@ -182,7 +181,7 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { given(userQueryService.getById(userId)).willReturn(user); // when - UserDto updatedUserDto = sut.updateContactLink(userId, userId, contactLink); + UserDto updatedUserDto = sut.updateContactLink(userId, contactLink); // then then(userQueryService).should().getById(userId); @@ -198,7 +197,7 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class); // when - Throwable t = catchThrowable(() -> sut.updateContactLink(userId, userId, contactLink)); + Throwable t = catchThrowable(() -> sut.updateContactLink(userId, contactLink)); // then then(userQueryService).should().getById(userId); @@ -206,24 +205,6 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { assertThat(t).isInstanceOf(UserNotFoundByIdException.class); } - @Test - void 주어진_유저_ID가_API를_호출한_유저의_ID와_일치하지_않는다면_에러가_발생한다() throws Exception { - // given - Long loginUserId = 1L; - Long userId = 2L; - String contactLink = "https://new-contactLink"; - User user = createUser(userId, "$2a$abc123", "12345"); - given(userQueryService.getById(userId)).willReturn(user); - - // when - Throwable t = catchThrowable(() -> sut.updateContactLink(loginUserId, userId, contactLink)); - - // then - assertThat(t).isInstanceOf(UserIdForbiddenException.class); - then(userQueryService).should().getById(userId); - verifyEveryMocksShouldHaveNoMoreInteractions(); - } - private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userQueryService).shouldHaveNoMoreInteractions(); then(userRepository).shouldHaveNoMoreInteractions(); From 7a1d76aa51e41053ec4bcc9cdb30bc1c063e18f9 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Thu, 21 Mar 2024 11:10:12 +0900 Subject: [PATCH 11/11] =?UTF-8?q?refactor:=20#72=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20?= =?UTF-8?q?=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hertz/common/exception/constant/CustomExceptionType.java | 1 - .../hertz/unit/domain/user/controller/UserControllerTest.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java b/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java index 7953f3e..8557cf6 100644 --- a/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java +++ b/src/main/java/com/ajou/hertz/common/exception/constant/CustomExceptionType.java @@ -40,7 +40,6 @@ public enum CustomExceptionType { USER_KAKAO_UID_DUPLICATION(2204, "이미 가입한 계정입니다."), USER_NOT_FOUND_BY_KAKAO_UID(2205, "일치하는 회원을 찾을 수 없습니다."), USER_NOT_FOUND_BY_PHONE(2206, "일치하는 회원을 찾을 수 없습니다."), - USER_ID_FORBIDDEN(2207, "해당 유저는 접근이 금지되었습니다."), KAKAO_CLIENT(10000, "카카오 서버와의 통신 중 오류가 발생했습니다."), diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index ced0d36..6e46499 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -219,7 +219,7 @@ public void securitySetUp() throws Exception { String newContactLink = "https://new-contact-link.com"; UserDto expectedResult = createUserDto(userId); UserDetails testUser = createTestUser(userId); - given(userCommandService.updateContactLink(anyLong(), anyString())).willReturn(expectedResult); + given(userCommandService.updateContactLink(userId, newContactLink)).willReturn(expectedResult); // when & then mvc.perform(