From 8a08e9322f596c3cf0741743e845a9dd1d1094dd Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 6 Apr 2022 22:53:33 +0100 Subject: [PATCH] org settings events + tests + stub license events --- packages/backend-core/src/events/constants.js | 8 ++- .../backend-core/src/events/handlers/index.js | 4 +- .../src/events/handlers/license.js | 30 ++++++++++ .../src/events/handlers/licensing.js | 10 ---- .../src/tests/utilities/mocks/events.js | 8 +++ .../src/api/controllers/global/configs.js | 59 ++++++++++++++----- .../src/api/routes/tests/configs.spec.js | 50 ++++++++++++++++ .../worker/src/tests/structures/configs.js | 4 +- 8 files changed, 143 insertions(+), 30 deletions(-) create mode 100644 packages/backend-core/src/events/handlers/license.js delete mode 100644 packages/backend-core/src/events/handlers/licensing.js diff --git a/packages/backend-core/src/events/constants.js b/packages/backend-core/src/events/constants.js index 712e41b17f..5fba4d733e 100644 --- a/packages/backend-core/src/events/constants.js +++ b/packages/backend-core/src/events/constants.js @@ -130,8 +130,12 @@ exports.Events = { AUTOMATION_STEP_DELETED: "automation:step:deleted", AUTOMATION_TRIGGER_UPDATED: "automation:trigger:updated", - // LICENSING - LICENSING_QUOTA_EXCEEDED: "licensing:quota:exceeded", + // LICENSE + LICENSE_UPGRADED: "license:upgraded", + LICENSE_DOWNGRADED: "license:downgraded", + LICENSE_UPDATED: "license:updated", + LICENSE_PAIRED: "license:paired", + LICENSE_QUOTA_EXCEEDED: "license:quota:exceeded", // ACCOUNT ACCOUNT_CREATED: "account:created", diff --git a/packages/backend-core/src/events/handlers/index.js b/packages/backend-core/src/events/handlers/index.js index 8cd523e2c0..5b825e7ad9 100644 --- a/packages/backend-core/src/events/handlers/index.js +++ b/packages/backend-core/src/events/handlers/index.js @@ -4,7 +4,7 @@ const auth = require("./auth") const automation = require("./automation") const datasource = require("./datasource") const email = require("./email") -const licensing = require("./licensing") +const license = require("./license") const layout = require("./layout") const org = require("./org") const query = require("./query") @@ -21,7 +21,7 @@ module.exports = { automation, datasource, email, - licensing, + license, layout, org, query, diff --git a/packages/backend-core/src/events/handlers/license.js b/packages/backend-core/src/events/handlers/license.js new file mode 100644 index 0000000000..8e9befc088 --- /dev/null +++ b/packages/backend-core/src/events/handlers/license.js @@ -0,0 +1,30 @@ +const events = require("../events") +const { Events } = require("../constants") + +exports.updgraded = () => { + const properties = {} + events.processEvent(Events.LICENSE_UPGRADED, properties) +} + +exports.downgraded = () => { + const properties = {} + events.processEvent(Events.LICENSE_DOWNGRADED, properties) +} + +exports.updated = () => { + const properties = {} + events.processEvent(Events.LICENSE_UPDATED, properties) +} + +exports.paired = () => { + const properties = {} + events.processEvent(Events.LICENSE_PAIRED, properties) +} + +exports.quotaExceeded = (quotaName, value) => { + const properties = { + name: quotaName, + value, + } + events.processEvent(Events.LICENSE_QUOTA_EXCEEDED, properties) +} diff --git a/packages/backend-core/src/events/handlers/licensing.js b/packages/backend-core/src/events/handlers/licensing.js deleted file mode 100644 index 144a9abf78..0000000000 --- a/packages/backend-core/src/events/handlers/licensing.js +++ /dev/null @@ -1,10 +0,0 @@ -const events = require("../events") -const { Events } = require("../constants") - -exports.quotaExceeded = (quotaName, value) => { - const properties = { - name: quotaName, - value, - } - events.processEvent(Events.LICENSING_QUOTA_EXCEEDED, properties) -} diff --git a/packages/backend-core/src/tests/utilities/mocks/events.js b/packages/backend-core/src/tests/utilities/mocks/events.js index 0ee2eb0b27..ee80e362ab 100644 --- a/packages/backend-core/src/tests/utilities/mocks/events.js +++ b/packages/backend-core/src/tests/utilities/mocks/events.js @@ -48,6 +48,14 @@ jest.mock("../../../events", () => { created: jest.fn(), deleted: jest.fn(), }, + org: { + nameUpdated: jest.fn(), + logoUpdated: jest.fn(), + platformURLUpdated: jest.fn(), + versionChecked: jest.fn(), + analyticsOptOut: jest.fn(), + npsSubmitted: jest.fn(), + }, } }) diff --git a/packages/worker/src/api/controllers/global/configs.js b/packages/worker/src/api/controllers/global/configs.js index 9ee2648d89..69c4982272 100644 --- a/packages/worker/src/api/controllers/global/configs.js +++ b/packages/worker/src/api/controllers/global/configs.js @@ -29,39 +29,52 @@ const getEventFns = async (db, config) => { if (!existing) { switch (config.type) { - case Configs.SMTP: + case Configs.SMTP: { fns.push(events.email.SMTPCreated) break - case Configs.GOOGLE: + } + case Configs.GOOGLE: { fns.push(() => events.auth.SSOCreated(type)) if (config.config.activated) { fns.push(() => events.auth.SSOActivated(type)) } break - case Configs.OIDC: + } + case Configs.OIDC: { fns.push(() => events.auth.SSOCreated(type)) if (config.config.configs[0].activated) { fns.push(() => events.auth.SSOActivated(type)) } break - case Configs.SETTINGS: - if (config.company) { + } + case Configs.SETTINGS: { + // company + const company = config.config.company + if (company && company !== "Budibase") { fns.push(events.org.nameUpdated) } - if (config.logoUrl) { + + // logo + const logoUrl = config.config.logoUrl + if (logoUrl) { fns.push(events.org.logoUpdated) } - if (config.platformUrl) { + + // platform url + const platformUrl = config.config.platformUrl + if (platformUrl && platformUrl !== "http://localhost:10000") { fns.push(events.org.platformURLUpdated) } break + } } } else { switch (config.type) { - case Configs.SMTP: + case Configs.SMTP: { fns.push(events.email.SMTPUpdated) break - case Configs.GOOGLE: + } + case Configs.GOOGLE: { fns.push(() => events.auth.SSOUpdated(type)) if (!existing.config.activated && config.config.activated) { fns.push(() => events.auth.SSOActivated(type)) @@ -69,7 +82,8 @@ const getEventFns = async (db, config) => { fns.push(() => events.auth.SSODeactivated(type)) } break - case Configs.OIDC: + } + case Configs.OIDC: { fns.push(() => events.auth.SSOUpdated(type)) if ( !existing.config.configs[0].activated && @@ -83,17 +97,34 @@ const getEventFns = async (db, config) => { fns.push(() => events.auth.SSODeactivated(type)) } break - case Configs.SETTINGS: - if (config.company && existing.company !== config.company) { + } + case Configs.SETTINGS: { + // company + const existingCompany = existing.config.company + const company = config.config.company + if (company && company !== "Budibase" && existingCompany !== company) { fns.push(events.org.nameUpdated) } - if (config.logoUrl && existing.logoUrl !== config.logoUrl) { + + // logo + const existingLogoUrl = existing.config.logoUrl + const logoUrl = config.config.logoUrl + if (logoUrl && existingLogoUrl !== logoUrl) { fns.push(events.org.logoUpdated) } - if (config.platformUrl && existing.platformUrl !== config.platformUrl) { + + // platform url + const existingPlatformUrl = existing.config.platformUrl + const platformUrl = config.config.platformUrl + if ( + platformUrl && + platformUrl !== "http://localhost:10000" && + existingPlatformUrl !== platformUrl + ) { fns.push(events.org.platformURLUpdated) } break + } } } diff --git a/packages/worker/src/api/routes/tests/configs.spec.js b/packages/worker/src/api/routes/tests/configs.spec.js index 98e0fef17f..b2dc8124da 100644 --- a/packages/worker/src/api/routes/tests/configs.spec.js +++ b/packages/worker/src/api/routes/tests/configs.spec.js @@ -177,6 +177,56 @@ describe("configs", () => { }) }) }) + + describe("settings", () => { + const saveSettingsConfig = async (conf, _id, _rev) => { + const settingsConfig = structures.configs.settings(conf) + return saveConfig(settingsConfig, _id, _rev) + } + + describe("create", () => { + it ("should create settings config with default settings", async () => { + await config.deleteConfig(Configs.SETTINGS) + + await saveSettingsConfig() + + expect(events.org.nameUpdated).not.toBeCalled() + expect(events.org.logoUpdated).not.toBeCalled() + expect(events.org.platformURLUpdated).not.toBeCalled() + }) + + it ("should create settings config with non-default settings", async () => { + await config.deleteConfig(Configs.SETTINGS) + const conf = { + company: "acme", + logoUrl: "http://example.com", + platformUrl: "http://example.com" + } + + await saveSettingsConfig(conf) + + expect(events.org.nameUpdated).toBeCalledTimes(1) + expect(events.org.logoUpdated).toBeCalledTimes(1) + expect(events.org.platformURLUpdated).toBeCalledTimes(1) + }) + }) + + describe("update", () => { + it ("should update settings config", async () => { + await config.deleteConfig(Configs.SETTINGS) + const settingsConfig = await saveSettingsConfig() + settingsConfig.config.company = "acme" + settingsConfig.config.logoUrl = "http://example.com" + settingsConfig.config.platformUrl = "http://example.com" + + await saveSettingsConfig(settingsConfig.config, settingsConfig._id, settingsConfig._rev) + + expect(events.org.nameUpdated).toBeCalledTimes(1) + expect(events.org.logoUpdated).toBeCalledTimes(1) + expect(events.org.platformURLUpdated).toBeCalledTimes(1) + }) + }) + }) }) it("should return the correct checklist status based on the state of the budibase installation", async () => { diff --git a/packages/worker/src/tests/structures/configs.js b/packages/worker/src/tests/structures/configs.js index 4856df572c..41ba283eaf 100644 --- a/packages/worker/src/tests/structures/configs.js +++ b/packages/worker/src/tests/structures/configs.js @@ -1,4 +1,4 @@ -const { Configs, LOGO_URL } = require("../../constants") +const { Configs } = require("../../constants") const { utils } = require("@budibase/backend-core") exports.oidc = conf => { @@ -68,7 +68,7 @@ exports.settings = conf => { type: Configs.SETTINGS, config: { platformUrl: "http://localhost:10000", - logoUrl: LOGO_URL, + logoUrl: "", company: "Budibase", ...conf, },