Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 온보딩 멤버 서비스 테스트 코드 #294

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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, "[email protected]");

@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("[email protected]");
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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}