From e240947ad800b811e4d9c7173a2fb001efd8fbae Mon Sep 17 00:00:00 2001 From: Maciej Zieniuk Date: Fri, 24 Jan 2025 09:04:10 +0000 Subject: [PATCH] unit tests fix --- .../biometric-message-handler.service.spec.ts | 32 +++++++++++++++++-- libs/common/spec/fake-account-service.ts | 4 +-- .../biometric-state.service.spec.ts | 4 +-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/services/biometric-message-handler.service.spec.ts b/apps/desktop/src/services/biometric-message-handler.service.spec.ts index a7f0f555ca2..bfe7491efc4 100644 --- a/apps/desktop/src/services/biometric-message-handler.service.spec.ts +++ b/apps/desktop/src/services/biometric-message-handler.service.spec.ts @@ -2,7 +2,7 @@ import { NgZone } from "@angular/core"; import { mock, MockProxy } from "jest-mock-extended"; import { of } from "rxjs"; -import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service"; @@ -27,7 +27,7 @@ import { BiometricMessageHandlerService } from "./biometric-message-handler.serv const SomeUser = "SomeUser" as UserId; const AnotherUser = "SomeOtherUser" as UserId; -const accounts = { +const accounts: Record = { [SomeUser]: { name: "some user", email: "some.user@example.com", @@ -108,6 +108,30 @@ describe("BiometricMessageHandlerService", () => { }); describe("setup encryption", () => { + it("should ignore when public key missing in message", async () => { + await service.handleMessage({ + appId: "appId", + message: { + command: "setupEncryption", + messageId: 0, + userId: "unknownUser" as UserId, + }, + }); + expect((global as any).ipc.platform.nativeMessaging.sendMessage).not.toHaveBeenCalled(); + }); + + it("should ignore when user id missing in message", async () => { + await service.handleMessage({ + appId: "appId", + message: { + command: "setupEncryption", + messageId: 0, + publicKey: Utils.fromUtf8ToB64("publicKey"), + }, + }); + expect((global as any).ipc.platform.nativeMessaging.sendMessage).not.toHaveBeenCalled(); + }); + it("should reject when user is not in app", async () => { await service.handleMessage({ appId: "appId", @@ -115,6 +139,7 @@ describe("BiometricMessageHandlerService", () => { command: "setupEncryption", messageId: 0, userId: "unknownUser" as UserId, + publicKey: Utils.fromUtf8ToB64("publicKey"), }, }); expect((global as any).ipc.platform.nativeMessaging.sendMessage).toHaveBeenCalledWith({ @@ -362,12 +387,15 @@ describe("BiometricMessageHandlerService", () => { // always reload when another user is active than the requested one [SomeUser, AuthenticationStatus.Unlocked, AnotherUser, false, true], [SomeUser, AuthenticationStatus.Locked, AnotherUser, false, true], + // don't reload when no active user + [null, AuthenticationStatus.Unlocked, AnotherUser, false, false], // don't reload in dev mode [SomeUser, AuthenticationStatus.Unlocked, SomeUser, true, false], [SomeUser, AuthenticationStatus.Locked, SomeUser, true, false], [SomeUser, AuthenticationStatus.Unlocked, AnotherUser, true, false], [SomeUser, AuthenticationStatus.Locked, AnotherUser, true, false], + [null, AuthenticationStatus.Unlocked, AnotherUser, true, false], ]; it.each(testCases)( diff --git a/libs/common/spec/fake-account-service.ts b/libs/common/spec/fake-account-service.ts index 05e44d5db18..2a84bec91be 100644 --- a/libs/common/spec/fake-account-service.ts +++ b/libs/common/spec/fake-account-service.ts @@ -1,7 +1,7 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { mock } from "jest-mock-extended"; -import { ReplaySubject, combineLatest, map } from "rxjs"; +import { ReplaySubject, combineLatest, map, Observable } from "rxjs"; import { Account, AccountInfo, AccountService } from "../src/auth/abstractions/account.service"; import { UserId } from "../src/types/guid"; @@ -52,7 +52,7 @@ export class FakeAccountService implements AccountService { }), ); } - get nextUpAccount$() { + get nextUpAccount$(): Observable { return combineLatest([this.accounts$, this.activeAccount$, this.sortedUserIds$]).pipe( map(([accounts, activeAccount, sortedUserIds]) => { const nextId = sortedUserIds.find((id) => id !== activeAccount?.id && accounts[id] != null); diff --git a/libs/key-management/src/biometrics/biometric-state.service.spec.ts b/libs/key-management/src/biometrics/biometric-state.service.spec.ts index 850f461d908..338826dfcba 100644 --- a/libs/key-management/src/biometrics/biometric-state.service.spec.ts +++ b/libs/key-management/src/biometrics/biometric-state.service.spec.ts @@ -7,11 +7,9 @@ import { FakeStateProvider, FakeGlobalState, FakeSingleUserState, -} from "@bitwarden/common/spec"; -import { FakeAccountService, mockAccountServiceWith, -} from "@bitwarden/common/spec/fake-account-service"; +} from "@bitwarden/common/spec"; import { UserId } from "@bitwarden/common/types/guid"; import { BiometricStateService, DefaultBiometricStateService } from "./biometric-state.service";