diff --git a/packages/server/src/api/routes/tests/data/agency-client-portal.tar.gz b/packages/server/src/api/routes/tests/data/agency-client-portal.tar.gz new file mode 100644 index 0000000000..e839dc73ee Binary files /dev/null and b/packages/server/src/api/routes/tests/data/agency-client-portal.tar.gz differ diff --git a/packages/server/src/api/routes/tests/templates.spec.js b/packages/server/src/api/routes/tests/templates.spec.js deleted file mode 100644 index 1406a75c59..0000000000 --- a/packages/server/src/api/routes/tests/templates.spec.js +++ /dev/null @@ -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) - }) - }) -}) diff --git a/packages/server/src/api/routes/tests/templates.spec.ts b/packages/server/src/api/routes/tests/templates.spec.ts new file mode 100644 index 0000000000..8a5cfb3537 --- /dev/null +++ b/packages/server/src/api/routes/tests/templates.spec.ts @@ -0,0 +1,80 @@ +import * as setup from "./utilities" +import nock from "nock" + +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(`/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", + "packages/server/src/api/routes/tests/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 res = await config.api.templates.fetch() + expect(res).toHaveLength(1) + expect(res[0].name).toBe("Agency Client Portal") + }) + }) +}) diff --git a/packages/server/src/tests/utilities/api/index.ts b/packages/server/src/tests/utilities/api/index.ts index d66acd86fd..554fa36588 100644 --- a/packages/server/src/tests/utilities/api/index.ts +++ b/packages/server/src/tests/utilities/api/index.ts @@ -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) } } diff --git a/packages/server/src/tests/utilities/api/template.ts b/packages/server/src/tests/utilities/api/template.ts new file mode 100644 index 0000000000..6dc2a7a4da --- /dev/null +++ b/packages/server/src/tests/utilities/api/template.ts @@ -0,0 +1,8 @@ +import { Template } from "@budibase/types" +import { Expectations, TestAPI } from "./base" + +export class TemplateAPI extends TestAPI { + fetch = async (expectations?: Expectations): Promise => { + return await this._get("/api/templates", { expectations }) + } +}