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

View File

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

View File

@ -1,35 +1,16 @@
import { import { ProcessAttachmentResponse } from "@budibase/types"
APIError, import { Expectations, TestAPI } from "./base"
Datasource,
ProcessAttachmentResponse,
} from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base"
import fs from "fs" import fs from "fs"
export class AttachmentAPI extends TestAPI { export class AttachmentAPI extends TestAPI {
constructor(config: TestConfiguration) {
super(config)
}
process = async ( process = async (
name: string, name: string,
file: Buffer | fs.ReadStream | string, file: Buffer | fs.ReadStream | string,
{ expectStatus } = { expectStatus: 200 } expectations?: Expectations
): Promise<ProcessAttachmentResponse> => { ): Promise<ProcessAttachmentResponse> => {
const result = await this.request return await this._post(`/api/attachments/process`, {
.post(`/api/attachments/process`) files: { file: { name, file } },
.attach("file", file, name) expectations,
.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
} }
} }

View File

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