|
| 1 | +import { AppError } from '../lib/errors'; |
| 2 | +import { createReport } from './reports.service'; |
| 3 | + |
| 4 | +jest.mock('../lib/prisma', () => ({ |
| 5 | + __esModule: true, |
| 6 | + default: { |
| 7 | + post: { |
| 8 | + findUnique: jest.fn(), |
| 9 | + }, |
| 10 | + report: { |
| 11 | + findFirst: jest.fn(), |
| 12 | + create: jest.fn(), |
| 13 | + }, |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +import prisma from '../lib/prisma'; |
| 18 | + |
| 19 | +const mockPost = prisma.post as jest.Mocked<typeof prisma.post>; |
| 20 | +const mockReport = prisma.report as jest.Mocked<typeof prisma.report>; |
| 21 | + |
| 22 | +const VALID_POST = { |
| 23 | + id: 'post-id-1', |
| 24 | + slug: 'abc123', |
| 25 | + title: 'Test', |
| 26 | + content: '<p>hello</p>', |
| 27 | + passwordHash: 'hash', |
| 28 | + expiresAt: null, |
| 29 | + createdAt: new Date(), |
| 30 | + updatedAt: new Date(), |
| 31 | +}; |
| 32 | + |
| 33 | +describe('reports.service', () => { |
| 34 | + beforeEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('createReport', () => { |
| 39 | + it('정상적인 신고를 생성하고 postId, reason, ip를 저장한다', async () => { |
| 40 | + (mockPost.findUnique as jest.Mock).mockResolvedValue(VALID_POST); |
| 41 | + (mockReport.findFirst as jest.Mock).mockResolvedValue(null); |
| 42 | + (mockReport.create as jest.Mock).mockResolvedValue({}); |
| 43 | + |
| 44 | + await expect( |
| 45 | + createReport('abc123', 'ILLEGAL_CONTENT', undefined, '1.2.3.4') |
| 46 | + ).resolves.toBeUndefined(); |
| 47 | + |
| 48 | + expect(mockReport.create).toHaveBeenCalledWith({ |
| 49 | + data: { |
| 50 | + postId: 'post-id-1', |
| 51 | + reason: 'ILLEGAL_CONTENT', |
| 52 | + description: undefined, |
| 53 | + reporterIp: '1.2.3.4', |
| 54 | + }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('description이 있으면 함께 저장한다', async () => { |
| 59 | + (mockPost.findUnique as jest.Mock).mockResolvedValue(VALID_POST); |
| 60 | + (mockReport.findFirst as jest.Mock).mockResolvedValue(null); |
| 61 | + (mockReport.create as jest.Mock).mockResolvedValue({}); |
| 62 | + |
| 63 | + await createReport('abc123', 'OTHER', '상세 설명', '1.2.3.4'); |
| 64 | + |
| 65 | + const createArg = (mockReport.create as jest.Mock).mock.calls[0][0]; |
| 66 | + expect(createArg.data.description).toBe('상세 설명'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('존재하지 않는 게시글이면 NOT_FOUND를 던진다', async () => { |
| 70 | + (mockPost.findUnique as jest.Mock).mockResolvedValue(null); |
| 71 | + |
| 72 | + await expect( |
| 73 | + createReport('no-exist', 'PHISHING', undefined, '1.2.3.4') |
| 74 | + ).rejects.toMatchObject({ statusCode: 404, code: 'NOT_FOUND' }); |
| 75 | + |
| 76 | + expect(mockReport.create).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('만료된 게시글이면 EXPIRED를 던진다', async () => { |
| 80 | + (mockPost.findUnique as jest.Mock).mockResolvedValue({ |
| 81 | + ...VALID_POST, |
| 82 | + expiresAt: new Date(Date.now() - 1000), |
| 83 | + }); |
| 84 | + |
| 85 | + await expect( |
| 86 | + createReport('abc123', 'DEFAMATION', undefined, '1.2.3.4') |
| 87 | + ).rejects.toMatchObject({ statusCode: 410, code: 'EXPIRED' }); |
| 88 | + |
| 89 | + expect(mockReport.create).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('동일 IP가 같은 게시글을 같은 사유로 이미 신고했으면 ALREADY_REPORTED를 던진다', async () => { |
| 93 | + (mockPost.findUnique as jest.Mock).mockResolvedValue(VALID_POST); |
| 94 | + (mockReport.findFirst as jest.Mock).mockResolvedValue({ id: 'existing-report' }); |
| 95 | + |
| 96 | + await expect( |
| 97 | + createReport('abc123', 'COPYRIGHT', undefined, '1.2.3.4') |
| 98 | + ).rejects.toMatchObject({ statusCode: 409, code: 'ALREADY_REPORTED' }); |
| 99 | + |
| 100 | + expect(mockReport.create).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('동일 IP라도 다른 사유로는 같은 게시글을 신고할 수 있다', async () => { |
| 104 | + (mockPost.findUnique as jest.Mock).mockResolvedValue(VALID_POST); |
| 105 | + (mockReport.findFirst as jest.Mock).mockResolvedValue(null); |
| 106 | + (mockReport.create as jest.Mock).mockResolvedValue({}); |
| 107 | + |
| 108 | + await expect( |
| 109 | + createReport('abc123', 'DEFAMATION', undefined, '1.2.3.4') |
| 110 | + ).resolves.toBeUndefined(); |
| 111 | + |
| 112 | + expect(mockReport.findFirst).toHaveBeenCalledWith( |
| 113 | + expect.objectContaining({ where: expect.objectContaining({ reason: 'DEFAMATION' }) }) |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('동일 IP라도 다른 게시글은 신고할 수 있다', async () => { |
| 118 | + (mockPost.findUnique as jest.Mock).mockResolvedValue({ ...VALID_POST, id: 'post-id-2' }); |
| 119 | + (mockReport.findFirst as jest.Mock).mockResolvedValue(null); |
| 120 | + (mockReport.create as jest.Mock).mockResolvedValue({}); |
| 121 | + |
| 122 | + await expect( |
| 123 | + createReport('xyz789', 'PERSONAL_INFO', undefined, '1.2.3.4') |
| 124 | + ).resolves.toBeUndefined(); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments