Skip to content

Commit

Permalink
feat: 스터디 validator 테스트 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmondBreez3 committed Aug 16, 2024
1 parent ee35677 commit 9790339
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,6 @@ public boolean isAdmin() {
}

public boolean isMentor() {
return manageRole.equals(MENTOR);
return studyRole.equals(MENTOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void validateStudyMentor(Member currentMember, Study study) {
}

// 어드민이 아니고 멘토 역할도 아니면 경우에 예외가 밸생합니다.
if (!currentMember.isAdmin() || !currentMember.isMentor()) {
if (!currentMember.isAdmin() && !currentMember.isMentor()) {
throw new CustomException(STUDY_MENTOR_IS_UNAUTHORIZED);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gdschongik.gdsc.domain.study.domain;

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

import com.gdschongik.gdsc.domain.member.domain.Member;
Expand All @@ -17,14 +18,27 @@ public class StudyValidatorTest {

StudyValidator studyValidator = new StudyValidator();

// FixtureHelper 래핑 메서드
private Member createMentor(Long id) {
return fixtureHelper.createMentor(id);
}

private Member createMember(Long id) {
return fixtureHelper.createAssociateMember(id);
}

private Member createAdmin(Long id) {
return fixtureHelper.createAdmin(id);
}

@Nested
class 스터디_수강원_명단_조회시 {

@Test
public void 멘토가_아니라면_실패한다() {
public void 멘토역할이_아니라면_실패한다() {
// given
Member currentMember = fixtureHelper.createAssociateMember(1L);
Member mentor = fixtureHelper.createAssociateMember(2L);
Member currentMember = createMember(1L);
Member mentor = createMentor(2L);
LocalDateTime assignmentCreatedDate = LocalDateTime.now().minusDays(1);
Study study = fixtureHelper.createStudy(
mentor,
Expand All @@ -34,14 +48,14 @@ class 스터디_수강원_명단_조회시 {
// when & then
assertThatThrownBy(() -> studyValidator.validateStudyMentor(currentMember, study))
.isInstanceOf(CustomException.class)
.hasMessage(STUDY_MENTOR_INVALID.getMessage());
.hasMessage(STUDY_MENTOR_IS_UNAUTHORIZED.getMessage());
}

@Test
public void 멘토이지만_자신이_맡은_스터디가_아니라면_실패한다() {
// given
Member currentMember = fixtureHelper.createAssociateMember(1L);
Member mentor = fixtureHelper.createAssociateMember(2L);
Member currentMember = createMentor(1L);
Member mentor = createMentor(2L);
LocalDateTime assignmentCreatedDate = LocalDateTime.now().minusDays(1);
Study study = fixtureHelper.createStudy(
mentor,
Expand All @@ -63,18 +77,17 @@ class 스터디_수강원_명단_조회시 {
@Test
public void 어드민이라면_성공한다() {
// given
Member admin = fixtureHelper.createAssociateMember(1L);
Member mentor = fixtureHelper.createAssociateMember(2L);
Member admin = createAdmin(1L);
Member mentor = createMentor(2L);
LocalDateTime assignmentCreatedDate = LocalDateTime.now().minusDays(1);
Study study = fixtureHelper.createStudy(
mentor,
Period.createPeriod(assignmentCreatedDate.plusDays(5), assignmentCreatedDate.plusDays(10)),
Period.createPeriod(assignmentCreatedDate.minusDays(5), assignmentCreatedDate));

// when & then
assertThatThrownBy(() -> studyValidator.validateStudyMentor(admin, study))
.isInstanceOf(CustomException.class)
.hasMessage(STUDY_MENTOR_INVALID.getMessage());
assertThatCode(() -> studyValidator.validateStudyMentor(admin, study))
.doesNotThrowAnyException();
}
}
}
16 changes: 15 additions & 1 deletion src/test/java/com/gdschongik/gdsc/helper/FixtureHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.gdschongik.gdsc.helper;

import static com.gdschongik.gdsc.domain.member.domain.Department.*;
import static com.gdschongik.gdsc.domain.member.domain.Member.*;
import static com.gdschongik.gdsc.domain.member.domain.MemberManageRole.ADMIN;
import static com.gdschongik.gdsc.domain.member.domain.MemberStudyRole.MENTOR;
import static com.gdschongik.gdsc.global.common.constant.MemberConstant.*;
import static com.gdschongik.gdsc.global.common.constant.RecruitmentConstant.*;
import static com.gdschongik.gdsc.global.common.constant.SemesterConstant.*;
Expand Down Expand Up @@ -46,6 +47,19 @@ public Member createRegularMember(Long id) {
return member;
}

public Member createAdmin(Long id) {
Member member = createRegularMember(id);
ReflectionTestUtils.setField(member, "manageRole", ADMIN);
return member;
}

public Member createMentor(Long id) {
Member member = createRegularMember(id);
member.assignToMentor();
ReflectionTestUtils.setField(member, "studyRole", MENTOR);
return member;
}

public RecruitmentRound createRecruitmentRound(
LocalDateTime startDate,
LocalDateTime endDate,
Expand Down

0 comments on commit 9790339

Please sign in to comment.