Skip to content

Commit

Permalink
Test : 이메일 인증 단위테스트(진행중)
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhun0331 committed Aug 9, 2024
1 parent b2d83bd commit 2694ce8
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import umc.kkijuk.server.member.emailauth.MailAddressDto;
import umc.kkijuk.server.member.emailauth.MailCertificationDto;
import umc.kkijuk.server.member.emailauth.MailCertificationResponse;
import umc.kkijuk.server.member.emailauth.MailService;
import umc.kkijuk.server.member.emailauth.MailServiceImpl;

@Tag(name = "auth", description = "이메일 인증 API")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class EmailAuthController {

private final MailService mailService;
private final MailServiceImpl mailService;

@Operation(
summary = "이메일 인증번호 요청",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import umc.kkijuk.server.member.controller.response.*;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.dto.*;
import umc.kkijuk.server.member.emailauth.MailServiceImpl;
import umc.kkijuk.server.member.service.MemberService;
import lombok.RequiredArgsConstructor;

Expand All @@ -23,7 +24,7 @@
public class MemberController {

private final MemberService memberService;
// private final MailService mailService;
private final MailServiceImpl mailService;
private int authRandomNumber; // 이메일 인증 숫자를 저장하는 변수

@Operation(
Expand All @@ -43,7 +44,7 @@ public ResponseEntity<CreateMemberResponse> saveMember(@RequestBody @Valid Membe
description = "회원가입시 이메일 중복을 확인합니다.")
@PostMapping("/confirmEmail")
public ResponseEntity<Boolean> confirmEmail(@RequestBody MemberEmailDto memberEmailDto){
Boolean result = memberService.confirmDupEmail(memberEmailDto);
Boolean result = mailService.confirmDupEmail(memberEmailDto);
return ResponseEntity.ok(result);
}

Expand Down
78 changes: 6 additions & 72 deletions src/main/java/umc/kkijuk/server/member/emailauth/MailService.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,12 @@
package umc.kkijuk.server.member.emailauth;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import umc.kkijuk.server.common.domian.exception.CertificationNumberMismatchException;
import umc.kkijuk.server.common.domian.exception.MemberAlreadyExistsException;
import umc.kkijuk.server.member.repository.MemberJpaRepository;
import umc.kkijuk.server.member.dto.MemberEmailDto;

import java.util.Random;
public interface MailService {

@Service
@RequiredArgsConstructor
public class MailService {
MailCertificationResponse sendMail(String mail);

private final MailCertification mailCertification;
private final JavaMailSender javaMailSender;
private final MemberJpaRepository memberJpaRepository;
Boolean verifyMail(MailCertificationDto requestDto);

@Value("${spring.mail.username}")
private String senderEmail;


private String createRandomNumber() {
Random rand = new Random();
String randomNum = "";
for (int i = 0; i < 6; i++) {
String random = Integer.toString(rand.nextInt(10));
randomNum += random;
}

return randomNum;
}

public MailCertificationResponse sendMail(String mail) {
if(memberJpaRepository.findByEmail(mail).isPresent()){
throw new MemberAlreadyExistsException();
}

MimeMessage message = javaMailSender.createMimeMessage();
String randomNum = createRandomNumber();

try {
message.setFrom(senderEmail);
message.setRecipients(MimeMessage.RecipientType.TO, mail);
message.setSubject("이메일 인증");
String body = "";
body += "<h3>" + "요청하신 인증 번호입니다." + "</h3>";
body += "<h1>" + randomNum + "</h1>";
body += "<h3>" + "감사합니다." + "</h3>";
message.setText(body,"UTF-8", "html");
} catch (MessagingException e) {
e.printStackTrace();
}

javaMailSender.send(message);
mailCertification.createMailCertification(mail,randomNum);
return new MailCertificationResponse(mail, randomNum);
}

// 인증 번호 검증
public Boolean verifyMail(MailCertificationDto requestDto) {
if (isVerify(requestDto)) {
throw new CertificationNumberMismatchException();
}
mailCertification.deleteMailCertification(requestDto.getEmail());

return true;
}
private boolean isVerify(MailCertificationDto requestDto) {
return !(mailCertification.hasKey(requestDto.getEmail()) &&
mailCertification.getMailCertification(requestDto.getEmail())
.equals(requestDto.getAuthNumber()));
}
}
Boolean confirmDupEmail(MemberEmailDto memberEmailDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package umc.kkijuk.server.member.emailauth;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import umc.kkijuk.server.common.domian.exception.CertificationNumberMismatchException;
import umc.kkijuk.server.common.domian.exception.MemberAlreadyExistsException;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.dto.MemberEmailDto;
import umc.kkijuk.server.member.repository.MemberRepository;

import java.util.Optional;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService{

private final MailCertification mailCertification;
private final JavaMailSender javaMailSender;
private final MemberRepository memberRepository;

@Value("${spring.mail.username}")
private String senderEmail;


private String createRandomNumber() {
Random rand = new Random();
String randomNum = "";
for (int i = 0; i < 6; i++) {
String random = Integer.toString(rand.nextInt(10));
randomNum += random;
}

return randomNum;
}

public MailCertificationResponse sendMail(String mail) {
if(memberRepository.findByEmail(mail).isPresent()){
throw new MemberAlreadyExistsException();
}

MimeMessage message = javaMailSender.createMimeMessage();
String randomNum = createRandomNumber();

try {
message.setFrom(senderEmail);
message.setRecipients(MimeMessage.RecipientType.TO, mail);
message.setSubject("이메일 인증");
String body = "";
body += "<h3>" + "요청하신 인증 번호입니다." + "</h3>";
body += "<h1>" + randomNum + "</h1>";
body += "<h3>" + "감사합니다." + "</h3>";
message.setText(body,"UTF-8", "html");
} catch (MessagingException e) {
e.printStackTrace();
}

javaMailSender.send(message);
mailCertification.createMailCertification(mail,randomNum);
return new MailCertificationResponse(mail, randomNum);
}

// 인증 번호 검증
public Boolean verifyMail(MailCertificationDto requestDto) {
if (isVerify(requestDto)) {
throw new CertificationNumberMismatchException();
}
mailCertification.deleteMailCertification(requestDto.getEmail());

return true;
}
private Boolean isVerify(MailCertificationDto requestDto) {
return !(mailCertification.hasKey(requestDto.getEmail()) &&
mailCertification.getMailCertification(requestDto.getEmail())
.equals(requestDto.getAuthNumber()));
}

@Override
public Boolean confirmDupEmail(MemberEmailDto memberEmailDto) {
Optional<Member> member = memberRepository.findByEmail(memberEmailDto.getEmail());
if(member.isEmpty()){
return true;
}else{
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package umc.kkijuk.server.member.repository;

import umc.kkijuk.server.member.domain.Member;

import java.util.Optional;

public interface MemberRepository {
Optional<Member> findById(Long id);
Optional<Member> findByEmail(String email);
Member save(Member member);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package umc.kkijuk.server.member.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import umc.kkijuk.server.member.domain.Member;

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class MemberRepositoryImpl implements MemberRepository{
private final MemberJpaRepository memberJpaRepository;

@Override
public Optional<Member> findById(Long id) {
return memberJpaRepository.findById(id);
}

@Override
public Optional<Member> findByEmail(String email) {
return memberJpaRepository.findByEmail(email);
}

@Override
public Member save(Member member) {
return memberJpaRepository.save(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface MemberService {
Member changeMemberPassword(Long memberId, MemberPasswordChangeDto memberPasswordChangeDto);
Member myPagePasswordAuth(Long memberId, MyPagePasswordAuthDto myPagePasswordAuthDto);
MemberEmailResponse getMemberEmail(Long memberId);
EmailAuthResponse getEmailAuth(EmailAddressDto emailAddressDto, int number);
// EmailAuthResponse getEmailAuth(EmailAddressDto emailAddressDto, int number);
MemberStateResponse changeMemberState(Long memberId);
Boolean confirmDupEmail(MemberEmailDto memberEmailDto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import umc.kkijuk.server.member.domain.State;
import umc.kkijuk.server.member.dto.*;
import umc.kkijuk.server.member.repository.MemberJpaRepository;
import umc.kkijuk.server.member.repository.MemberRepository;

import java.util.List;
import java.util.Optional;
Expand All @@ -21,12 +22,12 @@
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberServiceImpl implements MemberService {
private final MemberJpaRepository memberJpaRepository;
private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;

@Override
public Member getById(Long memberId) {
return memberJpaRepository.findById(memberId)
return memberRepository.findById(memberId)
.orElseThrow(() -> new ResourceNotFoundException("Member", memberId));
}

Expand All @@ -43,7 +44,7 @@ public Member join(MemberJoinDto memberJoinDto) {
String encodedPassword = passwordEncoder.encode(memberJoinDto.getPassword());
joinMember.changeMemberPassword(encodedPassword);

return memberJpaRepository.save(joinMember);
return memberRepository.save(joinMember);
}

@Override
Expand Down Expand Up @@ -85,7 +86,7 @@ public Member updateMemberField(Long memberId, MemberFieldDto memberFieldDto){
if(!member.getField().equals(memberFieldDto.getField())){
throw new FieldUpdateException();
}
return memberJpaRepository.save(member);
return memberRepository.save(member);
}

@Override
Expand All @@ -96,7 +97,7 @@ public Member updateMemberInfo(Long memberId, MemberInfoChangeDto memberInfoChan
throw new InvalidMemberDataException();
}
member.changeMemberInfo(memberInfoChangeDto.getPhoneNumber(), memberInfoChangeDto.getBirthDate(), memberInfoChangeDto.getMarketingAgree());
return memberJpaRepository.save(member);
return memberRepository.save(member);
}

@Override
Expand All @@ -113,7 +114,7 @@ public Member changeMemberPassword(Long memberId, MemberPasswordChangeDto member
String encodedPassword = passwordEncoder.encode(memberPasswordChangeDto.getNewPassword());
member.changeMemberPassword(encodedPassword);

return memberJpaRepository.save(member);
return memberRepository.save(member);
}

@Override
Expand All @@ -127,15 +128,6 @@ public Member myPagePasswordAuth(Long memberId, MyPagePasswordAuthDto myPagePass
return member;
}

@Override
@Transactional
public EmailAuthResponse getEmailAuth(EmailAddressDto emailAddressDto, int authRandomNumber){
return EmailAuthResponse.builder()
.email(emailAddressDto.getEmail())
.authNumber(authRandomNumber)
.build();
}

@Override
@Transactional
public MemberStateResponse changeMemberState(Long memberId){
Expand All @@ -147,21 +139,22 @@ else if(member.getUserState().equals(State.ACTIVATE)){
member.inactivate();
}

memberJpaRepository.save(member);
memberRepository.save(member);

return MemberStateResponse.builder()
.deleteDate(member.getDeleteDate())
.build();
}

@Override
public Boolean confirmDupEmail(MemberEmailDto memberEmailDto) {
Optional<Member> member = memberJpaRepository.findByEmail(memberEmailDto.getEmail());
if(member.isEmpty()){
return true;
}else{
return false;
}
}
// @Override
// @Transactional
// public EmailAuthResponse getEmailAuth(EmailAddressDto emailAddressDto, int authRandomNumber){
// return EmailAuthResponse.builder()
// .email(emailAddressDto.getEmail())
// .authNumber(authRandomNumber)
// .build();
// }



}
Loading

0 comments on commit 2694ce8

Please sign in to comment.