Updating test cases - trying to get everything in order for merge.

This commit is contained in:
mike12345567 2022-11-14 20:25:05 +00:00
parent 380ee221b1
commit 1af47003c5
10 changed files with 134 additions and 95 deletions

View File

@ -58,7 +58,7 @@
"testEnvironment": "node",
"moduleNameMapper": {
"@budibase/types": "<rootDir>/../types/src",
"axios": "axios/dist/node/axios.cjs"
"^axios.*$": "<rootDir>/node_modules/axios/lib/axios.js"
},
"setupFiles": [
"./scripts/jestSetup.ts"

View File

@ -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

View File

@ -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]
}

View File

@ -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", () => {

View File

@ -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", () => {

View File

@ -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
}

View File

@ -43,7 +43,7 @@
"@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
"@budibase/backend-core": "<rootDir>/../backend-core/src",
"@budibase/types": "<rootDir>/../types/src",
"axios": "axios/dist/node/axios.cjs"
"^axios.*$": "<rootDir>/node_modules/axios/lib/axios.js"
},
"setupFiles": [
"./scripts/jestSetup.js"

View File

@ -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)
})
})
})

View File

@ -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)
})
})

View File

@ -99,7 +99,7 @@
"@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
"@budibase/backend-core": "<rootDir>/../backend-core/src",
"@budibase/types": "<rootDir>/../types/src",
"axios": "axios/dist/node/axios.cjs"
"^axios.*$": "<rootDir>/node_modules/axios/lib/axios.js"
},
"setupFiles": [
"./scripts/jestSetup.js"