diff --git a/ghost/limit-service/lib/config.js b/ghost/limit-service/lib/config.js index 66c1e9e75a..4c37d967f3 100644 --- a/ghost/limit-service/lib/config.js +++ b/ghost/limit-service/lib/config.js @@ -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: {} }; diff --git a/ghost/limit-service/lib/limit-service.js b/ghost/limit-service/lib/limit-service.js index e2f3f35594..6ab6b85e67 100644 --- a/ghost/limit-service/lib/limit-service.js +++ b/ghost/limit-service/lib/limit-service.js @@ -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) { diff --git a/ghost/limit-service/test/limit-service.test.js b/ghost/limit-service/test/limit-service.test.js index 19c1031dd4..8b59e3d8ed 100644 --- a/ghost/limit-service/test/limit-service.test.js +++ b/ghost/limit-service/test/limit-service.test.js @@ -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(); + }); + }); });