From 49d36fc1a13a54a593f3754a630d2e8fa93aaf64 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 25 Sep 2023 11:07:00 +0200 Subject: [PATCH] Allowed config to override labs flags fixes https://github.com/TryGhost/DevOps/issues/72 - in order to have greater control of labs flags outside of Ghost, this commit allows Ghost to respect the value of `labs: { flagName: boolean }` - this means we can hardcode a value to true or false, irrespective of the value in the DB or GA flags array - also adds tests to check functionality --- ghost/core/core/shared/labs.js | 5 ++++ ghost/core/test/unit/shared/labs.test.js | 38 ++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/ghost/core/core/shared/labs.js b/ghost/core/core/shared/labs.js index 77e026ff43..ebe76072c4 100644 --- a/ghost/core/core/shared/labs.js +++ b/ghost/core/core/shared/labs.js @@ -62,6 +62,11 @@ module.exports.getAll = () => { labs[gaKey] = true; }); + const labsConfig = config.get('labs') || {}; + Object.keys(labsConfig).forEach((key) => { + labs[key] = labsConfig[key]; + }); + labs.members = settingsCache.get('members_signup_access') !== 'none'; return labs; diff --git a/ghost/core/test/unit/shared/labs.test.js b/ghost/core/test/unit/shared/labs.test.js index ee77f1b913..e779728972 100644 --- a/ghost/core/test/unit/shared/labs.test.js +++ b/ghost/core/test/unit/shared/labs.test.js @@ -6,13 +6,14 @@ const labs = require('../../../core/shared/labs'); const settingsCache = require('../../../core/shared/settings-cache'); function expectedLabsObject(obj) { - const withGA = Object.assign({}, obj); + let enabledFlags = {}; labs.GA_KEYS.forEach((key) => { - withGA[key] = true; + enabledFlags[key] = true; }); - return withGA; + enabledFlags = Object.assign(enabledFlags, obj); + return enabledFlags; } describe('Labs Service', function () { @@ -64,6 +65,37 @@ describe('Labs Service', function () { assert.equal(labs.isSet('urlCache'), false); }); + it('respects the value in config over settings', function () { + configUtils.set('labs', { + collections: false + }); + sinon.stub(settingsCache, 'get'); + settingsCache.get.withArgs('labs').returns({ + collections: true, + members: true + }); + + assert.deepEqual(labs.getAll(), expectedLabsObject({ + collections: false, + members: true + })); + + assert.equal(labs.isSet('collections'), false); + }); + + it('respects the value in config over GA keys', function () { + configUtils.set('labs', { + audienceFeedback: false + }); + + assert.deepEqual(labs.getAll(), expectedLabsObject({ + audienceFeedback: false, + members: true + })); + + assert.equal(labs.isSet('audienceFeedback'), false); + }); + it('members flag is true when members_signup_access setting is "all"', function () { sinon.stub(settingsCache, 'get'); settingsCache.get.withArgs('members_signup_access').returns('all');