diff --git a/packages/pro b/packages/pro index 6c193aa50b..874321ba70 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 6c193aa50bc5eca7757db886c8457f516dafc1b8 +Subproject commit 874321ba70cfe6ae3d7515b3b7d66185c4fe03cc diff --git a/packages/server/src/api/routes/public/tests/applications.spec.ts b/packages/server/src/api/routes/public/tests/applications.spec.ts new file mode 100644 index 0000000000..fae049da75 --- /dev/null +++ b/packages/server/src/api/routes/public/tests/applications.spec.ts @@ -0,0 +1,81 @@ +import * as setup from "../../tests/utilities" +import { + generateMakeRequest, + generateMakeRequestWithFormData, + MakeRequestResponse, + MakeRequestWithFormDataResponse, +} from "./utils" +import { User } from "@budibase/types" +import { join } from "path" +import { mocks } from "@budibase/backend-core/tests" + +const PASSWORD = "testtest" +const NO_LICENSE_MSG = "Endpoint unavailable, license required." + +let config = setup.getConfig() +let apiKey: string, + globalUser: User, + makeRequest: MakeRequestResponse, + makeRequestFormData: MakeRequestWithFormDataResponse + +beforeAll(async () => { + await config.init() + globalUser = await config.globalUser() + apiKey = await config.generateApiKey(globalUser._id) + makeRequest = generateMakeRequest(apiKey) + makeRequestFormData = generateMakeRequestWithFormData(apiKey) +}) + +afterAll(setup.afterAll) + +describe("check export/import", () => { + async function runExport() { + return await makeRequest("post", `/applications/${config.appId}/export`, { + encryptionPassword: PASSWORD, + excludeRows: true, + }) + } + + async function runImport() { + const pathToExport = join( + __dirname, + "..", + "..", + "tests", + "assets", + "export.tar.gz" + ) + return await makeRequestFormData( + "post", + `/applications/${config.appId}/import`, + { + encryptionPassword: PASSWORD, + appExport: { path: pathToExport }, + } + ) + } + + it("check licensing for export", async () => { + const res = await runExport() + expect(res.status).toBe(403) + expect(res.body.message).toBe(NO_LICENSE_MSG) + }) + + it("check licensing for import", async () => { + const res = await runImport() + expect(res.status).toBe(403) + expect(res.body.message).toBe(NO_LICENSE_MSG) + }) + + it("should be able to export app", async () => { + mocks.licenses.useExpandedPublicApi() + const res = await runExport() + expect(res.status).toBe(200) + }) + + it("should be able to import app", async () => { + mocks.licenses.useExpandedPublicApi() + const res = await runImport() + expect(res.status).toBe(204) + }) +}) diff --git a/packages/server/src/api/routes/public/tests/utils.ts b/packages/server/src/api/routes/public/tests/utils.ts index 755e2d659f..1b57682af9 100644 --- a/packages/server/src/api/routes/public/tests/utils.ts +++ b/packages/server/src/api/routes/public/tests/utils.ts @@ -11,6 +11,32 @@ export type MakeRequestResponse = ( intAppId?: string ) => Promise +export type MakeRequestWithFormDataResponse = ( + method: HttpMethod, + endpoint: string, + fields: Record, + intAppId?: string +) => Promise + +function base( + apiKey: string, + endpoint: string, + intAppId: string | null, + isInternal: boolean +) { + const extraHeaders: any = { + "x-budibase-api-key": apiKey, + } + if (intAppId) { + extraHeaders["x-budibase-app-id"] = intAppId + } + + const url = isInternal + ? endpoint + : checkSlashesInUrl(`/api/public/v1/${endpoint}`) + return { headers: extraHeaders, url } +} + export function generateMakeRequest( apiKey: string, isInternal = false @@ -23,18 +49,8 @@ export function generateMakeRequest( body?: any, intAppId: string | null = config.getAppId() ) => { - const extraHeaders: any = { - "x-budibase-api-key": apiKey, - } - if (intAppId) { - extraHeaders["x-budibase-app-id"] = intAppId - } - - const url = isInternal - ? endpoint - : checkSlashesInUrl(`/api/public/v1/${endpoint}`) - - const req = request[method](url).set(config.defaultHeaders(extraHeaders)) + const { headers, url } = base(apiKey, endpoint, intAppId, isInternal) + const req = request[method](url).set(config.defaultHeaders(headers)) if (body) { req.send(body) } @@ -43,3 +59,30 @@ export function generateMakeRequest( return res } } + +export function generateMakeRequestWithFormData( + apiKey: string, + isInternal = false +): MakeRequestWithFormDataResponse { + const request = setup.getRequest()! + const config = setup.getConfig()! + return async ( + method: HttpMethod, + endpoint: string, + fields: Record, + intAppId: string | null = config.getAppId() + ) => { + const { headers, url } = base(apiKey, endpoint, intAppId, isInternal) + const req = request[method](url).set(config.defaultHeaders(headers)) + for (let [field, value] of Object.entries(fields)) { + if (typeof value === "string") { + req.field(field, value) + } else { + req.attach(field, value.path) + } + } + const res = await req + expect(res.body).toBeDefined() + return res + } +}