From fde16cf54820164c00fe268cc7217f9f351d40bc Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Tue, 5 Apr 2022 23:14:53 +0100 Subject: [PATCH] google/oidc created/deleted events + tests --- .../src/tests/utilities/mocks/events.js | 8 ++ .../src/api/controllers/global/configs.js | 124 +++++++++--------- .../src/api/routes/tests/configs.spec.js | 69 +++++++++- 3 files changed, 139 insertions(+), 62 deletions(-) diff --git a/packages/backend-core/src/tests/utilities/mocks/events.js b/packages/backend-core/src/tests/utilities/mocks/events.js index 97caac4171..f6eb2eddcc 100644 --- a/packages/backend-core/src/tests/utilities/mocks/events.js +++ b/packages/backend-core/src/tests/utilities/mocks/events.js @@ -21,12 +21,20 @@ jest.mock("../../../events", () => { auth: { login: jest.fn(), logout: jest.fn(), + SSOCreated: jest.fn(), + SSOUpdated: jest.fn(), + SSOActivated: jest.fn(), + SSODeactivated: jest.fn(), }, datasource: { created: jest.fn(), updated: jest.fn(), deleted: jest.fn(), }, + email: { + SMTPCreated: jest.fn(), + SMTPUpdated: jest.fn(), + }, } }) diff --git a/packages/worker/src/api/controllers/global/configs.js b/packages/worker/src/api/controllers/global/configs.js index 997615b34e..0b388edbad 100644 --- a/packages/worker/src/api/controllers/global/configs.js +++ b/packages/worker/src/api/controllers/global/configs.js @@ -14,72 +14,74 @@ const { const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy") const env = require("../../../environment") const { googleCallbackUrl, oidcCallbackUrl } = require("./auth") -// const { events } = require("@budibase/backend-core") +const { events } = require("@budibase/backend-core") const BB_TENANT_CDN = "https://tenants.cdn.budi.live" -// const getEventFns = (db, config) => { -// const fns = [] -// const isNew = !!config._id -// const type = config.type +const getEventFns = async (db, config) => { + const fns = [] + const type = config.type -// let existing -// if (!isNew) { -// existing = db.get(config._id) -// } -// if (!existing) { -// switch (config.type) { -// case Configs.SMTP: -// fns.push(events.emailSMTPCreated) -// break -// case Configs.GOOGLE: -// fns.push(() => events.authSSOCreated(type)) -// break -// case Configs.OIDC: -// fns.push(() => events.authSSOCreated(type)) -// break -// case Configs.SETTINGS: -// if (config.company) { -// fns.push(events.orgNameUpdated) -// } -// if (config.logoUrl) { -// fns.push(events.orgLogoUpdated) -// } -// if (config.platformUrl) { -// fns.push(events.orgPlatformURLUpdated) -// } -// break -// } -// } else { -// switch (config.type) { -// case Configs.SMTP: -// fns.push(events.emailSMTPUpdated) -// break -// case Configs.GOOGLE: -// fns.push(() => events.authSSOUpdated(type)) -// break -// case Configs.OIDC: -// fns.push(() => events.authSSOUpdated(type)) -// break -// case Configs.SETTINGS: -// if (config.company && existing.company !== config.company) { -// fns.push(events.orgNameUpdated) -// } -// if (config.logoUrl && existing.logoUrl !== config.logoUrl) { -// fns.push(events.orgLogoUpdated) -// } -// if (config.platformUrl && existing.platformUrl !== config.platformUrl) { -// fns.push(events.orgPlatformURLUpdated) -// } -// break -// } -// } -// } + let existing + if (config._id) { + existing = await db.get(config._id) + } + + if (!existing) { + switch (config.type) { + case Configs.SMTP: + fns.push(events.email.SMTPCreated) + break + case Configs.GOOGLE: + fns.push(() => events.auth.SSOCreated(type)) + break + case Configs.OIDC: + fns.push(() => events.auth.SSOCreated(type)) + break + case Configs.SETTINGS: + if (config.company) { + fns.push(events.org.nameUpdated) + } + if (config.logoUrl) { + fns.push(events.org.logoUpdated) + } + if (config.platformUrl) { + fns.push(events.org.platformURLUpdated) + } + break + } + } else { + switch (config.type) { + case Configs.SMTP: + fns.push(events.email.SMTPUpdated) + break + case Configs.GOOGLE: + fns.push(() => events.auth.SSOUpdated(type)) + break + case Configs.OIDC: + fns.push(() => events.auth.SSOUpdated(type)) + break + case Configs.SETTINGS: + if (config.company && existing.company !== config.company) { + fns.push(events.org.nameUpdated) + } + if (config.logoUrl && existing.logoUrl !== config.logoUrl) { + fns.push(events.org.logoUpdated) + } + if (config.platformUrl && existing.platformUrl !== config.platformUrl) { + fns.push(events.org.platformURLUpdated) + } + break + } + } + + return fns +} exports.save = async function (ctx) { const db = getGlobalDB() const { type, workspace, user, config } = ctx.request.body - // let eventFns = getEventFns(db, ctx.request.body) + let eventFns = await getEventFns(db, ctx.request.body) // Config does not exist yet if (!ctx.request.body._id) { ctx.request.body._id = generateConfigID({ @@ -101,9 +103,9 @@ exports.save = async function (ctx) { try { const response = await db.put(ctx.request.body) - // for (const fn of eventFns) { - // fn() - // } + for (const fn of eventFns) { + fn() + } ctx.body = { type, _id: response.id, diff --git a/packages/worker/src/api/routes/tests/configs.spec.js b/packages/worker/src/api/routes/tests/configs.spec.js index dfb1f49c58..76cb6d447e 100644 --- a/packages/worker/src/api/routes/tests/configs.spec.js +++ b/packages/worker/src/api/routes/tests/configs.spec.js @@ -2,8 +2,10 @@ jest.mock("nodemailer") const setup = require("./utilities") setup.emailMock() +const { Configs } = require("@budibase/backend-core/constants") +const { events } = require("@budibase/backend-core") -describe("/api/global/configs/checklist", () => { +describe("configs", () => { let request = setup.getRequest() let config = setup.getConfig() @@ -11,8 +13,73 @@ describe("/api/global/configs/checklist", () => { await config.init() }) + beforeEach(() => { + jest.clearAllMocks() + }) + afterAll(setup.afterAll) + describe("post /api/global/configs", () => { + + const saveConfig = async (type, _id, _rev) => { + const data = { + type, + _id, + _rev + } + const res = await request + .post(`/api/global/configs`) + .send(data) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + + return res.body + } + + describe("google", () => { + const saveGoogleConfig = async (_id, _rev) => { + return saveConfig(Configs.GOOGLE, _id, _rev) + } + + it ("should create google config", async () => { + await saveGoogleConfig() + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE) + await config.deleteConfig(Configs.GOOGLE) + }) + + it ("should update google config", async () => { + const googleConf = await saveGoogleConfig() + await saveGoogleConfig(googleConf._id, googleConf._rev) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE) + await config.deleteConfig(Configs.GOOGLE) + }) + }) + + describe("oidc", () => { + const saveOIDCConfig = async (_id, _rev) => { + return saveConfig(Configs.OIDC, _id, _rev) + } + + it ("should create OIDC config", async () => { + await saveOIDCConfig() + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC) + await config.deleteConfig(Configs.OIDC) + }) + + it ("should update OIDC config", async () => { + const oidcConf = await saveOIDCConfig() + await saveOIDCConfig(oidcConf._id, oidcConf._rev) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC) + await config.deleteConfig(Configs.OIDC) + }) + }) + }) + it("should return the correct checklist status based on the state of the budibase installation", async () => { await config.saveSmtpConfig()