diff --git a/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.spec.ts b/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.spec.ts index 79ad18f9ae..8cd92e7326 100644 --- a/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.spec.ts +++ b/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.spec.ts @@ -1,5 +1,6 @@ import { MockProxy, mock } from "jest-mock-extended"; +import { Duo2faResult } from "@bitwarden/auth/angular"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { WebTwoFactorAuthDuoComponentService } from "./web-two-factor-auth-duo-component.service"; @@ -10,74 +11,24 @@ describe("WebTwoFactorAuthDuoComponentService", () => { let platformUtilsService: MockProxy; let mockBroadcastChannel: jest.Mocked; + let eventTarget: EventTarget; beforeEach(() => { jest.clearAllMocks(); platformUtilsService = mock(); + eventTarget = new EventTarget(); + mockBroadcastChannel = { name: "duoResult", - postMessage: jest.fn().mockImplementation(function (this: BroadcastChannel, data) { - // Simulate the message being posted by calling onmessage with the data - if (this.onmessage) { - this.onmessage({ - data, - lastEventId: "", - origin: "", - ports: [], - source: null, - currentTarget: null, - bubbles: false, - cancelable: false, - cancelBubble: false, - composed: false, - defaultPrevented: false, - eventPhase: 0, - isTrusted: false, - returnValue: false, - srcElement: null, - target: null, - timeStamp: Date.now(), - type: "message", - composedPath: function () { - return []; - }, - initEvent: function (type: string, bubbles?: boolean, cancelable?: boolean): void { - throw new Error("Function not implemented."); - }, - preventDefault: function (): void { - throw new Error("Function not implemented."); - }, - stopImmediatePropagation: function (): void { - throw new Error("Function not implemented."); - }, - stopPropagation: function (): void { - throw new Error("Function not implemented."); - }, - NONE: 0, - CAPTURING_PHASE: 1, - AT_TARGET: 2, - BUBBLING_PHASE: 3, - initMessageEvent: function ( - type: string, - bubbles?: boolean, - cancelable?: boolean, - data?: any, - origin?: string, - lastEventId?: string, - source?: MessageEventSource, - ports?: MessagePort[], - ): void { - throw new Error("Function not implemented."); - }, - }); - } - }), + postMessage: jest.fn(), close: jest.fn(), onmessage: jest.fn(), onmessageerror: jest.fn(), - addEventListener: jest.fn(), + addEventListener: jest.fn().mockImplementation((type, listener) => { + eventTarget.addEventListener(type, listener); + }), removeEventListener: jest.fn(), dispatchEvent: jest.fn(), }; @@ -95,30 +46,30 @@ describe("WebTwoFactorAuthDuoComponentService", () => { }); describe("listenForDuo2faResult$", () => { - // TODO: figure out how to test this - // it("should return an observable that emits a duo 2FA result when a duo result message is received", (done) => { - // const expectedResult: Duo2faResult = { - // code: "123456", - // state: "verified", - // token: "123456|verified", - // }; - // const mockMessageEvent = new MessageEvent("message", { - // data: { - // code: "123456", - // state: "verified", - // }, - // lastEventId: "", - // origin: "", - // ports: [], - // source: null, - // }); - // webTwoFactorAuthDuoComponentService.listenForDuo2faResult$().subscribe((result) => { - // expect(result).toEqual(expectedResult); - // done(); - // }); - // // Trigger the message event - // mockBroadcastChannel.postMessage(mockMessageEvent.data); - // }); + it("should return an observable that emits a duo 2FA result when a duo result message is received", (done) => { + const expectedResult: Duo2faResult = { + code: "123456", + state: "verified", + token: "123456|verified", + }; + const mockMessageEvent = new MessageEvent("message", { + data: { + code: "123456", + state: "verified", + }, + lastEventId: "", + origin: "", + ports: [], + source: null, + }); + webTwoFactorAuthDuoComponentService.listenForDuo2faResult$().subscribe((result) => { + expect(result).toEqual(expectedResult); + done(); + }); + + // Trigger the message event + eventTarget.dispatchEvent(mockMessageEvent); + }); }); describe("launchDuoFrameless", () => { diff --git a/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.ts b/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.ts index cee7b2559f..ac8eccb519 100644 --- a/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.ts +++ b/apps/web/src/app/auth/core/services/two-factor-auth/web-two-factor-auth-duo-component.service.ts @@ -3,7 +3,6 @@ import { fromEvent, map, Observable, share } from "rxjs"; import { TwoFactorAuthDuoComponentService, Duo2faResult } from "@bitwarden/auth/angular"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -// TODO: add tests export class WebTwoFactorAuthDuoComponentService implements TwoFactorAuthDuoComponentService { private duo2faResult$: Observable;