Skip to content

Commit

Permalink
Merge branch 'dev' into refactor/339
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongyun1206 authored Jan 10, 2025
2 parents 42c7d0b + 8b4773c commit 942618c
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class MemberController {

@PostMapping("/join")
@Operation(summary = "회원가입", description = "username과 password를 입력받아 회원가입을 진행합니다.")
public ApiResponse<?> joinProcess(@RequestBody JoinDTO joinDTO) {
public ApiResponse<?> joinProcess(@RequestBody JoinRequest joinRequest) {

joinService.join(joinDTO);
joinService.join(joinRequest);
return ApiResponse.onSuccess("회원가입 성공");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Member {
@Builder
public Member(String name, String email, Role role, Provider provider, String username, String password,
String region, Boolean isNewbie, Boolean isFirst, Long monthPriceMax,
List<String> wineSort, List<String> wineArea, boolean agreement, boolean isAdult) {
List<String> wineSort, List<String> wineArea, boolean agreement, boolean isAdult, String imageUrl) {
this.name = name;
this.email = email;
this.role = role;
Expand All @@ -96,6 +96,7 @@ public Member(String name, String email, Role role, Provider provider, String us
this.wineArea = wineArea != null ? wineArea : new ArrayList<>();
this.agreement = agreement;
this.isAdult = isAdult;
this.imageUrl = imageUrl;
}


Expand All @@ -111,7 +112,7 @@ public void updateImageUrl(String imageUrl) {

public static Member createMember(String username, String password, boolean isFirst) {
return Member.builder()
.username("drinkeg "+username)
.username(username)
.password(password)
.email(username)
.provider(Provider.DRINKEG)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.drinkeg.drinkeg.domain.member.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Setter
import java.time.LocalDate;
import java.util.List;

@Getter
public class JoinDTO {
@Builder
public class JoinRequest {

private String username;
private String password;
private String rePassword;


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public class JoinService {


@Transactional
public void join(JoinDTO joinDTO) {
public void join(JoinRequest joinRequest) {

String username = joinDTO.getUsername();
String password = joinDTO.getPassword();
String rePassword = joinDTO.getRePassword();
String username = joinRequest.getUsername();
String password = joinRequest.getPassword();
String rePassword = joinRequest.getRePassword();

if (memberRepository.existsByUsername("drinkeg "+ username)) {
if (memberRepository.existsByUsername(username)) {
throw new GeneralException(ErrorStatus.MEMBER_ALREADY_EXIST);
}
if (!password.equals(rePassword)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Member loadMemberByPrincipalDetail(PrincipalDetail principalDetail) {
return memberRepository.findByUsername(username).orElseThrow(()
-> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));
}

@Override
@Transactional
public void deleteMemberByUsername(String username){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ public String createJwt(String category,String username, String role, Long expir



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void findByNoExistUsernameTest(){

}


@DisplayName("존재하는 username으로 조회 했을 때 true를 반환한다.")
@Test
void existByusernameTest(){
Expand All @@ -66,14 +67,17 @@ void existByusernameTest(){

}


@DisplayName("존재하지 않는 username으로 조회 했을 때 false를 반환한다.")
@Test
void notExistByusernameTest(){

//given
Member member1 = memberRepository.save(createMember("user1","윤따" ));

//when
Boolean result = memberRepository.existsByUsername("user2");

//then
assertThat(result).isFalse();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.drinkeg.drinkeg.domain.member.service;

import com.drinkeg.drinkeg.IntegrationTestSupport;
import com.drinkeg.drinkeg.domain.member.domain.Member;
import com.drinkeg.drinkeg.domain.member.dto.JoinRequest;
import com.drinkeg.drinkeg.domain.member.enums.Role;
import com.drinkeg.drinkeg.domain.member.repostitory.MemberRepository;
import com.drinkeg.drinkeg.global.apipayLoad.code.status.ErrorStatus;
import com.drinkeg.drinkeg.global.exception.GeneralException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;

public class JoinServiceTest extends IntegrationTestSupport {

@Autowired
MemberRepository memberRepository;

@Autowired
JoinService joinService;

@DisplayName("새로운 멤버 정보를 저장한다.")
@Test
void joinMember(){

//given
JoinRequest joinRequest = createJoinRequest("itsme", "22azaz1234", "22azaz1234");

//when
joinService.join(joinRequest);

//then
Optional<Member> savedMember = memberRepository.findByUsername("itsme");
Member member = savedMember.get();
assertThat(member.getUsername()).isEqualTo("itsme");
assertThat(member.getPassword()).isNotEqualTo("22azaz1234"); //bCryptPasswordEncoder는 매번 다른 값을 생성
assertThat(member.isAdult()).isFalse();


}

@Test
@DisplayName("중복된 사용자 이름으로 회원가입 시 예외를 반환한다.")
void joinMember_DuplicateUsername() {
// Given
memberRepository.save(createMember("itsme", "notPassword"));
JoinRequest joinRequest = createJoinRequest("itsme", "22azaz1234", "22azaz1234");

// When & Then
assertThatThrownBy(() -> joinService.join(joinRequest))
.isInstanceOf(GeneralException.class)
.hasMessageContaining(ErrorStatus.MEMBER_ALREADY_EXIST.getMessage());
}

@Test
@DisplayName("비밀번호와 비밀번호 확인이 일치하지 않을 경우 예외를 반환한다.")
void joinMember_PasswordMismatch() {
// Given
JoinRequest joinRequest = createJoinRequest("itsme", "22azaz1234", "differentPassword");

// When & Then
assertThatThrownBy(() -> joinService.join(joinRequest))
.isInstanceOf(GeneralException.class)
.hasMessageContaining(ErrorStatus.PASSWORD_NOT_MATCH.getMessage());
}

@Test
@DisplayName("비밀번호가 유효하지 않을 경우 예외를 반환한다.")
void joinMember_InvalidPassword() {
// Given
JoinRequest joinRequest = createJoinRequest("itsme", "short", "short"); // 예: 너무 짧은 비밀번호

// When & Then
assertThatThrownBy(() -> joinService.join(joinRequest))
.isInstanceOf(GeneralException.class)
.hasMessageContaining(ErrorStatus.PASSWORD_NOT_INVALID.getMessage());
}
private JoinRequest createJoinRequest(String username, String password, String rePassword){
return JoinRequest.builder()
.username(username)
.password(password)
.rePassword(rePassword)
.build();


}

private Member createMember(String username,String password) {
return Member.builder()
.username(username)
.password(password)
.isFirst(false)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,159 @@
package com.drinkeg.drinkeg.domain.member.service;

public class MemberServiceTest {
import com.drinkeg.drinkeg.IntegrationTestSupport;
import com.drinkeg.drinkeg.domain.member.domain.Member;
import com.drinkeg.drinkeg.domain.member.dto.MemberInfoResponse;
import com.drinkeg.drinkeg.domain.member.enums.Provider;
import com.drinkeg.drinkeg.domain.member.enums.Role;
import com.drinkeg.drinkeg.domain.member.repostitory.MemberRepository;
import com.drinkeg.drinkeg.global.apipayLoad.code.status.ErrorStatus;
import com.drinkeg.drinkeg.global.exception.GeneralException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

public class MemberServiceTest extends IntegrationTestSupport {

@Autowired
MemberRepository memberRepository;

@Autowired
MemberService memberService;

@DisplayName("존재하는 username으로 사용자 정보를 불러온다.")
@Test
void findByUsernameTest(){

//given
Member member1 = memberRepository.save(createMember("user1", "윤다영","[email protected]","서울","Drinkeg", true,"https://drinkeg-bucket-1.s3.ap-northeast-2.amazonaws.com/member/profile/70af4bd2-717c-48d8-93d5-1a563de091a2" ));
Member member2 = memberRepository.save(createMember("user2","주민영","[email protected]","광주","Kakao", true ,null));


//when
MemberInfoResponse memberInfoResponse = memberService.showMemberInfo("user1");

//then
assertThat(memberInfoResponse).isNotNull();
assertThat(memberInfoResponse)
.extracting(
MemberInfoResponse::getUsername, // memberInfoResponse.getUsername() 호출
MemberInfoResponse::getEmail, // memberInfoResponse.getEmail() 호출
MemberInfoResponse::getCity, // memberInfoResponse.getCity() 호출
MemberInfoResponse::getAuthType, // memberInfoResponse.getAuthType() 호출
MemberInfoResponse::isAdult,
MemberInfoResponse::getImageUrl
)
.containsExactly(
"윤다영",
"[email protected]",
"서울",
"Drinkeg",
true,
"https://drinkeg-bucket-1.s3.ap-northeast-2.amazonaws.com/member/profile/70af4bd2-717c-48d8-93d5-1a563de091a2"
);
}

@DisplayName("존재하지 않는 username으로 조회 시 GeneralException을 반환한다")
@Test
void showMemberInfo_ThrowsException() {
// given

memberRepository.save(createMember("user1", "윤다영", "[email protected]", "서울", "Drinkeg", true," https://drinkeg-bucket-1.s3.ap-northeast-2.amazonaws.com/member/profile/70af4bd2-717c-48d8-93d5-1a563de091a2"));

// when & then
assertThatThrownBy(() -> memberService.showMemberInfo("user2"))
.isInstanceOf(GeneralException.class)
.hasMessageContaining(ErrorStatus.MEMBER_NOT_FOUND.getMessage());
}

@DisplayName("member의 해당 속성 값이 null일 경우 null을 그대로 반환한다.")
@Test
void memberInfoResponse_is_null_Test(){

//given
Member member2 = memberRepository.save(createMember("user1","주민영","[email protected]","광주","Kakao", true ,null));

//when
MemberInfoResponse memberInfoResponse = memberService.showMemberInfo("user1");

//then
assertThat(memberInfoResponse).isNotNull();
assertThat(memberInfoResponse)
.extracting(
MemberInfoResponse::getUsername, // memberInfoResponse.getUsername() 호출
MemberInfoResponse::getEmail, // memberInfoResponse.getEmail() 호출
MemberInfoResponse::getCity, // memberInfoResponse.getCity() 호출
MemberInfoResponse::getAuthType, // memberInfoResponse.getAuthType() 호출
MemberInfoResponse::isAdult,
MemberInfoResponse::getImageUrl
)
.containsExactly(
"주민영",
"[email protected]",
"광주",
"Kakao",
true,
null );
}

@Test
@DisplayName("존재하는 memberId로 멤버를 조회한다.")
void getMemberById_Success() {
// Given
Member member1 = memberRepository.save(createMember("user1", "윤다영", "[email protected]", "서울", "Drinkeg", true," https://drinkeg-bucket-1.s3.ap-northeast-2.amazonaws.com/member/profile/70af4bd2-717c-48d8-93d5-1a563de091a2"));

// When
Member foundMember = memberService.getMemberById(member1.getId());

// Then
assertThat(foundMember).isNotNull();
assertThat(foundMember.getUsername()).isEqualTo("user1");
assertThat(foundMember.getName()).isEqualTo("윤다영");
assertThat(foundMember.getEmail()).isEqualTo("[email protected]");
assertThat(foundMember.getRegion()).isEqualTo("서울");
assertThat(foundMember.getProvider().getValue()).isEqualTo("Drinkeg");
assertThat(foundMember.isAdult()).isTrue();
assertThat(foundMember.getImageUrl()).isNull();
}

@Test
@DisplayName("존재하지 않는 memberId로 조회 시 GeneralException 발생")
void getMemberById_Failure_MemberNotFound() {
// Given
Long nonExistentId = 999L;

// When & Then
assertThatThrownBy(() -> memberService.getMemberById(nonExistentId))
.isInstanceOf(GeneralException.class)
.hasMessageContaining(ErrorStatus.MEMBER_NOT_FOUND.getMessage());
}



private Member createMember(
String username,
String name,
String email,
String city,
String authType,
boolean adult,
String imageUrl
) {
return Member.builder()
.username(username)
.name(name)
.email(email)
.region(city)
.provider(Provider.fromValue(authType))
.isAdult(adult)
.role(Role.ROLE_USER)
.isFirst(false)
.imageUrl(imageUrl)
.build();
}
}

0 comments on commit 942618c

Please sign in to comment.