google/oidc created/deleted events + tests

This commit is contained in:
Rory Powell 2022-04-05 23:14:53 +01:00
parent 5b8ea1cdbe
commit fde16cf548
3 changed files with 139 additions and 62 deletions

View File

@ -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(),
},
}
})

View File

@ -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,

View File

@ -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()