2022-02-01 10:05:14 +13:00
|
|
|
jest.mock("nodemailer")
|
2022-11-12 04:43:41 +13:00
|
|
|
import { TestConfiguration, mocks } from "../../../../tests"
|
2022-04-07 03:57:56 +12:00
|
|
|
const sendMailMock = mocks.email.mock()
|
2022-08-26 06:41:47 +12:00
|
|
|
import { events } from "@budibase/backend-core"
|
2021-05-06 04:49:34 +12:00
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
describe("/api/global/auth", () => {
|
2022-08-26 06:41:47 +12:00
|
|
|
const config = new TestConfiguration()
|
2021-05-06 04:49:34 +12:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-04-07 03:57:56 +12:00
|
|
|
await config.beforeAll()
|
2021-05-06 04:49:34 +12:00
|
|
|
})
|
|
|
|
|
2022-04-07 03:57:56 +12:00
|
|
|
afterAll(async () => {
|
|
|
|
await config.afterAll()
|
|
|
|
})
|
2021-05-06 04:49:34 +12:00
|
|
|
|
2021-07-12 04:54:18 +12:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2022-04-12 23:34:36 +12:00
|
|
|
it("should logout", async () => {
|
2022-11-12 04:43:41 +13:00
|
|
|
await config.api.auth.logout()
|
2022-04-13 02:37:49 +12:00
|
|
|
expect(events.auth.logout).toBeCalledTimes(1)
|
2022-04-12 23:34:36 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should be able to generate password reset email", async () => {
|
2022-11-12 04:43:41 +13:00
|
|
|
const { res, code } = await config.api.auth.requestPasswordReset(
|
|
|
|
sendMailMock
|
|
|
|
)
|
2022-04-12 23:34:36 +12:00
|
|
|
const user = await config.getUser("test@test.com")
|
|
|
|
|
2022-08-26 06:41:47 +12:00
|
|
|
expect(res.body).toEqual({
|
|
|
|
message: "Please check your email for a reset link.",
|
|
|
|
})
|
2022-04-12 23:34:36 +12:00
|
|
|
expect(sendMailMock).toHaveBeenCalled()
|
2022-08-26 06:41:47 +12:00
|
|
|
|
2021-05-06 04:49:34 +12:00
|
|
|
expect(code).toBeDefined()
|
2022-04-12 23:34:36 +12:00
|
|
|
expect(events.user.passwordResetRequested).toBeCalledTimes(1)
|
|
|
|
expect(events.user.passwordResetRequested).toBeCalledWith(user)
|
2021-05-06 04:49:34 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow resetting user password with code", async () => {
|
2022-11-12 04:43:41 +13:00
|
|
|
const { code } = await config.api.auth.requestPasswordReset(sendMailMock)
|
2022-04-12 23:34:36 +12:00
|
|
|
const user = await config.getUser("test@test.com")
|
2022-08-26 06:41:47 +12:00
|
|
|
delete user.password
|
|
|
|
|
2022-11-12 04:43:41 +13:00
|
|
|
const res = await config.api.auth.updatePassword(code)
|
2022-04-12 23:34:36 +12:00
|
|
|
|
2021-05-06 04:49:34 +12:00
|
|
|
expect(res.body).toEqual({ message: "password reset successfully." })
|
2022-04-12 23:34:36 +12:00
|
|
|
expect(events.user.passwordReset).toBeCalledTimes(1)
|
|
|
|
expect(events.user.passwordReset).toBeCalledWith(user)
|
2021-05-06 04:49:34 +12:00
|
|
|
})
|
2021-07-12 04:54:18 +12:00
|
|
|
|
|
|
|
describe("oidc", () => {
|
2022-01-13 00:32:14 +13:00
|
|
|
const auth = require("@budibase/backend-core/auth")
|
2021-07-12 04:54:18 +12:00
|
|
|
|
2022-07-04 20:04:55 +12:00
|
|
|
const passportSpy = jest.spyOn(auth.passport, "authenticate")
|
|
|
|
let oidcConf
|
2022-08-26 06:41:47 +12:00
|
|
|
let chosenConfig: any
|
|
|
|
let configId: string
|
2022-07-04 20:04:55 +12:00
|
|
|
|
2021-07-12 04:54:18 +12:00
|
|
|
// mock the oidc strategy implementation and return value
|
2022-06-11 09:57:12 +12:00
|
|
|
let strategyFactory = jest.fn()
|
|
|
|
let mockStrategyReturn = jest.fn()
|
2022-07-04 20:04:55 +12:00
|
|
|
let mockStrategyConfig = jest.fn()
|
|
|
|
auth.oidc.fetchStrategyConfig = mockStrategyConfig
|
2022-08-26 06:41:47 +12:00
|
|
|
|
2021-07-12 04:54:18 +12:00
|
|
|
strategyFactory.mockReturnValue(mockStrategyReturn)
|
|
|
|
auth.oidc.strategyFactory = strategyFactory
|
|
|
|
|
2021-07-13 21:28:15 +12:00
|
|
|
beforeEach(async () => {
|
|
|
|
oidcConf = await config.saveOIDCConfig()
|
2021-07-14 04:07:48 +12:00
|
|
|
chosenConfig = oidcConf.config.configs[0]
|
|
|
|
configId = chosenConfig.uuid
|
2022-07-04 20:04:55 +12:00
|
|
|
mockStrategyConfig.mockReturnValue(chosenConfig)
|
2021-07-13 21:28:15 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-08-26 06:41:47 +12:00
|
|
|
expect(strategyFactory).toBeCalledWith(chosenConfig, expect.any(Function))
|
2021-07-13 21:28:15 +12:00
|
|
|
})
|
2021-07-12 04:54:18 +12:00
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
describe("oidc configs", () => {
|
2021-07-13 21:28:15 +12:00
|
|
|
it("should load strategy and delegate to passport", async () => {
|
2022-11-12 04:43:41 +13:00
|
|
|
await config.api.configs.getOIDCConfig(configId)
|
2021-07-12 04:54:18 +12:00
|
|
|
|
|
|
|
expect(passportSpy).toBeCalledWith(mockStrategyReturn, {
|
2022-08-26 06:41:47 +12:00
|
|
|
scope: ["profile", "email", "offline_access"],
|
2021-07-12 04:54:18 +12:00
|
|
|
})
|
2022-08-26 06:41:47 +12:00
|
|
|
expect(passportSpy.mock.calls.length).toBe(1)
|
2021-07-12 04:54:18 +12:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
describe("oidc callback", () => {
|
2021-07-13 21:28:15 +12:00
|
|
|
it("should load strategy and delegate to passport", async () => {
|
2022-11-12 04:43:41 +13:00
|
|
|
await config.api.configs.OIDCCallback(configId)
|
2022-08-26 06:41:47 +12:00
|
|
|
|
|
|
|
expect(passportSpy).toBeCalledWith(
|
|
|
|
mockStrategyReturn,
|
|
|
|
{
|
|
|
|
successRedirect: "/",
|
|
|
|
failureRedirect: "/error",
|
|
|
|
},
|
|
|
|
expect.anything()
|
|
|
|
)
|
|
|
|
expect(passportSpy.mock.calls.length).toBe(1)
|
2021-07-12 04:54:18 +12:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2022-01-13 00:32:14 +13:00
|
|
|
})
|