From 91a2e5448440086467dba3ae36b459e64dbaa699 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 26 Oct 2021 15:42:10 +0400 Subject: [PATCH] Added ability to pass in "currentCount" for limited resource refs https://linear.app/tryghost/issue/CORE-121/create-a-video-storage-adapter - When checking limits for a nondb-resource type (like file size) there is no way to "currentCountQuery", so the value has to be passed in directly into the limit to evaluate against configured "max" limit --- ghost/limit-service/lib/limit.js | 5 +-- ghost/limit-service/test/limit.test.js | 45 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ghost/limit-service/lib/limit.js b/ghost/limit-service/lib/limit.js index b029ebfd50..61dd188fef 100644 --- a/ghost/limit-service/lib/limit.js +++ b/ghost/limit-service/lib/limit.js @@ -114,9 +114,10 @@ class MaxLimit extends Limit { * * @param {Object} options * @param {Number} [options.max] - overrides configured default max value to perform checks against + * @param {Number} [options.currentCount] - overrides currentCountQuery to perform checks against */ - async errorIfIsOverLimit({max} = {}) { - let currentCount = await this.currentCountQuery(); + async errorIfIsOverLimit({max, currentCount} = {}) { + currentCount = currentCount || await this.currentCountQuery(); if (currentCount > (max || this.max)) { throw this.generateError(currentCount); diff --git a/ghost/limit-service/test/limit.test.js b/ghost/limit-service/test/limit.test.js index eaaa0057fa..b8efaf7989 100644 --- a/ghost/limit-service/test/limit.test.js +++ b/ghost/limit-service/test/limit.test.js @@ -45,6 +45,23 @@ describe('Limit Service', function () { describe('Max Limit', function () { describe('Constructor', function () { + it('passes if within the limit and custom currentCount overriding currentCountQuery', async function () { + const config = { + max: 5, + error: 'You have gone over the limit', + currentCountQuery: function () { + throw new Error('Should not be called'); + } + }; + + try { + const limit = new MaxLimit({name: '', config, errors}); + await limit.errorIfIsOverLimit({currentCount: 4}); + } catch (error) { + should.fail('Should have not errored', error); + } + }); + it('throws if initialized without a max limit', function () { const config = {}; @@ -74,6 +91,34 @@ describe('Limit Service', function () { err.message.should.match(/max limit without a current count query/); } }); + + it('throws when would go over the limit and custom currentCount overriding currentCountQuery', async function () { + const _5MB = 5000000; + const config = { + max: _5MB, + error: 'You have exceeded the maximum file size {{ max }}', + currentCountQuery: function () { + throw new Error('Should not be called'); + } + }; + + try { + const limit = new MaxLimit({ + name: 'fileSize', + config, + errors + }); + const _10MB = 10000000; + + await limit.errorIfIsOverLimit({currentCount: _10MB}); + } catch (error) { + error.errorType.should.equal('HostLimitError'); + error.errorDetails.name.should.equal('fileSize'); + error.errorDetails.limit.should.equal(5000000); + error.errorDetails.total.should.equal(10000000); + error.message.should.equal('You have exceeded the maximum file size 5,000,000'); + } + }); }); describe('Is over limit', function () {