Skip to content

Commit

Permalink
Refactor/149 (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
betgws authored Jan 10, 2025
2 parents eedf53f + 7700c70 commit 8b4773c
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand All @@ -27,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 @@ -78,7 +78,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 @@ -95,6 +95,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 @@ -105,7 +106,7 @@ public Member(String name, String email, Role role, Provider provider, String us

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 @@ -2,7 +2,6 @@

import com.drinkeg.drinkeg.domain.member.dto.*;
import com.drinkeg.drinkeg.global.apipayLoad.code.status.ErrorStatus;
import com.drinkeg.drinkeg.domain.member.converter.MemberConverter;
import com.drinkeg.drinkeg.domain.member.domain.Member;
import com.drinkeg.drinkeg.domain.member.repostitory.MemberRepository;
import com.drinkeg.drinkeg.global.exception.GeneralException;
Expand All @@ -24,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 @@ -53,9 +53,8 @@ public void deleteMemberByUsername(String username){
@Override
public MemberInfoResponse showMemberInfo(String username){

Member member = memberRepository.findMemberByUsername(username);


Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

return MemberInfoResponse.create(member);
}
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();
}

}
Loading

0 comments on commit 8b4773c

Please sign in to comment.