1
0
Fork 0
mirror of synced 2024-07-05 22:40:39 +12:00

Migrate ApplicationAPI

This commit is contained in:
Sam Rose 2024-02-28 17:27:15 +00:00
parent 3e76511ffd
commit 7a48fd85ac
No known key found for this signature in database
6 changed files with 149 additions and 159 deletions

View file

@ -184,7 +184,7 @@ describe("/applications", () => {
it("app should not sync if production", async () => {
const { message } = await config.api.application.sync(
app.appId.replace("_dev", ""),
{ statusCode: 400 }
{ status: 400 }
)
expect(message).toEqual(

View file

@ -30,9 +30,9 @@ describe("migrations", () => {
const appId = config.getAppId()
const response = await config.api.application.getRaw(appId)
expect(response.headers[Header.MIGRATING_APP]).toBeUndefined()
await config.api.application.get(appId, {
headersNotPresent: [Header.MIGRATING_APP],
})
})
it("accessing an app that has pending migrations will attach the migrating header", async () => {
@ -46,8 +46,10 @@ describe("migrations", () => {
func: async () => {},
})
const response = await config.api.application.getRaw(appId)
expect(response.headers[Header.MIGRATING_APP]).toEqual(appId)
await config.api.application.get(appId, {
headers: {
[Header.MIGRATING_APP]: appId,
},
})
})
})

View file

@ -1,12 +1,12 @@
import { Response } from "supertest"
import {
App,
PublishResponse,
type CreateAppRequest,
type FetchAppDefinitionResponse,
type FetchAppPackageResponse,
} from "@budibase/types"
import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base"
import { Expectations, TestAPI } from "./base"
import { AppStatus } from "../../../db/utils"
import { constants } from "@budibase/backend-core"
@ -15,179 +15,124 @@ export class ApplicationAPI extends TestAPI {
super(config)
}
create = async (app: CreateAppRequest): Promise<App> => {
const request = this.request
.post("/api/applications")
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
for (const key of Object.keys(app)) {
request.field(key, (app as any)[key])
}
if (app.templateFile) {
request.attach("templateFile", app.templateFile)
}
const result = await request
if (result.statusCode !== 200) {
throw new Error(JSON.stringify(result.body))
}
return result.body as App
create = async (
app: CreateAppRequest,
expectations?: Expectations
): Promise<App> => {
const files = app.templateFile ? { templateFile: app.templateFile } : {}
delete app.templateFile
return await this._post<App>("/api/applications", {
fields: app,
files,
expectations,
})
}
delete = async (appId: string): Promise<void> => {
await this.request
.delete(`/api/applications/${appId}`)
.set(this.config.defaultHeaders())
.expect(200)
delete = async (
appId: string,
expectations?: Expectations
): Promise<void> => {
await this._delete(`/api/applications/${appId}`, { expectations })
}
publish = async (
appId: string
): Promise<{ _id: string; status: string; appUrl: string }> => {
// While the publish endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
let headers = {
...this.config.defaultHeaders(),
[constants.Header.APP_ID]: appId,
}
const result = await this.request
.post(`/api/applications/${appId}/publish`)
.set(headers)
.expect("Content-Type", /json/)
.expect(200)
return result.body as { _id: string; status: string; appUrl: string }
publish = async (appId: string): Promise<PublishResponse> => {
return await this._post<PublishResponse>(
`/api/applications/${appId}/publish`,
{
// While the publish endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
headers: {
[constants.Header.APP_ID]: appId,
},
}
)
}
unpublish = async (appId: string): Promise<void> => {
await this.request
.post(`/api/applications/${appId}/unpublish`)
.set(this.config.defaultHeaders())
.expect(204)
await this._post(`/api/applications/${appId}/unpublish`, {
expectations: { status: 204 },
})
}
sync = async (
appId: string,
{ statusCode }: { statusCode: number } = { statusCode: 200 }
expectations?: Expectations
): Promise<{ message: string }> => {
const result = await this.request
.post(`/api/applications/${appId}/sync`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(statusCode)
return result.body
return await this._post<{ message: string }>(
`/api/applications/${appId}/sync`,
{ expectations }
)
}
getRaw = async (appId: string): Promise<Response> => {
// While the appPackage endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
let headers = {
...this.config.defaultHeaders(),
[constants.Header.APP_ID]: appId,
}
const result = await this.request
.get(`/api/applications/${appId}/appPackage`)
.set(headers)
.expect("Content-Type", /json/)
.expect(200)
return result
}
get = async (appId: string): Promise<App> => {
const result = await this.getRaw(appId)
return result.body.application as App
get = async (appId: string, expectations?: Expectations): Promise<App> => {
return await this._get<App>(`/api/applications/${appId}`, {
// While the get endpoint does take an :appId parameter, it doesn't use
// it. It uses the appId from the context.
headers: {
[constants.Header.APP_ID]: appId,
},
expectations,
})
}
getDefinition = async (
appId: string
appId: string,
expectations?: Expectations
): Promise<FetchAppDefinitionResponse> => {
const result = await this.request
.get(`/api/applications/${appId}/definition`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return result.body as FetchAppDefinitionResponse
return await this._get<FetchAppDefinitionResponse>(
`/api/applications/${appId}/definition`,
{ expectations }
)
}
getAppPackage = async (appId: string): Promise<FetchAppPackageResponse> => {
const result = await this.request
.get(`/api/applications/${appId}/appPackage`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return result.body
getAppPackage = async (
appId: string,
expectations?: Expectations
): Promise<FetchAppPackageResponse> => {
return await this._get<FetchAppPackageResponse>(
`/api/applications/${appId}/appPackage`,
{ expectations }
)
}
update = async (
appId: string,
app: { name?: string; url?: string }
app: { name?: string; url?: string },
expectations?: Expectations
): Promise<App> => {
const request = this.request
.put(`/api/applications/${appId}`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
for (const key of Object.keys(app)) {
request.field(key, (app as any)[key])
}
const result = await request
if (result.statusCode !== 200) {
throw new Error(JSON.stringify(result.body))
}
return result.body as App
return await this._put<App>(`/api/applications/${appId}`, {
fields: app,
expectations,
})
}
updateClient = async (appId: string): Promise<void> => {
// While the updateClient endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
let headers = {
...this.config.defaultHeaders(),
[constants.Header.APP_ID]: appId,
}
const response = await this.request
.post(`/api/applications/${appId}/client/update`)
.set(headers)
.expect("Content-Type", /json/)
if (response.statusCode !== 200) {
throw new Error(JSON.stringify(response.body))
}
updateClient = async (
appId: string,
expectations?: Expectations
): Promise<void> => {
await this._post(`/api/applications/${appId}/client/update`, {
// While the updateClient endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
headers: {
[constants.Header.APP_ID]: appId,
},
expectations,
})
}
revertClient = async (appId: string): Promise<void> => {
// While the revertClient endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
let headers = {
...this.config.defaultHeaders(),
[constants.Header.APP_ID]: appId,
}
const response = await this.request
.post(`/api/applications/${appId}/client/revert`)
.set(headers)
.expect("Content-Type", /json/)
if (response.statusCode !== 200) {
throw new Error(JSON.stringify(response.body))
}
await this._post(`/api/applications/${appId}/client/revert`, {
// While the revertClient endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context.
headers: {
[constants.Header.APP_ID]: appId,
},
})
}
fetch = async ({ status }: { status?: AppStatus } = {}): Promise<App[]> => {
let query = []
if (status) {
query.push(`status=${status}`)
}
const result = await this.request
.get(`/api/applications${query.length ? `?${query.join("&")}` : ""}`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
return result.body as App[]
return await this._get<App[]>("/api/applications", {
query: { status },
})
}
}

View file

@ -1,3 +1,4 @@
import exp from "constants"
import TestConfiguration from "../TestConfiguration"
import { SuperTest, Test } from "supertest"
@ -11,10 +12,13 @@ export interface TestAPIOpts {
export interface Expectations {
status?: number
contentType?: string | RegExp
headers?: Record<string, string | RegExp>
headersNotPresent?: string[]
}
export interface RequestOpts {
headers?: Headers
query?: Record<string, string | undefined>
body?: Record<string, any>
fields?: Record<string, any>
files?: Record<string, any>
@ -30,11 +34,8 @@ export abstract class TestAPI {
this.request = config.request!
}
protected _get = async <T>(
url: string,
expectations?: Expectations
): Promise<T> => {
return await this._request<T>("get", url, { expectations })
protected _get = async <T>(url: string, opts?: RequestOpts): Promise<T> => {
return await this._request<T>("get", url, opts)
}
protected _post = async <T>(url: string, opts?: RequestOpts): Promise<T> => {
@ -61,9 +62,26 @@ export abstract class TestAPI {
url: string,
opts?: RequestOpts
): Promise<T> => {
const { headers = {}, body, fields, files, expectations } = opts || {}
const {
headers = {},
query = {},
body,
fields,
files,
expectations,
} = opts || {}
const { status = 200, contentType = /json/ } = expectations || {}
let queryParams = []
for (const [key, value] of Object.entries(query)) {
if (value) {
queryParams.push(`${key}=${value}`)
}
}
if (queryParams.length) {
url += `?${queryParams.join("&")}`
}
let request = this.request[method](url).set(this.config.defaultHeaders())
if (headers) {
request = request.set(headers)
@ -81,13 +99,22 @@ export abstract class TestAPI {
request = request.attach(key, value)
}
}
if (contentType) {
if (contentType && status !== 204) {
if (contentType instanceof RegExp) {
request = request.expect("Content-Type", contentType)
} else {
request = request.expect("Content-Type", contentType)
}
}
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)
}
}
}
const response = await request
@ -99,6 +126,16 @@ export abstract class TestAPI {
)
}
if (expectations?.headersNotPresent) {
for (const header of expectations.headersNotPresent) {
if (response.headers[header]) {
throw new Error(
`Expected header ${header} not to be present, found value "${response.headers[header]}"`
)
}
}
}
return response.body as T
}
}

View file

@ -19,11 +19,11 @@ export class TableAPI extends TestAPI {
}
fetch = async (expectations?: Expectations): Promise<Table[]> => {
return await this._get<Table[]>("/api/tables", expectations)
return await this._get<Table[]>("/api/tables", { expectations })
}
get = async (tableId: string, expectations: Expectations): Promise<Table> => {
return await this._get<Table>(`/api/tables/${tableId}`, expectations)
return await this._get<Table>(`/api/tables/${tableId}`, { expectations })
}
migrate = async (

View file

@ -27,3 +27,9 @@ export interface FetchAppPackageResponse {
clientLibPath: string
hasLock: boolean
}
export interface PublishResponse {
_id: string
status: string
appUrl: string
}