2021-04-01 07:17:45 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
|
|
|
|
|
|
|
const {MaxLimit} = require('../lib/limit');
|
|
|
|
|
|
|
|
describe('Limit Service', function () {
|
|
|
|
describe('Max Limit', function () {
|
2021-04-01 08:03:32 +03:00
|
|
|
describe('Constructor', function () {
|
|
|
|
it('throws if initialized without a max limit', function () {
|
|
|
|
const config = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const limit = new MaxLimit({name: 'no limits!', config});
|
|
|
|
should.fail(limit, 'Should have errored');
|
|
|
|
} catch (err) {
|
|
|
|
should.exist(err);
|
|
|
|
should.exist(err.errorType);
|
|
|
|
should.equal(err.errorType, 'IncorrectUsageError');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws if initialized without a current count query', function () {
|
|
|
|
const config = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const limit = new MaxLimit({name: 'no accountability!', config});
|
|
|
|
should.fail(limit, 'Should have errored');
|
|
|
|
} catch (err) {
|
|
|
|
should.exist(err);
|
|
|
|
should.exist(err.errorType);
|
|
|
|
should.equal(err.errorType, 'IncorrectUsageError');
|
|
|
|
}
|
|
|
|
});
|
2021-04-01 07:20:56 +03:00
|
|
|
});
|
|
|
|
|
2021-04-01 08:10:07 +03:00
|
|
|
describe('Is over limit', function () {
|
|
|
|
it('throws if is over the limit', async function () {
|
|
|
|
const config = {
|
|
|
|
max: 3,
|
|
|
|
currentCountQuery: () => 42
|
|
|
|
};
|
|
|
|
const limit = new MaxLimit({name: 'maxy', config});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await limit.errorIfIsOverLimit();
|
|
|
|
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: 1,
|
|
|
|
currentCountQuery: () => 1
|
|
|
|
};
|
|
|
|
|
|
|
|
const limit = new MaxLimit({name: 'maxy', config});
|
|
|
|
|
|
|
|
await limit.errorIfIsOverLimit();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
describe('Would go over limit', function () {
|
|
|
|
it('throws if would go over the limit', async function () {
|
|
|
|
const config = {
|
|
|
|
max: 1,
|
|
|
|
currentCountQuery: () => 1
|
|
|
|
};
|
|
|
|
const limit = new MaxLimit({name: 'maxy', config});
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
try {
|
|
|
|
await limit.errorIfWouldGoOverLimit();
|
|
|
|
should.fail(limit, 'Should have errored');
|
|
|
|
} catch (err) {
|
|
|
|
should.exist(err);
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
should.exist(err.errorType);
|
|
|
|
should.equal(err.errorType, 'HostLimitError');
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
should.exist(err.errorDetails);
|
|
|
|
should.equal(err.errorDetails.name, 'maxy');
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
should.exist(err.message);
|
|
|
|
should.equal(err.message, 'This action would exceed the maxy limit on your current plan.');
|
|
|
|
}
|
|
|
|
});
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
it('passes if does not go over the limit', async function () {
|
|
|
|
const config = {
|
|
|
|
max: 2,
|
|
|
|
currentCountQuery: () => 1
|
|
|
|
};
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
const limit = new MaxLimit({name: 'maxy', config});
|
2021-04-01 07:17:45 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
await limit.errorIfWouldGoOverLimit();
|
|
|
|
});
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
it('ignores default configured max limit when it is passed explicitly', async function () {
|
|
|
|
const config = {
|
|
|
|
max: 10,
|
|
|
|
currentCountQuery: () => 10
|
|
|
|
};
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
const limit = new MaxLimit({name: 'maxy', config});
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
// should pass as the limit is overridden to 10 + 1 = 11
|
|
|
|
await limit.errorIfWouldGoOverLimit({max: 11});
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
try {
|
|
|
|
// should fail because limit is overridden to 10 + 1 < 1
|
|
|
|
await limit.errorIfWouldGoOverLimit({max: 1});
|
|
|
|
should.fail(limit, 'Should have errored');
|
|
|
|
} catch (err) {
|
|
|
|
should.exist(err);
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
should.exist(err.errorType);
|
|
|
|
should.equal(err.errorType, 'HostLimitError');
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
should.exist(err.errorDetails);
|
|
|
|
should.equal(err.errorDetails.name, 'maxy');
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-04-01 08:03:32 +03:00
|
|
|
should.exist(err.message);
|
|
|
|
should.equal(err.message, 'This action would exceed the maxy limit on your current plan.');
|
|
|
|
}
|
|
|
|
});
|
2021-04-01 07:59:52 +03:00
|
|
|
});
|
2021-04-01 07:17:45 +03:00
|
|
|
});
|
|
|
|
});
|