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
This commit is contained in:
parent
9c695a363c
commit
91a2e54484
@ -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);
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user