From aaff5260d31bc70af349f4b2967d30b8403fa186 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 16 Nov 2021 17:40:31 +0000 Subject: [PATCH] Adding a script to be able to debug backend like cypress runs, without needing to build everytime - fixing an issue that appears to only occur occasionally in the cypress environment. --- packages/auth/src/cache/appMetadata.js | 18 ++++++++++++-- packages/builder/cypress/setup.js | 1 + packages/server/scripts/likeCypress.ts | 33 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/server/scripts/likeCypress.ts diff --git a/packages/auth/src/cache/appMetadata.js b/packages/auth/src/cache/appMetadata.js index 400714987b..982f95dbe5 100644 --- a/packages/auth/src/cache/appMetadata.js +++ b/packages/auth/src/cache/appMetadata.js @@ -18,11 +18,16 @@ const populateFromDB = async (appId, CouchDB = null) => { return db.get(DocumentTypes.APP_METADATA) } +const isInvalid = metadata => { + return !metadata || metadata.state === AppState.INVALID +} + /** * Get the requested app metadata by id. * Use redis cache to first read the app metadata. * If not present fallback to loading the app metadata directly and re-caching. - * @param {*} appId the id of the app to get metadata from. + * @param {string} appId the id of the app to get metadata from. + * @param {object} CouchDB the database being passed * @returns {object} the app metadata. */ exports.getAppMetadata = async (appId, CouchDB = null) => { @@ -44,10 +49,19 @@ exports.getAppMetadata = async (appId, CouchDB = null) => { throw err } } + // needed for cypress/some scenarios where the caching happens + // so quickly the requests can get slightly out of sync + // might store its invalid just before it stores its valid + if (isInvalid(metadata)) { + const temp = await client.get(appId) + if (temp) { + metadata = temp + } + } await client.store(appId, metadata, expiry) } // we've stored in the cache an object to tell us that it is currently invalid - if (!metadata || metadata.state === AppState.INVALID) { + if (isInvalid(metadata)) { throw { status: 404, message: "No app metadata found" } } return metadata diff --git a/packages/builder/cypress/setup.js b/packages/builder/cypress/setup.js index 1a6f1d5b2b..7657303853 100644 --- a/packages/builder/cypress/setup.js +++ b/packages/builder/cypress/setup.js @@ -17,6 +17,7 @@ process.env.JWT_SECRET = cypressConfig.env.JWT_SECRET process.env.COUCH_URL = `leveldb://${tmpdir}/.data/` process.env.SELF_HOSTED = 1 process.env.WORKER_URL = "http://localhost:10002/" +process.env.APPS_URL = `http://localhost:${MAIN_PORT}/` process.env.MINIO_URL = `http://localhost:${MAIN_PORT}/` process.env.MINIO_ACCESS_KEY = "budibase" process.env.MINIO_SECRET_KEY = "budibase" diff --git a/packages/server/scripts/likeCypress.ts b/packages/server/scripts/likeCypress.ts new file mode 100644 index 0000000000..505dbef523 --- /dev/null +++ b/packages/server/scripts/likeCypress.ts @@ -0,0 +1,33 @@ +/****************************************************** + * This script just makes it easy to re-create * + * a cypress like environment for testing the backend * + ******************************************************/ +const path = require("path") +const tmpdir = path.join(require("os").tmpdir(), ".budibase") + +const MAIN_PORT = "4002" +const WORKER_PORT = "4001" + +// @ts-ignore +process.env.PORT = "4001" +process.env.BUDIBASE_API_KEY = "6BE826CB-6B30-4AEC-8777-2E90464633DE" +process.env.NODE_ENV = "cypress" +process.env.ENABLE_ANALYTICS = "false" +process.env.JWT_SECRET = "budibase" +process.env.COUCH_URL = `leveldb://${tmpdir}/.data/` +process.env.SELF_HOSTED = "1" +process.env.WORKER_URL = "http://localhost:4002/" +process.env.MINIO_URL = `http://localhost:4001/` +process.env.MINIO_ACCESS_KEY = "budibase" +process.env.MINIO_SECRET_KEY = "budibase" +process.env.COUCH_DB_USER = "budibase" +process.env.COUCH_DB_PASSWORD = "budibase" +process.env.INTERNAL_API_KEY = "budibase" +process.env.ALLOW_DEV_AUTOMATIONS = "1" + +// don't make this a variable or top level require +// it will cause environment module to be loaded prematurely +const server = require("../src/app") +process.env.PORT = "4002" +const worker = require("../../worker/src/index") +process.env.PORT = "4001" \ No newline at end of file