|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { Hono } from 'hono'; |
| 3 | +import { FILE_TRANSFER_LIMITS } from '../../shared/transport/file-transfer.js'; |
| 4 | + |
| 5 | +const { sendFileTransferRequestMock, isDaemonConnectedMock } = vi.hoisted(() => ({ |
| 6 | + sendFileTransferRequestMock: vi.fn(), |
| 7 | + isDaemonConnectedMock: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('../src/security/authorization.js', () => ({ |
| 11 | + requireAuth: () => async (c: { req: { header: (name: string) => string | undefined }; set: (key: string, value: string) => void }, next: () => Promise<void>) => { |
| 12 | + if (!c.req.header('Authorization')) { |
| 13 | + return new Response(JSON.stringify({ error: 'unauthorized' }), { |
| 14 | + status: 401, |
| 15 | + headers: { 'Content-Type': 'application/json' }, |
| 16 | + }); |
| 17 | + } |
| 18 | + c.set('userId', 'user-1'); |
| 19 | + return next(); |
| 20 | + }, |
| 21 | + resolveServerRole: vi.fn().mockResolvedValue('owner'), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('../src/ws/bridge.js', () => ({ |
| 25 | + WsBridge: { |
| 26 | + get: () => ({ |
| 27 | + isDaemonConnected: isDaemonConnectedMock, |
| 28 | + sendFileTransferRequest: sendFileTransferRequestMock, |
| 29 | + }), |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock('../src/util/logger.js', () => ({ |
| 34 | + default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('../src/security/crypto.js', () => ({ |
| 38 | + randomHex: (bytes: number) => 'a'.repeat(bytes * 2), |
| 39 | +})); |
| 40 | + |
| 41 | +import { fileTransferRoutes } from '../src/routes/file-transfer.js'; |
| 42 | + |
| 43 | +function makeApp(): Hono { |
| 44 | + const app = new Hono(); |
| 45 | + app.use('/*', async (c, next) => { |
| 46 | + (c as never as { env: { DB: unknown } }).env = { DB: {} }; |
| 47 | + return next(); |
| 48 | + }); |
| 49 | + app.route('/api/server', fileTransferRoutes); |
| 50 | + return app; |
| 51 | +} |
| 52 | + |
| 53 | +describe('file-transfer upload route', () => { |
| 54 | + beforeEach(() => { |
| 55 | + sendFileTransferRequestMock.mockReset(); |
| 56 | + isDaemonConnectedMock.mockReset(); |
| 57 | + isDaemonConnectedMock.mockReturnValue(true); |
| 58 | + sendFileTransferRequestMock.mockResolvedValue({ |
| 59 | + type: 'file.upload_done', |
| 60 | + attachment: { |
| 61 | + id: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt', |
| 62 | + source: 'upload', |
| 63 | + daemonPath: '/tmp/upload.txt', |
| 64 | + downloadable: true, |
| 65 | + }, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('rejects oversized legacy uploads from content-length before daemon relay', async () => { |
| 70 | + const res = await makeApp().request('/api/server/srv-1/upload', { |
| 71 | + method: 'POST', |
| 72 | + headers: { |
| 73 | + Authorization: 'Bearer test', |
| 74 | + 'Content-Type': 'multipart/form-data; boundary=x', |
| 75 | + 'Content-Length': String(FILE_TRANSFER_LIMITS.MAX_FILE_SIZE + 1024 * 1024 + 1), |
| 76 | + }, |
| 77 | + body: '--x\r\n', |
| 78 | + }); |
| 79 | + |
| 80 | + expect(res.status).toBe(413); |
| 81 | + await expect(res.json()).resolves.toEqual({ |
| 82 | + error: 'file_too_large', |
| 83 | + maxBytes: FILE_TRANSFER_LIMITS.MAX_FILE_SIZE, |
| 84 | + }); |
| 85 | + expect(sendFileTransferRequestMock).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('relays a valid legacy upload with base64 content', async () => { |
| 89 | + const form = new FormData(); |
| 90 | + form.append('file', new File(['hello'], 'hello.txt', { type: 'text/plain' })); |
| 91 | + |
| 92 | + const res = await makeApp().request('/api/server/srv-1/upload', { |
| 93 | + method: 'POST', |
| 94 | + headers: { Authorization: 'Bearer test' }, |
| 95 | + body: form, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(res.status).toBe(200); |
| 99 | + await expect(res.json()).resolves.toMatchObject({ |
| 100 | + ok: true, |
| 101 | + attachment: { |
| 102 | + serverId: 'srv-1', |
| 103 | + daemonPath: '/tmp/upload.txt', |
| 104 | + }, |
| 105 | + }); |
| 106 | + expect(sendFileTransferRequestMock).toHaveBeenCalledWith( |
| 107 | + expect.any(String), |
| 108 | + expect.objectContaining({ |
| 109 | + type: 'file.upload', |
| 110 | + originalName: 'hello.txt', |
| 111 | + mime: 'text/plain', |
| 112 | + size: 5, |
| 113 | + content: Buffer.from('hello').toString('base64'), |
| 114 | + }), |
| 115 | + FILE_TRANSFER_LIMITS.UPLOAD_TIMEOUT_MS, |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
0 commit comments