0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added optional max limit override to errorIfIsOverLimit

refs https://github.com/TryGhost/Team/issues/587
refs 73e7319406

- It's a symmetric change to the one introduce in the refenreced commit
- TLDR: allows to check if limit was reached if the user changes the limit
This commit is contained in:
Naz 2021-04-01 18:27:29 +13:00
parent 5dd3752f15
commit 2d5aff434c
2 changed files with 38 additions and 2 deletions

View file

@ -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); let currentCount = await this.currentCountQuery(this.db);
if (currentCount > this.max) {
if (currentCount > (max || this.max)) {
throw this.generateError(currentCount); throw this.generateError(currentCount);
} }
} }

View file

@ -69,6 +69,35 @@ describe('Limit Service', function () {
await limit.errorIfIsOverLimit(); 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 () { describe('Would go over limit', function () {