Convert BackupAPI.

This commit is contained in:
Sam Rose 2024-02-29 14:33:09 +00:00
parent 6b306266b5
commit 5163434b08
No known key found for this signature in database
3 changed files with 62 additions and 55 deletions

View File

@ -19,11 +19,8 @@ describe("/backups", () => {
describe("/api/backups/export", () => {
it("should be able to export app", async () => {
const { body, headers } = await config.api.backup.exportBasicBackup(
config.getAppId()!
)
const body = await config.api.backup.exportBasicBackup(config.getAppId()!)
expect(body instanceof Buffer).toBe(true)
expect(headers["content-type"]).toEqual("application/gzip")
expect(events.app.exported).toBeCalledTimes(1)
})
@ -38,15 +35,13 @@ describe("/backups", () => {
it("should infer the app name from the app", async () => {
tk.freeze(mocks.date.MOCK_DATE)
const { headers } = await config.api.backup.exportBasicBackup(
config.getAppId()!
)
expect(headers["content-disposition"]).toEqual(
`attachment; filename="${
config.getApp().name
}-export-${mocks.date.MOCK_DATE.getTime()}.tar.gz"`
)
await config.api.backup.exportBasicBackup(config.getAppId()!, {
headers: {
"content-disposition": `attachment; filename="${
config.getApp().name
}-export-${mocks.date.MOCK_DATE.getTime()}.tar.gz"`,
},
})
})
})

View File

@ -2,42 +2,38 @@ import {
CreateAppBackupResponse,
ImportAppBackupResponse,
} from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base"
import { Expectations, TestAPI } from "./base"
export class BackupAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
exportBasicBackup = async (appId: string) => {
const result = await this.request
.post(`/api/backups/export?appId=${appId}`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /application\/gzip/)
.expect(200)
return {
body: result.body as Buffer,
headers: result.headers,
exportBasicBackup = async (appId: string, expectations?: Expectations) => {
const exp = {
...expectations,
headers: {
...expectations?.headers,
"Content-Type": "application/gzip",
},
}
return await this._post<Buffer>(`/api/backups/export`, {
query: { appId },
expectations: exp,
})
}
createBackup = async (appId: string) => {
const result = await this.request
.post(`/api/apps/${appId}/backups`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return result.body as CreateAppBackupResponse
createBackup = async (appId: string, expectations?: Expectations) => {
return await this._post<CreateAppBackupResponse>(
`/api/apps/${appId}/backups`,
{ expectations }
)
}
waitForBackupToComplete = async (appId: string, backupId: string) => {
for (let i = 0; i < 10; i++) {
await new Promise(resolve => setTimeout(resolve, 1000))
const result = await this.request
.get(`/api/apps/${appId}/backups/${backupId}/file`)
.set(this.config.defaultHeaders())
if (result.status === 200) {
const response = await this._requestRaw(
"get",
`/api/apps/${appId}/backups/${backupId}/file`
)
if (response.status === 200) {
return
}
}
@ -46,13 +42,12 @@ export class BackupAPI extends TestAPI {
importBackup = async (
appId: string,
backupId: string
backupId: string,
expectations?: Expectations
): Promise<ImportAppBackupResponse> => {
const result = await this.request
.post(`/api/apps/${appId}/backups/${backupId}/import`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return result.body as ImportAppBackupResponse
return await this._post<ImportAppBackupResponse>(
`/api/apps/${appId}/backups/${backupId}/import`,
{ expectations }
)
}
}

View File

@ -1,5 +1,5 @@
import TestConfiguration from "../TestConfiguration"
import { SuperTest, Test } from "supertest"
import { SuperTest, Test, Response } from "supertest"
import { ReadStream } from "fs"
type Headers = Record<string, string | string[] | undefined>
@ -22,7 +22,6 @@ function isAttachedFile(file: any): file is AttachedFile {
export interface Expectations {
status?: number
contentType?: string | RegExp
headers?: Record<string, string | RegExp>
headersNotPresent?: string[]
}
@ -71,11 +70,11 @@ export abstract class TestAPI {
return await this._request<T>("delete", url, opts)
}
protected _request = async <T>(
protected _requestRaw = async (
method: "get" | "post" | "put" | "patch" | "delete",
url: string,
opts?: RequestOpts
): Promise<T> => {
): Promise<Response> => {
const {
headers = {},
query = {},
@ -84,7 +83,12 @@ export abstract class TestAPI {
files = {},
expectations,
} = opts || {}
const { status = 200, contentType = /json/ } = expectations || {}
const { status = 200 } = expectations || {}
const expectHeaders = expectations?.headers || {}
if (status !== 204 && !expectHeaders["Content-Type"]) {
expectHeaders["Content-Type"] = "application/json"
}
let queryParams = []
for (const [key, value] of Object.entries(query)) {
@ -118,16 +122,29 @@ export abstract class TestAPI {
request = request.attach(key, value as any)
}
}
if (contentType && status !== 204) {
request = request.expect("Content-Type", contentType as any)
}
if (expectations?.headers) {
for (const [key, value] of Object.entries(expectations.headers)) {
if (value === undefined) {
throw new Error(
`Got an undefined expected value for header "${key}", if you want to check for the absence of a header, use headersNotPresent`
)
}
request = request.expect(key, value as any)
}
}
const response = await request
return await request
}
protected _request = async <T>(
method: "get" | "post" | "put" | "patch" | "delete",
url: string,
opts?: RequestOpts
): Promise<T> => {
const { expectations } = opts || {}
const { status = 200 } = expectations || {}
const response = await this._requestRaw(method, url, opts)
if (response.status !== status) {
let message = `Expected status ${status} but got ${response.status}`