Skip to content

Commit

Permalink
Merge pull request #135 from HandTris/#132
Browse files Browse the repository at this point in the history
ํšŒ์› ์ •๋ณด ์ˆ˜์ • ๊ธฐ๋Šฅ ์„ธ๋ถ„ํ™”
  • Loading branch information
thun0514 authored Jul 15, 2024
2 parents 65ebe12 + 0d34e92 commit 1a23c2e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jungle.HandTris.application.impl;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.Transactional;
import jungle.HandTris.application.service.MemberProfileService;
import jungle.HandTris.application.service.MemberRecordService;
Expand All @@ -10,8 +9,8 @@
import jungle.HandTris.domain.repo.MemberRepository;
import jungle.HandTris.global.jwt.JWTUtil;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import jungle.HandTris.presentation.dto.response.ReissueTokenRes;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.util.Pair;
Expand Down Expand Up @@ -44,42 +43,51 @@ public Pair<String, MemberRecordDetailRes> getMemberProfileWithStatsByNickname(S
}

@Override
public MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage) {
public ReissueTokenRes changeMemberNickname(String nickname, MemberUpdateReq memberUpdateReq) {

// ํ˜„์žฌ ๋‹‰๋„ค์ž„๊ณผ ๋™์ผํ•œ์ง€ ๊ฒ€์ฆ
if (memberUpdateReq.changeNickname().equals(nickname)) {
throw new NicknameNotChangedException();
}

boolean nicknameExists = memberRepository.existsByNickname(memberUpdateReq.changeNickname());
// ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋‹‰๋„ค์ž„์ธ์ง€ ๊ฒ€์ฆ
if (nicknameExists) {
throw new DuplicateNicknameException();
}

Boolean nicknameChanged = false;
String token = jwtUtil.resolveAccessToken(request);
String nickname = jwtUtil.getNickname(token);
Member member = memberRepository.findByNickname(nickname)
.orElseThrow(MemberNotFoundException::new);

member.updateNickname(memberUpdateReq.changeNickname());

// ๋ณ€๊ฒฝํ•  ๋‹‰๋„ค์ž„์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ
if (memberUpdateReq.nickname() != null && !memberUpdateReq.nickname().isEmpty()) {
// ํ˜„์žฌ ๋‹‰๋„ค์ž„๊ณผ ๋™์ผํ•œ์ง€ ๊ฒ€์ฆ
if (memberUpdateReq.nickname().equals(member.getNickname())) {
throw new NicknameNotChangedException();
}

boolean nicknameExists = memberRepository.existsByNickname(memberUpdateReq.nickname());
// ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋‹‰๋„ค์ž„์ธ์ง€ ๊ฒ€์ฆ
if (nicknameExists) {
throw new DuplicateNicknameException();
}
member.updateNickname(memberUpdateReq.nickname());
nicknameChanged = true;
}
String accessToken = jwtUtil.createAccessToken(memberUpdateReq.changeNickname());
String refreshToken = jwtUtil.createRefreshToken(memberUpdateReq.changeNickname());

member.updateRefreshToken(refreshToken);

return new ReissueTokenRes(accessToken, refreshToken);
}

if (deleteProfileImage) {
member.updateProfileImageUrl(defaultImage);
} else if (profileImage != null && profileImage.getSize() > 0) {
@Override
public void changeMemberProfileImage(String nickname, MultipartFile profileImage) {
if (profileImage != null && profileImage.getSize() > 0){
// ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ
validateImage(profileImage);

Member member = memberRepository.findByNickname(nickname)
.orElseThrow(MemberNotFoundException::new);

uploadImage(profileImage, member);
}
}

String accessToken = nicknameChanged ? jwtUtil.createAccessToken(member.getNickname()) : null;
@Override
public void deleteMemberProfileImage(String nickname) {
Member member = memberRepository.findByNickname(nickname)
.orElseThrow(MemberNotFoundException::new);

return new MemberProfileUpdateDetailsRes(member.getNickname(), member.getProfileImageUrl(), accessToken);
member.updateProfileImageUrl(defaultImage);
}

private void validateImage(MultipartFile profileImage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package jungle.HandTris.application.service;

import jakarta.servlet.http.HttpServletRequest;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import jungle.HandTris.presentation.dto.response.ReissueTokenRes;
import org.springframework.data.util.Pair;
import org.springframework.web.multipart.MultipartFile;

public interface MemberProfileService {
Pair<String, MemberRecordDetailRes> getMemberProfileWithStatsByNickname(String nickname);

MemberProfileUpdateDetailsRes updateMemberProfile(HttpServletRequest request, MemberUpdateReq memberUpdateReq, MultipartFile profileImage, Boolean deleteProfileImage);
ReissueTokenRes changeMemberNickname(String nickname, MemberUpdateReq memberUpdateReq);
void changeMemberProfileImage(String nickname, MultipartFile profileImage);
void deleteMemberProfileImage(String nickname);
}
2 changes: 1 addition & 1 deletion src/main/java/jungle/HandTris/global/dto/WhiteListURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

@Getter
public class WhiteListURI {
public static final List<String> WhiteListURI = List.of("mypage", "favicon", "actuator", "ws", "app", "topic");
public static final List<String> WhiteListURI = List.of("change", "favicon", "actuator", "ws", "app", "topic");
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
package jungle.HandTris.presentation;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jungle.HandTris.application.service.MemberProfileService;
import jungle.HandTris.application.service.MemberRecordService;
import jungle.HandTris.domain.MemberRecord;
import jungle.HandTris.global.dto.ResponseEnvelope;
import jungle.HandTris.global.validation.UserNicknameFromJwt;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberProfileRes;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.ReissueTokenRes;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/member")
@RequestMapping("/change")
public class MemberProfileController {
private final MemberProfileService memberProfileService;
private final MemberRecordService memberRecordService;

@GetMapping("/mypage")
public ResponseEnvelope<MemberProfileRes> myPage(@UserNicknameFromJwt String nickname) {
MemberRecord memberRecord = memberRecordService.getMemberRecord(nickname);
MemberProfileRes memberProfileRes = new MemberProfileRes(memberRecord);
return ResponseEnvelope.of(memberProfileRes);
@PutMapping("/profileNickname") // ๋‹‰๋„ค์ž„ ๋ณ€๊ฒฝ
public ResponseEnvelope<ReissueTokenRes> changeNickname (@UserNicknameFromJwt String nickname, @Valid @RequestBody MemberUpdateReq memberUpdateReq) {

ReissueTokenRes token = memberProfileService.changeMemberNickname(nickname, memberUpdateReq);

return ResponseEnvelope.of(token);
}

@PatchMapping("/mypage")
public ResponseEnvelope<MemberProfileUpdateDetailsRes> updateInfo(
HttpServletRequest request,
@Valid MemberUpdateReq memberUpdateReq,
@RequestPart(value = "profileImage", required = false) MultipartFile profileImage,
@RequestParam(value = "deleteProfileImage", required = false, defaultValue = "false") boolean deleteProfileImage
) {
@PatchMapping("/profileImage") // ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ
public ResponseEnvelope<String> changeProfileImage (@UserNicknameFromJwt String nickname, @RequestPart(value = "profileImage") MultipartFile profileImage) {

MemberProfileUpdateDetailsRes updateMemberDetails = memberProfileService.updateMemberProfile(request, memberUpdateReq, profileImage, deleteProfileImage);
memberProfileService.changeMemberProfileImage(nickname, profileImage);

return ResponseEnvelope.of(updateMemberDetails);
return ResponseEnvelope.of("Profile Image Change Successful");
}

@DeleteMapping("/profileImage") // ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ์ œ๊ฑฐ
public ResponseEnvelope<String> deleteProfileImage (@UserNicknameFromJwt String nickname) {

memberProfileService.deleteMemberProfileImage(nickname);

return ResponseEnvelope.of("Profile Image Delete Successful");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public record MemberUpdateReq (@Size(min = 4, max = 20, message = "๋‹‰๋„ค์ž„์€ ์ตœ์†Œ 4์ž ์ด์ƒ, 20์ž ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค")
@Pattern(regexp = "^[a-zA-Z0-9๊ฐ€-ํžฃ]+$", message = "๋‹‰๋„ค์ž„์€ ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž, ์ˆซ์ž, ํ•œ๊ธ€๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.")
@Pattern(regexp = "^((?!admin).)*$", message = "admin์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
String nickname) {
String changeNickname) {
}
54 changes: 19 additions & 35 deletions src/test/java/jungle/HandTris/Member/AuthServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import jungle.HandTris.domain.repo.MemberRepository;
import jungle.HandTris.presentation.dto.request.MemberRequest;
import jungle.HandTris.presentation.dto.request.MemberUpdateReq;
import jungle.HandTris.presentation.dto.response.MemberProfileUpdateDetailsRes;
import jungle.HandTris.presentation.dto.response.MemberRecordDetailRes;
import jungle.HandTris.presentation.dto.response.ReissueTokenRes;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -339,7 +339,7 @@ public void testMyPageConnectWrongUser() {

@Nested
@DisplayName("ํšŒ์› ์ •๋ณด ์š”์ฒญ")
class GetMemberProfile {
class GetMemberProfile {

}

Expand All @@ -365,12 +365,11 @@ public void testChangeMemberNicknameSuccess() {
MemberUpdateReq changeNickname = new MemberUpdateReq("user2");

// when
MemberProfileUpdateDetailsRes changeMemberProfile = memberProfileService.updateMemberProfile(
request, changeNickname, null, false);
ReissueTokenRes changeMemberProfile = memberProfileService.changeMemberNickname(
requestMember.nickname(), changeNickname);

// then
Assertions.assertThat(changeMemberProfile.nickname()).isEqualTo(member.getNickname());
Assertions.assertThat(changeMemberProfile.token()).isNotEqualTo(token);
Assertions.assertThat(changeMemberProfile.access()).isNotEqualTo(token);
}

@Test
Expand All @@ -390,8 +389,8 @@ public void testChangeMemberNicknameWithSameNickname() {

// when & then
Assertions.assertThatThrownBy(() -> {
memberProfileService.updateMemberProfile(
request, changeNickname, null, null);
memberProfileService.changeMemberNickname(
requestMember.nickname(), changeNickname);
}).isInstanceOf(NicknameNotChangedException.class);
}

Expand All @@ -414,8 +413,8 @@ public void testChangeMemberNicknameWithDuplicateNickname() {

// when & then
Assertions.assertThatThrownBy(() -> {
memberProfileService.updateMemberProfile(
request, changeNickname, null, null);
memberProfileService.changeMemberNickname(
requestMember1.nickname(), changeNickname);
}).isInstanceOf(DuplicateNicknameException.class);
}
}
Expand All @@ -440,7 +439,7 @@ public void testChangeMemberProfileImageSuccess() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Authorization", "Bearer " + token);

MemberUpdateReq changeNickname = new MemberUpdateReq(null);
String originImageUrl = member.getProfileImageUrl();

// ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ํ”„๋กœํ•„ ์‚ฌ์ง„
MockMultipartFile changeProfileImage = new MockMultipartFile(
Expand All @@ -452,11 +451,10 @@ public void testChangeMemberProfileImageSuccess() {
);

// when
MemberProfileUpdateDetailsRes changeMemberProfile = memberProfileService.updateMemberProfile(
request, changeNickname, changeProfileImage, false);
memberProfileService.changeMemberProfileImage(requestMember.nickname(), changeProfileImage);

// then
Assertions.assertThat(changeMemberProfile.profileImageUrl()).isEqualTo(member.getProfileImageUrl());
Assertions.assertThat(originImageUrl).isEqualTo(member.getProfileImageUrl());
}

@Test
Expand All @@ -472,7 +470,8 @@ public void testChangeMemberDefaultProfileImageSuccess() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Authorization", "Bearer " + token);

MemberUpdateReq changeNickname = new MemberUpdateReq(null);
String originImageUrl = member.getProfileImageUrl();

// ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ํ”„๋กœํ•„ ์‚ฌ์ง„
MockMultipartFile changeProfileImage = new MockMultipartFile(
"image", // ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„
Expand All @@ -481,14 +480,12 @@ public void testChangeMemberDefaultProfileImageSuccess() {
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=".getBytes() // PNG ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ (Base64)
);

MemberProfileUpdateDetailsRes changeMemberProfile = memberProfileService.updateMemberProfile(
request, changeNickname, changeProfileImage, false);
memberProfileService.changeMemberProfileImage(requestMember.nickname(), changeProfileImage);

Assertions.assertThat(changeMemberProfile.profileImageUrl()).isEqualTo(member.getProfileImageUrl());
Assertions.assertThat(originImageUrl).isEqualTo(member.getProfileImageUrl());

// when
memberProfileService.updateMemberProfile(
request, changeNickname, null, true);
memberProfileService.deleteMemberProfileImage(requestMember.nickname());
// then
Assertions.assertThat(defaultImage).isEqualTo(member.getProfileImageUrl());
}
Expand All @@ -499,14 +496,7 @@ public void testChangeMemberNicknameWithSameNickname() {
// given
MemberRequest requestMember = new MemberRequest("user1", "1q2w3e4r!", "user1");
authService.signup(requestMember);
Pair<Member, String> SigninResult = authService.signin(requestMember);
Member member = SigninResult.getFirst();
String token = SigninResult.getSecond();

MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Authorization", "Bearer " + token);

MemberUpdateReq changeNickname = new MemberUpdateReq(null);
// ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ํ”„๋กœํ•„ ์‚ฌ์ง„
MockMultipartFile changeProfileImage = new MockMultipartFile(
"image", // ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„
Expand All @@ -517,8 +507,7 @@ public void testChangeMemberNicknameWithSameNickname() {

// when & then
Assertions.assertThatThrownBy(() -> {
memberProfileService.updateMemberProfile(
request, changeNickname, changeProfileImage, false);
memberProfileService.changeMemberProfileImage(requestMember.nickname(), changeProfileImage);
}).isInstanceOf(InvalidImageTypeException.class);
}

Expand All @@ -532,10 +521,6 @@ public void testChangeMemberNicknameWithDuplicateNickname() {
Member member = SigninResult.getFirst();
String token = SigninResult.getSecond();

MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Authorization", "Bearer " + token);

MemberUpdateReq changeNickname = new MemberUpdateReq(null);
// ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ํ”„๋กœํ•„ ์‚ฌ์ง„
MockMultipartFile changeProfileImage = new MockMultipartFile(
"image", // ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„
Expand All @@ -550,8 +535,7 @@ public void testChangeMemberNicknameWithDuplicateNickname() {

// when & then
Assertions.assertThatThrownBy(() -> {
memberProfileService.updateMemberProfile(
request, changeNickname, changeProfileImage, false);
memberProfileService.changeMemberProfileImage(requestMember.nickname(), changeProfileImage);
}).isInstanceOf(InvalidImageTypeException.class);
}
}
Expand Down

0 comments on commit 1a23c2e

Please sign in to comment.