Changed casing of limit names + fixed handling

refs: https://github.com/TryGhost/Team/issues/510

- Ghost config always uses camelcase. This was incorrectly implemented with snake case originally
- Swap to use camelCase by default, which is desirable, but support both
- It's really easy to support both in the loader and isLimited check, so we do this to stop ourselves tripping on this later
This commit is contained in:
Hannah Wolfe 2021-03-04 20:11:54 +00:00
parent 314a1138c3
commit 2163ec6057
3 changed files with 67 additions and 5 deletions

View File

@ -21,11 +21,11 @@ module.exports = {
return result.length;
}
},
custom_integrations: {
customIntegrations: {
currentCountQuery: async (db) => {
let result = await db.knex('integrations').count('id', {as: 'count'}).whereNotIn('type', ['internal', 'builtin']).first();
return result.count;
}
},
custom_themes: {}
customThemes: {}
};

View File

@ -10,6 +10,8 @@ class LimitService {
loadLimits({limits, helpLink, db}) {
Object.keys(limits).forEach((name) => {
name = _.camelCase(name);
if (config[name]) {
let limitConfig = _.merge({}, limits[name], config[name]);
@ -23,7 +25,7 @@ class LimitService {
}
isLimited(limitName) {
return !!this.limits[limitName];
return !!this.limits[_.camelCase(limitName)];
}
async checkIsOverLimit(limitName) {

View File

@ -2,6 +2,9 @@
// const testUtils = require('./utils');
require('./utils');
const LimitService = require('../lib/limit-service');
const {MaxLimit, FlagLimit} = require('../lib/limit');
describe('Limit Service', function () {
describe('Lodash Template', function () {
it('Does not get clobbered by this lib', function () {
@ -14,8 +17,6 @@ describe('Limit Service', function () {
describe('Error Messages', function () {
it('Formats numbers correctly', function () {
const {MaxLimit} = require('../lib/limit');
let limit = new MaxLimit({name: 'test', config: {max: 35000000, currentCountQuery: () => {}, error: 'Your plan supports up to {{max}} staff users. Please upgrade to add more.'}});
let error = limit.generateError(35000001);
@ -25,4 +26,63 @@ describe('Limit Service', function () {
error.errorDetails.total.should.eql(35000001);
});
});
describe('Loader', function () {
it('can load a basic limit', function () {
const limitService = new LimitService();
let limits = {staff: {max: 2}};
limitService.loadLimits({limits});
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});
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});
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});
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();
});
});
});