From 4d3f44f9825ebb01ed4816a5663fb47214773105 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Sun, 11 Jul 2021 17:54:18 +0100 Subject: [PATCH] Unit test auth controller oidc functions --- .../worker/src/api/routes/tests/auth.spec.js | 51 +++++++++++++++++++ .../tests/utilities/TestConfiguration.js | 15 ++++++ 2 files changed, 66 insertions(+) diff --git a/packages/worker/src/api/routes/tests/auth.spec.js b/packages/worker/src/api/routes/tests/auth.spec.js index 8b9b699839..5cfcec5373 100644 --- a/packages/worker/src/api/routes/tests/auth.spec.js +++ b/packages/worker/src/api/routes/tests/auth.spec.js @@ -14,6 +14,10 @@ describe("/api/admin/auth", () => { afterAll(setup.afterAll) + afterEach(() => { + jest.clearAllMocks() + }) + it("should be able to generate password reset email", async () => { // initially configure settings await config.saveSmtpConfig() @@ -46,4 +50,51 @@ describe("/api/admin/auth", () => { .expect(200) expect(res.body).toEqual({ message: "password reset successfully." }) }) + + describe("oidc", () => { + const auth = require("@budibase/auth").auth + + // mock the oidc strategy implementation and return value + strategyFactory = jest.fn() + mockStrategyReturn = jest.fn() + strategyFactory.mockReturnValue(mockStrategyReturn) + auth.oidc.strategyFactory = strategyFactory + + const passportSpy = jest.spyOn(auth.passport, "authenticate") + + describe("/api/admin/auth/oidc", () => { + it("should load the oidc config and calculate the correct callback url", async () => { + const oidcConf = await config.saveOIDCConfig() + + await request.get(`/api/admin/auth/oidc`) + + expect(strategyFactory).toBeCalledWith( + oidcConf.config, + "http://127.0.0.1:4003/api/admin/auth/oidc/callback" // calculated url + ) + + expect(passportSpy).toBeCalledWith(mockStrategyReturn, { + scope: ["profile", "email"], + }) + + expect(passportSpy.mock.calls.length).toBe(1); + }) + }) + + describe("/api/admin/auth/oidc/callback", () => { + it("should do something", async () => { + const oidcConf = await config.saveOIDCConfig() + const passportSpy = jest.spyOn(auth.passport, "authenticate") + + await request.get(`/api/admin/auth/oidc/callback`) + + expect(passportSpy).toBeCalledWith(mockStrategyReturn, { + successRedirect: "/", failureRedirect: "/error" + }, expect.anything()) + + expect(passportSpy.mock.calls.length).toBe(1); + }) + }) + + }) }) \ No newline at end of file diff --git a/packages/worker/src/api/routes/tests/utilities/TestConfiguration.js b/packages/worker/src/api/routes/tests/utilities/TestConfiguration.js index c205a45e38..863e9fd6bc 100644 --- a/packages/worker/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/worker/src/api/routes/tests/utilities/TestConfiguration.js @@ -155,6 +155,21 @@ class TestConfiguration { ) } + async saveOIDCConfig() { + await this.deleteConfig(Configs.OIDC) + const config = { + type: Configs.OIDC, + config: { + configUrl: "http://someconfigurl", + clientID: "clientId", + clientSecret: "clientSecret", + }, + } + + await this._req(config, null, controllers.config.save) + return config + } + async saveSmtpConfig() { await this.deleteConfig(Configs.SMTP) await this._req(