|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { NextRequest } from 'next/server' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockResolveShare, mockRateLimit, mockValidateAuth, mockDownloadFile, mockResolveImage } = |
| 8 | + vi.hoisted(() => ({ |
| 9 | + mockResolveShare: vi.fn(), |
| 10 | + mockRateLimit: vi.fn(), |
| 11 | + mockValidateAuth: vi.fn(), |
| 12 | + mockDownloadFile: vi.fn(), |
| 13 | + mockResolveImage: vi.fn(), |
| 14 | + })) |
| 15 | + |
| 16 | +vi.mock('@/lib/public-shares/share-manager', () => ({ |
| 17 | + resolveActiveShareByToken: mockResolveShare, |
| 18 | +})) |
| 19 | +vi.mock('@/lib/public-shares/rate-limit', () => ({ enforcePublicFileRateLimit: mockRateLimit })) |
| 20 | +vi.mock('@/lib/core/security/deployment-auth', () => ({ validateDeploymentAuth: mockValidateAuth })) |
| 21 | +vi.mock('@/lib/uploads/core/storage-service', () => ({ downloadFile: mockDownloadFile })) |
| 22 | +vi.mock('@/lib/uploads/server/inline-image', () => ({ |
| 23 | + resolveWorkspaceInlineImage: mockResolveImage, |
| 24 | +})) |
| 25 | + |
| 26 | +import { GET } from '@/app/api/files/public/[token]/inline/route' |
| 27 | + |
| 28 | +const TOKEN = 'tok_share_123456' |
| 29 | +const DOC_KEY = 'workspace/ws-1/doc.md' |
| 30 | +const IMG_KEY = 'workspace/ws-1/photo.png' |
| 31 | +const FILE_ID = 'wf_YwDXi8eWOkTxn0sbgChlB' |
| 32 | +const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00]) |
| 33 | + |
| 34 | +const params = { params: Promise.resolve({ token: TOKEN }) } |
| 35 | +const req = (q: string) => new NextRequest(`http://localhost/api/files/public/${TOKEN}/inline?${q}`) |
| 36 | + |
| 37 | +const share = { |
| 38 | + share: { id: 'sh_1', token: TOKEN, authType: 'public' }, |
| 39 | + file: { id: 'wf_doc', key: DOC_KEY, workspaceId: 'ws-1', originalName: 'doc.md' }, |
| 40 | + workspaceName: 'Acme', |
| 41 | + ownerName: 'Jane', |
| 42 | +} |
| 43 | + |
| 44 | +/** doc bytes embed the image via the view form; image bytes are a real PNG */ |
| 45 | +function downloadByKey(docContent = ``) { |
| 46 | + return ({ key }: { key: string }) => |
| 47 | + Promise.resolve(key === DOC_KEY ? Buffer.from(docContent, 'utf-8') : PNG) |
| 48 | +} |
| 49 | + |
| 50 | +describe('GET /api/files/public/[token]/inline', () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks() |
| 53 | + mockRateLimit.mockResolvedValue(null) |
| 54 | + mockResolveShare.mockResolvedValue(share) |
| 55 | + mockValidateAuth.mockResolvedValue({ authorized: true }) |
| 56 | + mockResolveImage.mockResolvedValue({ |
| 57 | + key: IMG_KEY, |
| 58 | + contentType: 'image/png', |
| 59 | + filename: 'photo.png', |
| 60 | + }) |
| 61 | + mockDownloadFile.mockImplementation(downloadByKey()) |
| 62 | + }) |
| 63 | + |
| 64 | + it('serves a same-workspace image referenced by the doc, typed from its bytes', async () => { |
| 65 | + const res = await GET(req(`fileId=${FILE_ID}`), params) |
| 66 | + expect(res.status).toBe(200) |
| 67 | + expect(res.headers.get('content-type')).toBe('image/png') |
| 68 | + }) |
| 69 | + |
| 70 | + it('serves a key-referenced image', async () => { |
| 71 | + mockDownloadFile.mockImplementation( |
| 72 | + downloadByKey(`}?context=workspace)`) |
| 73 | + ) |
| 74 | + const res = await GET(req(`key=${encodeURIComponent(IMG_KEY)}`), params) |
| 75 | + expect(res.status).toBe(200) |
| 76 | + }) |
| 77 | + |
| 78 | + it('404s when the reference is not embedded in the shared document', async () => { |
| 79 | + mockDownloadFile.mockImplementation(downloadByKey('no images here')) |
| 80 | + const res = await GET(req(`fileId=${FILE_ID}`), params) |
| 81 | + expect(res.status).toBe(404) |
| 82 | + expect(mockResolveImage).not.toHaveBeenCalled() |
| 83 | + }) |
| 84 | + |
| 85 | + it('404s when the referenced file is not in the document workspace', async () => { |
| 86 | + mockResolveImage.mockResolvedValue(null) |
| 87 | + const res = await GET(req(`fileId=${FILE_ID}`), params) |
| 88 | + expect(res.status).toBe(404) |
| 89 | + }) |
| 90 | + |
| 91 | + it('404s when the bytes are not a renderable image', async () => { |
| 92 | + mockDownloadFile.mockImplementation(({ key }: { key: string }) => |
| 93 | + Promise.resolve( |
| 94 | + key === DOC_KEY |
| 95 | + ? Buffer.from(``, 'utf-8') |
| 96 | + : Buffer.from('<svg/>', 'utf-8') |
| 97 | + ) |
| 98 | + ) |
| 99 | + const res = await GET(req(`fileId=${FILE_ID}`), params) |
| 100 | + expect(res.status).toBe(404) |
| 101 | + }) |
| 102 | + |
| 103 | + it('401s and never reads storage when the share is unauthorized', async () => { |
| 104 | + mockValidateAuth.mockResolvedValue({ authorized: false, error: 'auth_required_password' }) |
| 105 | + const res = await GET(req(`fileId=${FILE_ID}`), params) |
| 106 | + expect(res.status).toBe(401) |
| 107 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 108 | + }) |
| 109 | + |
| 110 | + it('404s for an unknown or inactive token', async () => { |
| 111 | + mockResolveShare.mockResolvedValue(null) |
| 112 | + const res = await GET(req(`fileId=${FILE_ID}`), params) |
| 113 | + expect(res.status).toBe(404) |
| 114 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | +}) |
0 commit comments