Skip to content

Commit

Permalink
Merge pull request #94 from Ajou-Hertz/feature/#24-edit-password
Browse files Browse the repository at this point in the history
내 비밀번호 변경 API 구현
  • Loading branch information
Wo-ogie authored Mar 27, 2024
2 parents d7d03c2 + 9821467 commit ba748bc
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.request.UpdatePasswordRequest;
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;
Expand Down Expand Up @@ -152,5 +153,23 @@ public UserResponse updateContactLinkV1(
updateContactLinkRequest.getContactLink());
return UserResponse.from(userUpdated);
}

@Operation(
summary = "비밀번호 변경",
description = "회원의 비밀번호를 변경합니다.",
security = @SecurityRequirement(name = "access-token")
)
@PutMapping(value = "/me/password", headers = API_VERSION_HEADER_NAME + "=" + 1)
public UserResponse updatePasswordV1(
@RequestBody @Valid UpdatePasswordRequest updatePasswordRequest,
@AuthenticationPrincipal UserPrincipal userPrincipal
) {
UserDto userUpdated = userCommandService.updatePassword(
userPrincipal.getUserId(),
updatePasswordRequest.getPassword()
);
return UserResponse.from(userUpdated);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ajou.hertz.domain.user.dto.request;

import com.ajou.hertz.common.validator.Password;

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 UpdatePasswordRequest {

@Schema(description = "비밀번호", example = "newpwd1234!!")
@NotBlank
@Password
private String password;
}
4 changes: 4 additions & 0 deletions src/main/java/com/ajou/hertz/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ public void changeContactLink(String contactLink) {
this.contactLink = contactLink;
}

public void changePassword(String encodedPassword) {
this.password = encodedPassword;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,18 @@ public UserDto updateContactLink(Long userId, String contactLink) {
return UserDto.from(user);
}

/**
* 유저의 비밀번호를 변경합니다.
*
* @param userId 유저의 ID
* @param password 변경할 비밀번호
*
* @return 변경된 유저 정보
*/
public UserDto updatePassword(Long userId, String password) {
User user = userQueryService.getById(userId);
user.changePassword(passwordEncoder.encode(password));
return UserDto.from(user);
}

}
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,25 @@ 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);
}

private void verifyEveryMocksShouldHaveNoMoreInteractions() {
then(userQueryService).shouldHaveNoMoreInteractions();
then(userRepository).shouldHaveNoMoreInteractions();
Expand Down

0 comments on commit ba748bc

Please sign in to comment.