Fix worker tests.

This commit is contained in:
Sam Rose 2024-08-07 17:33:47 +01:00
parent a9b4d0017f
commit d7e07bb44a
No known key found for this signature in database
6 changed files with 70 additions and 35 deletions

View File

@ -71,7 +71,7 @@ describe("feature flags", () => {
({ tenant, flags, expected }) =>
context.doInTenant(tenant, () =>
withEnv({ TENANT_FEATURE_FLAGS: flags }, () =>
expect(fetch()).rejects.toThrow(expected)
expect(fetch).rejects.toThrow(expected)
)
)
)
@ -159,5 +159,17 @@ describe("feature flags", () => {
expect(flags._TEST_NUMBER).toBe(123)
})
})
it("should not fail when a flag is not known", async () => {
mockFlags({
featureFlags: {
_SOME_RANDOM_FLAG: true,
},
})
await context.doInIdentityContext(identity, async () => {
await expect(fetch()).resolves.not.toThrow()
})
})
})
})

View File

@ -1,4 +1,4 @@
import { structures, mocks } from "../../../../../tests"
import { structures } from "../../../../../tests"
import { testEnv } from "../../../../../tests/extra"
import { SSOAuthDetails, User } from "@budibase/types"

View File

@ -292,9 +292,9 @@ describe("/api/global/auth", () => {
it("redirects to auth provider", async () => {
nock("http://someconfigurl").get("/").times(1).reply(200, {
issuer: "test",
authorization_endpoint: "http://localhost/auth",
token_endpoint: "http://localhost/token",
userinfo_endpoint: "http://localhost/userinfo",
authorization_endpoint: "http://example.com/auth",
token_endpoint: "http://example.com/token",
userinfo_endpoint: "http://example.com/userinfo",
})
const configId = await generateOidcConfig()
@ -305,7 +305,7 @@ describe("/api/global/auth", () => {
const location: string = res.get("location")
expect(
location.startsWith(
`http://localhost/auth?response_type=code&client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A10000%2Fapi%2Fglobal%2Fauth%2F${config.tenantId}%2Foidc%2Fcallback&scope=openid%20profile%20email%20offline_access`
`http://example.com/auth?response_type=code&client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A10000%2Fapi%2Fglobal%2Fauth%2F${config.tenantId}%2Foidc%2Fcallback&scope=openid%20profile%20email%20offline_access`
)
).toBe(true)
})
@ -313,11 +313,13 @@ describe("/api/global/auth", () => {
describe("GET /api/global/auth/:tenantId/oidc/callback", () => {
it("logs in", async () => {
const email = `${generator.guid()}@example.com`
nock("http://someconfigurl").get("/").times(2).reply(200, {
issuer: "test",
authorization_endpoint: "http://localhost/auth",
token_endpoint: "http://localhost/token",
userinfo_endpoint: "http://localhost/userinfo",
authorization_endpoint: "http://example.com/auth",
token_endpoint: "http://example.com/token",
userinfo_endpoint: "http://example.com/userinfo",
})
const token = jwt.sign(
@ -326,20 +328,20 @@ describe("/api/global/auth", () => {
sub: "sub",
aud: "clientId",
exp: Math.floor(Date.now() / 1000) + 60 * 60,
email: "oauth@example.com",
email,
},
"secret"
)
nock("http://localhost").post("/token").reply(200, {
nock("http://example.com").post("/token").reply(200, {
access_token: "access",
refresh_token: "refresh",
id_token: token,
})
nock("http://localhost").get("/userinfo?schema=openid").reply(200, {
nock("http://example.com").get("/userinfo?schema=openid").reply(200, {
sub: "sub",
email: "oauth@example.com",
email,
})
const configId = await generateOidcConfig()
@ -351,10 +353,7 @@ describe("/api/global/auth", () => {
)
}
expect(events.auth.login).toHaveBeenCalledWith(
"oidc",
"oauth@example.com"
)
expect(events.auth.login).toHaveBeenCalledWith("oidc", email)
expect(events.auth.login).toHaveBeenCalledTimes(1)
expect(res.status).toBe(302)
const location: string = res.get("location")

View File

@ -12,6 +12,33 @@ const nodemailer = require("nodemailer")
// for the real email tests give them a long time to try complete/fail
jest.setTimeout(30000)
function cancelableTimeout(timeout: number): [Promise<unknown>, () => void] {
let timeoutId: NodeJS.Timeout
return [
new Promise((resolve, reject) => {
timeoutId = setTimeout(() => {
reject({
status: 301,
errno: "ETIME",
})
}, timeout)
}),
() => {
clearTimeout(timeoutId)
},
]
}
async function withTimeout<T>(
timeout: number,
promise: Promise<T>
): Promise<T> {
const [timeoutPromise, cancel] = cancelableTimeout(timeout)
const result = (await Promise.race([promise, timeoutPromise])) as T
cancel()
return result
}
describe("/api/global/email", () => {
const config = new TestConfiguration()
@ -30,19 +57,8 @@ describe("/api/global/email", () => {
) {
let response, text
try {
const timeout = () =>
new Promise((resolve, reject) =>
setTimeout(
() =>
reject({
status: 301,
errno: "ETIME",
}),
20000
)
)
await Promise.race([config.saveEtherealSmtpConfig(), timeout()])
await Promise.race([config.saveSettingsConfig(), timeout()])
await withTimeout(20000, config.saveEtherealSmtpConfig())
await withTimeout(20000, config.saveSettingsConfig())
let res
if (attachments) {
res = await config.api.emails

View File

@ -40,7 +40,7 @@ export class ConfigAPI extends TestAPI {
const sessionContent = JSON.parse(
Buffer.from(koaSession, "base64").toString("utf-8")
)
const handle = sessionContent["openidconnect:localhost"].state.handle
const handle = sessionContent["openidconnect:example.com"].state.handle
return this.request
.get(`/api/global/auth/${this.config.getTenantId()}/oidc/callback`)
.query({ code: "test", state: handle })

View File

@ -1,13 +1,21 @@
import { mocks, testContainerUtils } from "@budibase/backend-core/tests"
import env from "../environment"
import { env as coreEnv, timers } from "@budibase/backend-core"
// must explicitly enable fetch mock
mocks.fetch.enable()
import nock from "nock"
// mock all dates to 2020-01-01T00:00:00.000Z
// use tk.reset() to use real dates in individual tests
const tk = require("timekeeper")
import tk from "timekeeper"
nock.disableNetConnect()
nock.enableNetConnect(host => {
return (
host.includes("localhost") ||
host.includes("127.0.0.1") ||
host.includes("::1") ||
host.includes("ethereal.email") // used in realEmail.spec.ts
)
})
tk.freeze(mocks.date.MOCK_DATE)