mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Added addedCount to max and maxPeriodic limits
refs https://github.com/TryGhost/Team/issues/588 - The `addedCount` parameter in `errorIfWouldGoOverLimit` method allows to specify a custom resource count that is about to be added. Example usecase is when we'd want to send a 100 emails and current limit is 99, and none have been sent so far. With previous implementation the check would've passed because it only checked for single resource that would be added through "+1". Current implementation allows to specify the amount of recources to be added
This commit is contained in:
parent
4f0a7fa1d3
commit
612cc2b513
2 changed files with 117 additions and 4 deletions
|
@ -89,10 +89,10 @@ class MaxLimit extends Limit {
|
|||
* @param {Object} options
|
||||
* @param {Number} [options.max] - overrides configured default max value to perform checks against
|
||||
*/
|
||||
async errorIfWouldGoOverLimit({max} = {}) {
|
||||
async errorIfWouldGoOverLimit({max, addedCount = 1} = {}) {
|
||||
let currentCount = await this.currentCountQuery(this.db);
|
||||
|
||||
if ((currentCount + 1) > (max || this.max)) {
|
||||
if ((currentCount + addedCount) > (max || this.max)) {
|
||||
throw this.generateError(currentCount);
|
||||
}
|
||||
}
|
||||
|
@ -191,10 +191,10 @@ class MaxPeriodicLimit extends Limit {
|
|||
* @param {Object} options
|
||||
* @param {Number} [options.max] - overrides configured default maxPeriodic value to perform checks against
|
||||
*/
|
||||
async errorIfWouldGoOverLimit({max} = {}) {
|
||||
async errorIfWouldGoOverLimit({max, addedCount = 1} = {}) {
|
||||
let currentCount = await this.currentCountQuery(this.db);
|
||||
|
||||
if ((currentCount + 1) > (max || this.maxPeriodic)) {
|
||||
if ((currentCount + addedCount) > (max || this.maxPeriodic)) {
|
||||
throw this.generateError(currentCount);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,6 +167,30 @@ describe('Limit Service', function () {
|
|||
}
|
||||
});
|
||||
|
||||
it('throws if would go over the limit with with custom added count', async function () {
|
||||
const config = {
|
||||
max: 23,
|
||||
currentCountQuery: () => 13
|
||||
};
|
||||
const limit = new MaxLimit({name: 'maxy', config, errors});
|
||||
|
||||
try {
|
||||
await limit.errorIfWouldGoOverLimit({addedCount: 11});
|
||||
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.');
|
||||
}
|
||||
});
|
||||
|
||||
it('passes if does not go over the limit', async function () {
|
||||
const config = {
|
||||
max: 2,
|
||||
|
@ -330,6 +354,95 @@ describe('Limit Service', function () {
|
|||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Would go over limit', function () {
|
||||
it('passes if within the limit', async function () {
|
||||
const currentCountyQueryMock = sinon.mock().returns(4);
|
||||
|
||||
const config = {
|
||||
maxPeriodic: 5,
|
||||
error: 'You have exceeded the number of emails you can send within your billing period.',
|
||||
interval: 'month',
|
||||
startDate: '2021-01-01T00:00:00Z',
|
||||
currentCountQuery: currentCountyQueryMock
|
||||
};
|
||||
|
||||
try {
|
||||
const limit = new MaxPeriodicLimit({name: 'mailguard', config, errors});
|
||||
await limit.errorIfWouldGoOverLimit();
|
||||
} catch (error) {
|
||||
should.fail('MaxPeriodicLimit errorIfWouldGoOverLimit check should not have errored');
|
||||
}
|
||||
});
|
||||
|
||||
it('throws if would go over limit', async function () {
|
||||
const currentCountyQueryMock = sinon.mock().returns(5);
|
||||
|
||||
const config = {
|
||||
maxPeriodic: 5,
|
||||
error: 'You have exceeded the number of emails you can send within your billing period.',
|
||||
interval: 'month',
|
||||
startDate: '2021-01-01T00:00:00Z',
|
||||
currentCountQuery: currentCountyQueryMock
|
||||
};
|
||||
|
||||
try {
|
||||
const limit = new MaxPeriodicLimit({name: 'mailguard', config, errors});
|
||||
await limit.errorIfWouldGoOverLimit();
|
||||
} catch (error) {
|
||||
error.errorType.should.equal('HostLimitError');
|
||||
error.errorDetails.name.should.equal('mailguard');
|
||||
error.errorDetails.limit.should.equal(5);
|
||||
error.errorDetails.total.should.equal(5);
|
||||
|
||||
currentCountyQueryMock.callCount.should.equal(1);
|
||||
should(currentCountyQueryMock.args).not.be.undefined();
|
||||
should(currentCountyQueryMock.args[0][0]).be.undefined(); //knex db connection
|
||||
|
||||
const nowDate = new Date();
|
||||
const startOfTheMonthDate = new Date(Date.UTC(
|
||||
nowDate.getUTCFullYear(),
|
||||
nowDate.getUTCMonth()
|
||||
)).toISOString();
|
||||
|
||||
currentCountyQueryMock.args[0][1].should.equal(startOfTheMonthDate);
|
||||
}
|
||||
});
|
||||
|
||||
it('throws if would go over limit with custom added count', async function () {
|
||||
const currentCountyQueryMock = sinon.mock().returns(5);
|
||||
|
||||
const config = {
|
||||
maxPeriodic: 13,
|
||||
error: 'You have exceeded the number of emails you can send within your billing period.',
|
||||
interval: 'month',
|
||||
startDate: '2021-01-01T00:00:00Z',
|
||||
currentCountQuery: currentCountyQueryMock
|
||||
};
|
||||
|
||||
try {
|
||||
const limit = new MaxPeriodicLimit({name: 'mailguard', config, errors});
|
||||
await limit.errorIfWouldGoOverLimit({addedCount: 9});
|
||||
} catch (error) {
|
||||
error.errorType.should.equal('HostLimitError');
|
||||
error.errorDetails.name.should.equal('mailguard');
|
||||
error.errorDetails.limit.should.equal(13);
|
||||
error.errorDetails.total.should.equal(5);
|
||||
|
||||
currentCountyQueryMock.callCount.should.equal(1);
|
||||
should(currentCountyQueryMock.args).not.be.undefined();
|
||||
should(currentCountyQueryMock.args[0][0]).be.undefined(); //knex db connection
|
||||
|
||||
const nowDate = new Date();
|
||||
const startOfTheMonthDate = new Date(Date.UTC(
|
||||
nowDate.getUTCFullYear(),
|
||||
nowDate.getUTCMonth()
|
||||
)).toISOString();
|
||||
|
||||
currentCountyQueryMock.args[0][1].should.equal(startOfTheMonthDate);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Allowlist limit', function () {
|
||||
|
|
Loading…
Add table
Reference in a new issue