1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-15 01:11:47 +01:00

PM-8113 - WebTwoFactorAuthDuoComponentService - test listenForDuo2faResult

This commit is contained in:
Jared Snider 2025-02-04 10:41:57 -05:00
parent 5f30790437
commit 5f27a8eb4f
No known key found for this signature in database
GPG Key ID: A149DDD612516286
2 changed files with 32 additions and 82 deletions

View File

@ -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<PlatformUtilsService>;
let mockBroadcastChannel: jest.Mocked<BroadcastChannel>;
let eventTarget: EventTarget;
beforeEach(() => {
jest.clearAllMocks();
platformUtilsService = mock<PlatformUtilsService>();
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", () => {

View File

@ -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<Duo2faResult>;