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: { auth: {
login: jest.fn(), login: jest.fn(),
logout: jest.fn(), logout: jest.fn(),
SSOCreated: jest.fn(),
SSOUpdated: jest.fn(),
SSOActivated: jest.fn(),
SSODeactivated: jest.fn(),
}, },
datasource: { datasource: {
created: jest.fn(), created: jest.fn(),
updated: jest.fn(), updated: jest.fn(),
deleted: 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 { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
const env = require("../../../environment") const env = require("../../../environment")
const { googleCallbackUrl, oidcCallbackUrl } = require("./auth") 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 BB_TENANT_CDN = "https://tenants.cdn.budi.live"
// const getEventFns = (db, config) => { const getEventFns = async (db, config) => {
// const fns = [] const fns = []
// const isNew = !!config._id const type = config.type
// const type = config.type
// let existing let existing
// if (!isNew) { if (config._id) {
// existing = db.get(config._id) existing = await db.get(config._id)
// } }
// if (!existing) {
// switch (config.type) { if (!existing) {
// case Configs.SMTP: switch (config.type) {
// fns.push(events.emailSMTPCreated) case Configs.SMTP:
// break fns.push(events.email.SMTPCreated)
// case Configs.GOOGLE: break
// fns.push(() => events.authSSOCreated(type)) case Configs.GOOGLE:
// break fns.push(() => events.auth.SSOCreated(type))
// case Configs.OIDC: break
// fns.push(() => events.authSSOCreated(type)) case Configs.OIDC:
// break fns.push(() => events.auth.SSOCreated(type))
// case Configs.SETTINGS: break
// if (config.company) { case Configs.SETTINGS:
// fns.push(events.orgNameUpdated) if (config.company) {
// } fns.push(events.org.nameUpdated)
// if (config.logoUrl) { }
// fns.push(events.orgLogoUpdated) if (config.logoUrl) {
// } fns.push(events.org.logoUpdated)
// if (config.platformUrl) { }
// fns.push(events.orgPlatformURLUpdated) if (config.platformUrl) {
// } fns.push(events.org.platformURLUpdated)
// break }
// } break
// } else { }
// switch (config.type) { } else {
// case Configs.SMTP: switch (config.type) {
// fns.push(events.emailSMTPUpdated) case Configs.SMTP:
// break fns.push(events.email.SMTPUpdated)
// case Configs.GOOGLE: break
// fns.push(() => events.authSSOUpdated(type)) case Configs.GOOGLE:
// break fns.push(() => events.auth.SSOUpdated(type))
// case Configs.OIDC: break
// fns.push(() => events.authSSOUpdated(type)) case Configs.OIDC:
// break fns.push(() => events.auth.SSOUpdated(type))
// case Configs.SETTINGS: break
// if (config.company && existing.company !== config.company) { case Configs.SETTINGS:
// fns.push(events.orgNameUpdated) if (config.company && existing.company !== config.company) {
// } fns.push(events.org.nameUpdated)
// if (config.logoUrl && existing.logoUrl !== config.logoUrl) { }
// fns.push(events.orgLogoUpdated) if (config.logoUrl && existing.logoUrl !== config.logoUrl) {
// } fns.push(events.org.logoUpdated)
// if (config.platformUrl && existing.platformUrl !== config.platformUrl) { }
// fns.push(events.orgPlatformURLUpdated) if (config.platformUrl && existing.platformUrl !== config.platformUrl) {
// } fns.push(events.org.platformURLUpdated)
// break }
// } break
// } }
// } }
return fns
}
exports.save = async function (ctx) { exports.save = async function (ctx) {
const db = getGlobalDB() const db = getGlobalDB()
const { type, workspace, user, config } = ctx.request.body 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 // Config does not exist yet
if (!ctx.request.body._id) { if (!ctx.request.body._id) {
ctx.request.body._id = generateConfigID({ ctx.request.body._id = generateConfigID({
@ -101,9 +103,9 @@ exports.save = async function (ctx) {
try { try {
const response = await db.put(ctx.request.body) const response = await db.put(ctx.request.body)
// for (const fn of eventFns) { for (const fn of eventFns) {
// fn() fn()
// } }
ctx.body = { ctx.body = {
type, type,
_id: response.id, _id: response.id,

View File

@ -2,8 +2,10 @@
jest.mock("nodemailer") jest.mock("nodemailer")
const setup = require("./utilities") const setup = require("./utilities")
setup.emailMock() 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 request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()
@ -11,8 +13,73 @@ describe("/api/global/configs/checklist", () => {
await config.init() await config.init()
}) })
beforeEach(() => {
jest.clearAllMocks()
})
afterAll(setup.afterAll) 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 () => { it("should return the correct checklist status based on the state of the budibase installation", async () => {
await config.saveSmtpConfig() await config.saveSmtpConfig()