Refactor test suite to replace getAndDeleteKey with getKey for template resource handling
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit updates the test cases in `templateresources.test.js` to replace instances of the mocked function `getAndDeleteKey` with `getKey`. The changes ensure that the tests accurately reflect the current implementation of the resource retrieval logic, enhancing clarity and maintainability. Additionally, the test descriptions are updated for better alignment with the functionality being tested.
This commit is contained in:
Tom Butcher 2026-09-15 02:03:46 +01:00
parent d157fcdc9e
commit 763e50a632

View File

@ -1,12 +1,12 @@
import { EventEmitter } from 'events';
import { jest } from '@jest/globals';
const getAndDeleteKey = jest.fn();
const getKey = jest.fn();
const findById = jest.fn();
const downloadFile = jest.fn();
jest.unstable_mockModule('../../../database/redis.js', () => ({
redisServer: { getAndDeleteKey },
redisServer: { getKey },
}));
jest.unstable_mockModule('../../../database/ceph.js', () => ({
BUCKETS: { FILES: 'files' },
@ -38,14 +38,14 @@ describe('template resource route', () => {
const res = response();
await getTemplateResourceRouteHandler({ params: { token } }, res);
expect(res.status).toHaveBeenCalledWith(404);
expect(getAndDeleteKey).not.toHaveBeenCalled();
expect(getKey).not.toHaveBeenCalled();
});
it('atomically consumes the token and streams the stored file inline', async () => {
it('loads the token and streams the stored file inline', async () => {
const token = 'a'.repeat(64);
const fileId = '507f1f77bcf86cd799439011';
const body = Buffer.from('image');
getAndDeleteKey.mockResolvedValue({ fileId });
getKey.mockResolvedValue({ fileId });
findById.mockReturnValue({
lean: jest.fn().mockResolvedValue({
_id: fileId,
@ -60,7 +60,7 @@ describe('template resource route', () => {
await getTemplateResourceRouteHandler({ params: { token } }, res);
expect(getAndDeleteKey).toHaveBeenCalledWith(`templateresources:${token}`);
expect(getKey).toHaveBeenCalledWith(`templateresources:${token}`);
expect(downloadFile).toHaveBeenCalledWith('files', `files/${fileId}.png`);
expect(res.set).toHaveBeenCalledWith(
'Content-Disposition',
@ -73,12 +73,12 @@ describe('template resource route', () => {
});
it('returns the same 404 for missing, used, expired, or deleted resources', async () => {
getAndDeleteKey.mockResolvedValue(null);
getKey.mockResolvedValue(null);
const res = response();
await getTemplateResourceRouteHandler({ params: { token: 'b'.repeat(64) } }, res);
expect(res.status).toHaveBeenCalledWith(404);
getAndDeleteKey.mockResolvedValue({ fileId: '507f1f77bcf86cd799439011' });
getKey.mockResolvedValue({ fileId: '507f1f77bcf86cd799439011' });
findById.mockReturnValue({ lean: jest.fn().mockResolvedValue(null) });
const deletedRes = response();
await getTemplateResourceRouteHandler({ params: { token: 'c'.repeat(64) } }, deletedRes);