2021-05-06 04:49:34 +12:00
|
|
|
const setup = require("./utilities")
|
2021-08-04 21:02:24 +12:00
|
|
|
const { Cookies } = require("@budibase/auth").constants
|
2021-05-06 04:49:34 +12:00
|
|
|
|
|
|
|
jest.mock("nodemailer")
|
|
|
|
const sendMailMock = setup.emailMock()
|
|
|
|
|
2021-08-04 21:02:24 +12:00
|
|
|
describe("/api/admin/auth", () => {
|
2021-05-06 04:49:34 +12:00
|
|
|
let request = setup.getRequest()
|
|
|
|
let config = setup.getConfig()
|
|
|
|
let code
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await config.init()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(setup.afterAll)
|
|
|
|
|
2021-07-12 04:54:18 +12:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2021-05-06 04:49:34 +12:00
|
|
|
it("should be able to generate password reset email", async () => {
|
|
|
|
// initially configure settings
|
|
|
|
await config.saveSmtpConfig()
|
|
|
|
await config.saveSettingsConfig()
|
|
|
|
await config.createUser("test@test.com")
|
|
|
|
const res = await request
|
2021-08-04 21:02:24 +12:00
|
|
|
.post(`/api/admin/auth/reset`)
|
2021-05-06 04:49:34 +12:00
|
|
|
.send({
|
|
|
|
email: "test@test.com",
|
|
|
|
})
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(200)
|
|
|
|
expect(res.body).toEqual({ message: "Please check your email for a reset link." })
|
|
|
|
expect(sendMailMock).toHaveBeenCalled()
|
|
|
|
const emailCall = sendMailMock.mock.calls[0][0]
|
|
|
|
// after this URL there should be a code
|
2021-08-04 21:02:24 +12:00
|
|
|
const parts = emailCall.html.split("http://localhost:10000/builder/auth/reset?code=")
|
|
|
|
code = parts[1].split("\"")[0]
|
2021-05-06 04:49:34 +12:00
|
|
|
expect(code).toBeDefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should allow resetting user password with code", async () => {
|
|
|
|
const res = await request
|
2021-08-04 21:02:24 +12:00
|
|
|
.post(`/api/admin/auth/reset/update`)
|
2021-05-06 04:49:34 +12:00
|
|
|
.send({
|
|
|
|
password: "newpassword",
|
|
|
|
resetCode: code,
|
|
|
|
})
|
|
|
|
.expect("Content-Type", /json/)
|
|
|
|
.expect(200)
|
|
|
|
expect(res.body).toEqual({ message: "password reset successfully." })
|
|
|
|
})
|
2021-07-12 04:54:18 +12:00
|
|
|
|
|
|
|
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")
|
2021-07-13 21:28:15 +12:00
|
|
|
let oidcConf
|
2021-07-14 04:07:48 +12:00
|
|
|
let chosenConfig
|
|
|
|
let configId
|
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
|
2021-07-13 21:28:15 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
expect(strategyFactory).toBeCalledWith(
|
2021-07-14 04:07:48 +12:00
|
|
|
chosenConfig,
|
2021-08-04 21:02:24 +12:00
|
|
|
`http://127.0.0.1:4003/api/admin/auth/oidc/callback` // calculated url
|
2021-07-13 21:28:15 +12:00
|
|
|
)
|
|
|
|
})
|
2021-07-12 04:54:18 +12:00
|
|
|
|
2021-08-04 21:02:24 +12:00
|
|
|
describe("/api/admin/auth/oidc/configs", () => {
|
2021-07-13 21:28:15 +12:00
|
|
|
it("should load strategy and delegate to passport", async () => {
|
2021-08-04 21:02:24 +12:00
|
|
|
await request.get(`/api/admin/auth/oidc/configs/${configId}`)
|
2021-07-12 04:54:18 +12:00
|
|
|
|
|
|
|
expect(passportSpy).toBeCalledWith(mockStrategyReturn, {
|
|
|
|
scope: ["profile", "email"],
|
|
|
|
})
|
|
|
|
expect(passportSpy.mock.calls.length).toBe(1);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-08-04 21:02:24 +12:00
|
|
|
describe("/api/admin/auth/oidc/callback", () => {
|
2021-07-13 21:28:15 +12:00
|
|
|
it("should load strategy and delegate to passport", async () => {
|
2021-08-04 21:02:24 +12:00
|
|
|
await request.get(`/api/admin/auth/oidc/callback`)
|
2021-07-16 03:20:31 +12:00
|
|
|
.set(config.getOIDConfigCookie(configId))
|
2021-07-12 04:54:18 +12:00
|
|
|
|
|
|
|
expect(passportSpy).toBeCalledWith(mockStrategyReturn, {
|
|
|
|
successRedirect: "/", failureRedirect: "/error"
|
|
|
|
}, expect.anything())
|
|
|
|
expect(passportSpy.mock.calls.length).toBe(1);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2021-05-06 04:49:34 +12:00
|
|
|
})
|