Skip to content

Commit

Permalink
[merge]: 팀 삭제 버그 수정 (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanguk01 authored Nov 14, 2024
2 parents a800384 + 0e89e97 commit 91373ee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface TeamInvitationRepository extends JpaRepository<TeamInvitation,
List<TeamInvitation> findAllByUserAndStatus(User user, InvitationStatus status);
List<TeamInvitation> findAllByTeamAndStatus(Team team, InvitationStatus status);
Optional<TeamInvitation> findByTeamAndUserAndStatus(Team team, User user, InvitationStatus status);
void deleteAllByTeam(Team team);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public interface TeamUserRelationRepository extends JpaRepository<TeamUserRelati
@Query("SELECT t FROM TeamUserRelation t WHERE t.team.id = :teamId AND t.user.id = :userId")
Optional<TeamUserRelation> findByTeamIdAndUserId(@Param("teamId") Long teamId, @Param("userId") Long userId);
int countByTeam(Team team);
void deleteAllByTeam(Team team);

}
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,13 @@ public void deleteTeam(Long teamId, Long adminId) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}

// 팀 삭제 - 연관된 엔티티도 함께 삭제되도록 설정
// 관련된 초대 데이터 삭제
teamInvitationRepository.deleteAllByTeam(team);

// 관련된 사용자 관계 삭제
teamUserRelationRepository.deleteAllByTeam(team);

// 팀 삭제
teamRepository.delete(team);
}

Expand Down
66 changes: 44 additions & 22 deletions src/test/java/com/splanet/splanet/team/service/TeamServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,6 @@ public void testCreateTeam_InvalidInput() {
assertEquals(ErrorCode.TEAM_NAME_NOT_FOUND, exception.getErrorCode());
}

@Test
public void testDeleteTeam_Success() {
when(teamUserRelationRepository.findByTeamAndUser(any(Team.class), any(User.class)))
.thenReturn(Optional.of(testRelation));

teamService.deleteTeam(1L, 1L);

verify(teamRepository, times(1)).delete(testTeam);
}

@Test
public void testDeleteTeam_AccessDenied() {
TeamUserRelation memberRelation = new TeamUserRelation(testTeam, testUser, UserTeamRole.MEMBER);
when(teamUserRelationRepository.findByTeamAndUser(any(Team.class), any(User.class)))
.thenReturn(Optional.of(memberRelation));

BusinessException exception = assertThrows(BusinessException.class, () ->
teamService.deleteTeam(1L, 1L)
);

assertEquals(ErrorCode.ACCESS_DENIED, exception.getErrorCode());
}

@Test
public void testLeaveTeam_Success() {
Expand Down Expand Up @@ -238,5 +216,49 @@ public void testInviteUserToTeam_InvitationAlreadySent() {

assertEquals(ErrorCode.INVITATION_ALREADY_SENT, exception.getErrorCode());
}
@Test
public void testDeleteTeam_Success() {
when(teamUserRelationRepository.findByTeamAndUser(testTeam, testUser))
.thenReturn(Optional.of(testRelation));

// 팀 삭제 시 관련 데이터 삭제 확인
doNothing().when(teamInvitationRepository).deleteAllByTeam(testTeam);
doNothing().when(teamUserRelationRepository).deleteAllByTeam(testTeam);
doNothing().when(teamRepository).delete(testTeam);

teamService.deleteTeam(1L, 1L);

verify(teamInvitationRepository, times(1)).deleteAllByTeam(testTeam);
verify(teamUserRelationRepository, times(1)).deleteAllByTeam(testTeam);
verify(teamRepository, times(1)).delete(testTeam);
}

@Test
public void testDeleteTeam_AccessDenied() {
TeamUserRelation memberRelation = new TeamUserRelation(testTeam, testUser, UserTeamRole.MEMBER);
when(teamUserRelationRepository.findByTeamAndUser(testTeam, testUser))
.thenReturn(Optional.of(memberRelation));

BusinessException exception = assertThrows(BusinessException.class, () ->
teamService.deleteTeam(1L, 1L)
);

assertEquals(ErrorCode.ACCESS_DENIED, exception.getErrorCode());
verify(teamRepository, never()).delete(testTeam);
}

@Test
public void testDeleteTeam_TeamNotFound() {
when(teamRepository.findById(1L)).thenReturn(Optional.empty());

BusinessException exception = assertThrows(BusinessException.class, () ->
teamService.deleteTeam(1L, 1L)
);

assertEquals(ErrorCode.TEAM_NOT_FOUND, exception.getErrorCode());
verify(teamInvitationRepository, never()).deleteAllByTeam(any());
verify(teamUserRelationRepository, never()).deleteAllByTeam(any());
verify(teamRepository, never()).delete(any());
}

}

0 comments on commit 91373ee

Please sign in to comment.