diff --git a/ghost/limit-service/lib/limit.js b/ghost/limit-service/lib/limit.js index 4731d61f17..44287cafc7 100644 --- a/ghost/limit-service/lib/limit.js +++ b/ghost/limit-service/lib/limit.js @@ -95,9 +95,16 @@ class MaxLimit extends Limit { } } - async errorIfIsOverLimit() { + /** + * Throws a HostLimitError if the configured or passed max limit is ecceded by currentCountQuery + * + * @param {Object} options + * @param {Number} [options.max] - overrides configured default max value to perform checks against + */ + async errorIfIsOverLimit({max} = {}) { let currentCount = await this.currentCountQuery(this.db); - if (currentCount > this.max) { + + 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 4d2f8c82a5..ed6493d82b 100644 --- a/ghost/limit-service/test/limit.test.js +++ b/ghost/limit-service/test/limit.test.js @@ -69,6 +69,35 @@ describe('Limit Service', function () { await limit.errorIfIsOverLimit(); }); + + it('ignores default configured max limit when it is passed explicitly', async function () { + const config = { + max: 10, + currentCountQuery: () => 10 + }; + + const limit = new MaxLimit({name: 'maxy', config}); + + // should pass as the limit is exactly on the limit 10 >= 10 + await limit.errorIfIsOverLimit({max: 10}); + + try { + // should fail because limit is overridden to 10 < 9 + await limit.errorIfIsOverLimit({max: 9}); + should.fail(limit, 'Should have errored'); + } catch (err) { + should.exist(err); + + should.exist(err.errorType); + should.equal(err.errorType, 'HostLimitError'); + + should.exist(err.errorDetails); + should.equal(err.errorDetails.name, 'maxy'); + + should.exist(err.message); + should.equal(err.message, 'This action would exceed the maxy limit on your current plan.'); + } + }); }); describe('Would go over limit', function () {