From 36f0e121a46912134f0095a02a3d4e8a589e0bf8 Mon Sep 17 00:00:00 2001 From: stoically Date: Wed, 18 Dec 2019 09:19:01 +0100 Subject: [PATCH] Adapt existing tests to use webextensions-jsdom --- test/features/assignment.test.js | 49 ++-- test/features/external-webextensions.test.js | 48 ++-- test/issues/940.test.js | 236 +++++++++---------- 3 files changed, 153 insertions(+), 180 deletions(-) diff --git a/test/features/assignment.test.js b/test/features/assignment.test.js index 632c60b..43bb981 100644 --- a/test/features/assignment.test.js +++ b/test/features/assignment.test.js @@ -1,12 +1,12 @@ describe("Assignment Feature", () => { - const activeTab = { - id: 1, - cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0 - }; + const url = "http://example.com"; + + let activeTab; beforeEach(async () => { - await helper.browser.initializeWithTab(activeTab); + activeTab = await helper.browser.initializeWithTab({ + cookieStoreId: "firefox-container-1", + url + }); }); describe("click the 'Always open in' checkbox in the popup", () => { @@ -16,23 +16,24 @@ describe("Assignment Feature", () => { }); describe("open new Tab with the assigned URL in the default container", () => { - const newTab = { - id: 2, - cookieStoreId: "firefox-default", - url: activeTab.url, - index: 1, - active: true - }; + let newTab; beforeEach(async () => { // new Tab opening activeTab.url in default container - await helper.browser.openNewTab(newTab); + newTab = await helper.browser.openNewTab({ + cookieStoreId: "firefox-default", + url + }, { + options: { + webRequestError: true // because request is canceled due to reopening + } + }); }); it("should open the confirm page", async () => { // should have created a new tab with the confirm page - background.browser.tabs.create.should.have.been.calledWith({ - url: "moz-extension://multi-account-containers/confirm-page.html?" + - `url=${encodeURIComponent(activeTab.url)}` + + background.browser.tabs.create.should.have.been.calledWithMatch({ + url: "moz-extension://fake/confirm-page.html?" + + `url=${encodeURIComponent(url)}` + `&cookieStoreId=${activeTab.cookieStoreId}`, cookieStoreId: undefined, openerTabId: null, @@ -53,16 +54,12 @@ describe("Assignment Feature", () => { }); describe("open new Tab with the no longer assigned URL in the default container", () => { - const newTab = { - id: 3, - cookieStoreId: "firefox-default", - url: activeTab.url, - index: 3, - active: true - }; beforeEach(async () => { // new Tab opening activeTab.url in default container - await helper.browser.openNewTab(newTab); + await helper.browser.openNewTab({ + cookieStoreId: "firefox-default", + url + }); }); it("should not open the confirm page", async () => { diff --git a/test/features/external-webextensions.test.js b/test/features/external-webextensions.test.js index fe32e4d..30e6b49 100644 --- a/test/features/external-webextensions.test.js +++ b/test/features/external-webextensions.test.js @@ -1,12 +1,11 @@ describe("External Webextensions", () => { - const activeTab = { - id: 1, - cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0 - }; + const url = "http://example.com"; + beforeEach(async () => { - await helper.browser.initializeWithTab(activeTab); + await helper.browser.initializeWithTab({ + cookieStoreId: "firefox-container-1", + url + }); await helper.popup.clickElementById("container-page-assigned"); }); @@ -18,25 +17,18 @@ describe("External Webextensions", () => { const message = { method: "getAssignment", - url: "http://example.com" + url }; const sender = { id: "external-webextension" }; - // currently not possible to get the return value of yielding with sinon - // so we expect that if no error is thrown and the storage was called, everything is ok - // maybe i get around to provide a PR https://github.com/sinonjs/sinon/issues/903 - // - // the alternative would be to expose the actual messageHandler and call it directly - // but personally i think that goes against the black-box-ish nature of these feature tests - const rejectionStub = sinon.stub(); - process.on("unhandledRejection", rejectionStub); - background.browser.runtime.onMessageExternal.addListener.yield(message, sender); - await nextTick(); - process.removeListener("unhandledRejection", rejectionStub); - rejectionStub.should.not.have.been.called; - background.browser.storage.local.get.should.have.been.called; + const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender); + const answer = await promise; + expect(answer).to.deep.equal({ + userContextId: "1", + neverAsk: false + }); }); }); @@ -48,20 +40,16 @@ describe("External Webextensions", () => { const message = { method: "getAssignment", - url: "http://example.com" + url }; const sender = { id: "external-webextension" }; - const rejectionStub = sinon.spy(); - process.on("unhandledRejection", rejectionStub); - background.browser.runtime.onMessageExternal.addListener.yield(message, sender); - await nextTick(); - process.removeListener("unhandledRejection", rejectionStub); - rejectionStub.should.have.been.calledWith(sinon.match({ - message: "Missing contextualIdentities permission" - })); + const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender); + return promise.catch(error => { + expect(error.message).to.equal("Missing contextualIdentities permission"); + }); }); }); }); diff --git a/test/issues/940.test.js b/test/issues/940.test.js index b889035..4a7eb0e 100644 --- a/test/issues/940.test.js +++ b/test/issues/940.test.js @@ -1,112 +1,82 @@ describe("#940", () => { describe("when other onBeforeRequestHandlers are faster and redirect with the same requestId", () => { it("should not open two confirm pages", async () => { - // init - const activeTab = { - id: 1, + await helper.browser.initializeWithTab({ cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0 - }; - await helper.browser.initializeWithTab(activeTab); - // assign the activeTab.url + url: "http://example.com" + }); await helper.popup.clickElementById("container-page-assigned"); - // start request and don't await the requests at all - // so the second request below is actually comparable to an actual redirect that also fires immediately - const newTab = { - id: 2, - cookieStoreId: "firefox-default", - url: activeTab.url, - index: 1, - active: true - }; - helper.browser.openNewTab(newTab, { - requestId: 1 + const responses = {}; + await helper.browser.openNewTab({ + url: "http://example.com" + }, { + options: { + webRequestRedirects: ["https://example.com"], + webRequestError: true, + instantRedirects: true + }, + responses }); - // other addon sees the same request - // and redirects to the https version of activeTab.url - // since it's a redirect the request has the same requestId - background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://example.com", - requestId: 1 + const result = await responses.webRequest.onBeforeRequest[1]; + expect(result).to.deep.equal({ + cancel: true }); - await nextTick(); - background.browser.tabs.create.should.have.been.calledOnce; }); }); describe("when redirects change requestId midflight", () => { - let promiseResults; + let newTab; + const newTabResponses = {}; + const redirectedRequest = async (options = {}) => { + global.clock = sinon.useFakeTimers(); + newTab = await helper.browser.openNewTab({ + url: "http://youtube.com" + }, { + options: Object.assign({ + webRequestRedirects: [ + "https://youtube.com", + "https://www.youtube.com", + { + url: "https://www.youtube.com", + webRequest: { + requestId: 2 + } + } + ], + webRequestError: true, + instantRedirects: true + }, options), + responses: newTabResponses + }); + }; + beforeEach(async () => { - // init - const activeTab = { - id: 1, + await helper.browser.initializeWithTab({ cookieStoreId: "firefox-container-1", - url: "https://www.youtube.com", - index: 0 - }; - await helper.browser.initializeWithTab(activeTab); - // assign the activeTab.url + url: "https://www.youtube.com" + }); await helper.popup.clickElementById("container-page-assigned"); - - // http://youtube.com - const newTab = { - id: 2, - cookieStoreId: "firefox-default", - url: "http://youtube.com", - index: 1, - active: true - }; - const promise1 = helper.browser.openNewTab(newTab, { - requestId: 1 - }); - - // https://youtube.com - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://youtube.com", - requestId: 1 - }); - - // https://www.youtube.com - const [promise3] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://www.youtube.com", - requestId: 1 - }); - - // https://www.youtube.com - const [promise4] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: newTab.id, - url: "https://www.youtube.com", - requestId: 2 - }); - - promiseResults = await Promise.all([promise1, promise2, promise3, promise4]); }); it("should not open two confirm pages", async () => { + await redirectedRequest(); + // http://youtube.com is not assigned, no cancel, no reopening - expect(promiseResults[0]).to.deep.equal({}); + expect(await newTabResponses.webRequest.onBeforeRequest[0]).to.deep.equal({}); // https://youtube.com is not assigned, no cancel, no reopening - expect(promiseResults[1]).to.deep.equal({}); + expect(await newTabResponses.webRequest.onBeforeRequest[1]).to.deep.equal({}); // https://www.youtube.com is assigned, this triggers reopening, cancel - expect(promiseResults[2]).to.deep.equal({ + expect(await newTabResponses.webRequest.onBeforeRequest[2]).to.deep.equal({ cancel: true }); // https://www.youtube.com is assigned, this was a redirect, cancel early, no reopening - expect(promiseResults[3]).to.deep.equal({ + expect(await newTabResponses.webRequest.onBeforeRequest[3]).to.deep.equal({ cancel: true }); @@ -114,67 +84,85 @@ describe("#940", () => { }); it("should uncancel after webRequest.onCompleted", async () => { - const [promise1] = background.browser.webRequest.onCompleted.addListener.yield({ - tabId: 2 + await redirectedRequest(); + // remove onCompleted listeners because in the real world this request would never complete + // and thus might trigger unexpected behavior because the tab gets removed when reopening + background.browser.webRequest.onCompleted.addListener = sinon.stub(); + background.browser.tabs.create.resetHistory(); + // we create a tab with the same id and use the same request id to see if uncanceled + await helper.browser.openNewTab({ + id: newTab.id, + url: "https://www.youtube.com" + }, { + options: { + webRequest: { + requestId: newTabResponses.webRequest.request.requestId + } + } }); - await promise1; - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: 2, - url: "https://www.youtube.com", - requestId: 123 - }); - await promise2; - - background.browser.tabs.create.should.have.been.calledTwice; + background.browser.tabs.create.should.have.been.calledOnce; }); it("should uncancel after webRequest.onErrorOccurred", async () => { - const [promise1] = background.browser.webRequest.onErrorOccurred.addListener.yield({ - tabId: 2 + await redirectedRequest(); + background.browser.tabs.create.resetHistory(); + // we create a tab with the same id and use the same request id to see if uncanceled + await helper.browser.openNewTab({ + id: newTab.id, + url: "https://www.youtube.com" + }, { + options: { + webRequest: { + requestId: newTabResponses.webRequest.request.requestId + }, + webRequestError: true + } }); - await promise1; - // request to assigned url in same tab - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: 2, - url: "https://www.youtube.com", - requestId: 123 - }); - await promise2; - - background.browser.tabs.create.should.have.been.calledTwice; + background.browser.tabs.create.should.have.been.calledOnce; }); it("should uncancel after 2 seconds", async () => { - await new Promise(resolve => setTimeout(resolve, 2000)); - // request to assigned url in same tab - const [promise2] = background.browser.webRequest.onBeforeRequest.addListener.yield({ - frameId: 0, - tabId: 2, - url: "https://www.youtube.com", - requestId: 123 + await redirectedRequest({ + webRequestDontYield: ["onCompleted", "onErrorOccurred"] }); - await promise2; + global.clock.tick(2000); - background.browser.tabs.create.should.have.been.calledTwice; - }).timeout(2002); + background.browser.tabs.create.resetHistory(); + // we create a tab with the same id and use the same request id to see if uncanceled + await helper.browser.openNewTab({ + id: newTab.id, + url: "https://www.youtube.com" + }, { + options: { + webRequest: { + requestId: newTabResponses.webRequest.request.requestId + }, + webRequestError: true + } + }); + + background.browser.tabs.create.should.have.been.calledOnce; + }); it("should not influence the canceled url in other tabs", async () => { - const newTab = { - id: 123, + await redirectedRequest(); + background.browser.tabs.create.resetHistory(); + await helper.browser.openNewTab({ cookieStoreId: "firefox-default", - url: "https://www.youtube.com", - index: 10, - active: true - }; - await helper.browser.openNewTab(newTab, { - requestId: 321 + url: "https://www.youtube.com" + }, { + options: { + webRequestError: true + } }); - background.browser.tabs.create.should.have.been.calledTwice; + background.browser.tabs.create.should.have.been.calledOnce; + }); + + afterEach(() => { + global.clock.restore(); }); }); });