fix: keep public upload API unauthenticated
Deploy FileDrop / deploy (push) Successful in 37s
Deploy FileDrop / deploy (push) Successful in 37s
This commit is contained in:
+1
-1
@@ -60,7 +60,7 @@ const server = serve({
|
|||||||
port: config.port,
|
port: config.port,
|
||||||
routes: {
|
routes: {
|
||||||
'/api/upload': {
|
'/api/upload': {
|
||||||
POST: withRateLimit(requireAuth(handleUpload)),
|
POST: withRateLimit(handleUpload),
|
||||||
},
|
},
|
||||||
'/f/:public_id': {
|
'/f/:public_id': {
|
||||||
GET: withRateLimit(handleFileRedirect),
|
GET: withRateLimit(handleFileRedirect),
|
||||||
|
|||||||
+30
-1
@@ -22,18 +22,30 @@ const mockServe = mock((options: ServeOptions): MockServer => {
|
|||||||
const originalServe = Bun.serve;
|
const originalServe = Bun.serve;
|
||||||
Bun.serve = mockServe as unknown as typeof Bun.serve;
|
Bun.serve = mockServe as unknown as typeof Bun.serve;
|
||||||
|
|
||||||
|
type RouteHandler = (req: Request) => Response | Promise<Response>;
|
||||||
|
|
||||||
const mockStartBot = mock(() =>
|
const mockStartBot = mock(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
stop: mock(),
|
stop: mock(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
const mockHandleUpload = mock((_req: Request) => Promise.resolve(Response.json({ ok: true })));
|
||||||
|
const mockRequireAuth = mock(
|
||||||
|
(_handler: RouteHandler): RouteHandler =>
|
||||||
|
async () =>
|
||||||
|
Response.json({ error: 'Unauthorized' }, { status: 401 }),
|
||||||
|
);
|
||||||
|
|
||||||
mock.module('../src/bot', () => ({
|
mock.module('../src/bot', () => ({
|
||||||
startBot: mockStartBot,
|
startBot: mockStartBot,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
mock.module('../src/routes/upload', () => ({
|
mock.module('../src/routes/upload', () => ({
|
||||||
handleUpload: mock(),
|
handleUpload: mockHandleUpload,
|
||||||
|
}));
|
||||||
|
|
||||||
|
mock.module('../src/utils/auth', () => ({
|
||||||
|
requireAuth: mockRequireAuth,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
mock.module('../src/routes/files', () => ({
|
mock.module('../src/routes/files', () => ({
|
||||||
@@ -62,6 +74,8 @@ describe('Bootstrap Server', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockServe.mockClear();
|
mockServe.mockClear();
|
||||||
mockStartBot.mockClear();
|
mockStartBot.mockClear();
|
||||||
|
mockHandleUpload.mockClear();
|
||||||
|
mockRequireAuth.mockClear();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
@@ -86,5 +100,20 @@ describe('Bootstrap Server', () => {
|
|||||||
expect(serveCallArgs.routes).toHaveProperty('/api/v1/auth/logout');
|
expect(serveCallArgs.routes).toHaveProperty('/api/v1/auth/logout');
|
||||||
expect(serveCallArgs.routes).toHaveProperty('/api/v1/auth/me');
|
expect(serveCallArgs.routes).toHaveProperty('/api/v1/auth/me');
|
||||||
expect(serveCallArgs.routes).toHaveProperty('/api/v1/*');
|
expect(serveCallArgs.routes).toHaveProperty('/api/v1/*');
|
||||||
|
|
||||||
|
const uploadRoute = serveCallArgs.routes?.['/api/upload'] as { POST: RouteHandler };
|
||||||
|
const res = await uploadRoute.POST(
|
||||||
|
new Request('http://localhost/api/upload', { method: 'POST' }),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(await res.json()).toEqual({ ok: true });
|
||||||
|
expect(mockHandleUpload).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
const webApiRoute = serveCallArgs.routes?.['/api/v1/*'] as { GET: RouteHandler };
|
||||||
|
const protectedRes = await webApiRoute.GET(new Request('http://localhost/api/v1/files'));
|
||||||
|
|
||||||
|
expect(protectedRes.status).toBe(401);
|
||||||
|
expect(await protectedRes.json()).toEqual({ error: 'Unauthorized' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user