Finish modernising application.spec.ts

This commit is contained in:
Sam Rose 2024-02-26 16:00:12 +00:00
parent b9600d8330
commit 04e5699c9c
No known key found for this signature in database
3 changed files with 65 additions and 41 deletions

View File

@ -4,7 +4,6 @@ import * as deploymentController from "../controllers/deploy"
import authorized from "../../middleware/authorized" import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core" import { permissions } from "@budibase/backend-core"
import { applicationValidator } from "./utils/validators" import { applicationValidator } from "./utils/validators"
import { importToApp } from "../controllers/application"
const router: Router = new Router() const router: Router = new Router()

View File

@ -17,11 +17,9 @@ import { AppStatus } from "../../../db/utils"
import { events, utils, context } from "@budibase/backend-core" import { events, utils, context } from "@budibase/backend-core"
import env from "../../../environment" import env from "../../../environment"
import type { App } from "@budibase/types" import type { App } from "@budibase/types"
import tk from "timekeeper"
jest.setTimeout(150000000)
describe("/applications", () => { describe("/applications", () => {
let request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()
let app: App let app: App
@ -143,50 +141,37 @@ describe("/applications", () => {
describe("manage client library version", () => { describe("manage client library version", () => {
it("should be able to update the app client library version", async () => { it("should be able to update the app client library version", async () => {
await request await config.api.application.updateClient(app.appId)
.post(`/api/applications/${config.getAppId()}/client/update`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(events.app.versionUpdated).toBeCalledTimes(1) expect(events.app.versionUpdated).toBeCalledTimes(1)
}) })
it("should be able to revert the app client library version", async () => { it("should be able to revert the app client library version", async () => {
// We need to first update the version so that we can then revert await config.api.application.updateClient(app.appId)
await request await config.api.application.revertClient(app.appId)
.post(`/api/applications/${config.getAppId()}/client/update`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
await request
.post(`/api/applications/${config.getAppId()}/client/revert`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(events.app.versionReverted).toBeCalledTimes(1) expect(events.app.versionReverted).toBeCalledTimes(1)
}) })
}) })
describe("edited at", () => { describe("edited at", () => {
it("middleware should set edited at", async () => { it("middleware should set updatedAt", async () => {
const headers = config.defaultHeaders() const app = await tk.withFreeze(
headers["referer"] = `/${config.getAppId()}/test` "2021-01-01",
const res = await request async () => await config.api.application.create({ name: utils.newid() })
.put(`/api/applications/${config.getAppId()}`) )
.send({ expect(app.updatedAt).toEqual("2021-01-01T00:00:00.000Z")
name: "UPDATED_NAME",
}) const updatedApp = await tk.withFreeze(
.set(headers) "2021-02-01",
.expect("Content-Type", /json/) async () =>
.expect(200) await config.api.application.update(app.appId, {
expect(res.body._rev).toBeDefined() name: "UPDATED_NAME",
// retrieve the app to check it })
const getRes = await request )
.get(`/api/applications/${config.getAppId()}/appPackage`) expect(updatedApp._rev).toBeDefined()
.set(headers) expect(updatedApp.updatedAt).toEqual("2021-02-01T00:00:00.000Z")
.expect("Content-Type", /json/)
.expect(200) const fetchedApp = await config.api.application.get(app.appId)
expect(getRes.body.application.updatedAt).toBeDefined() expect(fetchedApp.updatedAt).toEqual("2021-02-01T00:00:00.000Z")
}) })
}) })

View File

@ -48,7 +48,7 @@ export class ApplicationAPI extends TestAPI {
publish = async ( publish = async (
appId: string appId: string
): Promise<{ _id: string; status: string; appUrl: string }> => { ): Promise<{ _id: string; status: string; appUrl: string }> => {
// While the publsih endpoint does take an :appId parameter, it doesn't // While the publish endpoint does take an :appId parameter, it doesn't
// use it. It uses the appId from the context. // use it. It uses the appId from the context.
let headers = { let headers = {
...this.config.defaultHeaders(), ...this.config.defaultHeaders(),
@ -82,9 +82,15 @@ export class ApplicationAPI extends TestAPI {
} }
getRaw = async (appId: string): Promise<Response> => { 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 const result = await this.request
.get(`/api/applications/${appId}/appPackage`) .get(`/api/applications/${appId}/appPackage`)
.set(this.config.defaultHeaders()) .set(headers)
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
return result return result
@ -137,6 +143,40 @@ export class ApplicationAPI extends TestAPI {
return result.body as App return result.body as App
} }
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))
}
}
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))
}
}
fetch = async ({ status }: { status?: AppStatus } = {}): Promise<App[]> => { fetch = async ({ status }: { status?: AppStatus } = {}): Promise<App[]> => {
let query = [] let query = []
if (status) { if (status) {