|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +const mockGetPostStatus = jest.fn() |
| 5 | +const mockIncrementPostLikes = jest.fn() |
| 6 | +const mockLikesRatelimit = { limit: jest.fn() } |
| 7 | +const mockLoggerError = jest.fn() |
| 8 | + |
| 9 | +jest.mock('@web/lib/db/queries/posts', () => ({ |
| 10 | + getPostStatus: (...args: unknown[]) => mockGetPostStatus(...args), |
| 11 | + incrementPostLikes: (...args: unknown[]) => mockIncrementPostLikes(...args), |
| 12 | +})) |
| 13 | +jest.mock('@web/lib/ratelimit', () => ({ |
| 14 | + get likesRatelimit() { |
| 15 | + return mockLikesRatelimit |
| 16 | + }, |
| 17 | +})) |
| 18 | +jest.mock('@web/lib/logger', () => ({ |
| 19 | + logger: { |
| 20 | + error: mockLoggerError, |
| 21 | + info: jest.fn(), |
| 22 | + debug: jest.fn(), |
| 23 | + warn: jest.fn(), |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +const { POST } = require('./route') as { |
| 28 | + POST: ( |
| 29 | + req: Request, |
| 30 | + ctx: { params: Promise<{ id: string }> }, |
| 31 | + ) => Promise<Response> |
| 32 | +} |
| 33 | + |
| 34 | +function makeRequest(headers: Record<string, string> = {}) { |
| 35 | + return new Request('http://localhost/api/posts/some-id/like', { |
| 36 | + method: 'POST', |
| 37 | + headers, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +function makeParams(id = 'post-ulid-123') { |
| 42 | + return { params: Promise.resolve({ id }) } |
| 43 | +} |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + jest.clearAllMocks() |
| 47 | + mockLikesRatelimit.limit.mockResolvedValue({ success: true }) |
| 48 | + mockGetPostStatus.mockResolvedValue('published') |
| 49 | + mockIncrementPostLikes.mockResolvedValue(42) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('POST /api/posts/[id]/like', () => { |
| 53 | + it('increments likes and returns count', async () => { |
| 54 | + const res = await POST(makeRequest(), makeParams()) |
| 55 | + expect(res.status).toBe(200) |
| 56 | + expect(await res.json()).toEqual({ likes: 42 }) |
| 57 | + expect(mockIncrementPostLikes).toHaveBeenCalledWith('post-ulid-123') |
| 58 | + }) |
| 59 | + |
| 60 | + it('returns 429 when rate limit exceeded', async () => { |
| 61 | + mockLikesRatelimit.limit.mockResolvedValue({ success: false }) |
| 62 | + const res = await POST(makeRequest(), makeParams()) |
| 63 | + expect(res.status).toBe(429) |
| 64 | + expect(await res.json()).toEqual({ error: 'Already liked' }) |
| 65 | + expect(mockIncrementPostLikes).not.toHaveBeenCalled() |
| 66 | + }) |
| 67 | + |
| 68 | + it('uses x-forwarded-for ip for rate limit key', async () => { |
| 69 | + const res = await POST( |
| 70 | + makeRequest({ 'x-forwarded-for': '1.2.3.4, 5.6.7.8' }), |
| 71 | + makeParams(), |
| 72 | + ) |
| 73 | + expect(res.status).toBe(200) |
| 74 | + expect(mockLikesRatelimit.limit).toHaveBeenCalledWith( |
| 75 | + 'like:post-ulid-123:1.2.3.4', |
| 76 | + ) |
| 77 | + }) |
| 78 | + |
| 79 | + it('uses x-real-ip when x-forwarded-for absent', async () => { |
| 80 | + await POST(makeRequest({ 'x-real-ip': '9.9.9.9' }), makeParams()) |
| 81 | + expect(mockLikesRatelimit.limit).toHaveBeenCalledWith( |
| 82 | + 'like:post-ulid-123:9.9.9.9', |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it('falls back to anonymous when no ip headers', async () => { |
| 87 | + await POST(makeRequest(), makeParams()) |
| 88 | + expect(mockLikesRatelimit.limit).toHaveBeenCalledWith( |
| 89 | + 'like:post-ulid-123:anonymous', |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + it('returns 404 when post not found', async () => { |
| 94 | + mockGetPostStatus.mockResolvedValue(null) |
| 95 | + const res = await POST(makeRequest(), makeParams()) |
| 96 | + expect(res.status).toBe(404) |
| 97 | + expect(mockIncrementPostLikes).not.toHaveBeenCalled() |
| 98 | + }) |
| 99 | + |
| 100 | + it('returns 404 when post is not published', async () => { |
| 101 | + mockGetPostStatus.mockResolvedValue('draft') |
| 102 | + const res = await POST(makeRequest(), makeParams()) |
| 103 | + expect(res.status).toBe(404) |
| 104 | + expect(mockIncrementPostLikes).not.toHaveBeenCalled() |
| 105 | + }) |
| 106 | + |
| 107 | + it('returns 500 when increment throws', async () => { |
| 108 | + mockIncrementPostLikes.mockRejectedValue(new Error('db error')) |
| 109 | + const res = await POST(makeRequest(), makeParams()) |
| 110 | + expect(res.status).toBe(500) |
| 111 | + expect(mockLoggerError).toHaveBeenCalled() |
| 112 | + }) |
| 113 | + |
| 114 | + it('skips rate limit when likesRatelimit is null', async () => { |
| 115 | + jest.resetModules() |
| 116 | + jest.mock('@web/lib/ratelimit', () => ({ likesRatelimit: null })) |
| 117 | + jest.mock('@web/lib/db/queries/posts', () => ({ |
| 118 | + getPostStatus: () => Promise.resolve('published'), |
| 119 | + incrementPostLikes: () => Promise.resolve(1), |
| 120 | + })) |
| 121 | + jest.mock('@web/lib/logger', () => ({ |
| 122 | + logger: { |
| 123 | + error: jest.fn(), |
| 124 | + info: jest.fn(), |
| 125 | + debug: jest.fn(), |
| 126 | + warn: jest.fn(), |
| 127 | + }, |
| 128 | + })) |
| 129 | + const { POST: POST2 } = require('./route') as typeof import('./route') |
| 130 | + const res = await POST2(makeRequest(), makeParams()) |
| 131 | + expect(res.status).toBe(200) |
| 132 | + }) |
| 133 | +}) |
0 commit comments