From 1af47003c5fda7d2fc3ab09a126f94bc681beee0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 14 Nov 2022 20:25:05 +0000 Subject: [PATCH] Updating test cases - trying to get everything in order for merge. --- packages/backend-core/package.json | 2 +- packages/backend-core/src/db/couch/pouchDB.ts | 15 +- packages/backend-core/src/db/db.ts | 13 +- .../backend-core/src/db/tests/pouch.spec.js | 2 +- .../backend-core/src/db/tests/utils.spec.js | 6 +- packages/server/__mocks__/node-fetch.ts | 6 +- packages/server/package.json | 2 +- .../server/src/db/tests/linkTests.spec.js | 31 ++-- .../src/middleware/tests/currentapp.spec.js | 150 ++++++++++-------- packages/worker/package.json | 2 +- 10 files changed, 134 insertions(+), 95 deletions(-) diff --git a/packages/backend-core/package.json b/packages/backend-core/package.json index 2fbc087214..63e0d9ff9b 100644 --- a/packages/backend-core/package.json +++ b/packages/backend-core/package.json @@ -58,7 +58,7 @@ "testEnvironment": "node", "moduleNameMapper": { "@budibase/types": "/../types/src", - "axios": "axios/dist/node/axios.cjs" + "^axios.*$": "/node_modules/axios/lib/axios.js" }, "setupFiles": [ "./scripts/jestSetup.ts" diff --git a/packages/backend-core/src/db/couch/pouchDB.ts b/packages/backend-core/src/db/couch/pouchDB.ts index 07f7b6c90d..e7efbf6dcd 100644 --- a/packages/backend-core/src/db/couch/pouchDB.ts +++ b/packages/backend-core/src/db/couch/pouchDB.ts @@ -66,7 +66,20 @@ const checkInitialised = () => { export function getPouchDB(dbName: string, opts?: any): PouchDB.Database { checkInitialised() - return new Pouch(dbName, opts) + const db = new Pouch(dbName, opts) + const dbPut = db.put + db.put = async (doc: any, options = {}) => { + if (!doc.createdAt) { + doc.createdAt = new Date().toISOString() + } + doc.updatedAt = new Date().toISOString() + return dbPut(doc, options) + } + db.exists = async () => { + const info = await db.info() + return !info.error + } + return db } // use this function if you have called getPouchDB - close diff --git a/packages/backend-core/src/db/db.ts b/packages/backend-core/src/db/db.ts index 909d286d32..beaea3d42c 100644 --- a/packages/backend-core/src/db/db.ts +++ b/packages/backend-core/src/db/db.ts @@ -1,19 +1,15 @@ import env from "../environment" -import { directCouchQuery, PouchLike } from "./couch" +import { directCouchQuery, PouchLike, getPouchDB } from "./couch" import { CouchFindOptions } from "@budibase/types" -let initialised = false const dbList = new Set() -const checkInitialised = () => { - if (!initialised) { - throw new Error("init has not been called") - } -} - export function getDB(dbName?: string, opts?: any): PouchLike { + // TODO: once using the test image, need to remove this if (env.isTest()) { dbList.add(dbName) + // @ts-ignore + return getPouchDB(dbName, opts) } return new PouchLike(dbName, opts) } @@ -32,7 +28,6 @@ export function allDbs() { if (!env.isTest()) { throw new Error("Cannot be used outside test environment.") } - checkInitialised() return [...dbList] } diff --git a/packages/backend-core/src/db/tests/pouch.spec.js b/packages/backend-core/src/db/tests/pouch.spec.js index 30cdd0f5ec..ce27ab7d22 100644 --- a/packages/backend-core/src/db/tests/pouch.spec.js +++ b/packages/backend-core/src/db/tests/pouch.spec.js @@ -1,5 +1,5 @@ require("../../../tests/utilities/TestConfiguration") -const getUrlInfo = require("../pouch").getUrlInfo +const getUrlInfo = require("../couch").getUrlInfo describe("pouch", () => { describe("Couch DB URL parsing", () => { diff --git a/packages/backend-core/src/db/tests/utils.spec.js b/packages/backend-core/src/db/tests/utils.spec.js index 5f9a224e7a..1700621334 100644 --- a/packages/backend-core/src/db/tests/utils.spec.js +++ b/packages/backend-core/src/db/tests/utils.spec.js @@ -1,4 +1,4 @@ -require("../../../tests/utilities/TestConfiguration"); +require("../../../tests/utilities/TestConfiguration") const { generateAppID, getDevelopmentAppID, @@ -8,8 +8,8 @@ const { getPlatformUrl, getScopedConfig } = require("../utils") -const tenancy = require("../../tenancy"); -const { Configs, DEFAULT_TENANT_ID } = require("../../constants"); +const tenancy = require("../../tenancy") +const { Configs, DEFAULT_TENANT_ID } = require("../../constants") const env = require("../../environment") describe("utils", () => { diff --git a/packages/server/__mocks__/node-fetch.ts b/packages/server/__mocks__/node-fetch.ts index 0e32c39edd..c3d6b623dd 100644 --- a/packages/server/__mocks__/node-fetch.ts +++ b/packages/server/__mocks__/node-fetch.ts @@ -2,7 +2,7 @@ module FetchMock { const fetch = jest.requireActual("node-fetch") let failCount = 0 - module.exports = async (url: any, opts: any) => { + const func = async (url: any, opts: any) => { function json(body: any, status = 200) { return { status, @@ -106,4 +106,8 @@ module FetchMock { } return fetch(url, opts) } + + func.Headers = fetch.Headers + + module.exports = func } diff --git a/packages/server/package.json b/packages/server/package.json index 6d75877c79..ad8f62739d 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -43,7 +43,7 @@ "@budibase/backend-core/(.*)": "/../backend-core/$1", "@budibase/backend-core": "/../backend-core/src", "@budibase/types": "/../types/src", - "axios": "axios/dist/node/axios.cjs" + "^axios.*$": "/node_modules/axios/lib/axios.js" }, "setupFiles": [ "./scripts/jestSetup.js" diff --git a/packages/server/src/db/tests/linkTests.spec.js b/packages/server/src/db/tests/linkTests.spec.js index 8bc26cde2a..0684255ea1 100644 --- a/packages/server/src/db/tests/linkTests.spec.js +++ b/packages/server/src/db/tests/linkTests.spec.js @@ -1,30 +1,34 @@ const TestConfig = require("../../tests/utilities/TestConfiguration") const { basicTable } = require("../../tests/utilities/structures") const linkUtils = require("../linkedRows/linkUtils") -const { getAppDB } = require("@budibase/backend-core/context") -const { doWithDB } = require("@budibase/backend-core/db") +const { context } = require("@budibase/backend-core") describe("test link functionality", () => { const config = new TestConfig(false) + let appId describe("getLinkedTable", () => { - let db, table + let table beforeEach(async () => { - await config.init() - db = getAppDB() + const app = await config.init() + appId = app.appId table = await config.createTable() }) it("should be able to retrieve a linked table from a list", async () => { - const retrieved = await linkUtils.getLinkedTable(table._id, [table]) - expect(retrieved._id).toBe(table._id) + await context.doInAppContext(appId, async () => { + const retrieved = await linkUtils.getLinkedTable(table._id, [table]) + expect(retrieved._id).toBe(table._id) + }) }) it("should be able to retrieve a table from DB and update list", async () => { const tables = [] - const retrieved = await linkUtils.getLinkedTable(table._id, tables) - expect(retrieved._id).toBe(table._id) - expect(tables[0]).toBeDefined() + await context.doInAppContext(appId, async () => { + const retrieved = await linkUtils.getLinkedTable(table._id, tables) + expect(retrieved._id).toBe(table._id) + expect(tables[0]).toBeDefined() + }) }) }) @@ -48,15 +52,14 @@ describe("test link functionality", () => { describe("getLinkDocuments", () => { it("should create the link view when it doesn't exist", async () => { // create the DB and a very basic app design DB - const output = await doWithDB("test", async db => { - await db.put({ _id: "_design/database", views: {} }) - return await linkUtils.getLinkDocuments({ + await context.doInAppContext(appId, async () => { + const output = await linkUtils.getLinkDocuments({ tableId: "test", rowId: "test", includeDocs: false, }) + expect(Array.isArray(output)).toBe(true) }) - expect(Array.isArray(output)).toBe(true) }) }) }) \ No newline at end of file diff --git a/packages/server/src/middleware/tests/currentapp.spec.js b/packages/server/src/middleware/tests/currentapp.spec.js index 638abcbba4..12f68bedfc 100644 --- a/packages/server/src/middleware/tests/currentapp.spec.js +++ b/packages/server/src/middleware/tests/currentapp.spec.js @@ -1,16 +1,8 @@ +require("../../db").init() mockAuthWithNoCookie() mockWorker() mockUserGroups() -jest.mock("@budibase/backend-core/db", () => { - const coreDb = jest.requireActual("@budibase/backend-core/db") - coreDb.init() - return { - ...coreDb, - dbExists: async () => true, - } -}) - function mockWorker() { jest.mock("../../utilities/workerRequests", () => ({ getGlobalSelf: () => { @@ -43,42 +35,51 @@ function mockUserGroups() { function mockAuthWithNoCookie() { jest.resetModules() mockWorker() - jest.mock("@budibase/backend-core/cache", () => ({ - user: { - getUser: () => { - return { - _id: "us_uuid1", - } + jest.mock("@budibase/backend-core", () => { + const core = jest.requireActual("@budibase/backend-core") + return { + ...core, + db: { + dbExists: () => true, }, - }, - })) - jest.mock("@budibase/backend-core/utils", () => ({ - getAppIdFromCtx: jest.fn(), - setCookie: jest.fn(), - getCookie: jest.fn(), - })) - jest.mock("@budibase/backend-core/constants", () => ({ - Cookies: {}, - })) + cache: { + user: { + getUser: () => { + return { + _id: "us_uuid1", + } + }, + }, + }, + utils: { + getAppIdFromCtx: jest.fn(), + setCookie: jest.fn(), + getCookie: jest.fn(), + }, + } + }) } function mockAuthWithCookie() { jest.resetModules() mockWorker() - jest.mock("@budibase/backend-core/utils", () => ({ - getAppIdFromCtx: () => { - return "app_test" - }, - setCookie: jest.fn(), - clearCookie: jest.fn(), - getCookie: () => ({appId: "app_different", roleId: "PUBLIC"}), - })) - jest.mock("@budibase/backend-core/constants", () => ({ - Cookies: { - Auth: "auth", - CurrentApp: "currentapp", - }, - })) + jest.mock("@budibase/backend-core", () => { + const core = jest.requireActual("@budibase/backend-core") + return { + ...core, + db: { + dbExists: () => true, + }, + utils: { + getAppIdFromCtx: () => { + return "app_test" + }, + setCookie: jest.fn(), + clearCookie: jest.fn(), + getCookie: () => ({ appId: "app_different", roleId: "PUBLIC" }), + }, + } + }) } class TestConfiguration { @@ -88,7 +89,16 @@ class TestConfiguration { this.ctx = { next: this.next, - throw: this.throw + throw: this.throw, + request: { + body: {}, + headers: {}, + }, + headers: {}, + path: "", + cookies: { + set: jest.fn(), + } } } @@ -101,6 +111,8 @@ class TestConfiguration { executeMiddleware() { // import as late as possible for mocks + jest.resetModules() + require("../../db").init() const currentAppMiddleware = require("../currentapp") return currentAppMiddleware(this.ctx, this.next) } @@ -138,11 +150,11 @@ describe("Current app middleware", () => { async function checkExpected(setCookie) { config.setUser() await config.executeMiddleware() - let { setCookie: cookieFn } = require("@budibase/backend-core/utils") + let { utils } = require("@budibase/backend-core") if (setCookie) { - expect(cookieFn).toHaveBeenCalled() + expect(utils.setCookie).toHaveBeenCalled() } else { - expect(cookieFn).not.toHaveBeenCalled() + expect(utils.setCookie).not.toHaveBeenCalled() } expect(config.ctx.roleId).toEqual("PUBLIC") expect(config.ctx.user.role._id).toEqual("PUBLIC") @@ -157,31 +169,43 @@ describe("Current app middleware", () => { it("should perform correct when no cookie exists", async () => { mockReset() - jest.mock("@budibase/backend-core/utils", () => ({ - getAppIdFromCtx: () => { - return "app_test" - }, - setCookie: jest.fn(), - getCookie: jest.fn(), - })) - jest.mock("@budibase/backend-core/constants", () => ({ - Cookies: {}, - })) + jest.mock("@budibase/backend-core", () => { + const core = jest.requireActual("@budibase/backend-core") + return { + ...core, + db: { + dbExists: () => true, + }, + utils: { + getAppIdFromCtx: () => { + return "app_test" + }, + setCookie: jest.fn(), + getCookie: jest.fn(), + }, + } + }) await checkExpected(true) }) it("lastly check what occurs when cookie doesn't need updated", async () => { mockReset() - jest.mock("@budibase/backend-core/utils", () => ({ - getAppIdFromCtx: () => { - return "app_test" - }, - setCookie: jest.fn(), - getCookie: () => ({appId: "app_test", roleId: "PUBLIC"}), - })) - jest.mock("@budibase/backend-core/constants", () => ({ - Cookies: {}, - })) + jest.mock("@budibase/backend-core", () => { + const core = jest.requireActual("@budibase/backend-core") + return { + ...core, + db: { + dbExists: () => true, + }, + utils: { + getAppIdFromCtx: () => { + return "app_test" + }, + setCookie: jest.fn(), + getCookie: () => ({ appId: "app_test", roleId: "PUBLIC" }), + }, + } + }) await checkExpected(false) }) }) diff --git a/packages/worker/package.json b/packages/worker/package.json index 844448b569..1e694adfc9 100644 --- a/packages/worker/package.json +++ b/packages/worker/package.json @@ -99,7 +99,7 @@ "@budibase/backend-core/(.*)": "/../backend-core/$1", "@budibase/backend-core": "/../backend-core/src", "@budibase/types": "/../types/src", - "axios": "axios/dist/node/axios.cjs" + "^axios.*$": "/node_modules/axios/lib/axios.js" }, "setupFiles": [ "./scripts/jestSetup.js"