Skip to content

Commit

Permalink
test: 재학생 인증 완료 여부에 대한 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook02 committed Jul 19, 2024
1 parent 2d3f282 commit 389ce0c
Showing 1 changed file with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
package com.gdschongik.gdsc.domain.email;
package com.gdschongik.gdsc.domain.email.domain;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static com.gdschongik.gdsc.global.exception.ErrorCode.*;
import static org.assertj.core.api.Assertions.*;

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.global.common.constant.MemberConstant;
import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.exception.ErrorCode;
import com.gdschongik.gdsc.global.util.email.HongikUnivEmailValidator;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

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

@Autowired
private HongikUnivEmailValidator hongikUnivEmailValidator;
HongikUnivEmailValidator hongikUnivEmailValidator = new HongikUnivEmailValidator();

@Test
@DisplayName("'g.hongik.ac.kr' 도메인을 가진 이메일을 검증할 수 있다.")
void validateEmailDomainTest() {
// given
String hongikDomainEmail = "[email protected]";
Optional<Member> optionalMember = Optional.empty();

assertThatCode(() -> hongikUnivEmailValidator.validate(hongikDomainEmail))
// when & then
assertThatCode(() -> hongikUnivEmailValidator.validate(hongikDomainEmail, optionalMember))
.doesNotThrowAnyException();
}

@ParameterizedTest
@ValueSource(strings = {"[email protected]", "[email protected]", "[email protected]", "[email protected]"})
@DisplayName("'g.hongik.ac.kr'가 아닌 도메인을 가진 이메일을 입력하면 예외를 발생시킨다.")
void validateEmailDomainMismatchTest(String email) {
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(email))
// given
Optional<Member> optionalMember = Optional.empty();

// when & then
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(email, optionalMember))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.UNIV_EMAIL_DOMAIN_MISMATCH.getMessage());
}

@Test
@DisplayName("Email의 '@' 앞 부분에는 연속되지 않은 점이 포함될 수 있다.")
void validateEmailFormatWithDotsTest() {
// given
String email = "[email protected]";
Optional<Member> optionalMember = Optional.empty();

assertThatCode(() -> hongikUnivEmailValidator.validate(email)).doesNotThrowAnyException();
assertThatCode(() -> hongikUnivEmailValidator.validate(email, optionalMember))
.doesNotThrowAnyException();
}

@ParameterizedTest
Expand All @@ -61,17 +68,39 @@ void validateEmailFormatWithDotsTest() {
})
@DisplayName("Email의 '@' 앞 부분에 '&', '=', ''', '-', '+', ',', '<', '>'가 포함되는 경우 예외를 발생시킨다.")
void validateEmailFormatMismatchTest(String email) {
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(email))
// given
Optional<Member> optionalMember = Optional.empty();

// when & then
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(email, optionalMember))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.UNIV_EMAIL_FORMAT_MISMATCH.getMessage());
}

@Test
@DisplayName("Email의 '@' 앞 부분에 '.'이 2개 연속 오는 경우 예외를 발생시킨다.")
void validateEmailFormatMismatchWithDotsTest() {
// given
String email = "[email protected]";
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(email))
Optional<Member> optionalMember = Optional.empty();

// when & then
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(email, optionalMember))
.isInstanceOf(CustomException.class)
.hasMessage(ErrorCode.UNIV_EMAIL_FORMAT_MISMATCH.getMessage());
}

@Test
void 이미_가입된_재학생_메일이라면_실패한다() {
// given
String hongikDomainEmail = "[email protected]";

Member member = Member.createGuestMember(MemberConstant.OAUTH_ID);
Optional<Member> optionalMember = Optional.of(member);

// when & then
assertThatThrownBy(() -> hongikUnivEmailValidator.validate(hongikDomainEmail, optionalMember))
.isInstanceOf(CustomException.class)
.hasMessage(UNIV_EMAIL_ALREADY_SATISFIED.getMessage());
}
}

0 comments on commit 389ce0c

Please sign in to comment.