Skip to content

Commit

Permalink
test: #24 내 비밀번호 변경 API test 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tinon1004 committed Mar 26, 2024
1 parent b76852d commit ac400aa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,41 @@ static Stream<Arguments> 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();
Expand Down

0 comments on commit ac400aa

Please sign in to comment.