Merge pull request #13814 from Budibase/test-template-import

Add tests for creating apps from templates.
This commit is contained in:
Sam Rose 2024-05-30 14:11:08 +01:00 committed by GitHub
commit be24a70024
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 146 additions and 24 deletions

View File

@ -1,24 +0,0 @@
const setup = require("./utilities")
describe("/templates", () => {
let request = setup.getRequest()
let config = setup.getConfig()
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
})
describe("fetch", () => {
it("should be able to fetch templates", async () => {
const res = await request
.get(`/api/templates`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
// this test is quite light right now, templates aren't heavily utilised yet
expect(Array.isArray(res.body)).toEqual(true)
})
})
})

View File

@ -0,0 +1,125 @@
import * as setup from "./utilities"
import path from "path"
import nock from "nock"
import { generator } from "@budibase/backend-core/tests"
interface App {
background: string
icon: string
category: string
description: string
name: string
url: string
type: string
key: string
image: string
}
interface Manifest {
templates: {
app: { [key: string]: App }
}
}
function setManifest(manifest: Manifest) {
nock("https://prod-budi-templates.s3-eu-west-1.amazonaws.com")
.get("/manifest.json")
.reply(200, manifest)
}
function mockApp(key: string, tarPath: string) {
nock("https://prod-budi-templates.s3-eu-west-1.amazonaws.com")
.get(`/templates/app/${key}.tar.gz`)
.replyWithFile(200, tarPath)
}
function mockAgencyClientPortal() {
setManifest({
templates: {
app: {
"Agency Client Portal": {
background: "#20a3a8",
icon: "Project",
category: "Portals",
description:
"Manage clients, streamline communications, and securely share files.",
name: "Agency Client Portal",
url: "https://budibase.com/portals/templates/agency-client-portal-template/",
type: "app",
key: "app/agency-client-portal",
image:
"https://prod-budi-templates.s3.eu-west-1.amazonaws.com/images/agency-client-portal.png",
},
},
},
})
mockApp(
"agency-client-portal",
path.resolve(__dirname, "data", "agency-client-portal.tar.gz")
)
}
describe("/templates", () => {
let config = setup.getConfig()
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
})
beforeEach(() => {
nock.cleanAll()
mockAgencyClientPortal()
})
describe("fetch", () => {
it("should be able to fetch templates", async () => {
const templates = await config.api.templates.fetch()
expect(templates).toHaveLength(1)
expect(templates[0].name).toBe("Agency Client Portal")
})
})
describe("create app from template", () => {
it.each(["sqs", "lucene"])(
`should be able to create an app from a template (%s)`,
async source => {
const env = {
SQS_SEARCH_ENABLE: source === "sqs" ? "true" : "false",
}
await config.withEnv(env, async () => {
const name = generator.guid().replaceAll("-", "")
const url = `/${name}`
const app = await config.api.application.create({
name,
url,
useTemplate: "true",
templateName: "Agency Client Portal",
templateKey: "app/agency-client-portal",
})
expect(app.name).toBe(name)
expect(app.url).toBe(url)
await config.withApp(app, async () => {
const tables = await config.api.table.fetch()
expect(tables).toHaveLength(2)
tables.sort((a, b) => a.name.localeCompare(b.name))
const [agencyProjects, users] = tables
expect(agencyProjects.name).toBe("Agency Projects")
expect(users.name).toBe("Users")
const { rows } = await config.api.row.search(agencyProjects._id!, {
tableId: agencyProjects._id!,
query: {},
})
expect(rows).toHaveLength(3)
})
})
}
)
})
})

View File

@ -314,6 +314,16 @@ export default class TestConfiguration {
}
}
async withApp(app: App | string, f: () => Promise<void>) {
const oldAppId = this.appId
this.appId = typeof app === "string" ? app : app.appId
try {
return await f()
} finally {
this.appId = oldAppId
}
}
// UTILS
_req<Req extends Record<string, any> | void, Res>(

View File

@ -12,6 +12,7 @@ import { AttachmentAPI } from "./attachment"
import { UserAPI } from "./user"
import { QueryAPI } from "./query"
import { RoleAPI } from "./role"
import { TemplateAPI } from "./template"
export default class API {
table: TableAPI
@ -27,6 +28,7 @@ export default class API {
user: UserAPI
query: QueryAPI
roles: RoleAPI
templates: TemplateAPI
constructor(config: TestConfiguration) {
this.table = new TableAPI(config)
@ -42,5 +44,6 @@ export default class API {
this.user = new UserAPI(config)
this.query = new QueryAPI(config)
this.roles = new RoleAPI(config)
this.templates = new TemplateAPI(config)
}
}

View File

@ -0,0 +1,8 @@
import { Template } from "@budibase/types"
import { Expectations, TestAPI } from "./base"
export class TemplateAPI extends TestAPI {
fetch = async (expectations?: Expectations): Promise<Template[]> => {
return await this._get<Template[]>("/api/templates", { expectations })
}
}