From ac400aaf6b3ae667f12ed25ccb38da31c72caa74 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Wed, 27 Mar 2024 01:41:39 +0900 Subject: [PATCH] =?UTF-8?q?test:=20#24=20=EB=82=B4=20=EB=B9=84=EB=B0=80?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EB=B3=80=EA=B2=BD=20API=20test=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserControllerTest.java | 27 ++++++++++++++ .../user/service/UserCommandServiceTest.java | 35 +++++++++++++++++++ 2 files changed, 62 insertions(+) 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 6ea3135..6595538 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 @@ -268,6 +268,33 @@ public void securitySetUp() throws Exception { verifyEveryMocksShouldHaveNoMoreInteractions(); } + @Test + void 주어진_유저의_id와_새로운_비밀번호로_기존_비밀번호를_업데이트한다() throws Exception { + // given + long userId = 1L; + String newPassword = "newPwd11!!"; + UserDetails testUser = createTestUser(userId); + UserDto expectedResult = createUserDto(userId); + given(userCommandService.updatePassword(userId, newPassword)).willReturn(expectedResult); + + // when & then + mvc.perform( + put("/api/users/me/password") + .header(API_VERSION_HEADER_NAME, 1) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(newPassword)) + .with(user(testUser)) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(expectedResult.getId())) + .andExpect(jsonPath("$.email").value(expectedResult.getEmail())) + .andExpect(jsonPath("$.birth").value(expectedResult.getBirth().toString())) + .andExpect(jsonPath("$.gender").value(expectedResult.getGender().name())) + .andExpect(jsonPath("$.contactLink").value(expectedResult.getContactLink())); + then(userCommandService).should().updatePassword(userId, newPassword); + 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 83e571b..cbe94c5 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 @@ -239,6 +239,41 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { assertThat(t).isInstanceOf(UserNotFoundByIdException.class); } + @Test + void 주어진_유저_ID와_새로운_비밀번호로_유저의_비밀번호를_변경한다() throws Exception { + // given + Long userId = 1L; + String newPassword = "newPwd1234!!"; + User user = createUser(userId, "$2a$abc123", "12345"); + given(userQueryService.getById(userId)).willReturn(user); + given(passwordEncoder.encode(newPassword)).willReturn(newPassword); + + // when + UserDto updatedUserDto = sut.updatePassword(userId, newPassword); + + // then + then(userQueryService).should().getById(userId); + then(passwordEncoder).should().encode(newPassword); + verifyEveryMocksShouldHaveNoMoreInteractions(); + assertThat(updatedUserDto.getPassword()).isEqualTo(newPassword); + } + + @Test + void 주어진_유저_ID와_새로운_비밀번호로_유저의_비밀번호를_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception { + // given + Long userId = 1L; + String newPassword = "newPwd1234!!"; + given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class); + + // when + Throwable t = catchThrowable(() -> sut.updatePassword(userId, newPassword)); + + // then + then(userQueryService).should().getById(userId); + verifyEveryMocksShouldHaveNoMoreInteractions(); + assertThat(t).isInstanceOf(UserNotFoundByIdException.class); + } + private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userQueryService).shouldHaveNoMoreInteractions(); then(userRepository).shouldHaveNoMoreInteractions();