[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 <Hinton@users.noreply.github.com>

* [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 <Hinton@users.noreply.github.com>
This commit is contained in:
Rui Tomé 2022-12-02 19:38:12 +00:00 committed by GitHub
parent 0a73290714
commit 9dc72428d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 133 additions and 1 deletions

View File

@ -150,6 +150,10 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
return Promise.resolve(BrowserApi.getApplicationVersion());
}
async getApplicationVersionNumber(): Promise<string> {
return (await this.getApplicationVersion()).split(RegExp("[+|-]"))[0].trim();
}
supportsWebAuthn(win: Window): boolean {
return typeof PublicKeyCredential !== "undefined";
}

View File

@ -88,6 +88,10 @@ export class CliPlatformUtilsService implements PlatformUtilsService {
return Promise.resolve(this.packageJson.version);
}
async getApplicationVersionNumber(): Promise<string> {
return (await this.getApplicationVersion()).split(RegExp("[+|-]"))[0].trim();
}
getApplicationVersionSync(): string {
return this.packageJson.version;
}

View File

@ -87,6 +87,10 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService {
return ipcRenderer.invoke("appVersion");
}
async getApplicationVersionNumber(): Promise<string> {
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 {

View File

@ -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("<empty>", 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("<empty>", 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");
});
});
});

View File

@ -108,6 +108,10 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
return Promise.resolve(process.env.APPLICATION_VERSION || "-");
}
async getApplicationVersionNumber(): Promise<string> {
return (await this.getApplicationVersion()).split(RegExp("[+|-]"))[0].trim();
}
supportsWebAuthn(win: Window): boolean {
return typeof PublicKeyCredential !== "undefined";
}

View File

@ -19,6 +19,7 @@ export abstract class PlatformUtilsService {
isViewOpen: () => Promise<boolean>;
launchUri: (uri: string, options?: any) => void;
getApplicationVersion: () => Promise<string>;
getApplicationVersionNumber: () => Promise<string>;
supportsWebAuthn: (win: Window) => boolean;
supportsDuo: () => boolean;
showToast: (

View File

@ -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);
}