From 9dc72428d096f0795ab583441f44361c772e0e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Tom=C3=A9?= <108268980+r-tome@users.noreply.github.com> Date: Fri, 2 Dec 2022 19:38:12 +0000 Subject: [PATCH] [EC-584] Update ApiService to remove any appendages to ClientVersion (#4008) * [EC-584] Update ApiService to remove any appendages to ClientVersion * [EC-584] Extract application version number logic from ApiService to PlatformUtils * Update libs/electron/src/services/electronPlatformUtils.service.ts Co-authored-by: Oscar Hinton * [EC-584] Use getApplicationVersion as source for getApplicationVersionNumber * [EC-584] Remove defaulting to dash on getApplicationVersionNumber and add unit tests Co-authored-by: Oscar Hinton --- .../services/browserPlatformUtils.service.ts | 4 + .../services/cli-platform-utils.service.ts | 4 + .../electron-platform-utils.service.ts | 4 + .../core/web-platform-utils.service.spec.ts | 115 ++++++++++++++++++ .../app/core/web-platform-utils.service.ts | 4 + .../src/abstractions/platformUtils.service.ts | 1 + libs/common/src/services/api.service.ts | 2 +- 7 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 apps/web/src/app/core/web-platform-utils.service.spec.ts diff --git a/apps/browser/src/services/browserPlatformUtils.service.ts b/apps/browser/src/services/browserPlatformUtils.service.ts index feb50a5ccf..3d33fa174e 100644 --- a/apps/browser/src/services/browserPlatformUtils.service.ts +++ b/apps/browser/src/services/browserPlatformUtils.service.ts @@ -150,6 +150,10 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService return Promise.resolve(BrowserApi.getApplicationVersion()); } + async getApplicationVersionNumber(): Promise { + return (await this.getApplicationVersion()).split(RegExp("[+|-]"))[0].trim(); + } + supportsWebAuthn(win: Window): boolean { return typeof PublicKeyCredential !== "undefined"; } diff --git a/apps/cli/src/services/cli-platform-utils.service.ts b/apps/cli/src/services/cli-platform-utils.service.ts index de7349c1a8..9a10cf62c2 100644 --- a/apps/cli/src/services/cli-platform-utils.service.ts +++ b/apps/cli/src/services/cli-platform-utils.service.ts @@ -88,6 +88,10 @@ export class CliPlatformUtilsService implements PlatformUtilsService { return Promise.resolve(this.packageJson.version); } + async getApplicationVersionNumber(): Promise { + return (await this.getApplicationVersion()).split(RegExp("[+|-]"))[0].trim(); + } + getApplicationVersionSync(): string { return this.packageJson.version; } diff --git a/apps/desktop/src/services/electron-platform-utils.service.ts b/apps/desktop/src/services/electron-platform-utils.service.ts index 1b710b85f2..8af865d845 100644 --- a/apps/desktop/src/services/electron-platform-utils.service.ts +++ b/apps/desktop/src/services/electron-platform-utils.service.ts @@ -87,6 +87,10 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService { return ipcRenderer.invoke("appVersion"); } + async getApplicationVersionNumber(): Promise { + return (await this.getApplicationVersion()).split(/[+|-]/)[0].trim(); + } + // Temporarily restricted to only Windows until https://github.com/electron/electron/pull/28349 // has been merged and an updated electron build is available. supportsWebAuthn(win: Window): boolean { diff --git a/apps/web/src/app/core/web-platform-utils.service.spec.ts b/apps/web/src/app/core/web-platform-utils.service.spec.ts new file mode 100644 index 0000000000..5b0271b822 --- /dev/null +++ b/apps/web/src/app/core/web-platform-utils.service.spec.ts @@ -0,0 +1,115 @@ +import { WebPlatformUtilsService } from "./web-platform-utils.service"; + +describe("Web Platform Utils Service", () => { + let webPlatformUtilsService: WebPlatformUtilsService; + + beforeEach(() => { + webPlatformUtilsService = new WebPlatformUtilsService(null, null, null); + }); + + afterEach(() => { + delete process.env.APPLICATION_VERSION; + }); + + describe("getApplicationVersion", () => { + test("null", async () => { + delete process.env.APPLICATION_VERSION; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("-"); + }); + + test("", async () => { + process.env.APPLICATION_VERSION = ""; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("-"); + }); + + test("{version number}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2"; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("2022.10.2"); + }); + + test("{version number} - {git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2 - 5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("2022.10.2 - 5f8c1c1"); + }); + + test("{version number}-{git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2-5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("2022.10.2-5f8c1c1"); + }); + + test("{version number} + {git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2 + 5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("2022.10.2 + 5f8c1c1"); + }); + + test("{version number}+{git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2+5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersion(); + expect(result).toBe("2022.10.2+5f8c1c1"); + }); + }); + + describe("getApplicationVersionNumber", () => { + test("null", async () => { + delete process.env.APPLICATION_VERSION; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe(""); + }); + + test("", async () => { + process.env.APPLICATION_VERSION = ""; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe(""); + }); + + test("{version number}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2"; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe("2022.10.2"); + }); + + test("{version number} - {git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2 - 5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe("2022.10.2"); + }); + + test("{version number}-{git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2-5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe("2022.10.2"); + }); + + test("{version number} + {git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2 + 5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe("2022.10.2"); + }); + + test("{version number}+{git hash}", async () => { + process.env.APPLICATION_VERSION = "2022.10.2+5f8c1c1"; + + const result = await webPlatformUtilsService.getApplicationVersionNumber(); + expect(result).toBe("2022.10.2"); + }); + }); +}); diff --git a/apps/web/src/app/core/web-platform-utils.service.ts b/apps/web/src/app/core/web-platform-utils.service.ts index 115d53401a..bb8bf7707e 100644 --- a/apps/web/src/app/core/web-platform-utils.service.ts +++ b/apps/web/src/app/core/web-platform-utils.service.ts @@ -108,6 +108,10 @@ export class WebPlatformUtilsService implements PlatformUtilsService { return Promise.resolve(process.env.APPLICATION_VERSION || "-"); } + async getApplicationVersionNumber(): Promise { + return (await this.getApplicationVersion()).split(RegExp("[+|-]"))[0].trim(); + } + supportsWebAuthn(win: Window): boolean { return typeof PublicKeyCredential !== "undefined"; } diff --git a/libs/common/src/abstractions/platformUtils.service.ts b/libs/common/src/abstractions/platformUtils.service.ts index c4300d1322..8f2f546127 100644 --- a/libs/common/src/abstractions/platformUtils.service.ts +++ b/libs/common/src/abstractions/platformUtils.service.ts @@ -19,6 +19,7 @@ export abstract class PlatformUtilsService { isViewOpen: () => Promise; launchUri: (uri: string, options?: any) => void; getApplicationVersion: () => Promise; + getApplicationVersionNumber: () => Promise; supportsWebAuthn: (win: Window) => boolean; supportsDuo: () => boolean; showToast: ( diff --git a/libs/common/src/services/api.service.ts b/libs/common/src/services/api.service.ts index d1bf0325b5..dddbe7a58b 100644 --- a/libs/common/src/services/api.service.ts +++ b/libs/common/src/services/api.service.ts @@ -2086,7 +2086,7 @@ export class ApiService implements ApiServiceAbstraction { request.headers.set("Bitwarden-Client-Name", this.platformUtilsService.getClientType()); request.headers.set( "Bitwarden-Client-Version", - await this.platformUtilsService.getApplicationVersion() + await this.platformUtilsService.getApplicationVersionNumber() ); return this.nativeFetch(request); }