diff --git a/libs/angular/src/auth/components/remove-password.component.spec.ts b/libs/angular/src/auth/components/remove-password.component.spec.ts index b1eff233f7d..ddabd690306 100644 --- a/libs/angular/src/auth/components/remove-password.component.spec.ts +++ b/libs/angular/src/auth/components/remove-password.component.spec.ts @@ -5,6 +5,7 @@ import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-conso import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { KeyConnectorService } from "@bitwarden/common/auth/abstractions/key-connector.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; +import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { SyncService } from "@bitwarden/common/platform/sync"; import { mockAccountServiceWith } from "@bitwarden/common/spec"; import { UserId } from "@bitwarden/common/types/guid"; @@ -37,6 +38,7 @@ describe("RemovePasswordComponent", () => { await accountService.switchAccount(userId); component = new RemovePasswordComponent( + mock(), mockRouter, accountService, mockSyncService, @@ -62,18 +64,22 @@ describe("RemovePasswordComponent", () => { expect(mockSyncService.fullSync).toHaveBeenCalledWith(false); }); - it("should throw an error if no active account is found", async () => { + it("should redirect to login when no active account is found", async () => { await accountService.switchAccount(null as unknown as UserId); - await expect(component.ngOnInit()).rejects.toThrow(new Error("No active account found")); + await component.ngOnInit(); + + expect(mockRouter.navigate).toHaveBeenCalledWith([""]); }); - it("should throw an error if no organization is found", async () => { + it("should redirect to login when no organization is found", async () => { mockKeyConnectorService.getManagingOrganization.mockResolvedValue( null as unknown as Organization, ); - await expect(component.ngOnInit()).rejects.toThrow(new Error("No organization found")); + await component.ngOnInit(); + + expect(mockRouter.navigate).toHaveBeenCalledWith([""]); }); }); @@ -183,7 +189,7 @@ describe("RemovePasswordComponent", () => { expect(mockRouter.navigate).not.toHaveBeenCalled(); }); - it("should not call leave if dialog is canceled", async () => { + it("should not call leave when dialog is canceled", async () => { mockDialogService.openSimpleDialog.mockResolvedValue(false); await component.leave(); diff --git a/libs/angular/src/auth/components/remove-password.component.ts b/libs/angular/src/auth/components/remove-password.component.ts index 53e3da2cc08..8fd4e7bba95 100644 --- a/libs/angular/src/auth/components/remove-password.component.ts +++ b/libs/angular/src/auth/components/remove-password.component.ts @@ -7,6 +7,7 @@ import { Organization } from "@bitwarden/common/admin-console/models/domain/orga import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { KeyConnectorService } from "@bitwarden/common/auth/abstractions/key-connector.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; +import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { UserId } from "@bitwarden/common/types/guid"; import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction"; import { DialogService, ToastService } from "@bitwarden/components"; @@ -21,6 +22,7 @@ export class RemovePasswordComponent implements OnInit { private activeUserId!: UserId; constructor( + private logService: LogService, private router: Router, private accountService: AccountService, private syncService: SyncService, @@ -34,13 +36,21 @@ export class RemovePasswordComponent implements OnInit { async ngOnInit() { const activeAccount = await firstValueFrom(this.accountService.activeAccount$); if (activeAccount == null) { - throw new Error("No active account found"); + this.logService.info( + "[Key Connector remove password] No active account found, redirecting to login.", + ); + await this.router.navigate([""]); + return; } this.activeUserId = activeAccount.id; this.organization = await this.keyConnectorService.getManagingOrganization(this.activeUserId); if (this.organization == null) { - throw new Error("No organization found"); + this.logService.info( + "[Key Connector remove password] No organization found, redirecting to login.", + ); + await this.router.navigate([""]); + return; } await this.syncService.fullSync(false); this.loading = false;