From 028ba8ba1279547e441af68c671de2ec74e926e7 Mon Sep 17 00:00:00 2001 From: rr-bw <102181210+rr-bw@users.noreply.github.com> Date: Thu, 18 Dec 2025 11:48:16 -0800 Subject: [PATCH] update Default, Web, and Extension tests to use mockAccountInfoWith() --- ...ion-auth-request-answering.service.spec.ts | 21 +++-- ...top-auth-request-answering.service.spec.ts | 28 +++--- ...ult-auth-request-answering.service.spec.ts | 92 +++++++------------ 3 files changed, 62 insertions(+), 79 deletions(-) diff --git a/apps/browser/src/auth/services/auth-request-answering/extension-auth-request-answering.service.spec.ts b/apps/browser/src/auth/services/auth-request-answering/extension-auth-request-answering.service.spec.ts index 9ca2522939f..4817576a8e0 100644 --- a/apps/browser/src/auth/services/auth-request-answering/extension-auth-request-answering.service.spec.ts +++ b/apps/browser/src/auth/services/auth-request-answering/extension-auth-request-answering.service.spec.ts @@ -1,7 +1,7 @@ import { mock, MockProxy } from "jest-mock-extended"; import { of } from "rxjs"; -import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { Account, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthRequestAnsweringService } from "@bitwarden/common/auth/abstractions/auth-request-answering/auth-request-answering.service.abstraction"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthServerNotificationTags } from "@bitwarden/common/auth/enums/auth-server-notification-tags"; @@ -17,6 +17,7 @@ import { SystemNotificationEvent, SystemNotificationsService, } from "@bitwarden/common/platform/system-notifications/system-notifications.service"; +import { mockAccountInfoWith } from "@bitwarden/common/spec"; import { LogService } from "@bitwarden/logging"; import { UserId } from "@bitwarden/user-core"; @@ -37,6 +38,15 @@ describe("ExtensionAuthRequestAnsweringService", () => { let sut: AuthRequestAnsweringService; const userId = "9f4c3452-6a45-48af-a7d0-74d3e8b65e4c" as UserId; + const userAccountInfo = mockAccountInfoWith({ + name: "User", + email: "user@example.com", + }); + const userAccount: Account = { + id: userId, + ...userAccountInfo, + }; + const authRequestId = "auth-request-id-123"; beforeEach(() => { @@ -55,14 +65,9 @@ describe("ExtensionAuthRequestAnsweringService", () => { // Common defaults authService.activeAccountStatus$ = of(AuthenticationStatus.Locked); - accountService.activeAccount$ = of({ - id: userId, - email: "user@example.com", - emailVerified: true, - name: "User", - }); + accountService.activeAccount$ = of(userAccount); accountService.accounts$ = of({ - [userId]: { email: "user@example.com", emailVerified: true, name: "User" }, + [userId]: userAccountInfo, }); platformUtilsService.isPopupOpen.mockResolvedValue(false); i18nService.t.mockImplementation( diff --git a/apps/desktop/src/auth/services/auth-request-answering/desktop-auth-request-answering.service.spec.ts b/apps/desktop/src/auth/services/auth-request-answering/desktop-auth-request-answering.service.spec.ts index aa2f17b8dee..2caaf713473 100644 --- a/apps/desktop/src/auth/services/auth-request-answering/desktop-auth-request-answering.service.spec.ts +++ b/apps/desktop/src/auth/services/auth-request-answering/desktop-auth-request-answering.service.spec.ts @@ -1,7 +1,7 @@ import { mock, MockProxy } from "jest-mock-extended"; import { of } from "rxjs"; -import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { Account, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthRequestAnsweringService } from "@bitwarden/common/auth/abstractions/auth-request-answering/auth-request-answering.service.abstraction"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; @@ -9,6 +9,7 @@ import { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/for import { PendingAuthRequestsStateService } from "@bitwarden/common/auth/services/auth-request-answering/pending-auth-requests.state"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; +import { mockAccountInfoWith } from "@bitwarden/common/spec"; import { LogService } from "@bitwarden/logging"; import { UserId } from "@bitwarden/user-core"; @@ -26,6 +27,15 @@ describe("DesktopAuthRequestAnsweringService", () => { let sut: AuthRequestAnsweringService; const userId = "9f4c3452-6a45-48af-a7d0-74d3e8b65e4c" as UserId; + const userAccountInfo = mockAccountInfoWith({ + name: "User", + email: "user@example.com", + }); + const userAccount: Account = { + id: userId, + ...userAccountInfo, + }; + const authRequestId = "auth-request-id-123"; beforeEach(() => { @@ -50,14 +60,9 @@ describe("DesktopAuthRequestAnsweringService", () => { // Common defaults authService.activeAccountStatus$ = of(AuthenticationStatus.Locked); - accountService.activeAccount$ = of({ - id: userId, - email: "user@example.com", - emailVerified: true, - name: "User", - }); + accountService.activeAccount$ = of(userAccount); accountService.accounts$ = of({ - [userId]: { email: "user@example.com", emailVerified: true, name: "User" }, + [userId]: userAccountInfo, }); (global as any).ipc.platform.isWindowVisible.mockResolvedValue(false); i18nService.t.mockImplementation( @@ -193,9 +198,10 @@ describe("DesktopAuthRequestAnsweringService", () => { const differentUserId = "different-user-id" as UserId; accountService.activeAccount$ = of({ id: differentUserId, - email: "different@example.com", - emailVerified: true, - name: "Different User", + ...mockAccountInfoWith({ + name: "Different User", + email: "different@example.com", + }), }); }); diff --git a/libs/common/src/auth/services/auth-request-answering/default-auth-request-answering.service.spec.ts b/libs/common/src/auth/services/auth-request-answering/default-auth-request-answering.service.spec.ts index 94ebdb0232f..624133d1e31 100644 --- a/libs/common/src/auth/services/auth-request-answering/default-auth-request-answering.service.spec.ts +++ b/libs/common/src/auth/services/auth-request-answering/default-auth-request-answering.service.spec.ts @@ -1,7 +1,7 @@ import { mock, MockProxy } from "jest-mock-extended"; import { BehaviorSubject, of, Subject } from "rxjs"; -import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { Account, 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 { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/force-set-password-reason"; @@ -10,6 +10,7 @@ import { ButtonLocation, SystemNotificationEvent, } from "@bitwarden/common/platform/system-notifications/system-notifications.service"; +import { mockAccountInfoWith } from "@bitwarden/common/spec"; import { UserId } from "@bitwarden/user-core"; import { AuthRequestAnsweringService } from "../../abstractions/auth-request-answering/auth-request-answering.service.abstraction"; @@ -30,7 +31,24 @@ describe("DefaultAuthRequestAnsweringService", () => { let sut: AuthRequestAnsweringService; const userId = "9f4c3452-6a45-48af-a7d0-74d3e8b65e4c" as UserId; + const userAccountInfo = mockAccountInfoWith({ + name: "User", + email: "user@example.com", + }); + const userAccount: Account = { + id: userId, + ...userAccountInfo, + }; + const otherUserId = "554c3112-9a75-23af-ab80-8dk3e9bl5i8e" as UserId; + const otherUserAccountInfo = mockAccountInfoWith({ + name: "Other", + email: "other@example.com", + }); + const otherUserAccount: Account = { + id: otherUserId, + ...otherUserAccountInfo, + }; beforeEach(() => { accountService = mock(); @@ -43,15 +61,10 @@ describe("DefaultAuthRequestAnsweringService", () => { // Common defaults authService.activeAccountStatus$ = of(AuthenticationStatus.Locked); - accountService.activeAccount$ = of({ - id: userId, - email: "user@example.com", - emailVerified: true, - name: "User", - }); + accountService.activeAccount$ = of(userAccount); accountService.accounts$ = of({ - [userId]: { email: "user@example.com", emailVerified: true, name: "User" }, - [otherUserId]: { email: "other@example.com", emailVerified: true, name: "Other User" }, + [userId]: userAccountInfo, + [otherUserId]: otherUserAccountInfo, }); sut = new DefaultAuthRequestAnsweringService( @@ -124,19 +137,14 @@ describe("DefaultAuthRequestAnsweringService", () => { describe("setupUnlockListenersForProcessingAuthRequests()", () => { let destroy$: Subject; - let activeAccount$: BehaviorSubject; + let activeAccount$: BehaviorSubject; let activeAccountStatus$: BehaviorSubject; let authStatusForSubjects: Map>; let pendingRequestMarkers: PendingAuthUserMarker[]; beforeEach(() => { destroy$ = new Subject(); - activeAccount$ = new BehaviorSubject({ - id: userId, - email: "user@example.com", - emailVerified: true, - name: "User", - }); + activeAccount$ = new BehaviorSubject(userAccount); activeAccountStatus$ = new BehaviorSubject(AuthenticationStatus.Locked); authStatusForSubjects = new Map(); pendingRequestMarkers = []; @@ -169,12 +177,7 @@ describe("DefaultAuthRequestAnsweringService", () => { sut.setupUnlockListenersForProcessingAuthRequests(destroy$); // Simulate account switching to an Unlocked account - activeAccount$.next({ - id: otherUserId, - email: "other@example.com", - emailVerified: true, - name: "Other", - }); + activeAccount$.next(otherUserAccount); // Assert await new Promise((resolve) => setTimeout(resolve, 0)); // Allows observable chain to complete before assertion @@ -189,12 +192,7 @@ describe("DefaultAuthRequestAnsweringService", () => { // Act sut.setupUnlockListenersForProcessingAuthRequests(destroy$); - activeAccount$.next({ - id: otherUserId, - email: "other@example.com", - emailVerified: true, - name: "Other", - }); + activeAccount$.next(otherUserAccount); // Assert await new Promise((resolve) => setTimeout(resolve, 0)); @@ -209,12 +207,7 @@ describe("DefaultAuthRequestAnsweringService", () => { // Act sut.setupUnlockListenersForProcessingAuthRequests(destroy$); - activeAccount$.next({ - id: otherUserId, - email: "other@example.com", - emailVerified: true, - name: "Other", - }); + activeAccount$.next(otherUserAccount); // Assert await new Promise((resolve) => setTimeout(resolve, 0)); @@ -237,9 +230,8 @@ describe("DefaultAuthRequestAnsweringService", () => { it("should handle multiple user switches correctly", async () => { // Arrange - const secondUserId = "second-user-id" as UserId; + authStatusForSubjects.set(userId, new BehaviorSubject(AuthenticationStatus.Locked)); authStatusForSubjects.set(otherUserId, new BehaviorSubject(AuthenticationStatus.Unlocked)); - authStatusForSubjects.set(secondUserId, new BehaviorSubject(AuthenticationStatus.Locked)); pendingRequestMarkers = [{ userId: otherUserId, receivedAtMs: Date.now() }]; pendingAuthRequestsState.getAll$.mockReturnValue(of(pendingRequestMarkers)); @@ -247,21 +239,11 @@ describe("DefaultAuthRequestAnsweringService", () => { sut.setupUnlockListenersForProcessingAuthRequests(destroy$); // Switch to unlocked user (should trigger) - activeAccount$.next({ - id: otherUserId, - email: "other@example.com", - emailVerified: true, - name: "Other", - }); + activeAccount$.next(otherUserAccount); await new Promise((resolve) => setTimeout(resolve, 0)); // Switch to locked user (should NOT trigger) - activeAccount$.next({ - id: secondUserId, - email: "second@example.com", - emailVerified: true, - name: "Second", - }); + activeAccount$.next(userAccount); await new Promise((resolve) => setTimeout(resolve, 0)); // Assert @@ -279,12 +261,7 @@ describe("DefaultAuthRequestAnsweringService", () => { // Act sut.setupUnlockListenersForProcessingAuthRequests(destroy$); - activeAccount$.next({ - id: otherUserId, - email: "other@example.com", - emailVerified: true, - name: "Other", - }); + activeAccount$.next(otherUserAccount); // Assert await new Promise((resolve) => setTimeout(resolve, 0)); @@ -437,12 +414,7 @@ describe("DefaultAuthRequestAnsweringService", () => { destroy$.next(); // Try to trigger processing after cleanup - activeAccount$.next({ - id: otherUserId, - email: "other@example.com", - emailVerified: true, - name: "Other", - }); + activeAccount$.next(otherUserAccount); activeAccountStatus$.next(AuthenticationStatus.Unlocked); // Assert