From 763e50a632a6e5444ecad7502d66fef53026446e Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Tue, 15 Sep 2026 02:03:46 +0100 Subject: [PATCH] Refactor test suite to replace getAndDeleteKey with getKey for template resource handling 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. --- .../misc/__tests__/templateresources.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/services/misc/__tests__/templateresources.test.js b/src/services/misc/__tests__/templateresources.test.js index f261949..4b8e670 100644 --- a/src/services/misc/__tests__/templateresources.test.js +++ b/src/services/misc/__tests__/templateresources.test.js @@ -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);