From 59a8262c0b7645ac162d6084e8297b238d289628 Mon Sep 17 00:00:00 2001 From: Hugo Duthil Date: Mon, 17 Aug 2020 17:33:45 +0200 Subject: [PATCH 1/3] Check localstorage availability before accessint it --- src/storageManager.js | 6 +++--- test/spec/unit/core/storageManager_spec.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/storageManager.js b/src/storageManager.js index 0d88a8ccea1..60e5a7706d0 100644 --- a/src/storageManager.js +++ b/src/storageManager.js @@ -154,7 +154,7 @@ export function newStorageManager({gvlid, moduleName, moduleType} = {}) { */ const setDataInLocalStorage = function (key, value, done) { let cb = function (result) { - if (result && result.valid) { + if (result && result.valid && hasLocalStorage()) { window.localStorage.setItem(key, value); } } @@ -174,7 +174,7 @@ export function newStorageManager({gvlid, moduleName, moduleType} = {}) { */ const getDataFromLocalStorage = function (key, done) { let cb = function (result) { - if (result && result.valid) { + if (result && result.valid && hasLocalStorage()) { return window.localStorage.getItem(key); } return null; @@ -194,7 +194,7 @@ export function newStorageManager({gvlid, moduleName, moduleType} = {}) { */ const removeDataFromLocalStorage = function (key, done) { let cb = function (result) { - if (result && result.valid) { + if (result && result.valid && hasLocalStorage()) { window.localStorage.removeItem(key); } } diff --git a/test/spec/unit/core/storageManager_spec.js b/test/spec/unit/core/storageManager_spec.js index 0b406242f90..04174175981 100644 --- a/test/spec/unit/core/storageManager_spec.js +++ b/test/spec/unit/core/storageManager_spec.js @@ -42,5 +42,22 @@ describe('storage manager', function() { storage.setCookie('foo1', 'baz1'); expect(deviceAccessSpy.calledOnce).to.equal(true); deviceAccessSpy.restore(); + }); + + it('should not throw if the localstorage is not accessible when setting/getting/removing from localstorage', function() { + const coreStorage = getStorageManager(); + + const localstorageStub = sinon.stub(global.window, 'localStorage').get(() => { throw Error }); + const errorLogSpy = sinon.spy(utils, 'logError'); + + coreStorage.setDataInLocalStorage('key', 'value'); + const val = coreStorage.getDataFromLocalStorage('key'); + coreStorage.removeDataFromLocalStorage('key'); + + expect(val).to.be.null; + sinon.assert.calledThrice(errorLogSpy); + + localstorageStub.restore(); + errorLogSpy.restore(); }) }); From d5204ada74b0f651aea22965c2e4e0c2f9ab4486 Mon Sep 17 00:00:00 2001 From: Hugo Duthil Date: Tue, 8 Sep 2020 14:43:39 +0200 Subject: [PATCH 2/3] Fix UTest for IE11 --- test/spec/unit/core/storageManager_spec.js | 34 +++++++++++++++------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/test/spec/unit/core/storageManager_spec.js b/test/spec/unit/core/storageManager_spec.js index 04174175981..2e6833bae50 100644 --- a/test/spec/unit/core/storageManager_spec.js +++ b/test/spec/unit/core/storageManager_spec.js @@ -44,20 +44,32 @@ describe('storage manager', function() { deviceAccessSpy.restore(); }); - it('should not throw if the localstorage is not accessible when setting/getting/removing from localstorage', function() { - const coreStorage = getStorageManager(); + describe('localstorage forbidden access in 3rd-party context', function() { + let errorLogSpy; + + beforeEach(function() { + var mock = { + get: function() { + throw Error; + } + }; + Object.defineProperty(window, 'localStorage', mock); + errorLogSpy = sinon.spy(utils, 'logError'); + }); - const localstorageStub = sinon.stub(global.window, 'localStorage').get(() => { throw Error }); - const errorLogSpy = sinon.spy(utils, 'logError'); + afterEach(function() { + errorLogSpy.restore(); + }) - coreStorage.setDataInLocalStorage('key', 'value'); - const val = coreStorage.getDataFromLocalStorage('key'); - coreStorage.removeDataFromLocalStorage('key'); + it('should not throw if the localstorage is not accessible when setting/getting/removing from localstorage', function() { + const coreStorage = getStorageManager(); - expect(val).to.be.null; - sinon.assert.calledThrice(errorLogSpy); + coreStorage.setDataInLocalStorage('key', 'value'); + const val = coreStorage.getDataFromLocalStorage('key'); + coreStorage.removeDataFromLocalStorage('key'); - localstorageStub.restore(); - errorLogSpy.restore(); + expect(val).to.be.null; + sinon.assert.calledThrice(errorLogSpy); + }) }) }); From cf3f0145a6d01a10e84dff6c8cf3d369eb7e7b1a Mon Sep 17 00:00:00 2001 From: Hugo Duthil Date: Mon, 14 Sep 2020 18:11:08 +0200 Subject: [PATCH 3/3] Restore window.localStorage property descriptor after each test --- test/spec/unit/core/storageManager_spec.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/spec/unit/core/storageManager_spec.js b/test/spec/unit/core/storageManager_spec.js index 2e6833bae50..de09df5b196 100644 --- a/test/spec/unit/core/storageManager_spec.js +++ b/test/spec/unit/core/storageManager_spec.js @@ -46,18 +46,16 @@ describe('storage manager', function() { describe('localstorage forbidden access in 3rd-party context', function() { let errorLogSpy; + const originalLocalStorage = { get: () => window.localStorage }; + const localStorageMock = { get: () => { throw Error } }; beforeEach(function() { - var mock = { - get: function() { - throw Error; - } - }; - Object.defineProperty(window, 'localStorage', mock); + Object.defineProperty(window, 'localStorage', localStorageMock); errorLogSpy = sinon.spy(utils, 'logError'); }); afterEach(function() { + Object.defineProperty(window, 'localStorage', originalLocalStorage); errorLogSpy.restore(); })