diff --git a/packages/worker/__mocks__/node-fetch.ts b/packages/worker/__mocks__/node-fetch.ts new file mode 100644 index 0000000000..113833a0b6 --- /dev/null +++ b/packages/worker/__mocks__/node-fetch.ts @@ -0,0 +1,23 @@ +import fs from "fs" +module FetchMock { + const fetch = jest.requireActual("node-fetch") + + const func = async (url: any, opts: any) => { + if (url.includes("http://someconfigurl")) { + return { + ok: true, + json: () => ({ + issuer: "test", + authorization_endpoint: "http://localhost/auth", + token_endpoint: "http://localhost/token", + userinfo_endpoint: "http://localhost/userinfo", + }), + } + } + return fetch(url, opts) + } + + func.Headers = fetch.Headers + + module.exports = func +} diff --git a/packages/worker/jest-testcontainers-config.js b/packages/worker/jest-testcontainers-config.js new file mode 100644 index 0000000000..8a19389492 --- /dev/null +++ b/packages/worker/jest-testcontainers-config.js @@ -0,0 +1,8 @@ +const { join } = require("path") +const { parsed: env } = require("dotenv").config({ + path: join(__dirname, "..", "..", "hosting", ".env"), +}) + +const jestTestcontainersConfigGenerator = require("../../jestTestcontainersConfigGenerator") + +module.exports = jestTestcontainersConfigGenerator(env) diff --git a/packages/worker/jest.config.ts b/packages/worker/jest.config.ts index 85f554f8da..8b0514211b 100644 --- a/packages/worker/jest.config.ts +++ b/packages/worker/jest.config.ts @@ -1,8 +1,10 @@ import { Config } from "@jest/types" import * as fs from "fs" +const preset = require("ts-jest/jest-preset") const config: Config.InitialOptions = { - testEnvironment: "node", + ...preset, + preset: "@trendyol/jest-testcontainers", setupFiles: ["./src/tests/jestEnv.ts"], setupFilesAfterEnv: ["./src/tests/jestSetup.ts"], collectCoverageFrom: ["src/**/*.{js,ts}"], diff --git a/packages/worker/src/api/routes/global/tests/auth.spec.ts b/packages/worker/src/api/routes/global/tests/auth.spec.ts index 0d47857ac1..d58d56ac87 100644 --- a/packages/worker/src/api/routes/global/tests/auth.spec.ts +++ b/packages/worker/src/api/routes/global/tests/auth.spec.ts @@ -1,7 +1,7 @@ jest.mock("nodemailer") import { TestConfiguration, mocks } from "../../../../tests" const sendMailMock = mocks.email.mock() -import { events } from "@budibase/backend-core" +import { events, tenancy, utils } from "@budibase/backend-core" const expectSetAuthCookie = (res: any) => { expect( @@ -40,36 +40,43 @@ describe("/api/global/auth", () => { describe("POST /api/global/auth/:tenantId/reset", () => { it("should generate password reset email", async () => { - const { res, code } = await config.api.auth.requestPasswordReset( - sendMailMock - ) - const user = await config.getUser("test@test.com") + await tenancy.doInTenant(config.tenant1User!.tenantId, async () => { + const userEmail = `${utils.newid()}@test.com` + const { res, code } = await config.api.auth.requestPasswordReset( + sendMailMock, + userEmail + ) + const user = await config.getUser(userEmail) - expect(res.body).toEqual({ - message: "Please check your email for a reset link.", + expect(res.body).toEqual({ + message: "Please check your email for a reset link.", + }) + expect(sendMailMock).toHaveBeenCalled() + + expect(code).toBeDefined() + expect(events.user.passwordResetRequested).toBeCalledTimes(1) + expect(events.user.passwordResetRequested).toBeCalledWith(user) }) - expect(sendMailMock).toHaveBeenCalled() - - expect(code).toBeDefined() - expect(events.user.passwordResetRequested).toBeCalledTimes(1) - expect(events.user.passwordResetRequested).toBeCalledWith(user) }) }) describe("POST /api/global/auth/:tenantId/reset/update", () => { it("should reset password", async () => { - const { code } = await config.api.auth.requestPasswordReset( - sendMailMock - ) - const user = await config.getUser("test@test.com") - delete user.password + await tenancy.doInTenant(config.tenant1User!.tenantId, async () => { + const userEmail = `${utils.newid()}@test.com` + const { code } = await config.api.auth.requestPasswordReset( + sendMailMock, + userEmail + ) + const user = await config.getUser(userEmail) + delete user.password - const res = await config.api.auth.updatePassword(code) - - expect(res.body).toEqual({ message: "password reset successfully." }) - expect(events.user.passwordReset).toBeCalledTimes(1) - expect(events.user.passwordReset).toBeCalledWith(user) + const res = await config.api.auth.updatePassword(code) + expect(res.body).toEqual({ message: "password reset successfully." }) + expect(events.user.passwordReset).toBeCalledTimes(1) + expect(events.user.passwordReset).toBeCalledWith(user) + }) // TODO: Login using new password }) }) diff --git a/packages/worker/src/api/routes/global/tests/configs.spec.ts b/packages/worker/src/api/routes/global/tests/configs.spec.ts index ed457e7bcd..f25fc01699 100644 --- a/packages/worker/src/api/routes/global/tests/configs.spec.ts +++ b/packages/worker/src/api/routes/global/tests/configs.spec.ts @@ -2,7 +2,7 @@ jest.mock("nodemailer") import { TestConfiguration, structures, mocks } from "../../../../tests" mocks.email.mock() -import { Config, events } from "@budibase/backend-core" +import { Config, context, events } from "@budibase/backend-core" describe("configs", () => { const config = new TestConfiguration() @@ -113,56 +113,64 @@ describe("configs", () => { describe("create", () => { it("should create activated OIDC config", async () => { - await saveOIDCConfig() - expect(events.auth.SSOCreated).toBeCalledTimes(1) - expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC) - expect(events.auth.SSODeactivated).not.toBeCalled() - expect(events.auth.SSOActivated).toBeCalledTimes(1) - expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC) - await config.deleteConfig(Config.OIDC) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + await saveOIDCConfig() + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC) + expect(events.auth.SSODeactivated).not.toBeCalled() + expect(events.auth.SSOActivated).toBeCalledTimes(1) + expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC) + await config.deleteConfig(Config.OIDC) + }) }) it("should create deactivated OIDC config", async () => { - await saveOIDCConfig({ activated: false }) - expect(events.auth.SSOCreated).toBeCalledTimes(1) - expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC) - expect(events.auth.SSOActivated).not.toBeCalled() - expect(events.auth.SSODeactivated).not.toBeCalled() - await config.deleteConfig(Config.OIDC) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + await saveOIDCConfig({ activated: false }) + expect(events.auth.SSOCreated).toBeCalledTimes(1) + expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC) + expect(events.auth.SSOActivated).not.toBeCalled() + expect(events.auth.SSODeactivated).not.toBeCalled() + await config.deleteConfig(Config.OIDC) + }) }) }) describe("update", () => { it("should update OIDC config to deactivated", async () => { - const oidcConf = await saveOIDCConfig() - jest.clearAllMocks() - await saveOIDCConfig( - { ...oidcConf.config.configs[0], activated: false }, - oidcConf._id, - oidcConf._rev - ) - expect(events.auth.SSOUpdated).toBeCalledTimes(1) - expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC) - expect(events.auth.SSOActivated).not.toBeCalled() - expect(events.auth.SSODeactivated).toBeCalledTimes(1) - expect(events.auth.SSODeactivated).toBeCalledWith(Config.OIDC) - await config.deleteConfig(Config.OIDC) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + const oidcConf = await saveOIDCConfig() + jest.clearAllMocks() + await saveOIDCConfig( + { ...oidcConf.config.configs[0], activated: false }, + oidcConf._id, + oidcConf._rev + ) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC) + expect(events.auth.SSOActivated).not.toBeCalled() + expect(events.auth.SSODeactivated).toBeCalledTimes(1) + expect(events.auth.SSODeactivated).toBeCalledWith(Config.OIDC) + await config.deleteConfig(Config.OIDC) + }) }) it("should update OIDC config to activated", async () => { - const oidcConf = await saveOIDCConfig({ activated: false }) - jest.clearAllMocks() - await saveOIDCConfig( - { ...oidcConf.config.configs[0], activated: true }, - oidcConf._id, - oidcConf._rev - ) - expect(events.auth.SSOUpdated).toBeCalledTimes(1) - expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC) - expect(events.auth.SSODeactivated).not.toBeCalled() - expect(events.auth.SSOActivated).toBeCalledTimes(1) - expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC) - await config.deleteConfig(Config.OIDC) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + const oidcConf = await saveOIDCConfig({ activated: false }) + jest.clearAllMocks() + await saveOIDCConfig( + { ...oidcConf.config.configs[0], activated: true }, + oidcConf._id, + oidcConf._rev + ) + expect(events.auth.SSOUpdated).toBeCalledTimes(1) + expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC) + expect(events.auth.SSODeactivated).not.toBeCalled() + expect(events.auth.SSOActivated).toBeCalledTimes(1) + expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC) + await config.deleteConfig(Config.OIDC) + }) }) }) }) @@ -179,22 +187,26 @@ describe("configs", () => { describe("create", () => { it("should create SMTP config", async () => { - await config.deleteConfig(Config.SMTP) - await saveSMTPConfig() - expect(events.email.SMTPUpdated).not.toBeCalled() - expect(events.email.SMTPCreated).toBeCalledTimes(1) - await config.deleteConfig(Config.SMTP) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + await config.deleteConfig(Config.SMTP) + await saveSMTPConfig() + expect(events.email.SMTPUpdated).not.toBeCalled() + expect(events.email.SMTPCreated).toBeCalledTimes(1) + await config.deleteConfig(Config.SMTP) + }) }) }) describe("update", () => { it("should update SMTP config", async () => { - const smtpConf = await saveSMTPConfig() - jest.clearAllMocks() - await saveSMTPConfig(smtpConf.config, smtpConf._id, smtpConf._rev) - expect(events.email.SMTPCreated).not.toBeCalled() - expect(events.email.SMTPUpdated).toBeCalledTimes(1) - await config.deleteConfig(Config.SMTP) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + const smtpConf = await saveSMTPConfig() + jest.clearAllMocks() + await saveSMTPConfig(smtpConf.config, smtpConf._id, smtpConf._rev) + expect(events.email.SMTPCreated).not.toBeCalled() + expect(events.email.SMTPUpdated).toBeCalledTimes(1) + await config.deleteConfig(Config.SMTP) + }) }) }) }) @@ -211,65 +223,73 @@ describe("configs", () => { describe("create", () => { it("should create settings config with default settings", async () => { - await config.deleteConfig(Config.SETTINGS) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + await config.deleteConfig(Config.SETTINGS) - await saveSettingsConfig() + await saveSettingsConfig() - expect(events.org.nameUpdated).not.toBeCalled() - expect(events.org.logoUpdated).not.toBeCalled() - expect(events.org.platformURLUpdated).not.toBeCalled() + expect(events.org.nameUpdated).not.toBeCalled() + expect(events.org.logoUpdated).not.toBeCalled() + expect(events.org.platformURLUpdated).not.toBeCalled() + }) }) it("should create settings config with non-default settings", async () => { - config.modeSelf() - await config.deleteConfig(Config.SETTINGS) - const conf = { - company: "acme", - logoUrl: "http://example.com", - platformUrl: "http://example.com", - } + await context.doInTenant(config.tenant1User!.tenantId, async () => { + config.modeSelf() + await config.deleteConfig(Config.SETTINGS) + const conf = { + company: "acme", + logoUrl: "http://example.com", + platformUrl: "http://example.com", + } - await saveSettingsConfig(conf) + await saveSettingsConfig(conf) - expect(events.org.nameUpdated).toBeCalledTimes(1) - expect(events.org.logoUpdated).toBeCalledTimes(1) - expect(events.org.platformURLUpdated).toBeCalledTimes(1) - config.modeCloud() + expect(events.org.nameUpdated).toBeCalledTimes(1) + expect(events.org.logoUpdated).toBeCalledTimes(1) + expect(events.org.platformURLUpdated).toBeCalledTimes(1) + config.modeCloud() + }) }) }) describe("update", () => { it("should update settings config", async () => { - config.modeSelf() - await config.deleteConfig(Config.SETTINGS) - const settingsConfig = await saveSettingsConfig() - settingsConfig.config.company = "acme" - settingsConfig.config.logoUrl = "http://example.com" - settingsConfig.config.platformUrl = "http://example.com" + await context.doInTenant(config.tenant1User!.tenantId, async () => { + config.modeSelf() + await config.deleteConfig(Config.SETTINGS) + const settingsConfig = await saveSettingsConfig() + settingsConfig.config.company = "acme" + settingsConfig.config.logoUrl = "http://example.com" + settingsConfig.config.platformUrl = "http://example.com" - await saveSettingsConfig( - settingsConfig.config, - settingsConfig._id, - settingsConfig._rev - ) + await saveSettingsConfig( + settingsConfig.config, + settingsConfig._id, + settingsConfig._rev + ) - expect(events.org.nameUpdated).toBeCalledTimes(1) - expect(events.org.logoUpdated).toBeCalledTimes(1) - expect(events.org.platformURLUpdated).toBeCalledTimes(1) - config.modeCloud() + expect(events.org.nameUpdated).toBeCalledTimes(1) + expect(events.org.logoUpdated).toBeCalledTimes(1) + expect(events.org.platformURLUpdated).toBeCalledTimes(1) + config.modeCloud() + }) }) }) }) }) it("should return the correct checklist status based on the state of the budibase installation", async () => { - await config.saveSmtpConfig() + await context.doInTenant(config.tenant1User!.tenantId, async () => { + await config.saveSmtpConfig() - const res = await config.api.configs.getConfigChecklist() - const checklist = res.body + const res = await config.api.configs.getConfigChecklist() + const checklist = res.body - expect(checklist.apps.checked).toBeFalsy() - expect(checklist.smtp.checked).toBeTruthy() - expect(checklist.adminUser.checked).toBeTruthy() + expect(checklist.apps.checked).toBeFalsy() + expect(checklist.smtp.checked).toBeTruthy() + expect(checklist.adminUser.checked).toBeTruthy() + }) }) }) diff --git a/packages/worker/src/api/routes/global/tests/roles.spec.ts b/packages/worker/src/api/routes/global/tests/roles.spec.ts index 2289273488..622a643f25 100644 --- a/packages/worker/src/api/routes/global/tests/roles.spec.ts +++ b/packages/worker/src/api/routes/global/tests/roles.spec.ts @@ -1,6 +1,7 @@ import { structures, TestConfiguration } from "../../../../tests" import { context, db, permissions, roles } from "@budibase/backend-core" import { Mock } from "jest-mock" +import { Database } from "@budibase/types" jest.mock("@budibase/backend-core", () => { const core = jest.requireActual("@budibase/backend-core") @@ -16,14 +17,13 @@ jest.mock("@budibase/backend-core", () => { } }) -const appDb = db.getDB("app_test") -const mockAppDB = context.getAppDB as Mock -mockAppDB.mockReturnValue(appDb) +let appId: string +let appDb: Database async function addAppMetadata() { await appDb.put({ _id: "app_metadata", - appId: "app_test", + appId: appId, name: "New App", version: "version", url: "url", @@ -39,12 +39,19 @@ describe("/api/global/roles", () => { ) beforeAll(async () => { - console.debug(role) - appDb.put(role) - await addAppMetadata() await config.beforeAll() }) + beforeEach(async () => { + appId = db.generateAppID() + appDb = db.getDB(appId) + const mockAppDB = context.getAppDB as Mock + mockAppDB.mockReturnValue(appDb) + + await addAppMetadata() + appDb.put(role) + }) + afterAll(async () => { await config.afterAll() }) @@ -57,16 +64,14 @@ describe("/api/global/roles", () => { it("retrieves roles", async () => { const res = await config.api.roles.get() expect(res.body).toBeDefined() - expect(res.body["app_test"].roles.length).toEqual(5) - expect(res.body["app_test"].roles.map((r: any) => r._id)).toContain( - role._id - ) + expect(res.body[appId].roles.length).toEqual(5) + expect(res.body[appId].roles.map((r: any) => r._id)).toContain(role._id) }) }) describe("GET api/global/roles/:appId", () => { it("finds a role by appId", async () => { - const res = await config.api.roles.find("app_test") + const res = await config.api.roles.find(appId) expect(res.body).toBeDefined() expect(res.body.name).toEqual("New App") }) @@ -79,9 +84,9 @@ describe("/api/global/roles", () => { app_test: "role1", } const userResponse = await config.createUser(user) - const res = await config.api.roles.remove("app_test") + const res = await config.api.roles.remove(appId) const updatedUser = await config.api.users.getUser(userResponse._id!) - expect(updatedUser.body.roles).not.toHaveProperty("app_test") + expect(updatedUser.body.roles).not.toHaveProperty(appId) expect(res.body.message).toEqual("App role removed from all users") }) }) diff --git a/packages/worker/src/api/routes/global/tests/users.spec.ts b/packages/worker/src/api/routes/global/tests/users.spec.ts index 10c29809b9..7ebc8d9e71 100644 --- a/packages/worker/src/api/routes/global/tests/users.spec.ts +++ b/packages/worker/src/api/routes/global/tests/users.spec.ts @@ -1,14 +1,9 @@ import { InviteUsersResponse, User } from "@budibase/types" jest.mock("nodemailer") -import { - TestConfiguration, - mocks, - structures, - TENANT_1, -} from "../../../../tests" +import { TestConfiguration, mocks, structures } from "../../../../tests" const sendMailMock = mocks.email.mock() -import { events, tenancy } from "@budibase/backend-core" +import { context, events, tenancy, utils } from "@budibase/backend-core" describe("/api/global/users", () => { const config = new TestConfiguration() @@ -27,16 +22,18 @@ describe("/api/global/users", () => { describe("invite", () => { it("should be able to generate an invitation", async () => { - const email = structures.users.newEmail() - const { code, res } = await config.api.users.sendUserInvite( - sendMailMock, - email - ) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + const email = structures.users.newEmail() + const { code, res } = await config.api.users.sendUserInvite( + sendMailMock, + email + ) - expect(res.body).toEqual({ message: "Invitation has been sent." }) - expect(sendMailMock).toHaveBeenCalled() - expect(code).toBeDefined() - expect(events.user.invited).toBeCalledTimes(1) + expect(res.body).toEqual({ message: "Invitation has been sent." }) + expect(sendMailMock).toHaveBeenCalled() + expect(code).toBeDefined() + expect(events.user.invited).toBeCalledTimes(1) + }) }) it("should not be able to generate an invitation for existing user", async () => { @@ -53,20 +50,22 @@ describe("/api/global/users", () => { }) it("should be able to create new user from invite", async () => { - const email = structures.users.newEmail() - const { code } = await config.api.users.sendUserInvite( - sendMailMock, - email - ) + await context.doInTenant(config.tenant1User!.tenantId, async () => { + const email = structures.users.newEmail() + const { code } = await config.api.users.sendUserInvite( + sendMailMock, + email + ) - const res = await config.api.users.acceptInvite(code) + const res = await config.api.users.acceptInvite(code) - expect(res.body._id).toBeDefined() - const user = await config.getUser(email) - expect(user).toBeDefined() - expect(user._id).toEqual(res.body._id) - expect(events.user.inviteAccepted).toBeCalledTimes(1) - expect(events.user.inviteAccepted).toBeCalledWith(user) + expect(res.body._id).toBeDefined() + const user = await config.getUser(email) + expect(user).toBeDefined() + expect(user._id).toEqual(res.body._id) + expect(events.user.inviteAccepted).toBeCalledTimes(1) + expect(events.user.inviteAccepted).toBeCalledWith(user) + }) }) }) @@ -118,7 +117,7 @@ describe("/api/global/users", () => { const user = await config.createUser() jest.clearAllMocks() - await tenancy.doInTenant(TENANT_1, async () => { + await tenancy.doInTenant(config.getTenantId(), async () => { const response = await config.api.users.bulkCreateUsers([user]) expect(response.created?.successful.length).toBe(0) @@ -231,7 +230,7 @@ describe("/api/global/users", () => { const user = await config.createUser() jest.clearAllMocks() - await tenancy.doInTenant(TENANT_1, async () => { + await tenancy.doInTenant(config.getTenantId(), async () => { delete user._id const response = await config.api.users.saveUser(user, 400) @@ -444,7 +443,7 @@ describe("/api/global/users", () => { }) it("should not be able to update email address", async () => { - const email = "email@test.com" + const email = `${utils.newid()}@test.com` const user = await config.createUser(structures.users.user({ email })) user.email = "new@test.com" diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index ecf7e14ebe..ce6106da87 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -20,8 +20,11 @@ import { auth, constants, env as coreEnv, + context, + utils, + DEFAULT_TENANT_ID, } from "@budibase/backend-core" -import structures, { TENANT_ID, TENANT_1, CSRF_TOKEN } from "./structures" +import structures, { TENANT_ID, CSRF_TOKEN } from "./structures" import { CreateUserResponse, User, AuthToken } from "@budibase/types" import API from "./api" @@ -36,6 +39,7 @@ class TestConfiguration { api: API defaultUser?: User tenant1User?: User + #tenantId?: string constructor( opts: { openServer: boolean; mode: Mode } = { @@ -112,10 +116,12 @@ class TestConfiguration { // SETUP / TEARDOWN async beforeAll() { + this.#tenantId = `tenant-${utils.newid()}` + await this.createDefaultUser() await this.createSession(this.defaultUser!) - await tenancy.doInTenant(TENANT_1, async () => { + await tenancy.doInTenant(this.#tenantId, async () => { await this.createTenant1User() await this.createSession(this.tenant1User!) }) @@ -152,7 +158,7 @@ class TestConfiguration { try { return tenancy.getTenantId() } catch (e: any) { - return TENANT_ID + return DEFAULT_TENANT_ID } } @@ -203,7 +209,7 @@ class TestConfiguration { const tenantId = this.getTenantId() if (tenantId === TENANT_ID) { return this.authHeaders(this.defaultUser!) - } else if (tenantId === TENANT_1) { + } else if (tenantId === this.getTenantId()) { return this.authHeaders(this.tenant1User!) } else { throw new Error("could not determine auth headers to use") @@ -222,7 +228,6 @@ class TestConfiguration { async createDefaultUser() { const user = structures.users.adminUser({ - email: "test@test.com", password: "test", }) this.defaultUser = await this.createUser(user) @@ -230,7 +235,6 @@ class TestConfiguration { async createTenant1User() { const user = structures.users.adminUser({ - email: "tenant1@test.com", password: "test", }) this.tenant1User = await this.createUser(user) diff --git a/packages/worker/src/tests/api/auth.ts b/packages/worker/src/tests/api/auth.ts index dda50976bd..ccbf747273 100644 --- a/packages/worker/src/tests/api/auth.ts +++ b/packages/worker/src/tests/api/auth.ts @@ -1,3 +1,4 @@ +import structures from "../structures" import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" @@ -24,14 +25,17 @@ export class AuthAPI extends TestAPI { .expect(200) } - requestPasswordReset = async (sendMailMock: any) => { + requestPasswordReset = async (sendMailMock: any, userEmail: string) => { await this.config.saveSmtpConfig() await this.config.saveSettingsConfig() - await this.config.createUser() + await this.config.createUser({ + ...structures.users.user(), + email: userEmail, + }) const res = await this.request .post(`/api/global/auth/${this.config.getTenantId()}/reset`) .send({ - email: "test@test.com", + email: userEmail, }) .expect("Content-Type", /json/) .expect(200) diff --git a/packages/worker/src/tests/api/configs.ts b/packages/worker/src/tests/api/configs.ts index 10413dfdd6..76a6f31415 100644 --- a/packages/worker/src/tests/api/configs.ts +++ b/packages/worker/src/tests/api/configs.ts @@ -10,8 +10,8 @@ export class ConfigAPI extends TestAPI { return this.request .get(`/api/global/configs/checklist`) .set(this.config.defaultHeaders()) - .expect("Content-Type", /json/) .expect(200) + .expect("Content-Type", /json/) } saveConfig = (data: any) => { @@ -19,8 +19,8 @@ export class ConfigAPI extends TestAPI { .post(`/api/global/configs`) .send(data) .set(this.config.defaultHeaders()) - .expect("Content-Type", /json/) .expect(200) + .expect("Content-Type", /json/) } OIDCCallback = (configId: string, preAuthRes: any) => { diff --git a/packages/worker/src/tests/api/users.ts b/packages/worker/src/tests/api/users.ts index f2c95a20a1..884336fa4e 100644 --- a/packages/worker/src/tests/api/users.ts +++ b/packages/worker/src/tests/api/users.ts @@ -26,8 +26,8 @@ export class UserAPI extends TestAPI { email, }) .set(this.config.defaultHeaders()) - .expect("Content-Type", /json/) .expect(status) + .expect("Content-Type", /json/) if (status !== 200) { return { code: undefined, res } @@ -99,7 +99,7 @@ export class UserAPI extends TestAPI { request = { email: structures.email(), password: generator.string(), - tenantId: structures.uuid(), + tenantId: `tenant-${structures.uuid()}`, } } const res = await this.request diff --git a/packages/worker/src/tests/jestEnv.ts b/packages/worker/src/tests/jestEnv.ts index 88ed8c2ed5..0b27cf52aa 100644 --- a/packages/worker/src/tests/jestEnv.ts +++ b/packages/worker/src/tests/jestEnv.ts @@ -1,16 +1,11 @@ -import env from "../environment" -import { env as coreEnv } from "@budibase/backend-core" - -env._set("SELF_HOSTED", "0") -env._set("NODE_ENV", "jest") -env._set("JWT_SECRET", "test-jwtsecret") -env._set("LOG_LEVEL", "silent") -env._set("MULTI_TENANCY", "1") -env._set("MINIO_URL", "http://localhost") -env._set("MINIO_ACCESS_KEY", "test") -env._set("MINIO_SECRET_KEY", "test") -env._set("PLATFORM_URL", "http://localhost:10000") -env._set("INTERNAL_API_KEY", "test") -env._set("DISABLE_ACCOUNT_PORTAL", "0") -coreEnv._set("COUCH_DB_USER", "budibase") -coreEnv._set("COUCH_DB_PASSWORD", "budibase") +process.env.SELF_HOSTED = "0" +process.env.NODE_ENV = "jest" +process.env.JWT_SECRET = "test-jwtsecret" +process.env.LOG_LEVEL = "silent" +process.env.MULTI_TENANCY = "1" +process.env.MINIO_URL = "http://localhost" +process.env.MINIO_ACCESS_KEY = "test" +process.env.MINIO_SECRET_KEY = "test" +process.env.PLATFORM_URL = "http://localhost:10000" +process.env.INTERNAL_API_KEY = "tet" +process.env.DISABLE_ACCOUNT_PORTAL = "0" diff --git a/packages/worker/src/tests/jestSetup.ts b/packages/worker/src/tests/jestSetup.ts index 9a948b674d..89ffb85b95 100644 --- a/packages/worker/src/tests/jestSetup.ts +++ b/packages/worker/src/tests/jestSetup.ts @@ -1,5 +1,8 @@ import { mocks } from "@budibase/backend-core/tests" +import env from "../environment" +import { env as coreEnv } from "@budibase/backend-core" + // must explicitly enable fetch mock mocks.fetch.enable() @@ -17,3 +20,24 @@ if (!process.env.CI) { // 100 seconds jest.setTimeout(100000) } + +function overrideConfigValue(key: string, value: string) { + env._set(key, value) + coreEnv._set(key, value) +} + +const globalSafe = global as any + +overrideConfigValue( + "COUCH_DB_PORT", + globalSafe.__TESTCONTAINERS_DEVENV_PORT_5984__ +) +overrideConfigValue( + "COUCH_DB_URL", + `http://${globalSafe.__TESTCONTAINERS_DEVENV_IP__}:${globalSafe.__TESTCONTAINERS_DEVENV_PORT_5984__}` +) + +overrideConfigValue( + "MINIO_URL", + `http://${globalSafe.__TESTCONTAINERS_DEVENV_IP__}:${globalSafe.__TESTCONTAINERS_DEVENV_PORT_9000__}` +) diff --git a/packages/worker/src/tests/structures/index.ts b/packages/worker/src/tests/structures/index.ts index 1223dfa019..dad055f7a7 100644 --- a/packages/worker/src/tests/structures/index.ts +++ b/packages/worker/src/tests/structures/index.ts @@ -5,7 +5,6 @@ import * as groups from "./groups" import { v4 as uuid } from "uuid" export const TENANT_ID = "default" -export const TENANT_1 = "tenant1" export const CSRF_TOKEN = "e3727778-7af0-4226-b5eb-f43cbe60a306" const pkg = { @@ -14,7 +13,6 @@ const pkg = { configs, users, TENANT_ID, - TENANT_1, CSRF_TOKEN, groups, }