|
|
@@ -1,19 +1,21 @@
|
|
|
import type { Request, Response } from 'express';
|
|
|
+import { mock } from 'vitest-mock-extended';
|
|
|
|
|
|
import { denyUploadsDirectAccess } from './deny-uploads-direct-access';
|
|
|
|
|
|
describe('denyUploadsDirectAccess', () => {
|
|
|
test('responds with 403 Forbidden', () => {
|
|
|
- const send = vi.fn();
|
|
|
- const status = vi.fn().mockReturnValue({ send });
|
|
|
- const req = {
|
|
|
- originalUrl: '/uploads/attachment/evil.html',
|
|
|
- } as unknown as Request;
|
|
|
- const res = { status } as unknown as Response;
|
|
|
+ const req = mock<Request>();
|
|
|
+ req.originalUrl = '/uploads/attachment/evil.html';
|
|
|
+
|
|
|
+ const res = mock<Response>();
|
|
|
+ // res.status(...) returns `this` (Response) in Express, enabling the
|
|
|
+ // status().send() chain. Mirror that so the chained send() can be asserted.
|
|
|
+ res.status.mockReturnValue(res);
|
|
|
|
|
|
denyUploadsDirectAccess(req, res);
|
|
|
|
|
|
- expect(status).toHaveBeenCalledWith(403);
|
|
|
- expect(send).toHaveBeenCalledWith('Forbidden');
|
|
|
+ expect(res.status).toHaveBeenCalledWith(403);
|
|
|
+ expect(res.send).toHaveBeenCalledWith('Forbidden');
|
|
|
});
|
|
|
});
|