From 74fe4ead31955c741deb119620893588c13c8e25 Mon Sep 17 00:00:00 2001 From: adrinr Date: Fri, 3 Feb 2023 09:59:57 +0000 Subject: [PATCH] Remove addTenantToGlobalDb from helpers --- .../src/api/routes/global/tests/auth.spec.ts | 29 +++++++++---------- .../api/routes/system/tests/tenants.spec.ts | 8 ++--- .../src/middleware/tests/tenancy.spec.ts | 4 +-- packages/worker/src/sdk/users/users.ts | 5 ---- .../worker/src/tests/TestConfiguration.ts | 11 ++----- 5 files changed, 22 insertions(+), 35 deletions(-) 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 4e1302f951..ee753d49dc 100644 --- a/packages/worker/src/api/routes/global/tests/auth.spec.ts +++ b/packages/worker/src/api/routes/global/tests/auth.spec.ts @@ -1,10 +1,9 @@ jest.mock("nodemailer") -import { TestConfiguration, mocks, } from "../../../../tests" +import { TestConfiguration, mocks } from "../../../../tests" const sendMailMock = mocks.email.mock() import { events, tenancy } from "@budibase/backend-core" import { structures } from "@budibase/backend-core/tests" - const expectSetAuthCookie = (res: any) => { expect( res.get("Set-Cookie").find((c: string) => c.startsWith("budibase:auth")) @@ -28,7 +27,7 @@ describe("/api/global/auth", () => { describe("password", () => { describe("POST /api/global/auth/:tenantId/login", () => { - it("should login", () => { }) + it("should login", () => {}) }) describe("POST /api/global/auth/logout", () => { @@ -85,35 +84,35 @@ describe("/api/global/auth", () => { }) describe("init", () => { - describe("POST /api/global/auth/init", () => { }) + describe("POST /api/global/auth/init", () => {}) - describe("GET /api/global/auth/init", () => { }) + describe("GET /api/global/auth/init", () => {}) }) describe("datasource", () => { // MULTI TENANT - describe("GET /api/global/auth/:tenantId/datasource/:provider", () => { }) + describe("GET /api/global/auth/:tenantId/datasource/:provider", () => {}) - describe("GET /api/global/auth/:tenantId/datasource/:provider/callback", () => { }) + describe("GET /api/global/auth/:tenantId/datasource/:provider/callback", () => {}) // SINGLE TENANT - describe("GET /api/global/auth/datasource/:provider/callback", () => { }) + describe("GET /api/global/auth/datasource/:provider/callback", () => {}) }) describe("google", () => { // MULTI TENANT - describe("GET /api/global/auth/:tenantId/google", () => { }) + describe("GET /api/global/auth/:tenantId/google", () => {}) - describe("GET /api/global/auth/:tenantId/google/callback", () => { }) + describe("GET /api/global/auth/:tenantId/google/callback", () => {}) // SINGLE TENANT - describe("GET /api/global/auth/google/callback", () => { }) + describe("GET /api/global/auth/google/callback", () => {}) - describe("GET /api/admin/auth/google/callback", () => { }) + describe("GET /api/admin/auth/google/callback", () => {}) }) describe("oidc", () => { @@ -178,10 +177,10 @@ describe("/api/global/auth", () => { // SINGLE TENANT - describe("GET /api/global/auth/oidc/callback", () => { }) + describe("GET /api/global/auth/oidc/callback", () => {}) - describe("GET /api/global/auth/oidc/callback", () => { }) + describe("GET /api/global/auth/oidc/callback", () => {}) - describe("GET /api/admin/auth/oidc/callback", () => { }) + describe("GET /api/admin/auth/oidc/callback", () => {}) }) }) diff --git a/packages/worker/src/api/routes/system/tests/tenants.spec.ts b/packages/worker/src/api/routes/system/tests/tenants.spec.ts index 40b55aba4a..af509b402e 100644 --- a/packages/worker/src/api/routes/system/tests/tenants.spec.ts +++ b/packages/worker/src/api/routes/system/tests/tenants.spec.ts @@ -18,7 +18,7 @@ describe("/api/global/tenants", () => { describe("DELETE /api/system/tenants/:tenantId", () => { it("allows deleting the current tenant", async () => { - const user = await config.createTenant({ addTenantToGlobalDb: true }) + const user = await config.createTenant() await config.api.tenants.delete(user.tenantId, { headers: config.authHeaders(user), @@ -26,9 +26,9 @@ describe("/api/global/tenants", () => { }) it("rejects deleting another tenant", async () => { - const user1 = await config.createTenant({ addTenantToGlobalDb: true }) + const user1 = await config.createTenant() // create a second user in another tenant - const user2 = await config.createTenant({ addTenantToGlobalDb: true }) + const user2 = await config.createTenant() const status = 403 const res = await config.api.tenants.delete(user1.tenantId, { @@ -43,7 +43,7 @@ describe("/api/global/tenants", () => { }) it("rejects non-admin", async () => { - const user1 = await config.createTenant({ addTenantToGlobalDb: true }) + const user1 = await config.createTenant() // create an internal non-admin user const user2 = await tenancy.doInTenant(user1.tenantId, () => { return config.createUser() diff --git a/packages/worker/src/middleware/tests/tenancy.spec.ts b/packages/worker/src/middleware/tests/tenancy.spec.ts index c861f486d4..a8b7a50e55 100644 --- a/packages/worker/src/middleware/tests/tenancy.spec.ts +++ b/packages/worker/src/middleware/tests/tenancy.spec.ts @@ -17,7 +17,7 @@ describe("tenancy middleware", () => { }) it("should get tenant id from user", async () => { - const user = await config.createTenant({ addTenantToGlobalDb: false }) + const user = await config.createTenant() await config.createSession(user) const res = await config.api.self.getSelf(user) expect(res.headers[constants.Header.TENANT_ID]).toBe(user.tenantId) @@ -54,7 +54,7 @@ describe("tenancy middleware", () => { }) it("should get tenant id from path variable", async () => { - const user = await config.createTenant({ addTenantToGlobalDb: false }) + const user = await config.createTenant() const res = await config.request .post(`/api/global/auth/${user.tenantId}/login`) .send({ diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 73c604e3ea..8410d0b2e0 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -264,11 +264,6 @@ export const save = async ( builtUser._rev = response.rev await eventHelpers.handleSaveEvents(builtUser, dbUser) - if (!env.isTest()) { - // Race conditions (existing in prod already) with multiple tests synchronising the global db - // https://github.com/Budibase/budibase/issues/6134 - await addTenant(tenantId, _id, email) - } await cache.user.invalidateUser(response.id) // let server know to sync user diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index 74fb2dd477..1454fa59aa 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -160,18 +160,11 @@ class TestConfiguration { // TENANCY - createTenant = async ({ - addTenantToGlobalDb, - }: { - addTenantToGlobalDb: boolean - }): Promise => { + createTenant = async (): Promise => { // create user / new tenant const res = await this.api.users.createAdminUser() - // This needs to be added because it was disabled for bulk testing: // https://github.com/Budibase/budibase/issues/6134 - if (addTenantToGlobalDb) { - await sdk.users.addTenant(res.tenantId, res.userId, res.email) - } + await sdk.users.addTenant(res.tenantId, res.userId, res.email) // return the created user const userRes = await this.api.users.getUser(res.userId, {