From a02e9017ec78b27be5bd37ea3d48b93a06757967 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 28 Nov 2022 19:12:23 +0000 Subject: [PATCH] Final fixes after re-running all test suites. --- packages/backend-core/src/environment.ts | 2 +- .../src/api/routes/tests/deployment.spec.ts | 2 +- .../server/src/api/routes/tests/table.spec.js | 3 +- .../tests/utilities/{index.js => index.ts} | 24 ++-- .../tests/utilities/{index.js => index.ts} | 33 ++--- ...tConfiguration.js => TestConfiguration.ts} | 119 +++++++++++------- packages/server/tsconfig.build.json | 1 - .../types/src/documents/app/automation.ts | 1 + 8 files changed, 108 insertions(+), 77 deletions(-) rename packages/server/src/api/routes/tests/utilities/{index.js => index.ts} (73%) rename packages/server/src/automations/tests/utilities/{index.js => index.ts} (50%) rename packages/server/src/tests/utilities/{TestConfiguration.js => TestConfiguration.ts} (84%) diff --git a/packages/backend-core/src/environment.ts b/packages/backend-core/src/environment.ts index b35f3ecaa4..51ab101b3c 100644 --- a/packages/backend-core/src/environment.ts +++ b/packages/backend-core/src/environment.ts @@ -85,7 +85,7 @@ for (let [key, value] of Object.entries(environment)) { // handle the edge case of "0" to disable an environment variable if (value === "0") { // @ts-ignore - env[key] = 0 + environment[key] = 0 } } diff --git a/packages/server/src/api/routes/tests/deployment.spec.ts b/packages/server/src/api/routes/tests/deployment.spec.ts index 0219e3f2b4..bd705f9f59 100644 --- a/packages/server/src/api/routes/tests/deployment.spec.ts +++ b/packages/server/src/api/routes/tests/deployment.spec.ts @@ -1,4 +1,4 @@ -import setup from "./utilities" +import * as setup from "./utilities" import { events } from "@budibase/backend-core" describe("/deployments", () => { diff --git a/packages/server/src/api/routes/tests/table.spec.js b/packages/server/src/api/routes/tests/table.spec.js index e42aaf07e4..4776878635 100644 --- a/packages/server/src/api/routes/tests/table.spec.js +++ b/packages/server/src/api/routes/tests/table.spec.js @@ -271,7 +271,7 @@ describe("/tables", () => { .expect(200) expect(res.body.message).toEqual(`Table ${testTable._id} deleted.`) expect(events.table.deleted).toBeCalledTimes(1) - expect(events.table.deleted).toBeCalledWith(testTable) + expect(events.table.deleted).toBeCalledWith({ ...testTable, tableId: testTable._id }) }) it("deletes linked references to the table after deletion", async () => { @@ -288,6 +288,7 @@ describe("/tables", () => { }, TestTable: { type: "link", + fieldName: "TestTable", tableId: testTable._id, constraints: { type: "array" diff --git a/packages/server/src/api/routes/tests/utilities/index.js b/packages/server/src/api/routes/tests/utilities/index.ts similarity index 73% rename from packages/server/src/api/routes/tests/utilities/index.js rename to packages/server/src/api/routes/tests/utilities/index.ts index b6dde0cb79..87a373a2c6 100644 --- a/packages/server/src/api/routes/tests/utilities/index.js +++ b/packages/server/src/api/routes/tests/utilities/index.ts @@ -1,6 +1,6 @@ -const TestConfig = require("../../../../tests/utilities/TestConfiguration") -const structures = require("../../../../tests/utilities/structures") -const env = require("../../../../environment") +import TestConfig from "../../../../tests/utilities/TestConfiguration" +import env from "../../../../environment" +export * as structures from "../../../../tests/utilities/structures" function user() { return { @@ -40,16 +40,18 @@ jest.mock("../../../../utilities/workerRequests", () => ({ removeAppFromUserRoles: jest.fn(), })) -exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms)) +export function delay(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)) +} -let request, config +let request: any, config: any -exports.beforeAll = () => { +export function beforeAll() { config = new TestConfig() request = config.getRequest() } -exports.afterAll = () => { +export function afterAll() { if (config) { config.end() } @@ -59,21 +61,21 @@ exports.afterAll = () => { config = null } -exports.getRequest = () => { +export function getRequest() { if (!request) { exports.beforeAll() } return request } -exports.getConfig = () => { +export function getConfig() { if (!config) { exports.beforeAll() } return config } -exports.switchToSelfHosted = async func => { +export async function switchToSelfHosted(func: any) { // self hosted stops any attempts to Dynamo env._set("NODE_ENV", "production") env._set("SELF_HOSTED", true) @@ -90,5 +92,3 @@ exports.switchToSelfHosted = async func => { throw error } } - -exports.structures = structures diff --git a/packages/server/src/automations/tests/utilities/index.js b/packages/server/src/automations/tests/utilities/index.ts similarity index 50% rename from packages/server/src/automations/tests/utilities/index.js rename to packages/server/src/automations/tests/utilities/index.ts index 1e705182dc..a18e931bab 100644 --- a/packages/server/src/automations/tests/utilities/index.js +++ b/packages/server/src/automations/tests/utilities/index.ts @@ -1,23 +1,23 @@ -const TestConfig = require("../../../tests/utilities/TestConfiguration") -const { context } = require("@budibase/backend-core") -const actions = require("../../actions") -const emitter = require("../../../events/index") -const env = require("../../../environment") +import TestConfig from "../../../tests/utilities/TestConfiguration" +import { context } from "@budibase/backend-core" +import { ACTION_DEFINITIONS, getAction } from "../../actions" +import emitter from "../../../events/index" +import env from "../../../environment" -let config +let config: any -exports.getConfig = () => { +export function getConfig() { if (!config) { config = new TestConfig(false) } return config } -exports.afterAll = () => { +export function afterAll() { config.end() } -exports.runInProd = async fn => { +export async function runInProd(fn: any) { env._set("NODE_ENV", "production") let error try { @@ -31,15 +31,19 @@ exports.runInProd = async fn => { } } -exports.runStep = async function runStep(stepId, inputs) { +export async function runStep(stepId: string, inputs: any) { async function run() { - let step = await actions.getAction(stepId) + let step = await getAction(stepId) expect(step).toBeDefined() + if (!step) { + throw new Error("No step found") + } return step({ + context: {}, inputs, appId: config ? config.getAppId() : null, // don't really need an API key, mocked out usage quota, not being tested here - apiKey: exports.apiKey, + apiKey, emitter, }) } @@ -52,6 +56,5 @@ exports.runStep = async function runStep(stepId, inputs) { } } -exports.apiKey = "test" - -exports.actions = actions.ACTION_DEFINITIONS +export const apiKey = "test" +export const actions = ACTION_DEFINITIONS diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.ts similarity index 84% rename from packages/server/src/tests/utilities/TestConfiguration.js rename to packages/server/src/tests/utilities/TestConfiguration.ts index fd9635e9f1..bbd940150f 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -1,6 +1,7 @@ -require("../../db").init() -const env = require("../../environment") -const { +import { init as dbInit } from "../../db" +dbInit() +import env from "../../environment" +import { basicTable, basicRow, basicRole, @@ -11,24 +12,24 @@ const { basicLayout, basicWebhook, TENANT_ID, -} = require("./structures") -const { +} from "./structures" +import { constants, tenancy, sessions, cache, context, - db: dbCore, + db as dbCore, encryption, auth, roles, -} = require("@budibase/backend-core") -const controllers = require("./controllers") +} from "@budibase/backend-core" +import * as controllers from "./controllers" +import { cleanup } from "../../utilities/fileSystem" +import newid from "../../db/newid" +import { generateUserMetadataID } from "../../db/utils" +import { startup } from "../../startup" const supertest = require("supertest") -const { cleanup } = require("../../utilities/fileSystem") -const newid = require("../../db/newid") -const { generateUserMetadataID } = require("../../db/utils") -const { startup } = require("../../startup") const GLOBAL_USER_ID = "us_uuid1" const EMAIL = "babs@babs.com" @@ -37,10 +38,26 @@ const LASTNAME = "Barbington" const CSRF_TOKEN = "e3727778-7af0-4226-b5eb-f43cbe60a306" class TestConfiguration { + server: any + request: any + started: boolean + appId: string | null + allApps: any[] + app: any + prodApp: any + prodAppId: any + user: any + globalUserId: any + userMetadataId: any + table: any + linkedTable: any + automation: any + datasource: any + constructor(openServer = true) { if (openServer) { // use a random port because it doesn't matter - env.PORT = 0 + env.PORT = "0" this.server = require("../../app") // we need the request for logging in, involves cookies, hard to fake this.request = supertest(this.server) @@ -81,7 +98,7 @@ class TestConfiguration { } } - async doInContext(appId, task) { + async doInContext(appId: string | null, task: any) { if (!appId) { appId = this.appId } @@ -125,9 +142,9 @@ class TestConfiguration { // UTILS - async _req(body, params, controlFunc) { + async _req(body: any, params: any, controlFunc: any) { // create a fake request ctx - const request = {} + const request: any = {} const appId = this.appId request.appId = appId // fake cookies, we don't need them @@ -156,8 +173,8 @@ class TestConfiguration { admin = false, email = EMAIL, roles, - } = {}) { - return tenancy.doWithGlobalDB(TENANT_ID, async db => { + }: any = {}) { + return tenancy.doWithGlobalDB(TENANT_ID, async (db: any) => { let existing try { existing = await db.get(id) @@ -221,7 +238,7 @@ class TestConfiguration { } } - async login({ roleId, userId, builder, prodApp = false } = {}) { + async login({ roleId, userId, builder, prodApp = false }: any = {}) { const appId = prodApp ? this.prodAppId : this.appId return context.doInAppContext(appId, async () => { userId = !userId ? `us_uuid1` : userId @@ -278,7 +295,7 @@ class TestConfiguration { } const authToken = auth.jwt.sign(authObj, env.JWT_SECRET) const appToken = auth.jwt.sign(app, env.JWT_SECRET) - const headers = { + const headers: any = { Accept: "application/json", Cookie: [ `${constants.Cookie.Auth}=${authToken}`, @@ -296,7 +313,7 @@ class TestConfiguration { publicHeaders({ prodApp = true } = {}) { const appId = prodApp ? this.prodAppId : this.appId - const headers = { + const headers: any = { Accept: "application/json", } if (appId) { @@ -317,7 +334,7 @@ class TestConfiguration { // API async generateApiKey(userId = GLOBAL_USER_ID) { - return tenancy.doWithGlobalDB(TENANT_ID, async db => { + return tenancy.doWithGlobalDB(TENANT_ID, async (db: any) => { const id = dbCore.generateDevInfoID(userId) let devInfo try { @@ -335,13 +352,15 @@ class TestConfiguration { // APP - async createApp(appName) { + async createApp(appName: string) { // create dev app // clear any old app this.appId = null + // @ts-ignore await context.updateAppId(null) this.app = await this._req({ name: appName }, null, controllers.app.create) this.appId = this.app.appId + // @ts-ignore await context.updateAppId(this.appId) // create production app @@ -355,6 +374,7 @@ class TestConfiguration { async deploy() { await this._req(null, null, controllers.deploy.deployApp) + // @ts-ignore const prodAppId = this.getAppId().replace("_dev", "") this.prodAppId = prodAppId @@ -366,29 +386,29 @@ class TestConfiguration { // TABLE - async updateTable(config = null) { + async updateTable(config?: any) { config = config || basicTable() this.table = await this._req(config, null, controllers.table.save) return this.table } - async createTable(config = null) { + async createTable(config?: any) { if (config != null && config._id) { delete config._id } return this.updateTable(config) } - async getTable(tableId = null) { + async getTable(tableId?: string) { tableId = tableId || this.table._id return this._req(null, { tableId }, controllers.table.find) } - async createLinkedTable(relationshipType = null, links = ["link"]) { + async createLinkedTable(relationshipType?: string, links: any = ["link"]) { if (!this.table) { throw "Must have created a table first." } - const tableConfig = basicTable() + const tableConfig: any = basicTable() tableConfig.primaryDisplay = "name" for (let link of links) { tableConfig.schema[link] = { @@ -407,7 +427,7 @@ class TestConfiguration { } async createAttachmentTable() { - const table = basicTable() + const table: any = basicTable() table.schema.attachment = { type: "attachment", } @@ -416,7 +436,7 @@ class TestConfiguration { // ROW - async createRow(config = null) { + async createRow(config: any = null) { if (!this.table) { throw "Test requires table to be configured." } @@ -425,11 +445,11 @@ class TestConfiguration { return this._req(config, { tableId }, controllers.row.save) } - async getRow(tableId, rowId) { + async getRow(tableId: string, rowId: string) { return this._req(null, { tableId, rowId }, controllers.row.find) } - async getRows(tableId) { + async getRows(tableId: string) { if (!tableId && this.table) { tableId = this.table._id } @@ -438,12 +458,12 @@ class TestConfiguration { // ROLE - async createRole(config = null) { + async createRole(config?: any) { config = config || basicRole() return this._req(config, null, controllers.role.save) } - async addPermission(roleId, resourceId, level = "read") { + async addPermission(roleId: string, resourceId: string, level = "read") { return this._req( null, { @@ -457,7 +477,7 @@ class TestConfiguration { // VIEW - async createView(config) { + async createView(config?: any) { if (!this.table) { throw "Test requires table to be configured." } @@ -470,7 +490,7 @@ class TestConfiguration { // AUTOMATION - async createAutomation(config) { + async createAutomation(config?: any) { config = config || basicAutomation() if (config._rev) { delete config._rev @@ -485,7 +505,7 @@ class TestConfiguration { return this._req(null, null, controllers.automation.fetch) } - async deleteAutomation(automation = null) { + async deleteAutomation(automation?: any) { automation = automation || this.automation if (!automation) { return @@ -497,7 +517,7 @@ class TestConfiguration { ) } - async createWebhook(config = null) { + async createWebhook(config?: any) { if (!this.automation) { throw "Must create an automation before creating webhook." } @@ -507,14 +527,14 @@ class TestConfiguration { // DATASOURCE - async createDatasource(config = null) { + async createDatasource(config?: any) { config = config || basicDatasource() const response = await this._req(config, null, controllers.datasource.save) this.datasource = response.datasource return this.datasource } - async updateDatasource(datasource) { + async updateDatasource(datasource: any) { const response = await this._req( datasource, { datasourceId: datasource._id }, @@ -524,7 +544,7 @@ class TestConfiguration { return this.datasource } - async restDatasource(cfg) { + async restDatasource(cfg?: any) { return this.createDatasource({ datasource: { ...basicDatasource().datasource, @@ -559,7 +579,14 @@ class TestConfiguration { // QUERY - async previewQuery(request, config, datasource, fields, params, verb) { + async previewQuery( + request: any, + config: any, + datasource: any, + fields: any, + params: any, + verb: string + ) { return request .post(`/api/queries/preview`) .send({ @@ -574,7 +601,7 @@ class TestConfiguration { .expect(200) } - async createQuery(config = null) { + async createQuery(config?: any) { if (!this.datasource && !config) { throw "No datasource created for query." } @@ -584,17 +611,17 @@ class TestConfiguration { // SCREEN - async createScreen(config = null) { + async createScreen(config?: any) { config = config || basicScreen() return this._req(config, null, controllers.screen.save) } // LAYOUT - async createLayout(config = null) { + async createLayout(config?: any) { config = config || basicLayout() return await this._req(config, null, controllers.layout.save) } } -module.exports = TestConfiguration +export = TestConfiguration diff --git a/packages/server/tsconfig.build.json b/packages/server/tsconfig.build.json index bbc0bfa0ee..2212a5e100 100644 --- a/packages/server/tsconfig.build.json +++ b/packages/server/tsconfig.build.json @@ -1,7 +1,6 @@ { "compilerOptions": { "target": "es6", - "allowJs": true, "module": "commonjs", "lib": ["es2020"], "strict": true, diff --git a/packages/types/src/documents/app/automation.ts b/packages/types/src/documents/app/automation.ts index f267aa339f..184fed629d 100644 --- a/packages/types/src/documents/app/automation.ts +++ b/packages/types/src/documents/app/automation.ts @@ -115,4 +115,5 @@ export type AutomationStepInput = { context: Record emitter: EventEmitter appId: string + apiKey?: string }