From 2bb4dab402efeb161cb85678f9f7d4de3eef4298 Mon Sep 17 00:00:00 2001 From: williamlardier Date: Wed, 26 Jun 2024 11:27:47 +0200 Subject: [PATCH] S3UTILS-168: remove any website configuration The website configuration object, when calling "fromObj" must be of type WebsiteConfiguration, otherwise, an assert will fail, and this will cause a crash of count items. --- CountItems/CountWorker.js | 5 +++++ tests/unit/CountItems/CountWorker.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/CountItems/CountWorker.js b/CountItems/CountWorker.js index b3093ff4..281f9b76 100644 --- a/CountItems/CountWorker.js +++ b/CountItems/CountWorker.js @@ -30,6 +30,11 @@ class CountWorker { if (!this.client.client) { return callback(new Error('NotConnected')); } + // 'fromObj' expects that the website configuration is an instance of + // WebsiteConfiguration As it it not used in CountItems, we nullify it. + if (bucketInfoObj._websiteConfiguration) { + Object.assign(bucketInfoObj, { _websiteConfiguration: null }); + } const bucketInfo = BucketInfo.fromObj(bucketInfoObj); const bucketName = bucketInfo.getName(); this.log.info(`${process.pid} handling ${bucketName}`); diff --git a/tests/unit/CountItems/CountWorker.js b/tests/unit/CountItems/CountWorker.js index b8d648ab..6a8f1cb0 100644 --- a/tests/unit/CountItems/CountWorker.js +++ b/tests/unit/CountItems/CountWorker.js @@ -196,4 +196,32 @@ describe('CountItems::CountWorker', () => { done(); }, 100); }); + + // test that the countworker's "countItems" method properly handles + // buckets with website + test('should correctly handle buckets with website', done => { + const testSendFn = jest.fn(); + const w = new CountWorker({ + log: new DummyLogger(), + sendFn: testSendFn, + client: mongoMock, + }); + const bucketInfo = { + _name: 'test-bucket', + _owner: 'any', + _ownerDisplayName: 'any', + _creationDate: Date.now().toString(), + website: { indexDocument: 'index.html' }, + }; + mongoMock.setup.mockImplementationOnce(cb => cb()); + mongoMock.close.mockImplementationOnce(cb => cb()); + mongoMock.client.isConnected.mockImplementationOnce(() => false); + mongoMock._getIsTransient.mockImplementationOnce((_a, _b, cb) => cb(null, true)); + mongoMock.getObjectMDStats.mockImplementationOnce((_a, _b, _c, _d, cb) => cb(null, { value: 42 })); + w.countItems(bucketInfo, (err, results) => { + expect(err).toBeNull(); + expect(results).toEqual({ value: 42 }); + done(); + }); + }); });