1
0
Fork 0
mirror of synced 2024-07-06 15:00:49 +12:00

Migrate AttachmentAPI.

This commit is contained in:
Sam Rose 2024-02-28 17:43:39 +00:00
parent 7a48fd85ac
commit d9cffa1878
No known key found for this signature in database
4 changed files with 43 additions and 55 deletions

View file

@ -29,7 +29,7 @@ describe("/api/applications/:appId/sync", () => {
let resp = (await config.api.attachment.process(
"ohno.exe",
Buffer.from([0]),
{ expectStatus: 400 }
{ status: 400 }
)) as unknown as APIError
expect(resp.message).toContain("invalid extension")
})
@ -40,7 +40,7 @@ describe("/api/applications/:appId/sync", () => {
let resp = (await config.api.attachment.process(
"OHNO.EXE",
Buffer.from([0]),
{ expectStatus: 400 }
{ status: 400 }
)) as unknown as APIError
expect(resp.message).toContain("invalid extension")
})
@ -51,7 +51,7 @@ describe("/api/applications/:appId/sync", () => {
undefined as any,
undefined as any,
{
expectStatus: 400,
status: 400,
}
)) as unknown as APIError
expect(resp.message).toContain("No file provided")

View file

@ -5,16 +5,11 @@ import {
type FetchAppDefinitionResponse,
type FetchAppPackageResponse,
} from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { Expectations, TestAPI } from "./base"
import { AppStatus } from "../../../db/utils"
import { constants } from "@budibase/backend-core"
export class ApplicationAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
create = async (
app: CreateAppRequest,
expectations?: Expectations

View file

@ -1,35 +1,16 @@
import {
APIError,
Datasource,
ProcessAttachmentResponse,
} from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base"
import { ProcessAttachmentResponse } from "@budibase/types"
import { Expectations, TestAPI } from "./base"
import fs from "fs"
export class AttachmentAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
process = async (
name: string,
file: Buffer | fs.ReadStream | string,
{ expectStatus } = { expectStatus: 200 }
expectations?: Expectations
): Promise<ProcessAttachmentResponse> => {
const result = await this.request
.post(`/api/attachments/process`)
.attach("file", file, name)
.set(this.config.defaultHeaders())
if (result.statusCode !== expectStatus) {
throw new Error(
`Expected status ${expectStatus} but got ${
result.statusCode
}, body: ${JSON.stringify(result.body)}`
)
}
return result.body
return await this._post(`/api/attachments/process`, {
files: { file: { name, file } },
expectations,
})
}
}

View file

@ -1,6 +1,6 @@
import exp from "constants"
import TestConfiguration from "../TestConfiguration"
import { SuperTest, Test } from "supertest"
import { ReadStream } from "fs"
type Headers = Record<string, string | string[] | undefined>
@ -9,6 +9,22 @@ export interface TestAPIOpts {
status?: number
}
export interface AttachedFile {
name: string
file: Buffer | ReadStream | string
}
function isAttachedFile(file: any): file is AttachedFile {
if (file === undefined) {
return false
}
const attachedFile = file as AttachedFile
return (
Object.hasOwnProperty.call(attachedFile, "file") &&
Object.hasOwnProperty.call(attachedFile, "name")
)
}
export interface Expectations {
status?: number
contentType?: string | RegExp
@ -21,7 +37,10 @@ export interface RequestOpts {
query?: Record<string, string | undefined>
body?: Record<string, any>
fields?: Record<string, any>
files?: Record<string, any>
files?: Record<
string,
Buffer | ReadStream | string | AttachedFile | undefined
>
expectations?: Expectations
}
@ -66,8 +85,8 @@ export abstract class TestAPI {
headers = {},
query = {},
body,
fields,
files,
fields = {},
files = {},
expectations,
} = opts || {}
const { status = 200, contentType = /json/ } = expectations || {}
@ -89,30 +108,23 @@ export abstract class TestAPI {
if (body) {
request = request.send(body)
}
if (fields) {
for (const [key, value] of Object.entries(fields)) {
request = request.field(key, value)
}
for (const [key, value] of Object.entries(fields)) {
request = request.field(key, value)
}
if (files) {
for (const [key, value] of Object.entries(files)) {
request = request.attach(key, value)
for (const [key, value] of Object.entries(files)) {
if (isAttachedFile(value)) {
request = request.attach(key, value.file, value.name)
} else {
request = request.attach(key, value as any)
}
}
if (contentType && status !== 204) {
if (contentType instanceof RegExp) {
request = request.expect("Content-Type", contentType)
} else {
request = request.expect("Content-Type", contentType)
}
request = request.expect("Content-Type", contentType as any)
}
if (expectations?.headers) {
for (const [key, value] of Object.entries(expectations.headers)) {
if (value instanceof RegExp) {
request = request.expect(key, value)
} else {
request = request.expect(key, value)
}
request = request.expect(key, value as any)
}
}