-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6651 fix localStorage error that causes infinite loading in embedded… (
#6667) * #6651 fix localStorage error that causes infinite loading in embedded map in incognito mode * made more robust the check on userSession for browser provider * add tests + refactor * finalize refactor plus adding more unit tests * fix typo error with new api for storage * refactor for some tests
- Loading branch information
Showing
19 changed files
with
350 additions
and
37 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
web/client/api/userPersistedStorage/__tests__/index-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2021, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import expect from 'expect'; | ||
import { setApi, getApi } from '..'; | ||
|
||
|
||
describe('UserPersistedSession', () => { | ||
describe('MemoryStorage', () => { | ||
let memoryApi; | ||
beforeEach(() => { | ||
setApi("memoryStorage"); | ||
memoryApi = getApi(); | ||
}); | ||
afterEach(() => { | ||
setApi("localStorage"); | ||
memoryApi.setAccessDenied(false); | ||
}); | ||
it('tests setItem & getItem', () => { | ||
memoryApi.setItem("key", "value"); | ||
memoryApi.setItem("key2", "value2"); | ||
expect(memoryApi.getItem("key")).toBe("value"); | ||
expect(memoryApi.getItem("key2")).toBe("value2"); | ||
}); | ||
it('tests removeItem', () => { | ||
memoryApi.removeItem("key"); | ||
expect(memoryApi.getItem("key2")).toBe("value2"); | ||
expect(memoryApi.getItem("key")).toBeFalsy(); | ||
}); | ||
it('tests accessDenied', (done) => { | ||
memoryApi.setAccessDenied(true); | ||
try { | ||
memoryApi.getItem("key"); | ||
} catch (e) { | ||
expect(e).toBeTruthy(); | ||
const err = new Error("Cannot Access memoryStorage"); | ||
expect(e).toEqual(err); | ||
done(); | ||
} | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2018, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
|
||
import get from 'lodash/get'; | ||
import set from 'lodash/set'; | ||
import unset from 'lodash/unset'; | ||
|
||
let UserPersistedSession = { | ||
api: localStorage | ||
}; | ||
let MemoryStorage = {}; | ||
const ApiProviders = { | ||
localStorage: window.localStorage, | ||
memoryStorage: { | ||
accessDenied: false, | ||
getItem: (path) => { | ||
if (ApiProviders.memoryStorage.accessDenied) { | ||
throw Error("Cannot Access memoryStorage"); | ||
} | ||
return get(MemoryStorage, path); | ||
}, | ||
setItem: (path, value) => { | ||
if (ApiProviders.memoryStorage.accessDenied) { | ||
throw Error("Cannot Access memoryStorage"); | ||
} | ||
set(MemoryStorage, path, value); | ||
}, | ||
removeItem: (path) => { | ||
if (ApiProviders.memoryStorage.accessDenied) { | ||
throw Error("Cannot Access memoryStorage"); | ||
} | ||
return unset(MemoryStorage, path); | ||
}, | ||
setAccessDenied: (status = false) => { | ||
ApiProviders.memoryStorage.accessDenied = status; | ||
} | ||
} | ||
}; | ||
export const api = "localStorage"; | ||
/** | ||
* Add a new API implementation | ||
* @param {string} name the key of the added api implementation | ||
* @param {object} api the api implementation | ||
*/ | ||
export const addApi = (name, apiName) => { | ||
ApiProviders[name] = apiName; | ||
}; | ||
/** | ||
* Set the current API | ||
* @param {string} name the key of the api implementation to be used | ||
*/ | ||
export const setApi = (name = "localStorage") => { | ||
UserPersistedSession.api = name; | ||
}; | ||
/** | ||
* Add a new api implementation | ||
* @return {object} Current api | ||
*/ | ||
export const getApi = () => { | ||
return ApiProviders[UserPersistedSession.api]; | ||
}; | ||
UserPersistedSession = { | ||
api, | ||
addApi, | ||
setApi, | ||
getApi | ||
}; | ||
|
||
export default UserPersistedSession; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2017, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import expect from 'expect'; | ||
import { testEpic, TEST_TIMEOUT, addTimeoutEpic} from './epicTestUtils'; | ||
import { LOCATION_CHANGE } from 'connected-react-router'; | ||
import { cookiePolicyChecker } from '../cookies'; | ||
import { SET_COOKIE_VISIBILITY } from '../../actions/cookie'; | ||
import { setApi, getApi } from '../../api/userPersistedStorage'; | ||
|
||
|
||
describe('Cookies epics', () => { | ||
it('test cookiePolicyChecker and enable cookie msg', done => { | ||
const NUM_ACTIONS = 1; | ||
testEpic(cookiePolicyChecker, NUM_ACTIONS, {type: LOCATION_CHANGE}, (actions) => { | ||
expect(actions.length).toBe(NUM_ACTIONS); | ||
const [action] = actions; | ||
expect(action.type).toBe(SET_COOKIE_VISIBILITY); | ||
expect(action.status).toBe(true); | ||
done(); | ||
}, {}); | ||
}); | ||
it('test cookiePolicyChecker and trigger error for accessing localStorage', done => { | ||
setApi("memoryStorage"); | ||
getApi().setAccessDenied(true); | ||
const NUM_ACTIONS = 1; | ||
testEpic(addTimeoutEpic(cookiePolicyChecker, 40), NUM_ACTIONS, {type: LOCATION_CHANGE}, (actions) => { | ||
expect(actions.length).toBe(NUM_ACTIONS); | ||
const [action] = actions; | ||
expect(action.type).toBe(TEST_TIMEOUT); | ||
getApi().setAccessDenied(false); | ||
setApi("localStorage"); | ||
done(); | ||
}, {}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.