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

[feat] 카테고리 ADMIN 역할을 포기하더라도 자신이 생성한 카테고리라면 구독 해제가 안되는 문제를 해결한다. #713

Merged
merged 4 commits into from
Oct 6, 2022
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
Expand Up @@ -102,6 +102,10 @@ public boolean isCreatorId(final Long creatorId) {
return member.hasSameId(creatorId);
}

public boolean isNormal() {
return categoryType == NORMAL;
}

public boolean isPersonal() {
return categoryType == PERSONAL;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.allog.dallog.domain.categoryrole.application;

import com.allog.dallog.domain.category.domain.Category;
import com.allog.dallog.domain.categoryrole.domain.CategoryAuthority;
import com.allog.dallog.domain.categoryrole.domain.CategoryRole;
import com.allog.dallog.domain.categoryrole.domain.CategoryRoleRepository;
import com.allog.dallog.domain.categoryrole.domain.CategoryRoleType;
import com.allog.dallog.domain.categoryrole.dto.request.CategoryRoleUpdateRequest;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToMangeRoleException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToChangeRoleException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,6 +29,9 @@ public void updateRole(final Long loginMemberId, final Long memberId, final Long
validateSoleAdmin(memberId, categoryId);

CategoryRole categoryRole = categoryRoleRepository.getByMemberIdAndCategoryId(memberId, categoryId);

validateCategoryType(categoryRole);

categoryRole.changeRole(roleType);
}

Expand All @@ -40,7 +44,15 @@ private void validateAuthority(final Long loginMemberId, final Long categoryId)
private void validateSoleAdmin(final Long memberId, final Long categoryId) {
boolean isSoleAdmin = categoryRoleRepository.isMemberSoleAdminInCategory(memberId, categoryId);
if (isSoleAdmin) {
throw new NotAbleToMangeRoleException("변경 대상 회원이 유일한 ADMIN이므로 다른 역할로 변경할 수 없습니다.");
throw new NotAbleToChangeRoleException("변경 대상 회원이 유일한 ADMIN이므로 다른 역할로 변경할 수 없습니다.");
}
}

private void validateCategoryType(final CategoryRole categoryRole) {
Category category = categoryRole.getCategory();

if (!category.isNormal()) {
throw new NotAbleToChangeRoleException("개인 카테고리 또는 외부 카테고리에 대한 회원의 역할을 변경할 수 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.allog.dallog.domain.categoryrole.exception;

public class NotAbleToChangeRoleException extends RuntimeException {

public NotAbleToChangeRoleException(final String message) {
super(message);
}

public NotAbleToChangeRoleException() {
super("역할을 변경할 수 없습니다.");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,10 @@ public void update(final Long id, final Long memberId, final SubscriptionUpdateR
@Transactional
public void delete(final Long id, final Long memberId) {
Subscription subscription = subscriptionRepository.getById(id);
deleteSubscription(id, memberId, subscription);
deleteCategoryRole(memberId, subscription);
}

private void deleteSubscription(final Long id, final Long memberId, final Subscription subscription) {
Comment on lines -98 to -102
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 로직이 제거되었군요!👍👍

subscription.validateDeletePossible(memberId);
subscriptionRepository.deleteById(id);

deleteCategoryRole(memberId, subscription);
}

private void deleteCategoryRole(final Long memberId, final Subscription subscription) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void change(final Color color, final boolean checked) {
}

public void validateDeletePossible(final Long memberId) {
if (category.isCreatorId(memberId)) {
throw new NoPermissionException("내가 만든 카테고리는 구독 취소 할 수 없습니다.");
if (!member.hasSameId(memberId)) {
throw new NoPermissionException("타인의 구독 정보에 접근할 수 없습니다.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.allog.dallog.domain.category.exception.NoSuchCategoryException;
import com.allog.dallog.domain.categoryrole.exception.NoCategoryAuthorityException;
import com.allog.dallog.domain.categoryrole.exception.NoSuchCategoryRoleException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToMangeRoleException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToChangeRoleException;
import com.allog.dallog.domain.member.exception.InvalidMemberException;
import com.allog.dallog.domain.member.exception.NoSuchMemberException;
import com.allog.dallog.domain.schedule.exception.InvalidScheduleException;
Expand Down Expand Up @@ -45,7 +45,7 @@ public class ControllerAdvice {
InvalidSubscriptionException.class,
ExistSubscriptionException.class,
ExistExternalCategoryException.class,
NotAbleToMangeRoleException.class
NotAbleToChangeRoleException.class
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

더욱 명확한 네이밍이군요!👍👍

})
public ResponseEntity<ErrorResponse> handleInvalidData(final RuntimeException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.allog.dallog.domain.categoryrole.application;

import static com.allog.dallog.common.fixtures.CategoryFixtures.BE_일정_생성_요청;
import static com.allog.dallog.common.fixtures.CategoryFixtures.내_일정_생성_요청;
import static com.allog.dallog.common.fixtures.CategoryFixtures.외부_BE_일정_생성_요청;
import static com.allog.dallog.common.fixtures.MemberFixtures.관리자;
import static com.allog.dallog.common.fixtures.MemberFixtures.매트;
import static com.allog.dallog.common.fixtures.MemberFixtures.후디;
Expand All @@ -16,7 +18,7 @@
import com.allog.dallog.domain.categoryrole.domain.CategoryRoleRepository;
import com.allog.dallog.domain.categoryrole.dto.request.CategoryRoleUpdateRequest;
import com.allog.dallog.domain.categoryrole.exception.NoCategoryAuthorityException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToMangeRoleException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToChangeRoleException;
import com.allog.dallog.domain.member.domain.Member;
import com.allog.dallog.domain.member.domain.MemberRepository;
import com.allog.dallog.domain.subscription.application.SubscriptionService;
Expand Down Expand Up @@ -139,6 +141,32 @@ class CategoryRoleServiceTest extends ServiceTest {
// when & then
CategoryRoleUpdateRequest request = new CategoryRoleUpdateRequest(NONE);
assertThatThrownBy(() -> categoryRoleService.updateRole(관리자.getId(), 관리자.getId(), BE_일정.getId(), request))
.isInstanceOf(NotAbleToMangeRoleException.class);
.isInstanceOf(NotAbleToChangeRoleException.class);
}

@DisplayName("개인 카테고리에 대한 회원의 역할을 변경할 경우 예외가 발생한다.")
@Test
void 개인_카테고리에_대한_회원의_역할을_변경할_경우_예외가_발생한다() {
// given
Member 후디 = memberRepository.save(후디());
CategoryResponse 개인_카테고리 = categoryService.save(후디.getId(), 내_일정_생성_요청);

// when & then
CategoryRoleUpdateRequest request = new CategoryRoleUpdateRequest(NONE);
assertThatThrownBy(() -> categoryRoleService.updateRole(후디.getId(), 후디.getId(), 개인_카테고리.getId(), request))
.isInstanceOf(NotAbleToChangeRoleException.class);
}

@DisplayName("외부 카테고리에 대한 회원의 역할을 변경할 경우 예외가 발생한다.")
@Test
void 외부_카테고리에_대한_회원의_역할을_변경할_경우_예외가_발생한다() {
Comment on lines +146 to +162
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

꼼꼼한 테스트 코드군요!!

// given
Member 후디 = memberRepository.save(후디());
CategoryResponse 외부_카테고리 = categoryService.save(후디.getId(), 외부_BE_일정_생성_요청);

// when & then
CategoryRoleUpdateRequest request = new CategoryRoleUpdateRequest(NONE);
assertThatThrownBy(() -> categoryRoleService.updateRole(후디.getId(), 후디.getId(), 외부_카테고리.getId(), request))
.isInstanceOf(NotAbleToChangeRoleException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static com.allog.dallog.common.fixtures.CategoryFixtures.내_일정_생성_요청;
import static com.allog.dallog.common.fixtures.MemberFixtures.관리자;
import static com.allog.dallog.common.fixtures.MemberFixtures.후디;
import static com.allog.dallog.common.fixtures.SubscriptionFixtures.색상1_구독;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
Expand All @@ -30,7 +29,6 @@
import com.allog.dallog.domain.member.domain.Member;
import com.allog.dallog.domain.member.domain.MemberRepository;
import com.allog.dallog.domain.subscription.domain.Color;
import com.allog.dallog.domain.subscription.domain.Subscription;
import com.allog.dallog.domain.subscription.domain.SubscriptionRepository;
import com.allog.dallog.domain.subscription.dto.request.SubscriptionUpdateRequest;
import com.allog.dallog.domain.subscription.dto.response.SubscriptionResponse;
Expand Down Expand Up @@ -260,19 +258,6 @@ class SubscriptionServiceTest extends ServiceTest {
.isInstanceOf(NoPermissionException.class);
}

@DisplayName("자신이 만든 카테고리에 대한 구독을 삭제할 경우 예외를 던진다")
@Test
void 자신이_만든_카테고리에_대한_구독을_삭제할_경우_예외를_던진다() {
// given
Member 관리자 = memberRepository.save(관리자());
Category 공통_일정 = categoryRepository.save(공통_일정(관리자));
Subscription 공통_일정_구독 = subscriptionRepository.save(색상1_구독(관리자, 공통_일정));

// when & then
assertThatThrownBy(() -> subscriptionService.delete(공통_일정_구독.getId(), 관리자.getId()))
.isInstanceOf(NoPermissionException.class);
}

Comment on lines -263 to -275
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 테스트코드가 삭제되었군요!

@DisplayName("카테고리를 구독하면 카테고리에 대한 NONE 역할이 생성된다")
@Test
void 카테고리를_구독하면_카테고리에_대한_NONE_역할이_생성된다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import com.allog.dallog.domain.categoryrole.dto.request.CategoryRoleUpdateRequest;
import com.allog.dallog.domain.categoryrole.exception.NoCategoryAuthorityException;
import com.allog.dallog.domain.categoryrole.exception.NoSuchCategoryRoleException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToMangeRoleException;
import com.allog.dallog.domain.categoryrole.exception.NotAbleToChangeRoleException;
import com.allog.dallog.domain.member.application.MemberService;
import com.allog.dallog.domain.member.dto.response.SubscribersResponse;
import java.util.List;
Expand Down Expand Up @@ -628,7 +628,7 @@ class CategoryControllerTest extends ControllerTest {
Long categoryId = 1L;
Long memberId = 2L;

willThrow(new NotAbleToMangeRoleException("변경 대상 회원이 유일한 ADMIN이므로 다른 역할로 변경할 수 없습니다."))
willThrow(new NotAbleToChangeRoleException("변경 대상 회원이 유일한 ADMIN이므로 다른 역할로 변경할 수 없습니다."))
.willDoNothing()
.given(categoryRoleService)
.updateRole(any(), any(), any(), any());
Expand Down