diff --git a/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.spec.ts b/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.spec.ts new file mode 100644 index 00000000000..e5b71bc0b37 --- /dev/null +++ b/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.spec.ts @@ -0,0 +1,131 @@ +import { ActivatedRoute, ActivatedRouteSnapshot, Router } from "@angular/router"; +import { mock } from "jest-mock-extended"; + +import { KeyConnectorService } from "@bitwarden/common/key-management/key-connector/abstractions/key-connector.service"; +import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; +import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; +import { SyncService } from "@bitwarden/common/platform/sync"; +import { UserId } from "@bitwarden/common/types/guid"; +import { KdfType } from "@bitwarden/key-management"; + +import { ConfirmKeyConnectorDomainComponent } from "./confirm-key-connector-domain.component"; + +describe("ConfirmKeyConnectorDomainComponent", () => { + let component: ConfirmKeyConnectorDomainComponent; + + const userId = "test-user-id" as UserId; + const organizationId = "test-organization-id"; + const keyConnectorUrl = "https://key-connector-url.com"; + const kdfType = KdfType.Argon2id; + const kdfIterations = 10; + const kdfMemory = 64; + const kdfParallelism = 4; + + const mockRoute = mock(); + const mockRouter = mock(); + const mockSyncService = mock(); + const mockKeyConnectorService = mock(); + const mockLogService = mock(); + const mockMessagingService = mock(); + + beforeEach(async () => { + jest.clearAllMocks(); + + component = new ConfirmKeyConnectorDomainComponent( + mockRoute, + mockRouter, + mockLogService, + mockKeyConnectorService, + mockMessagingService, + mockSyncService, + ); + + mockRoute.snapshot = mock(); + mockRoute.snapshot.queryParamMap.get = jest.fn((key) => mockQueryParamMapGet(key)); + }); + + describe("ngOnInit", () => { + it.each([["userId"], ["organizationId"], ["keyConnectorUrl"], ["kdf"], ["kdfIterations"]])( + "should logout when missing %s parameter", + async (missingKey) => { + mockRoute.snapshot.queryParamMap.get = jest.fn((key) => { + if (key === missingKey) { + return null; + } + return mockQueryParamMapGet(key); + }); + + await component.ngOnInit(); + + expect(mockMessagingService.send).toHaveBeenCalledWith("logout"); + expect(component.loading).toEqual(true); + }, + ); + + it("Should set component properties correctly", async () => { + await component.ngOnInit(); + + expect(component.userId).toEqual(userId); + expect(component.organizationId).toEqual(organizationId); + expect(component.keyConnectorUrl).toEqual(keyConnectorUrl); + expect(component.kdf).toEqual(kdfType); + expect(component.kdfIterations).toEqual(kdfIterations); + expect(component.kdfMemory).toEqual(kdfMemory); + expect(component.kdfParallelism).toEqual(kdfParallelism); + expect(component.loading).toEqual(false); + }); + }); + + describe("confirm", () => { + it("should call keyConnectorService.convertNewSsoUserToKeyConnector with full sync and navigation to home page", async () => { + await component.ngOnInit(); + + await component.confirm(); + + expect(mockKeyConnectorService.convertNewSsoUserToKeyConnector).toHaveBeenCalledWith( + organizationId, + userId, + keyConnectorUrl, + kdfType, + kdfIterations, + kdfMemory, + kdfParallelism, + ); + expect(mockSyncService.fullSync).toHaveBeenCalledWith(true); + expect(mockRouter.navigate).toHaveBeenCalledWith(["/"]); + expect(mockMessagingService.send).toHaveBeenCalledWith("loggedIn"); + }); + }); + + describe("cancel", () => { + it("should logout", async () => { + await component.ngOnInit(); + + await component.cancel(); + + expect(mockMessagingService.send).toHaveBeenCalledWith("logout"); + expect(mockKeyConnectorService.convertNewSsoUserToKeyConnector).not.toHaveBeenCalled(); + }); + }); + + function mockQueryParamMapGet(key: string) { + switch (key) { + case "userId": + return userId; + case "organizationId": + return organizationId; + case "keyConnectorUrl": + return keyConnectorUrl; + case "kdf": + return kdfType.toString(); + case "kdfIterations": + return kdfIterations.toString(); + case "kdfMemory": + return kdfMemory.toString(); + case "kdfParallelism": + return kdfParallelism.toString(); + default: + return null; + } + } +}); diff --git a/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.ts b/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.ts index 441c65b2d77..966cebdb3ff 100644 --- a/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.ts +++ b/libs/key-management-ui/src/key-connector/confirm-key-connector-domain.component.ts @@ -10,14 +10,14 @@ import { KdfType } from "@bitwarden/key-management"; @Directive() export class ConfirmKeyConnectorDomainComponent implements OnInit { - protected loading = true; - protected keyConnectorUrl!: string; - private userId!: UserId; - private organizationId!: string; - private kdf!: KdfType; - private kdfIterations!: number; - private kdfMemory?: number; - private kdfParallelism?: number; + loading = true; + keyConnectorUrl!: string; + userId!: UserId; + organizationId!: string; + kdf!: KdfType; + kdfIterations!: number; + kdfMemory?: number; + kdfParallelism?: number; constructor( private route: ActivatedRoute,