diff --git a/app/backend/src/groups/groups.repository.spec.ts b/app/backend/src/groups/groups.repository.spec.ts index 7a6416af..f1d94fbe 100644 --- a/app/backend/src/groups/groups.repository.spec.ts +++ b/app/backend/src/groups/groups.repository.spec.ts @@ -3,6 +3,7 @@ import { GroupsRepository } from './groups.repository'; import { PrismaService } from 'prisma/prisma.service'; import { CreateGroupsDto } from './dto/create-groups.dto'; import { Member } from '@prisma/client'; +import { ForbiddenException, NotFoundException } from '@nestjs/common'; describe('GroupsRepository', () => { let repository: GroupsRepository; @@ -25,6 +26,7 @@ describe('GroupsRepository', () => { findUnique: jest.fn(), create: jest.fn(), delete: jest.fn(), + count: jest.fn(), }, }, }, @@ -35,13 +37,22 @@ describe('GroupsRepository', () => { prismaService = module.get(PrismaService); }); + const mockMember: Member = { + id: BigInt(1), + providerId: '11111122222222', + email: 'morak@gmail.com', + nickname: 'morak morak', + profilePicture: 'morak morak.jpg', + socialType: 'google', + createdAt: new Date(), + }; + describe('getAllGroups', () => { it('모든 그룹을 반환해야 함', async () => { - const expectedGroups = [ - { id: BigInt(1), title: '부스트캠프 웹·모바일 8기', membersCount: 212 }, - { id: BigInt(2), title: '부스트캠프 웹·모바일 9기', membersCount: 187 }, - ]; + const expectedGroups = [{ id: BigInt(1), title: '부스트캠프 웹·모바일 8기', membersCount: 212 }]; + const groupId = 212; jest.spyOn(prismaService.group, 'findMany').mockResolvedValue(expectedGroups); + jest.spyOn(prismaService.groupToUser, 'count').mockResolvedValue(groupId); const result = await repository.getAllGroups(); @@ -96,15 +107,6 @@ describe('GroupsRepository', () => { describe('joinGroup', () => { it('그룹에 참여해야 함', async () => { const groupId = 1; - const member: Member = { - id: BigInt(1), - providerId: '11111122222222', - email: 'morak@gmail.com', - nickname: 'morak morak', - profilePicture: 'morak morak.jpg', - socialType: 'google', - createdAt: new Date(), - }; jest.spyOn(prismaService.group, 'findUnique').mockResolvedValue({ id: BigInt(1), @@ -113,33 +115,43 @@ describe('GroupsRepository', () => { jest.spyOn(prismaService.groupToUser, 'create').mockResolvedValue({ groupId: BigInt(1), - userId: member.id, + userId: mockMember.id, }); - await repository.joinGroup(groupId, member); + await repository.joinGroup(groupId, mockMember); // 231204 ldhbenecia | 해당 함수가 특정 인자와 함께 호출되었는지 여부를 확인 expect(prismaService.groupToUser.create).toHaveBeenCalledWith({ data: { groupId: BigInt(1), - userId: member.id, + userId: mockMember.id, }, }); }); + + it('가입할 그룹이 없는 경우 NotFoundException 발생', async () => { + jest.spyOn(prismaService.group, 'findUnique').mockResolvedValueOnce(null); + + await expect(repository.joinGroup(1, mockMember)).rejects.toThrowError(NotFoundException); + }); + + it('그룹에 이미 가입한 멤버가 가입 요청을 하면 Forbidden 발생', async () => { + jest.spyOn(prismaService.group, 'findUnique').mockResolvedValue({ + id: BigInt(1), + title: '부스트캠프 웹·모바일 8기', + }); + jest.spyOn(prismaService.groupToUser, 'findUnique').mockResolvedValueOnce({ + groupId: BigInt(1), + userId: mockMember.id, + }); + + await expect(repository.joinGroup(1, mockMember)).rejects.toThrowError(ForbiddenException); + }); }); describe('leaveGroup', () => { it('그룹 탈퇴를 해야 함', async () => { const groupId = 1; - const member: Member = { - id: BigInt(1), - providerId: '11111122222222', - email: 'morak@gmail.com', - nickname: 'morak morak', - profilePicture: 'morak morak.jpg', - socialType: 'google', - createdAt: new Date(), - }; jest.spyOn(prismaService.group, 'findUnique').mockResolvedValue({ id: BigInt(1), @@ -148,21 +160,21 @@ describe('GroupsRepository', () => { jest.spyOn(prismaService.groupToUser, 'findUnique').mockResolvedValue({ groupId: BigInt(1), - userId: member.id, + userId: mockMember.id, }); jest.spyOn(prismaService.groupToUser, 'delete').mockResolvedValue({ groupId: BigInt(1), - userId: member.id, + userId: mockMember.id, }); - await repository.leaveGroup(groupId, member); + await repository.leaveGroup(groupId, mockMember); expect(prismaService.groupToUser.findUnique).toHaveBeenCalledWith({ where: { groupId_userId: { groupId: groupId, - userId: member.id, + userId: mockMember.id, }, }, }); @@ -171,11 +183,23 @@ describe('GroupsRepository', () => { where: { groupId_userId: { groupId: groupId, - userId: member.id, + userId: mockMember.id, }, }, }); }); + + it('탈퇴할 그룹이 없는 경우 NotFoundException 발생', async () => { + jest.spyOn(prismaService.group, 'findUnique').mockResolvedValueOnce(null); + + await expect(repository.leaveGroup(1, mockMember)).rejects.toThrowError(NotFoundException); + }); + + it('해당 멤버가 그룹에 없는데 탈퇴 요청을 할 경우 NotFoundException 발생', async () => { + jest.spyOn(prismaService.groupToUser, 'findUnique').mockResolvedValueOnce(null); + + await expect(repository.leaveGroup(1, mockMember)).rejects.toThrowError(NotFoundException); + }); }); describe('getMyGroups', () => {