Unit test auth controller oidc functions

This commit is contained in:
Rory Powell 2021-07-11 17:54:18 +01:00
parent 2e55e03571
commit e1bea33d9a
2 changed files with 66 additions and 0 deletions

View File

@ -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);
})
})
})
})

View File

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