From e41dccab3d8b357c5b93c237f057b915bc5922bc Mon Sep 17 00:00:00 2001 From: Kendall Werts Date: Tue, 7 Jan 2020 14:14:19 -0600 Subject: [PATCH] working with tests --- src/js/background/identityState.js | 6 ++- src/js/background/sync.js | 53 ++++++------------- test/features/external-webextensions.test.js | 1 + test/features/sync.test.js | 54 +++++++++++++++++--- test/helper.js | 37 +++++++++++++- 5 files changed, 107 insertions(+), 44 deletions(-) diff --git a/src/js/background/identityState.js b/src/js/background/identityState.js index 92ced00..850ee50 100644 --- a/src/js/background/identityState.js +++ b/src/js/background/identityState.js @@ -11,6 +11,10 @@ const identityState = { const storeKey = this.getContainerStoreKey(cookieStoreId); const storageResponse = await this.area.get([storeKey]); if (storageResponse && storeKey in storageResponse) { + if (!storageResponse[storeKey].macAddonUUID){ + await identityState.addUUID(cookieStoreId); + return this.get(cookieStoreId); + } return storageResponse[storeKey]; } const identities = await browser.contextualIdentities.query({}); @@ -91,7 +95,7 @@ const identityState = { const containerState = await this.storageArea.get(cookieStoreId); containerState.macAddonUUID = uuid; await this.storageArea.set(cookieStoreId, containerState); - return; + return uuid; } throw new Error ("cookieStoreId or uuid missing"); }, diff --git a/src/js/background/sync.js b/src/js/background/sync.js index a8c8a40..a8175ea 100644 --- a/src/js/background/sync.js +++ b/src/js/background/sync.js @@ -37,7 +37,7 @@ const sync = { }, async backup(options) { - console.log("backup"); + if (SYNC_DEBUG) console.log("backup"); // remove listeners to avoid an infinite loop! browser.storage.onChanged.removeListener(sync.storageArea.onChangedListener); removeContextualIdentityListeners(syncCIListenerList); @@ -281,51 +281,32 @@ async function reconcileSiteAssignments() { for(const urlKey of Object.keys(assignedSitesFromSync)) { const assignedSite = assignedSitesFromSync[urlKey]; - if (assignedSitesLocal.hasOwnProperty(urlKey)) { - const syncUUID = - await lookupSyncSiteAssigmentIdentityUUID( - assignedSite, cookieStoreIDmap, urlKey - ); - - const localIdentityUUID = - await lookupLocalSiteAssignmentIdentityUUID(urlKey); - - if (syncUUID === localIdentityUUID) { - continue; - } - // overwrite with Sync data. Sync is the source of truth - await setAssignmentWithUUID(syncUUID, assignedSite, urlKey); + const syncUUID = + await lookupSyncSiteAssigmentIdentityUUID( + assignedSite, cookieStoreIDmap, urlKey + ); + if (syncUUID) { + // Sync is truth. + // Not even looking it up. Just overwrite + console.log("new assignment ", assignedSite, ": ", + assignedSite.userContextId); + const newUUID = cookieStoreIDmap[ + "firefox-container-" + assignedSite.userContextId + ]; + await setAssignmentWithUUID(newUUID, assignedSite, urlKey); continue; } - console.log("new assignment ", assignedSite, ": ", - assignedSite.userContextId); - const newUUID = cookieStoreIDmap[ - "firefox-container-" + assignedSite.userContextId - ]; - await setAssignmentWithUUID(newUUID, assignedSite, urlKey); - } - async function lookupLocalSiteAssignmentIdentityUUID(urlKey){ - const localAssignedSite = - await assignManager.storageArea.getByUrlKey(urlKey); - if (!localAssignedSite || !localAssignedSite.userContextId) - throw new Error (urlKey, "userContextId does not exist"); - const localCookieStoreId = "firefox-container-" + - localAssignedSite.userContextId; - return await identityState.storageArea - .get(localCookieStoreId).macAddonUUID; + // if there's no syncUUID, something is wrong, since these site + // assignments are from sync + throw new Error("Sync storage not aligned"); } async function lookupSyncSiteAssigmentIdentityUUID( assignedSite, cookieStoreIDmap, - urlKey ){ - if (!assignedSite.userContextId) - throw new Error (`${urlKey} userContextId does not exist`); const syncCookieStoreId = "firefox-container-" + assignedSite.userContextId; - if (!cookieStoreIDmap[syncCookieStoreId]) - throw new Error (syncCookieStoreId, " does not have a uuid"); return cookieStoreIDmap[syncCookieStoreId]; } } diff --git a/test/features/external-webextensions.test.js b/test/features/external-webextensions.test.js index 30e6b49..04ebb99 100644 --- a/test/features/external-webextensions.test.js +++ b/test/features/external-webextensions.test.js @@ -26,6 +26,7 @@ describe("External Webextensions", () => { const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender); const answer = await promise; expect(answer).to.deep.equal({ + hostname: "example.com", userContextId: "1", neverAsk: false }); diff --git a/test/features/sync.test.js b/test/features/sync.test.js index 8a9e3cb..9a91c98 100644 --- a/test/features/sync.test.js +++ b/test/features/sync.test.js @@ -1,10 +1,9 @@ describe("Sync", () => { - let tab; - beforeEach(async () => { - tab = await helper.browser.initializeWithTab(); - }); it.only("should init sync on startup", async () => { + console.log("!!!a") + const tab = await helper.browser.initializeWithTab(); + console.log(await background.browser.storage.local.get()); const mozContainer = await background.browser.contextualIdentities.create({ name: "Mozilla", color: "red", @@ -55,12 +54,55 @@ describe("Sync", () => { }); } })); - + console.log("!!!c"); await background.browser.runtime.onStartup.addListener.yield(); await nextTick(); const sync = await background.browser.storage.sync.get(); + console.log(await background.browser.storage.local.get()); expect(sync.identities.length).to.equal(5); + console.log("!!!b"); }); -}); \ No newline at end of file + + it("should sync for the first time", async () => { + const mozContainer = await background.browser.contextualIdentities.create({ + name:"Test", + color:"green", + icon:"pet" + }); + console.log(await background.browser.contextualIdentities.query({})); + await helper.browser.initSyncTest({localStorage:SYNC_TEST_1_LOCAL}); + console.log(await background.browser.storage.local.get()); + for (const containerName of SYNC_TEST_CONTAINERS) { + const storageKeyString = "identitiesState@@_" + containerName; + const answer = await background.browser.storage.local.get(storageKeyString); + expect(answer[storageKeyString].hasOwnProperty("macAddonUUID")).to.be.true; + } + const storageKeyString = "identitiesState@@_" + mozContainer.cookieStoreId; + const answer = await background.browser.storage.local.get(storageKeyString); + expect(answer[storageKeyString].hasOwnProperty("macAddonUUID")).to.be.true; + }); +}); + +const SYNC_TEST_1_LOCAL = { + "browserActionBadgesClicked":["6.1.1"], + "containerTabsOpened":6, + "identitiesState@@_firefox-container-1":{"hiddenTabs":[]}, + "identitiesState@@_firefox-container-2":{"hiddenTabs":[]}, + "identitiesState@@_firefox-container-3":{"hiddenTabs":[]}, + "identitiesState@@_firefox-container-4":{"hiddenTabs":[]}, + "identitiesState@@_firefox-container-6":{"hiddenTabs":[]}, + "identitiesState@@_firefox-default":{"hiddenTabs":[]}, + "onboarding-stage":5, + "siteContainerMap@@_twitter.com":{"userContextId":"1","neverAsk":true}, + "siteContainerMap@@_www.facebook.com":{"userContextId":"2","neverAsk":true}, + "siteContainerMap@@_www.linkedin.com":{"userContextId":"4","neverAsk":false} +}; + +const SYNC_TEST_CONTAINERS = [ + "firefox-container-1", + "firefox-container-2", + "firefox-container-3", + "firefox-container-4" +]; \ No newline at end of file diff --git a/test/helper.js b/test/helper.js index 2704bac..f14ed81 100644 --- a/test/helper.js +++ b/test/helper.js @@ -19,6 +19,7 @@ module.exports = { "achievements": [] }); window.browser.storage.local.set.resetHistory(); + window.browser.storage.sync.clear(); } } } @@ -29,7 +30,41 @@ module.exports = { async openNewTab(tab, options = {}) { return background.browser.tabs._create(tab, options); - } + }, + + async initSyncTest(details = {}) { + if (!details.cookieStoreId) details.cookieStoreId = "firefox-default"; + if (!details.localStorage) { + details.localStorage = { + "browserActionBadgesClicked": [], + "onboarding-stage": 5, + "achievements": [] + }; + } + if (!details.syncStorage) details.syncStorage = {}; + let tab; + await buildDom({ + background: { + async afterBuild(background) { + tab = await background.browser.tabs._create({ cookieStoreId: details.cookieStoreId }); + } + }, + popup: { + jsdom: { + beforeParse(window) { + window.browser.storage.clear(); + window.browser.storage.local.set(details.localStorage); + window.browser.storage.local.set.resetHistory(); + window.browser.storage.sync.clear(); + window.browser.storage.sync.set(details.syncStorage); + window.browser.storage.sync.set.resetHistory(); + } + }, + } + }); + + return tab; + }, }, popup: {