From 83d4994f55d356d2f841c2653bbcac1df9e34f54 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 25 Feb 2022 15:55:19 +0000 Subject: [PATCH] Starting to fix up test cases. --- .../src/middleware/authenticated.js | 1 + .../backend-core/src/objectStore/index.js | 25 +++-- .../api/controllers/public/applications.ts | 6 ++ .../src/api/routes/public/applications.ts | 4 +- .../server/src/api/routes/public/index.ts | 9 +- .../api/routes/public/tests/compare.spec.js | 99 +++++++++++++++++-- .../server/src/api/routes/utils/validators.js | 13 ++- .../src/tests/utilities/TestConfiguration.js | 4 + 8 files changed, 137 insertions(+), 24 deletions(-) diff --git a/packages/backend-core/src/middleware/authenticated.js b/packages/backend-core/src/middleware/authenticated.js index 1e920739c4..ee815ea330 100644 --- a/packages/backend-core/src/middleware/authenticated.js +++ b/packages/backend-core/src/middleware/authenticated.js @@ -136,6 +136,7 @@ module.exports = ( // allow configuring for public access if ((opts && opts.publicAllowed) || publicEndpoint) { finalise(ctx, { authenticated: false, version, publicEndpoint }) + return next() } else { ctx.throw(err.status || 403, err) } diff --git a/packages/backend-core/src/objectStore/index.js b/packages/backend-core/src/objectStore/index.js index b5d8475cee..2385149f4d 100644 --- a/packages/backend-core/src/objectStore/index.js +++ b/packages/backend-core/src/objectStore/index.js @@ -78,6 +78,7 @@ exports.ObjectStore = bucket => { const config = { s3ForcePathStyle: true, signatureVersion: "v4", + apiVersion: "2006-03-01", params: { Bucket: sanitizeBucket(bucket), }, @@ -102,17 +103,21 @@ exports.makeSureBucketExists = async (client, bucketName) => { .promise() } catch (err) { const promises = STATE.bucketCreationPromises + const doesntExist = err.statusCode === 404, + noAccess = err.statusCode === 403 if (promises[bucketName]) { await promises[bucketName] - } else if (err.statusCode === 404) { - // bucket doesn't exist create it - promises[bucketName] = client - .createBucket({ - Bucket: bucketName, - }) - .promise() - await promises[bucketName] - delete promises[bucketName] + } else if (doesntExist || noAccess) { + if (doesntExist) { + // bucket doesn't exist create it + promises[bucketName] = client + .createBucket({ + Bucket: bucketName, + }) + .promise() + await promises[bucketName] + delete promises[bucketName] + } // public buckets are quite hidden in the system, make sure // no bucket is set accidentally if (PUBLIC_BUCKETS.includes(bucketName)) { @@ -124,7 +129,7 @@ exports.makeSureBucketExists = async (client, bucketName) => { .promise() } } else { - throw err + throw new Error("Unable to write to object store bucket.") } } } diff --git a/packages/server/src/api/controllers/public/applications.ts b/packages/server/src/api/controllers/public/applications.ts index 8c65ea122a..844d5967ff 100644 --- a/packages/server/src/api/controllers/public/applications.ts +++ b/packages/server/src/api/controllers/public/applications.ts @@ -30,6 +30,12 @@ export async function search(ctx: any) { } export async function create(ctx: any) { + if (!ctx.request.body || !ctx.request.body.useTemplate) { + ctx.request.body = { + useTemplate: false, + ...ctx.request.body, + } + } await controller.create(ctx) await setResponseApp(ctx) } diff --git a/packages/server/src/api/routes/public/applications.ts b/packages/server/src/api/routes/public/applications.ts index b11e142663..09272c57b3 100644 --- a/packages/server/src/api/routes/public/applications.ts +++ b/packages/server/src/api/routes/public/applications.ts @@ -33,7 +33,7 @@ const read = [], */ write.push( new Endpoint("post", "/applications", controller.create).addMiddleware( - applicationValidator + applicationValidator() ) ) @@ -65,7 +65,7 @@ write.push( */ write.push( new Endpoint("put", "/applications/:appId", controller.update).addMiddleware( - applicationValidator + applicationValidator() ) ) diff --git a/packages/server/src/api/routes/public/index.ts b/packages/server/src/api/routes/public/index.ts index 6930ce1aae..719e6ee29d 100644 --- a/packages/server/src/api/routes/public/index.ts +++ b/packages/server/src/api/routes/public/index.ts @@ -20,6 +20,9 @@ const publicRouter = new Router({ }) function addMiddleware(endpoints: any, middleware: CtxFn) { + if (!endpoints) { + return + } if (!Array.isArray(endpoints)) { endpoints = [endpoints] } @@ -29,8 +32,10 @@ function addMiddleware(endpoints: any, middleware: CtxFn) { } function addToRouter(endpoints: any) { - for (let endpoint of endpoints) { - endpoint.apply(publicRouter) + if (endpoints) { + for (let endpoint of endpoints) { + endpoint.apply(publicRouter) + } } } diff --git a/packages/server/src/api/routes/public/tests/compare.spec.js b/packages/server/src/api/routes/public/tests/compare.spec.js index 8eda8c63ad..961738cd56 100644 --- a/packages/server/src/api/routes/public/tests/compare.spec.js +++ b/packages/server/src/api/routes/public/tests/compare.spec.js @@ -18,10 +18,12 @@ beforeAll(async () => { afterAll(setup.afterAll) -async function makeRequest(method, endpoint, body, appId) { +async function makeRequest(method, endpoint, body, appId = config.getAppId()) { const extraHeaders = { "x-budibase-api-key": apiKey, - "x-budibase-app-id": appId ? appId : config.getAppId(), + } + if (appId) { + extraHeaders["x-budibase-app-id"] = appId } const req = request [method](checkSlashesInUrl(`/api/public/v1/${endpoint}`)) @@ -39,36 +41,115 @@ describe("check the applications endpoints", () => { const res = await makeRequest("post", "/applications/search") expect(res).toSatisfyApiSpec() }) -}) -describe("check the tables endpoints", () => { - it("should allow retrieving applications through search", async () => { - const res = await makeRequest("post", "/tables/search") + it("should allow creating an application", async () => { + const res = await makeRequest("post", "/applications", { + name: "new App" + }, null) + expect(res).toSatisfyApiSpec() + }) + + it("should allow updating an application", async () => { + const app = config.getApp() + const appId = config.getAppId() + const res = await makeRequest("put", `/applications/${appId}`, { + ...app, + name: "updated app name", + }, appId) + expect(res).toSatisfyApiSpec() + }) + + it("should allow retrieving an application", async () => { + const res = await makeRequest("get", `/applications/${config.getAppId()}`) + expect(res).toSatisfyApiSpec() + }) + + it("should allow deleting an application", async () => { + const res = await makeRequest("delete", `/applications/${config.getAppId()}`) expect(res).toSatisfyApiSpec() }) }) +describe("check the tables endpoints", () => { + it("should allow retrieving tables through search", async () => { + const res = await makeRequest("post", "/tables/search") + expect(res).toSatisfyApiSpec() + }) + + it("should allow creating a table", async () => { + + }) + + it("should allow updating a table", async () => { + + }) + + it("should allow retrieving a table", async () => { + + }) + + it("should allow deleting a table", async () => { + + }) +}) + describe("check the rows endpoints", () => { - it("should allow retrieving applications through search", async () => { + it("should allow retrieving rows through search", async () => { const res = await makeRequest("post", `/tables/${table._id}/rows/search`, { query: { }, }) expect(res).toSatisfyApiSpec() }) + + it("should allow creating a row", async () => { + + }) + + it("should allow updating a row", async () => { + + }) + + it("should allow retrieving a row", async () => { + + }) + + it("should allow deleting a row", async () => { + + }) }) describe("check the users endpoints", () => { - it("should allow retrieving applications through search", async () => { + it("should allow retrieving users through search", async () => { const res = await makeRequest("post", "/users/search") expect(res).toSatisfyApiSpec() }) + + it("should allow creating a user", async () => { + + }) + + it("should allow updating a user", async () => { + + }) + + it("should allow retrieving a user", async () => { + + }) + + it("should allow deleting a user", async () => { + + }) }) describe("check the queries endpoints", () => { - it("should allow retrieving applications through search", async () => { + it("should allow retrieving queries through search", async () => { const res = await makeRequest("post", "/queries/search") expect(res).toSatisfyApiSpec() }) + + it("should allow executing a query", async () => { + + }) }) diff --git a/packages/server/src/api/routes/utils/validators.js b/packages/server/src/api/routes/utils/validators.js index 3830c4cddb..dac936911b 100644 --- a/packages/server/src/api/routes/utils/validators.js +++ b/packages/server/src/api/routes/utils/validators.js @@ -205,4 +205,15 @@ exports.automationValidator = (existing = false) => { }).unknown(true)) } -exports.applicationValidator = () => {} +exports.applicationValidator = () => { + // prettier-ignore + return joiValidator.body(Joi.object({ + _id: OPTIONAL_STRING, + _rev: OPTIONAL_STRING, + name: Joi.string().required(), + url: OPTIONAL_STRING, + template: Joi.object({ + templateString: OPTIONAL_STRING, + }).unknown(true), + }).unknown(true)) +} diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.js index 79b51ca363..a3544154f3 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.js @@ -49,6 +49,10 @@ class TestConfiguration { return this.request } + getApp() { + return this.app + } + getAppId() { return this.appId }