mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 20:34:02 +03:00
a1962f38cd
refs https://github.com/TryGhost/Team/issues/597
- To be able to transpile the library for different runtimes (make it polymorphic) had to get rid of dependencies that were not compatible with ES Modules
- By making errors an injectable constructor option it removes the depencency and allows to transpile the library for multiple targets
- The `errors` option is now a required parameter for `loadLimits` method. It errors if it's missing (error message copy inspired by content api error 69fcea0582/packages/content-api/lib/index.js (L21)
)
113 lines
4.2 KiB
JavaScript
113 lines
4.2 KiB
JavaScript
// Switch these lines once there are useful utils
|
|
// const testUtils = require('./utils');
|
|
require('./utils');
|
|
|
|
const LimitService = require('../lib/limit-service');
|
|
const {MaxLimit, FlagLimit} = require('../lib/limit');
|
|
|
|
const errors = require('./fixtures/errors');
|
|
|
|
describe('Limit Service', function () {
|
|
describe('Lodash Template', function () {
|
|
it('Does not get clobbered by this lib', function () {
|
|
require('../lib/limit');
|
|
let _ = require('lodash');
|
|
|
|
_.templateSettings.interpolate.should.eql(/<%=([\s\S]+?)%>/g);
|
|
});
|
|
});
|
|
|
|
describe('Error Messages', function () {
|
|
it('Formats numbers correctly', function () {
|
|
let limit = new MaxLimit({
|
|
name: 'test',
|
|
config: {
|
|
max: 35000000,
|
|
currentCountQuery: () => {},
|
|
error: 'Your plan supports up to {{max}} staff users. Please upgrade to add more.'
|
|
},
|
|
errors
|
|
});
|
|
|
|
let error = limit.generateError(35000001);
|
|
|
|
error.message.should.eql('Your plan supports up to 35,000,000 staff users. Please upgrade to add more.');
|
|
error.errorDetails.limit.should.eql(35000000);
|
|
error.errorDetails.total.should.eql(35000001);
|
|
});
|
|
});
|
|
|
|
describe('Loader', function () {
|
|
it('throws if errors configuration is not specified', function () {
|
|
const limitService = new LimitService();
|
|
|
|
let limits = {staff: {max: 2}};
|
|
|
|
try {
|
|
limitService.loadLimits({limits});
|
|
should.fail(limitService, 'Should have errored');
|
|
} catch (err) {
|
|
should.exist(err);
|
|
err.message.should.equal(`Config Missing: 'errors' is required`);
|
|
}
|
|
});
|
|
|
|
it('can load a basic limit', function () {
|
|
const limitService = new LimitService();
|
|
|
|
let limits = {staff: {max: 2}};
|
|
|
|
limitService.loadLimits({limits, errors});
|
|
|
|
limitService.limits.should.be.an.Object().with.properties(['staff']);
|
|
limitService.limits.staff.should.be.an.instanceOf(MaxLimit);
|
|
limitService.isLimited('staff').should.be.true();
|
|
limitService.isLimited('members').should.be.false();
|
|
});
|
|
|
|
it('can load multiple limits', function () {
|
|
const limitService = new LimitService();
|
|
|
|
let limits = {staff: {max: 2}, members: {max: 100}};
|
|
|
|
limitService.loadLimits({limits, errors});
|
|
|
|
limitService.limits.should.be.an.Object().with.properties(['staff', 'members']);
|
|
limitService.limits.staff.should.be.an.instanceOf(MaxLimit);
|
|
limitService.limits.members.should.be.an.instanceOf(MaxLimit);
|
|
limitService.isLimited('staff').should.be.true();
|
|
limitService.isLimited('members').should.be.true();
|
|
});
|
|
|
|
it('can load camel cased limits', function () {
|
|
const limitService = new LimitService();
|
|
|
|
let limits = {customThemes: {disabled: true}};
|
|
|
|
limitService.loadLimits({limits, errors});
|
|
|
|
limitService.limits.should.be.an.Object().with.properties(['customThemes']);
|
|
limitService.limits.customThemes.should.be.an.instanceOf(FlagLimit);
|
|
limitService.isLimited('staff').should.be.false();
|
|
limitService.isLimited('members').should.be.false();
|
|
limitService.isLimited('custom_themes').should.be.true();
|
|
limitService.isLimited('customThemes').should.be.true();
|
|
});
|
|
|
|
it('can load incorrectly cased limits', function () {
|
|
const limitService = new LimitService();
|
|
|
|
let limits = {custom_themes: {disabled: true}};
|
|
|
|
limitService.loadLimits({limits, errors});
|
|
|
|
limitService.limits.should.be.an.Object().with.properties(['customThemes']);
|
|
limitService.limits.customThemes.should.be.an.instanceOf(FlagLimit);
|
|
limitService.isLimited('staff').should.be.false();
|
|
limitService.isLimited('members').should.be.false();
|
|
limitService.isLimited('custom_themes').should.be.true();
|
|
limitService.isLimited('customThemes').should.be.true();
|
|
});
|
|
});
|
|
});
|