1
0
Fork 0
mirror of synced 2024-07-20 21:55:54 +12:00

Adding basic tests for public API app export/import.

This commit is contained in:
mike12345567 2023-09-22 18:43:18 +01:00
parent fe5cc6878b
commit c0a2940d8b
3 changed files with 137 additions and 13 deletions

@ -1 +1 @@
Subproject commit 6c193aa50bc5eca7757db886c8457f516dafc1b8
Subproject commit 874321ba70cfe6ae3d7515b3b7d66185c4fe03cc

View file

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

View file

@ -11,6 +11,32 @@ export type MakeRequestResponse = (
intAppId?: string
) => Promise<supertest.Response>
export type MakeRequestWithFormDataResponse = (
method: HttpMethod,
endpoint: string,
fields: Record<string, string | { path: string }>,
intAppId?: string
) => Promise<supertest.Response>
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<string, string | { path: string }>,
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
}
}