only filter content script and page script and update test
This commit is contained in:
parent
5e1f5b52e0
commit
d7f491d920
|
@ -15,17 +15,43 @@ jest.mock("../../../autofill/utils", () => ({
|
|||
}),
|
||||
}));
|
||||
|
||||
const originalGlobalThis = globalThis;
|
||||
const mockGlobalThisDocument = {
|
||||
...originalGlobalThis.document,
|
||||
contentType: "text/html",
|
||||
location: {
|
||||
...originalGlobalThis.document.location,
|
||||
href: "https://localhost",
|
||||
origin: "https://localhost",
|
||||
protocol: "https:",
|
||||
},
|
||||
};
|
||||
|
||||
describe("Fido2 Content Script", () => {
|
||||
beforeAll(() => {
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(
|
||||
() => mockGlobalThisDocument,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
let messenger: Messenger;
|
||||
const messengerForDOMCommunicationSpy = jest
|
||||
.spyOn(Messenger, "forDOMCommunication")
|
||||
.mockImplementation((window) => {
|
||||
const windowOrigin = window.location.origin;
|
||||
.mockImplementation((context) => {
|
||||
const windowOrigin = context.location.origin;
|
||||
|
||||
messenger = new Messenger({
|
||||
postMessage: (message, port) => window.postMessage(message, windowOrigin, [port]),
|
||||
addEventListener: (listener) => window.addEventListener("message", listener),
|
||||
removeEventListener: (listener) => window.removeEventListener("message", listener),
|
||||
postMessage: (message, port) => context.postMessage(message, windowOrigin, [port]),
|
||||
addEventListener: (listener) => context.addEventListener("message", listener),
|
||||
removeEventListener: (listener) => context.removeEventListener("message", listener),
|
||||
});
|
||||
messenger.destroy = jest.fn();
|
||||
return messenger;
|
||||
|
@ -33,16 +59,6 @@ describe("Fido2 Content Script", () => {
|
|||
const portSpy: MockProxy<chrome.runtime.Port> = createPortSpyMock(Fido2PortName.InjectedScript);
|
||||
chrome.runtime.connect = jest.fn(() => portSpy);
|
||||
|
||||
afterEach(() => {
|
||||
Object.defineProperty(document, "contentType", {
|
||||
value: "text/html",
|
||||
writable: true,
|
||||
});
|
||||
|
||||
jest.clearAllMocks();
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
it("destroys the messenger when the port is disconnected", () => {
|
||||
require("./content-script");
|
||||
|
||||
|
@ -151,11 +167,31 @@ describe("Fido2 Content Script", () => {
|
|||
await expect(result).rejects.toEqual(errorMessage);
|
||||
});
|
||||
|
||||
it("skips initializing the content script if the document content type is not 'text/html'", () => {
|
||||
Object.defineProperty(document, "contentType", {
|
||||
value: "application/json",
|
||||
writable: true,
|
||||
});
|
||||
it("skips initializing if the document content type is not 'text/html'", () => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(() => ({
|
||||
...mockGlobalThisDocument,
|
||||
contentType: "application/json",
|
||||
}));
|
||||
|
||||
require("./content-script");
|
||||
|
||||
expect(messengerForDOMCommunicationSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips initializing if the document location protocol is not 'https'", () => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(() => ({
|
||||
...mockGlobalThisDocument,
|
||||
location: {
|
||||
...mockGlobalThisDocument.location,
|
||||
href: "http://localhost",
|
||||
origin: "http://localhost",
|
||||
protocol: "http:",
|
||||
},
|
||||
}));
|
||||
|
||||
require("./content-script");
|
||||
|
||||
|
|
|
@ -5,11 +5,7 @@
|
|||
import { Fido2ContentScript } from "../enums/fido2-content-script.enum";
|
||||
|
||||
(function (globalContext) {
|
||||
const shouldExecuteContentScript =
|
||||
globalContext.document.contentType === "text/html" &&
|
||||
globalContext.document.location.protocol === "https:";
|
||||
|
||||
if (!shouldExecuteContentScript) {
|
||||
if (globalContext.document.contentType !== "text/html") {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,17 +10,29 @@ import { WebauthnUtils } from "../webauthn-utils";
|
|||
import { MessageType } from "./messaging/message";
|
||||
import { Messenger } from "./messaging/messenger";
|
||||
|
||||
const originalGlobalThis = globalThis;
|
||||
const mockGlobalThisDocument = {
|
||||
...originalGlobalThis.document,
|
||||
contentType: "text/html",
|
||||
location: {
|
||||
...originalGlobalThis.document.location,
|
||||
href: "https://localhost",
|
||||
origin: "https://localhost",
|
||||
protocol: "https:",
|
||||
},
|
||||
};
|
||||
|
||||
let messenger: Messenger;
|
||||
jest.mock("./messaging/messenger", () => {
|
||||
return {
|
||||
Messenger: class extends jest.requireActual("./messaging/messenger").Messenger {
|
||||
static forDOMCommunication: any = jest.fn((window) => {
|
||||
const windowOrigin = window.location.origin;
|
||||
static forDOMCommunication: any = jest.fn((context) => {
|
||||
const windowOrigin = context.location.origin;
|
||||
|
||||
messenger = new Messenger({
|
||||
postMessage: (message, port) => window.postMessage(message, windowOrigin, [port]),
|
||||
addEventListener: (listener) => window.addEventListener("message", listener),
|
||||
removeEventListener: (listener) => window.removeEventListener("message", listener),
|
||||
postMessage: (message, port) => context.postMessage(message, windowOrigin, [port]),
|
||||
addEventListener: (listener) => context.addEventListener("message", listener),
|
||||
removeEventListener: (listener) => context.removeEventListener("message", listener),
|
||||
});
|
||||
messenger.destroy = jest.fn();
|
||||
return messenger;
|
||||
|
@ -31,6 +43,10 @@ jest.mock("./messaging/messenger", () => {
|
|||
jest.mock("../webauthn-utils");
|
||||
|
||||
describe("Fido2 page script with native WebAuthn support", () => {
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(
|
||||
() => mockGlobalThisDocument,
|
||||
);
|
||||
|
||||
const mockCredentialCreationOptions = createCredentialCreationOptionsMock();
|
||||
const mockCreateCredentialsResult = createCreateCredentialResultMock();
|
||||
const mockCredentialRequestOptions = createCredentialRequestOptionsMock();
|
||||
|
@ -39,9 +55,12 @@ describe("Fido2 page script with native WebAuthn support", () => {
|
|||
|
||||
require("./page-script");
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
describe("creating WebAuthn credentials", () => {
|
||||
|
@ -118,4 +137,42 @@ describe("Fido2 page script with native WebAuthn support", () => {
|
|||
expect(messenger.destroy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("content script execution", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
it("skips initializing if the document content type is not 'text/html'", () => {
|
||||
jest.spyOn(Messenger, "forDOMCommunication");
|
||||
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(() => ({
|
||||
...mockGlobalThisDocument,
|
||||
contentType: "json/application",
|
||||
}));
|
||||
|
||||
require("./content-script");
|
||||
|
||||
expect(Messenger.forDOMCommunication).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips initializing if the document location protocol is not 'https'", () => {
|
||||
jest.spyOn(Messenger, "forDOMCommunication");
|
||||
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(() => ({
|
||||
...mockGlobalThisDocument,
|
||||
location: {
|
||||
...mockGlobalThisDocument.location,
|
||||
href: "http://localhost",
|
||||
origin: "http://localhost",
|
||||
protocol: "http:",
|
||||
},
|
||||
}));
|
||||
|
||||
require("./content-script");
|
||||
|
||||
expect(Messenger.forDOMCommunication).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -9,17 +9,29 @@ import { WebauthnUtils } from "../webauthn-utils";
|
|||
import { MessageType } from "./messaging/message";
|
||||
import { Messenger } from "./messaging/messenger";
|
||||
|
||||
const originalGlobalThis = globalThis;
|
||||
const mockGlobalThisDocument = {
|
||||
...originalGlobalThis.document,
|
||||
contentType: "text/html",
|
||||
location: {
|
||||
...originalGlobalThis.document.location,
|
||||
href: "https://localhost",
|
||||
origin: "https://localhost",
|
||||
protocol: "https:",
|
||||
},
|
||||
};
|
||||
|
||||
let messenger: Messenger;
|
||||
jest.mock("./messaging/messenger", () => {
|
||||
return {
|
||||
Messenger: class extends jest.requireActual("./messaging/messenger").Messenger {
|
||||
static forDOMCommunication: any = jest.fn((window) => {
|
||||
const windowOrigin = window.location.origin;
|
||||
static forDOMCommunication: any = jest.fn((context) => {
|
||||
const windowOrigin = context.location.origin;
|
||||
|
||||
messenger = new Messenger({
|
||||
postMessage: (message, port) => window.postMessage(message, windowOrigin, [port]),
|
||||
addEventListener: (listener) => window.addEventListener("message", listener),
|
||||
removeEventListener: (listener) => window.removeEventListener("message", listener),
|
||||
postMessage: (message, port) => context.postMessage(message, windowOrigin, [port]),
|
||||
addEventListener: (listener) => context.addEventListener("message", listener),
|
||||
removeEventListener: (listener) => context.removeEventListener("message", listener),
|
||||
});
|
||||
messenger.destroy = jest.fn();
|
||||
return messenger;
|
||||
|
@ -30,15 +42,22 @@ jest.mock("./messaging/messenger", () => {
|
|||
jest.mock("../webauthn-utils");
|
||||
|
||||
describe("Fido2 page script without native WebAuthn support", () => {
|
||||
(jest.spyOn(globalThis, "document", "get") as jest.Mock).mockImplementation(
|
||||
() => mockGlobalThisDocument,
|
||||
);
|
||||
|
||||
const mockCredentialCreationOptions = createCredentialCreationOptionsMock();
|
||||
const mockCreateCredentialsResult = createCreateCredentialResultMock();
|
||||
const mockCredentialRequestOptions = createCredentialRequestOptionsMock();
|
||||
const mockCredentialAssertResult = createAssertCredentialResultMock();
|
||||
require("./page-script");
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
describe("creating WebAuthn credentials", () => {
|
||||
|
|
Loading…
Reference in New Issue