From 90e869dc045abcf69852693a60a96b370f91e51b Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Sat, 8 Jul 2023 13:07:10 +0100 Subject: [PATCH] /api/global/license/offline/identifier API --- packages/types/src/api/web/global/license.ts | 6 ++++++ packages/types/src/sdk/licensing/license.ts | 10 ++++++++++ packages/worker/__mocks__/@budibase/pro.ts | 1 + .../src/api/controllers/global/license.ts | 7 +++++++ .../worker/src/api/routes/global/license.ts | 1 + .../src/api/routes/global/tests/license.spec.ts | 17 ++++++++++++++--- packages/worker/src/tests/api/license.ts | 5 +++++ 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/types/src/api/web/global/license.ts b/packages/types/src/api/web/global/license.ts index 0196b69c7f..4a36c4f1d8 100644 --- a/packages/types/src/api/web/global/license.ts +++ b/packages/types/src/api/web/global/license.ts @@ -17,3 +17,9 @@ export interface ActivateOfflineLicenseRequest { export interface GetOfflineLicenseResponse { offlineLicenseToken: string } + +// IDENTIFIER + +export interface GetOfflineIdentifierResponse { + identifierBase64: string +} diff --git a/packages/types/src/sdk/licensing/license.ts b/packages/types/src/sdk/licensing/license.ts index b287e67adf..ebc874bca0 100644 --- a/packages/types/src/sdk/licensing/license.ts +++ b/packages/types/src/sdk/licensing/license.ts @@ -1,5 +1,15 @@ import { PurchasedPlan, Quotas, Feature, Billing } from "." +export interface OfflineIdentifier { + installId: string, + tenantId: string +} + +// export interface OfflineLicense extends License { +// identifier?: OfflineIdentifier +// identifierBase64: string +// } + export interface License { features: Feature[] quotas: Quotas diff --git a/packages/worker/__mocks__/@budibase/pro.ts b/packages/worker/__mocks__/@budibase/pro.ts index a9611ba705..bd6250fede 100644 --- a/packages/worker/__mocks__/@budibase/pro.ts +++ b/packages/worker/__mocks__/@budibase/pro.ts @@ -11,6 +11,7 @@ const pro = { activateOfflineLicense: jest.fn(), getOfflineLicenseToken: jest.fn(), deleteOfflineLicenseToken: jest.fn(), + getIdentifierBase64: jest.fn() }, cache: { ...actual.licensing.cache, diff --git a/packages/worker/src/api/controllers/global/license.ts b/packages/worker/src/api/controllers/global/license.ts index b8c8566018..73b3c72d1e 100644 --- a/packages/worker/src/api/controllers/global/license.ts +++ b/packages/worker/src/api/controllers/global/license.ts @@ -3,6 +3,7 @@ import { ActivateLicenseKeyRequest, ActivateOfflineLicenseRequest, GetLicenseKeyResponse, + GetOfflineIdentifierResponse, GetOfflineLicenseResponse, UserCtx, } from "@budibase/types" @@ -53,6 +54,12 @@ export async function deleteOfflineLicense(ctx: UserCtx) { ctx.status = 204 } +export async function getOfflineLicenseIdentifier(ctx: UserCtx) { + const identifierBase64 = await licensing.offline.getIdentifierBase64() + ctx.body = { identifierBase64 } + ctx.status = 200 +} + // LICENSES export const refresh = async (ctx: any) => { diff --git a/packages/worker/src/api/routes/global/license.ts b/packages/worker/src/api/routes/global/license.ts index 889d7d1ed4..1d3d9c460b 100644 --- a/packages/worker/src/api/routes/global/license.ts +++ b/packages/worker/src/api/routes/global/license.ts @@ -24,5 +24,6 @@ router .post("/api/global/license/offline", activateOfflineLicenseValidator, controller.activateOfflineLicense) .get("/api/global/license/offline", controller.getOfflineLicense) .delete("/api/global/license/offline", controller.deleteOfflineLicense) + .get("/api/global/license/offline/identifier", controller.getOfflineLicenseIdentifier) export default router diff --git a/packages/worker/src/api/routes/global/tests/license.spec.ts b/packages/worker/src/api/routes/global/tests/license.spec.ts index c3c423c833..512d6c9c52 100644 --- a/packages/worker/src/api/routes/global/tests/license.spec.ts +++ b/packages/worker/src/api/routes/global/tests/license.spec.ts @@ -26,7 +26,7 @@ describe("/api/global/license", () => { }) describe("GET /api/global/license/usage", () => { - it("returns 200", async () => { + it("returns 200 + usage", async () => { const usage = structures.quotas.usage() quotas.getQuotaUsage.mockResolvedValue(usage) const res = await config.api.license.getUsage() @@ -79,7 +79,7 @@ describe("/api/global/license", () => { const res = await config.api.license.getOfflineLicense() expect(res.status).toBe(404) }) - it("returns 200", async () => { + it("returns 200 + offline license token", async () => { licensing.offline.getOfflineLicenseToken.mockResolvedValue("offlineLicenseToken") const res = await config.api.license.getOfflineLicense() expect(res.status).toBe(200) @@ -90,10 +90,21 @@ describe("/api/global/license", () => { }) describe("DELETE /api/global/license/offline", () => { - it("deletes offline license", async () => { + it("returns 204", async () => { const res = await config.api.license.deleteOfflineLicense() expect(res.status).toBe(204) expect(licensing.offline.deleteOfflineLicenseToken).toBeCalledTimes(1) }) }) + + describe("GET /api/global/license/offline/identifier", () => { + it("returns 200 + identifier base64", async () => { + licensing.offline.getIdentifierBase64.mockResolvedValue("base64") + const res = await config.api.license.getOfflineLicenseIdentifier() + expect(res.status).toBe(200) + expect(res.body).toEqual({ + identifierBase64: "base64" + }) + }) + }) }) diff --git a/packages/worker/src/tests/api/license.ts b/packages/worker/src/tests/api/license.ts index 89f85b25e3..76d3e88df7 100644 --- a/packages/worker/src/tests/api/license.ts +++ b/packages/worker/src/tests/api/license.ts @@ -51,4 +51,9 @@ export class LicenseAPI extends TestAPI { .delete("/api/global/license/offline") .set(this.config.defaultHeaders()) } + getOfflineLicenseIdentifier = async () => { + return this.request + .get("/api/global/license/offline/identifier") + .set(this.config.defaultHeaders()) + } }