Skip to content

Commit

Permalink
fix: 방장 자신에 대한 추방 버그 (#177)
Browse files Browse the repository at this point in the history
* fix: 방장 자신 추방 못하도록 validate 추가

* feature: 방 수정 전 정보 불러오기에 방장 ID 추가

* test: 테스트 코드 작성

* fix: 방 참여 기록 조회 최신순으로 변경
  • Loading branch information
ymkim97 authored Nov 29, 2023
1 parent ed724f0 commit bbf3974
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void mandateManager(Long managerId, Long roomId, Long memberId) {

@Transactional
public void deportParticipant(Long managerId, Long roomId, Long memberId) {
validateDeportParticipant(managerId, memberId);
Participant managerParticipant = getParticipant(managerId, roomId);
Participant memberParticipant = getParticipant(memberId, roomId);
validateManagerAuthorization(managerParticipant);
Expand Down Expand Up @@ -172,6 +173,12 @@ private Participant getParticipant(Long memberId, Long roomId) {
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
}

private void validateDeportParticipant(Long managerId, Long memberId) {
if (managerId.equals(memberId)) {
throw new BadRequestException(PARTICIPANT_DEPORT_ERROR);
}
}

private void validateManagerAuthorization(Participant participant) {
if (!participant.isManager()) {
throw new ForbiddenException(ROOM_MODIFY_UNAUTHORIZED_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public ManageRoomResponse getRoomForModification(Long memberId, Long roomId) {
participantResponses.add(ParticipantMapper.toParticipantResponse(member, contributionPoint));
}

return RoomMapper.toManageRoomResponse(room, routineResponses, participantResponses);
return RoomMapper.toManageRoomResponse(room, memberId, routineResponses, participantResponses);
}

public GetAllRoomsResponse getAllRooms(@Nullable RoomType roomType, @Nullable Long roomId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ public static RoomsHistoryResponse toRoomsHistoryResponse(List<RoomHistoryRespon
.build();
}

public static ManageRoomResponse toManageRoomResponse(Room room, List<RoutineResponse> routines,
public static ManageRoomResponse toManageRoomResponse(Room room, Long managerId, List<RoutineResponse> routines,
List<ParticipantResponse> participantResponses) {
return ManageRoomResponse.builder()
.roomId(room.getId())
.title(room.getTitle())
.managerId(managerId)
.announcement(room.getAnnouncement())
.roomType(room.getRoomType())
.certifyTime(room.getCertifyTime())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public List<Participant> findAllParticipantsByMemberId(Long memberId) {
.where(
participant.memberId.eq(memberId)
)
.orderBy(participant.createdAt.desc())
.fetch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public record ManageRoomResponse(
Long roomId,
String title,
Long managerId,
String announcement,
RoomType roomType,
int certifyTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ErrorMessage {
INVALID_REQUEST_URL("잘못된 URL 요청입니다."),
INVALID_CERTIFY_TIME("현재 인증 시간이 아닙니다."),
CERTIFICATION_NOT_FOUND("인증 정보가 없습니다."),
PARTICIPANT_DEPORT_ERROR("방장은 자신을 추방할 수 없습니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILED("네트워크 접근 실패입니다."),
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/moabam/api/presentation/RoomControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,25 @@ void deport_member_success() throws Exception {
assertThat(participantSearchRepository.findOne(member.getId(), room.getId())).isEmpty();
}

@DisplayName("방장 본인 추방 시도 - 예외 처리")
@WithMember(id = 1L)
@Test
void deport_self_fail() throws Exception {
// given
Room room = RoomFixture.room();

Participant managerParticipant = RoomFixture.participant(room, member.getId());
managerParticipant.enableManager();

roomRepository.save(room);
participantRepository.save(managerParticipant);

// expected
mockMvc.perform(delete("/rooms/" + room.getId() + "/members/" + member.getId()))
.andExpect(status().isBadRequest())
.andDo(print());
}

@DisplayName("방장 위임 성공")
@WithMember(id = 1L)
@Test
Expand Down

0 comments on commit bbf3974

Please sign in to comment.