diff --git a/src/test/java/com/gdschongik/gdsc/domain/member/application/OnboardingMemberServiceTest.java b/src/test/java/com/gdschongik/gdsc/domain/member/application/OnboardingMemberServiceTest.java new file mode 100644 index 000000000..0cd5d62b7 --- /dev/null +++ b/src/test/java/com/gdschongik/gdsc/domain/member/application/OnboardingMemberServiceTest.java @@ -0,0 +1,101 @@ +package com.gdschongik.gdsc.domain.member.application; + +import static org.assertj.core.api.Assertions.*; + +import com.gdschongik.gdsc.domain.member.dao.MemberRepository; +import com.gdschongik.gdsc.domain.member.domain.Department; +import com.gdschongik.gdsc.domain.member.domain.Member; +import com.gdschongik.gdsc.domain.member.domain.MemberRole; +import com.gdschongik.gdsc.domain.member.dto.request.MemberSignupRequest; +import com.gdschongik.gdsc.domain.member.dto.response.MemberInfoResponse; +import com.gdschongik.gdsc.global.exception.CustomException; +import com.gdschongik.gdsc.global.exception.ErrorCode; +import com.gdschongik.gdsc.integration.IntegrationTest; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +class OnboardingMemberServiceTest extends IntegrationTest { + + public static final MemberSignupRequest SIGNUP_REQUEST = + new MemberSignupRequest("C111001", "김홍익", "01012345678", Department.D015, "test@email.com"); + + @Autowired + private OnboardingMemberService onboardingMemberService; + + @Autowired + private MemberRepository memberRepository; + + private void setFixture() { + Member member = Member.createGuestMember("testOauthId"); + memberRepository.save(member); + } + + private void verifyEmail() { + Member member = memberRepository.findById(1L).get(); + member.completeUnivEmailVerification("test@g.hongik.ac.kr"); + memberRepository.save(member); + } + + @Nested + class 가입신청_수행시 { + + @Test + void 재학생_인증을_완료했다면_성공한다() { + // given + setFixture(); + logoutAndReloginAs(1L, MemberRole.GUEST); + verifyEmail(); + + // when + onboardingMemberService.signupMember(SIGNUP_REQUEST); + + // then + Member signupMember = memberRepository.findById(1L).get(); + assertThat(signupMember.isApplied()).isTrue(); + } + + @Test + void 재학생_인증을_미완료했다면_실패한다() { + // given + setFixture(); + logoutAndReloginAs(1L, MemberRole.GUEST); + + // when & then + assertThatThrownBy(() -> onboardingMemberService.signupMember(SIGNUP_REQUEST)) + .isInstanceOf(CustomException.class) + .hasMessage(ErrorCode.UNIV_NOT_VERIFIED.getMessage()); + } + } + + @Nested + class 회원정보_조회시 { + + @Test + void 가입신청을_완료헀다면_성공한다() { + // given + setFixture(); + logoutAndReloginAs(1L, MemberRole.GUEST); + verifyEmail(); + onboardingMemberService.signupMember(SIGNUP_REQUEST); + + // when + MemberInfoResponse response = onboardingMemberService.getMemberInfo(); + + // then + assertThat(response.memberId()).isEqualTo(1L); + } + + @Test + void 가입신청을_완료하지_않았다면_실패한다() { + // given + setFixture(); + logoutAndReloginAs(1L, MemberRole.GUEST); + + // when & then + assertThatThrownBy(() -> onboardingMemberService.getMemberInfo()) + .isInstanceOf(CustomException.class) + .hasMessage(ErrorCode.MEMBER_NOT_APPLIED.getMessage()); + } + } +} diff --git a/src/test/java/com/gdschongik/gdsc/integration/IntegrationTest.java b/src/test/java/com/gdschongik/gdsc/integration/IntegrationTest.java index c8634f8b6..cc577fe46 100644 --- a/src/test/java/com/gdschongik/gdsc/integration/IntegrationTest.java +++ b/src/test/java/com/gdschongik/gdsc/integration/IntegrationTest.java @@ -1,8 +1,13 @@ package com.gdschongik.gdsc.integration; +import com.gdschongik.gdsc.domain.member.domain.MemberRole; +import com.gdschongik.gdsc.global.security.PrincipalDetails; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @@ -16,4 +21,11 @@ public abstract class IntegrationTest { void setUp() { databaseCleaner.execute(); } + + protected void logoutAndReloginAs(Long memberId, MemberRole memberRole) { + PrincipalDetails principalDetails = new PrincipalDetails(memberId, memberRole); + Authentication authentication = + new UsernamePasswordAuthenticationToken(principalDetails, null, principalDetails.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authentication); + } }