Skip to content

Commit

Permalink
test: 회원가입 redis 인증 test code 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
hjj4060 committed Jul 5, 2024
1 parent 0d815d3 commit b2c06c1
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ out/
src/main/resources/app.key
src/main/resources/app.pub

!**/src/main/generated
/src/main/generated/
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Long signUp(CreateMemberRequest createMemberDTO) {
MemberEntity memberEntity = createMemberDTO.toMemberEntity(encodedPassword);

//인증코드 확인
boolean certifyStatus = verifyEmailCode(memberEntity.getEmail(), createMemberDTO.getCertifyNum());
boolean certifyStatus = verifyEmailCertifyNum(memberEntity.getEmail(), createMemberDTO.getCertifyNum());
if (certifyStatus) {
memberEntity = memberRepository.save(memberEntity);
} else {
Expand Down Expand Up @@ -129,12 +129,12 @@ private String sendEmailAuthentication(String toEmail) {
return certifyNum;
}

public Boolean verifyEmailCode(String email, String certifyNum) {
String codeFoundByEmail = redisUtil.getData(email);
if (codeFoundByEmail == null) {
public Boolean verifyEmailCertifyNum(String email, String certifyNum) {
String certifyNumFoundByRedis = redisUtil.getData(email);
if (certifyNumFoundByRedis == null) {
return false;
}

return codeFoundByEmail.equals(certifyNum);
return certifyNumFoundByRedis.equals(certifyNum);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spring:
# Redis
data:
redis:
host: localhost
host: ENC(GUuFDW7HjE98e7N28Vkb/xoIEgLeOCb5)
port: 6379
duration: 600

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class JasyptConfigTest {
@Test
void jasypt() {
String url = "";
String username = "[email protected]";
String password = "pcde pppv pnxa dfmg";
String username = "";
String password = "";

System.out.println(jasyptEncoding(url));
System.out.println(jasyptEncoding(username));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.store.clothstar.common.mail;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -10,7 +9,6 @@

@SpringBootTest
@ActiveProfiles("test")
@Disabled
class MailServiceTest {
@Autowired
MailService mailService;
Expand Down
26 changes: 18 additions & 8 deletions src/test/java/org/store/clothstar/common/redis/RedisUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package org.store.clothstar.common.redis;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class RedisUtilTest {

@Autowired
private RedisUtil redisUtil;


@DisplayName("redis 데이터가 생성되고 삭제되는지 확인한다.")
@Test
public void redisTest() throws Exception {
public void redisCreateAndDeleteTest() throws Exception {
//redis 데이터가 생성됐는지 확인한다.
//given
String email = "[email protected]";
String code = "aaa111";
String key = "[email protected]";
String value = "aaa111";

//when
redisUtil.setDataExpire(key, value);

//then
Assertions.assertTrue(redisUtil.existData(key));
Assertions.assertEquals(redisUtil.getData(key), value);

//redis 데이터가 삭제 됐는지 확인한다.
//when
redisUtil.setDataExpire(email, code);
redisUtil.deleteData(key);

//then
Assertions.assertTrue(redisUtil.existData("[email protected]"));
Assertions.assertFalse(redisUtil.existData("[email protected]"));
Assertions.assertEquals(redisUtil.getData(email), "aaa111");
Assertions.assertFalse(redisUtil.existData(key));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.store.clothstar.member.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,11 +12,9 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;
import org.store.clothstar.common.config.jwt.JwtUtil;
import org.store.clothstar.common.redis.RedisUtil;
import org.store.clothstar.member.domain.MemberRole;
import org.store.clothstar.member.dto.request.CreateSellerRequest;
import org.store.clothstar.member.dto.request.ModifyMemberRequest;
import org.store.clothstar.member.dto.request.ModifyPasswordRequest;
import org.store.clothstar.member.dto.request.*;
import org.store.clothstar.member.entity.MemberEntity;
import org.store.clothstar.member.repository.MemberRepository;
import org.store.clothstar.member.util.CreateObject;
Expand All @@ -41,23 +37,27 @@ class MemberAndSellerSignUpIntegrationTest {
private ObjectMapper objectMapper;

@Autowired
private JwtUtil jwtUtil;
private MemberRepository memberRepository;

@Autowired
private MemberRepository memberRepository;
private RedisUtil redisUtil;

private static final String MEMBER_URL = "/v1/members";
private static final String SELLER_URL = "/v1/sellers";
private MemberEntity memberEntity;

@DisplayName("회원가입을 완료한 후 memberId와 accessToken을 받아서 판매자 가입을 신청한 테스트이다.")
@DisplayName("회원가입 통합테스트")
@WithMockUser
@Disabled
@Test
void signUpAndSellerTest() throws Exception {
void signUpIntegrationTest() throws Exception {
//회원가입 통합 테스트
//given
final String requestBody = objectMapper.writeValueAsString(CreateObject.getCreateMemberRequest());
String email = "[email protected]";
String certifyNum = redisUtil.createdCertifyNum();
redisUtil.createRedisData(email, certifyNum);

CreateMemberRequest createMemberRequest = CreateObject.getCreateMemberRequest(email, certifyNum);
final String requestBody = objectMapper.writeValueAsString(createMemberRequest);

//when
ResultActions actions = mockMvc.perform(post(MEMBER_URL)
Expand All @@ -66,22 +66,39 @@ void signUpAndSellerTest() throws Exception {

//then
actions.andExpect(status().isCreated());
}

String responseBody = actions.andReturn().getResponse().getContentAsString();
JsonNode jsonNode = objectMapper.readTree(responseBody);
Long memberId = jsonNode.get("id").asLong();
MemberEntity memberEntity = memberRepository.findById(memberId).get();
String accessToken = jwtUtil.createAccessToken(memberEntity);
@DisplayName("회원가입시 인증번호 전송 통합테스트")
@WithMockUser
@Test
void certifyNumEmailSendTest() throws Exception {
//given
CertifyNumRequest certifyNumRequest = new CertifyNumRequest("[email protected]");
final String url = "/v1/members/auth";
final String requestBody = objectMapper.writeValueAsString(certifyNumRequest);

//when
ResultActions actions = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody));

//회원가입해서 받은 memberId로 판매자 신청 테스트
//then
actions.andExpect(status().isOk()).andDo(print());
}

@DisplayName("판매자(Seller) 가입 통합테스트")
@WithMockUser(roles = "SELLER")
@Test
void sellerSignUpIntegrationTest() throws Exception {
//given
memberEntity = memberRepository.save(CreateObject.getMemberEntityByCreateMemberRequestDTO());
Long memberId = memberEntity.getMemberId();
final String sellerUrl = SELLER_URL + "/" + memberId;
CreateSellerRequest createSellerRequest = getCreateSellerRequest(memberId);
final String sellerRequestBody = objectMapper.writeValueAsString(createSellerRequest);

//when
ResultActions sellerActions = mockMvc.perform(post(sellerUrl)
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(sellerRequestBody));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;
import org.store.clothstar.common.error.exception.SignupCertifyNumAuthFailedException;
import org.store.clothstar.member.dto.request.CreateMemberRequest;
import org.store.clothstar.member.dto.request.ModifyPasswordRequest;
import org.store.clothstar.member.repository.MemberRepository;
import org.store.clothstar.member.service.MemberService;
import org.store.clothstar.member.util.CreateObject;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand All @@ -36,9 +40,24 @@ public class MemberControllerValidationTest {
@Autowired
MemberRepository memberRepository;

@Autowired
MemberService memberService;

private static final String MEMBER_SIGN_UP_URL = "/v1/members";

@DisplayName("회원가입시 이메일 양식을 지켜야 한다..")
@DisplayName("회원가입시 인증번호가 틀리면 회원가입이 안돼야 한다.")
@Test
void signUpCertifyNumFailTest() {
//given
CreateMemberRequest createMemberRequest = CreateObject.getCreateMemberRequest();

//when & then
Throwable exception = assertThrows(SignupCertifyNumAuthFailedException.class, () -> {
memberService.signUp(createMemberRequest);
});
}

@DisplayName("회원가입시 이메일 양식을 지켜야 한다.")
@Test
void signUp_emailValidationTest() throws Exception {
//given
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/store/clothstar/member/util/CreateObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ public static CreateMemberRequest getCreateMemberRequest(String email) {
return createMemberRequest;
}

public static CreateMemberRequest getCreateMemberRequest(String email, String certifyNum) {
String password = "testl122";
String name = "현수";
String telNo = "010-1234-1245";

CreateMemberRequest createMemberRequest = new CreateMemberRequest(
email, password, name, telNo, certifyNum
);

return createMemberRequest;
}

public static MemberEntity getMemberEntityByCreateMemberRequestDTO() {
return getCreateMemberRequest().toMemberEntity();
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ spring:
auth: true
starttls:
enable: true
# Redis
data:
redis:
host: ENC(GUuFDW7HjE98e7N28Vkb/xoIEgLeOCb5)
port: 6379
duration: 600

jpa:
hibernate:
Expand Down

0 comments on commit b2c06c1

Please sign in to comment.