From 9ebdf837fca5bce3c98bea83710bdde225ef9368 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Wed, 6 Apr 2022 00:54:07 +0100 Subject: [PATCH] sso activated/deactivated events + tests --- .../backend-core/src/events/handlers/auth.js | 4 - .../src/api/controllers/global/configs.js | 22 +++ .../src/api/routes/tests/configs.spec.js | 155 ++++++++++++++---- 3 files changed, 145 insertions(+), 36 deletions(-) diff --git a/packages/backend-core/src/events/handlers/auth.js b/packages/backend-core/src/events/handlers/auth.js index 41b3a7535b..98bbbd2994 100644 --- a/packages/backend-core/src/events/handlers/auth.js +++ b/packages/backend-core/src/events/handlers/auth.js @@ -13,7 +13,6 @@ exports.logout = () => { events.processEvent(Events.AUTH_LOGOUT, properties) } -// TODO exports.SSOCreated = type => { const properties = { type, @@ -21,7 +20,6 @@ exports.SSOCreated = type => { events.processEvent(Events.AUTH_SSO_CREATED, properties) } -// TODO exports.SSOUpdated = type => { const properties = { type, @@ -29,7 +27,6 @@ exports.SSOUpdated = type => { events.processEvent(Events.AUTH_SSO_UPDATED, properties) } -// TODO exports.SSOActivated = type => { const properties = { type, @@ -37,7 +34,6 @@ exports.SSOActivated = type => { events.processEvent(Events.AUTH_SSO_ACTIVATED, properties) } -// TODO exports.SSODeactivated = type => { const properties = { type, diff --git a/packages/worker/src/api/controllers/global/configs.js b/packages/worker/src/api/controllers/global/configs.js index 0b388edbad..9ee2648d89 100644 --- a/packages/worker/src/api/controllers/global/configs.js +++ b/packages/worker/src/api/controllers/global/configs.js @@ -34,9 +34,15 @@ const getEventFns = async (db, config) => { break case Configs.GOOGLE: fns.push(() => events.auth.SSOCreated(type)) + if (config.config.activated) { + fns.push(() => events.auth.SSOActivated(type)) + } break 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) { @@ -57,9 +63,25 @@ const getEventFns = async (db, config) => { break case Configs.GOOGLE: fns.push(() => events.auth.SSOUpdated(type)) + if (!existing.config.activated && config.config.activated) { + fns.push(() => events.auth.SSOActivated(type)) + } else if (existing.config.activated && !config.config.activated) { + fns.push(() => events.auth.SSODeactivated(type)) + } break case Configs.OIDC: fns.push(() => events.auth.SSOUpdated(type)) + if ( + !existing.config.configs[0].activated && + config.config.configs[0].activated + ) { + fns.push(() => events.auth.SSOActivated(type)) + } else if ( + existing.config.configs[0].activated && + !config.config.configs[0].activated + ) { + fns.push(() => events.auth.SSODeactivated(type)) + } break case Configs.SETTINGS: if (config.company && existing.company !== config.company) { diff --git a/packages/worker/src/api/routes/tests/configs.spec.js b/packages/worker/src/api/routes/tests/configs.spec.js index 76cb6d447e..a89e394fdb 100644 --- a/packages/worker/src/api/routes/tests/configs.spec.js +++ b/packages/worker/src/api/routes/tests/configs.spec.js @@ -17,16 +17,20 @@ describe("configs", () => { jest.clearAllMocks() }) - afterAll(setup.afterAll) + afterAll(async () => { + await setup.afterAll() + }) describe("post /api/global/configs", () => { - const saveConfig = async (type, _id, _rev) => { + const saveConfig = async (conf, type, _id, _rev) => { const data = { type, + config: conf, _id, _rev } + const res = await request .post(`/api/global/configs`) .send(data) @@ -34,49 +38,136 @@ describe("configs", () => { .expect("Content-Type", /json/) .expect(200) - return res.body + return { + ...data, + ...res.body + } } describe("google", () => { - const saveGoogleConfig = async (_id, _rev) => { - return saveConfig(Configs.GOOGLE, _id, _rev) + const saveGoogleConfig = async (conf, _id, _rev) => { + const googleConfig = { + clientID: "clientID", + clientSecret: "clientSecret", + activated: true, + ...conf + } + + return saveConfig(googleConfig, 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("create", () => { + it ("should create activated google config", async () => { + await saveGoogleConfig() + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE) + expect(events.auth.SSODeactivated).not.toBeCalled() + expect(events.auth.SSOActivated).toBeCalledTimes(1) + expect(events.auth.SSOActivated).toBeCalledWith(Configs.GOOGLE) + await config.deleteConfig(Configs.GOOGLE) + }) + + it ("should create deactivated google config", async () => { + await saveGoogleConfig({ activated: false }) + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE) + expect(events.auth.SSOActivated).not.toBeCalled() + expect(events.auth.SSODeactivated).not.toBeCalled() + await config.deleteConfig(Configs.GOOGLE) + }) }) + + describe("update", () => { + it ("should update google config to deactivated", async () => { + const googleConf = await saveGoogleConfig() + jest.clearAllMocks() + await saveGoogleConfig({ ...googleConf.config, activated: false }, googleConf._id, googleConf._rev) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE) + expect(events.auth.SSOActivated).not.toBeCalled() + expect(events.auth.SSODeactivated).toBeCalledTimes(1) + expect(events.auth.SSODeactivated).toBeCalledWith(Configs.GOOGLE) + await config.deleteConfig(Configs.GOOGLE) + }) + + it ("should update google config to activated", async () => { + const googleConf = await saveGoogleConfig({ activated: false }) + jest.clearAllMocks() + await saveGoogleConfig({ ...googleConf.config, activated: true}, googleConf._id, googleConf._rev) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE) + expect(events.auth.SSODeactivated).not.toBeCalled() + expect(events.auth.SSOActivated).toBeCalledTimes(1) + expect(events.auth.SSOActivated).toBeCalledWith(Configs.GOOGLE) + await config.deleteConfig(Configs.GOOGLE) + }) + }) }) describe("oidc", () => { - const saveOIDCConfig = async (_id, _rev) => { - return saveConfig(Configs.OIDC, _id, _rev) + const saveOIDCConfig = async (conf, _id, _rev) => { + const oidcConfig = { + configs: [{ + clientID: "clientID", + clientSecret: "clientSecret", + configUrl: "http://example.com", + logo: "logo", + name: "oidc", + uuid: "uuid", + activated: true, + ...conf + }] + } + return saveConfig(oidcConfig, 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) + describe("create", () => { + it ("should create activated OIDC config", async () => { + await saveOIDCConfig() + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC) + expect(events.auth.SSODeactivated).not.toBeCalled() + expect(events.auth.SSOActivated).toBeCalledTimes(1) + expect(events.auth.SSOActivated).toBeCalledWith(Configs.OIDC) + await config.deleteConfig(Configs.OIDC) + }) + + it ("should create deactivated OIDC config", async () => { + await saveOIDCConfig({ activated: false }) + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC) + expect(events.auth.SSOActivated).not.toBeCalled() + expect(events.auth.SSODeactivated).not.toBeCalled() + 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) + + describe("update", () => { + it ("should update OIDC config to deactivated", async () => { + const oidcConf = await saveOIDCConfig() + jest.clearAllMocks() + await saveOIDCConfig({ ...oidcConf.config.configs[0], activated: false }, oidcConf._id, oidcConf._rev) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC) + expect(events.auth.SSOActivated).not.toBeCalled() + expect(events.auth.SSODeactivated).toBeCalledTimes(1) + expect(events.auth.SSODeactivated).toBeCalledWith(Configs.OIDC) + await config.deleteConfig(Configs.OIDC) + }) + + it ("should update google config to activated", async () => { + const oidcConf = await saveOIDCConfig({ activated: false }) + jest.clearAllMocks() + await saveOIDCConfig({ ...oidcConf.config.configs[0], activated: true}, oidcConf._id, oidcConf._rev) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC) + expect(events.auth.SSODeactivated).not.toBeCalled() + expect(events.auth.SSOActivated).toBeCalledTimes(1) + expect(events.auth.SSOActivated).toBeCalledWith(Configs.OIDC) + await config.deleteConfig(Configs.OIDC) + }) }) + }) })