From 05c25b04eecbf0fed8e85792ef0800203ec97fc9 Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro Date: Thu, 18 Jul 2024 22:00:23 +0000 Subject: [PATCH 1/5] dev/ simple solution --- modules/naveggIdSystem.js | 113 ++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 46 deletions(-) diff --git a/modules/naveggIdSystem.js b/modules/naveggIdSystem.js index 42c6b113566..3ee7e2b1e69 100644 --- a/modules/naveggIdSystem.js +++ b/modules/naveggIdSystem.js @@ -7,7 +7,7 @@ import { isStr, isPlainObject, logError } from '../src/utils.js'; import { submodule } from '../src/hook.js'; import { ajax } from '../src/ajax.js'; -import {getStorageManager} from '../src/storageManager.js'; +import {getStorageManager, STORAGE_TYPE_COOKIES, STORAGE_TYPE_LOCALSTORAGE} from '../src/storageManager.js'; import {MODULE_TYPE_UID} from '../src/activities/modules.js'; /** @@ -24,55 +24,59 @@ const INVALID_EXPIRE = 3600 * 1000; export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME}); -function getNaveggIdFromApi() { - const callbacks = { - success: response => { - if (response) { - try { - const responseObj = JSON.parse(response); - writeCookie(NAVEGG_ID, responseObj[NAVEGG_ID]); - } catch (error) { - logError(error); +/* eslint-disable no-console */ +function getIdFromAPI(toStore, storedName) { + const resp = function (callback) { + ajax(BASE_URL, { + success: response => { + if (response) { + try { + const responseObj = {'nvggid': 'bmF2ZWdnaWQK'} // JSON.parse(response); + console.log('responseObj: ' + responseObj) + callback(responseObj[NAVEGG_ID]) + } catch (error) { + logError(error); + callback() + } } + }, + error: error => { + logError('Navegg ID fetch encountered an error', error); } }, - error: error => { - logError('Navegg ID fetch encountered an error', error); - } - }; - ajax(BASE_URL, callbacks, undefined, { method: 'GET', withCredentials: false }); -} - -function writeCookie(key, value) { - try { - if (storage.cookiesAreEnabled) { - let expTime = new Date(); - const expires = value ? DEFAULT_EXPIRE : INVALID_EXPIRE; - expTime.setTime(expTime.getTime() + expires); - storage.setCookie(key, value, expTime.toUTCString(), 'none'); - } - } catch (e) { - logError(e); + undefined, { method: 'GET', withCredentials: false }); } + return resp } -function readnaveggIdFromLocalStorage() { +/** + * @returns {string | null} + */ +function readNaveggIdFromLocalStorage() { return storage.localStorageIsEnabled ? storage.getDataFromLocalStorage(NAVEGG_ID) : null; } - -function readnaveggIDFromCookie() { +/** + * @returns {string | null} + */ +function readNaveggIdFromCookie() { return storage.cookiesAreEnabled ? storage.getCookie(NAVEGG_ID) : null; } - -function readoldnaveggIDFromCookie() { +/** + * @returns {string | null} + */ +function readOldNaveggIdFromCookie() { return storage.cookiesAreEnabled ? storage.getCookie(OLD_NAVEGG_ID) : null; } - -function readnvgIDFromCookie() { +/** + * @returns {string | null} + */ +function readNvgIdFromCookie() { return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nvg') ? storage.findSimilarCookies('nvg')[0] : null) : null; } - -function readnavIDFromCookie() { +/** + * @returns {string | null} + */ +function readNavIdFromCookie() { return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nav') ? storage.findSimilarCookies('nav')[0] : null) : null; } @@ -90,28 +94,45 @@ export const naveggIdSubmodule = { * @return { Object | string | undefined } */ decode(value) { + console.log('decode value: ' + value) const naveggIdVal = value ? isStr(value) ? value : isPlainObject(value) ? value.id : undefined : undefined; return naveggIdVal ? { 'naveggId': naveggIdVal.split('|')[0] } : undefined; }, + /** * performs action to obtain id and return a value in the callback's response argument * @function * @param {SubmoduleConfig} config * @return {{id: string | undefined } | undefined} */ - getId() { - const naveggIdString = readnaveggIdFromLocalStorage() || readnaveggIDFromCookie() || getNaveggIdFromApi() || readoldnaveggIDFromCookie() || readnvgIDFromCookie() || readnavIDFromCookie(); + getId({ name, enabledStorageTypes = [], storage: storageConfig = {} }) { + const resp = getIdFromAPI(enabledStorageTypes, storageConfig.name) - if (typeof naveggIdString == 'string' && naveggIdString) { - try { - return { id: naveggIdString }; - } catch (error) { - logError(error); - } - } - return undefined; + // const naveggIdOldString = readOldNaveggIdFromCookie() || readNvgIdFromCookie() || readNavIdFromCookie() + // const naveggIdLocalStorage = readNaveggIdFromLocalStorage() + // const naveggIdCookie = readNaveggIdFromCookie() + // let toStore = [] + + // if (!naveggIdLocalStorage && enabledStorageTypes.indexOf(STORAGE_TYPE_LOCALSTORAGE) > -1) { toStore.push(STORAGE_TYPE_LOCALSTORAGE) } + // if (!naveggIdCookie && enabledStorageTypes.indexOf(STORAGE_TYPE_COOKIES) > -1) { toStore.push(STORAGE_TYPE_COOKIES) } + // saveNaveggIdFromApi(toStore, storageConfig.name) + + // const naveggIdString = readNaveggIdFromCookie() || readNaveggIdFromLocalStorage(); + + // console.log('naveggIdString: ' + naveggIdString) + // console.log('----------------------------') + + // if (typeof naveggIdString == 'string' && naveggIdString) { + // try { + // return { id: naveggIdString }; + // } catch (error) { + // logError(error); + // } + // } + // return undefined; + return {callback: resp} }, eids: { 'naveggId': { From c29b15bb2e8016097d730610a2c7c7cae7779cc2 Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro Date: Tue, 23 Jul 2024 14:55:31 +0000 Subject: [PATCH 2/5] dev: working script and working tests --- modules/naveggIdSystem.js | 89 +++++------- test/spec/modules/naveggIdSystem_spec.js | 172 +++++++++++++++++++---- 2 files changed, 175 insertions(+), 86 deletions(-) diff --git a/modules/naveggIdSystem.js b/modules/naveggIdSystem.js index 3ee7e2b1e69..e6794aab542 100644 --- a/modules/naveggIdSystem.js +++ b/modules/naveggIdSystem.js @@ -6,8 +6,8 @@ */ import { isStr, isPlainObject, logError } from '../src/utils.js'; import { submodule } from '../src/hook.js'; -import { ajax } from '../src/ajax.js'; -import {getStorageManager, STORAGE_TYPE_COOKIES, STORAGE_TYPE_LOCALSTORAGE} from '../src/storageManager.js'; +import * as ajaxLib from '../src/ajax.js'; +import {getStorageManager} from '../src/storageManager.js'; import {MODULE_TYPE_UID} from '../src/activities/modules.js'; /** @@ -19,47 +19,57 @@ const MODULE_NAME = 'naveggId'; const OLD_NAVEGG_ID = 'nid'; const NAVEGG_ID = 'nvggid'; const BASE_URL = 'https://id.navegg.com/uid/'; -const DEFAULT_EXPIRE = 8 * 24 * 3600 * 1000; -const INVALID_EXPIRE = 3600 * 1000; export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME}); /* eslint-disable no-console */ function getIdFromAPI(toStore, storedName) { const resp = function (callback) { - ajax(BASE_URL, { - success: response => { + ajaxLib.ajaxBuilder()( + BASE_URL, + response => { if (response) { + let responseObj; + try { - const responseObj = {'nvggid': 'bmF2ZWdnaWQK'} // JSON.parse(response); - console.log('responseObj: ' + responseObj) - callback(responseObj[NAVEGG_ID]) + responseObj = JSON.parse(response); + console.log(JSON.stringify(responseObj)) } catch (error) { logError(error); - callback() + const fallbackValue = getOldCookie(); + callback(fallbackValue) + } + + if (responseObj && responseObj[NAVEGG_ID]) { + callback(responseObj[NAVEGG_ID]); + } else { + const fallbackValue = getOldCookie(); + console.log('fallbackValue ' + fallbackValue) + callback(fallbackValue); } } }, - error: error => { + error => { logError('Navegg ID fetch encountered an error', error); - } - }, - undefined, { method: 'GET', withCredentials: false }); - } + const fallbackValue = getOldCookie(); + callback(fallbackValue) + }, + {method: 'GET', withCredentials: false}); + }; return resp } /** * @returns {string | null} */ -function readNaveggIdFromLocalStorage() { - return storage.localStorageIsEnabled ? storage.getDataFromLocalStorage(NAVEGG_ID) : null; +function readNvgIdFromCookie() { + return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nvg') ? storage.findSimilarCookies('nvg')[0] : null) : null; } /** * @returns {string | null} */ -function readNaveggIdFromCookie() { - return storage.cookiesAreEnabled ? storage.getCookie(NAVEGG_ID) : null; +function readNavIdFromCookie() { + return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nav') ? storage.findSimilarCookies('nav')[0] : null) : null; } /** * @returns {string | null} @@ -67,17 +77,10 @@ function readNaveggIdFromCookie() { function readOldNaveggIdFromCookie() { return storage.cookiesAreEnabled ? storage.getCookie(OLD_NAVEGG_ID) : null; } -/** - * @returns {string | null} - */ -function readNvgIdFromCookie() { - return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nvg') ? storage.findSimilarCookies('nvg')[0] : null) : null; -} -/** - * @returns {string | null} - */ -function readNavIdFromCookie() { - return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nav') ? storage.findSimilarCookies('nav')[0] : null) : null; + +function getOldCookie() { + const oldCookie = readOldNaveggIdFromCookie() || readNvgIdFromCookie() || readNavIdFromCookie(); + return oldCookie; } /** @type {Submodule} */ @@ -94,7 +97,6 @@ export const naveggIdSubmodule = { * @return { Object | string | undefined } */ decode(value) { - console.log('decode value: ' + value) const naveggIdVal = value ? isStr(value) ? value : isPlainObject(value) ? value.id : undefined : undefined; return naveggIdVal ? { 'naveggId': naveggIdVal.split('|')[0] @@ -107,31 +109,8 @@ export const naveggIdSubmodule = { * @param {SubmoduleConfig} config * @return {{id: string | undefined } | undefined} */ - getId({ name, enabledStorageTypes = [], storage: storageConfig = {} }) { + getId({ name, enabledStorageTypes = [], storage: storageConfig = {} }, gdprConsent, storedId) { const resp = getIdFromAPI(enabledStorageTypes, storageConfig.name) - - // const naveggIdOldString = readOldNaveggIdFromCookie() || readNvgIdFromCookie() || readNavIdFromCookie() - // const naveggIdLocalStorage = readNaveggIdFromLocalStorage() - // const naveggIdCookie = readNaveggIdFromCookie() - // let toStore = [] - - // if (!naveggIdLocalStorage && enabledStorageTypes.indexOf(STORAGE_TYPE_LOCALSTORAGE) > -1) { toStore.push(STORAGE_TYPE_LOCALSTORAGE) } - // if (!naveggIdCookie && enabledStorageTypes.indexOf(STORAGE_TYPE_COOKIES) > -1) { toStore.push(STORAGE_TYPE_COOKIES) } - // saveNaveggIdFromApi(toStore, storageConfig.name) - - // const naveggIdString = readNaveggIdFromCookie() || readNaveggIdFromLocalStorage(); - - // console.log('naveggIdString: ' + naveggIdString) - // console.log('----------------------------') - - // if (typeof naveggIdString == 'string' && naveggIdString) { - // try { - // return { id: naveggIdString }; - // } catch (error) { - // logError(error); - // } - // } - // return undefined; return {callback: resp} }, eids: { diff --git a/test/spec/modules/naveggIdSystem_spec.js b/test/spec/modules/naveggIdSystem_spec.js index 2c4f1cda859..d4b0fa11f99 100644 --- a/test/spec/modules/naveggIdSystem_spec.js +++ b/test/spec/modules/naveggIdSystem_spec.js @@ -1,45 +1,155 @@ -import { naveggIdSubmodule, storage } from 'modules/naveggIdSystem.js'; +import { naveggIdSubmodule, storage, getIdFromAPI } from 'modules/naveggIdSystem.js'; +import * as utils from '../../../src/utils.js'; +import { server } from 'test/mocks/xhr.js'; +import * as ajaxLib from 'src/ajax.js'; -describe('naveggId', function () { - let sandbox; - beforeEach(() => { - sandbox = sinon.sandbox.create(); - sandbox.stub(storage, 'getDataFromLocalStorage'); +const NAVEGGID_CONFIG_COOKIE_HTML5 = { + name: 'naveggId', + storage: { + name: 'nvggid', + type: 'cookie&html5', + expires: 8 + } +} + +const NAVEGGID_CONFIG_COOKIE = { + name: 'naveggId', + storage: { + name: 'nvggid', + type: 'cookie', + expires: 8 + } +} + +const NAVEGGID_CONFIG_HTML5 = { + name: 'naveggId', + storage: { + name: 'nvggid', + type: 'html5', + expires: 8 + } +} + +const MOCK_RESPONSE = { + nvggid: 'test_nvggid' +} + +const MOCK_RESPONSE_NULL = { + nvggid: null +} + +function mockResponse(responseText, isSuccess = true) { + return function(url, callbacks) { + if (isSuccess) { + callbacks.success(responseText) + } else { + callbacks.error(new Error('Mock Error')) + } + } +} + +function deleteAllCookies() { + document.cookie.split(';').forEach(cookie => { + const eqPos = cookie.indexOf('='); + const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie; + document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; + }); +} + +/* eslint-disable no-console */ +describe('getId', function () { + let ajaxStub, ajaxBuilderStub; + + beforeEach(function() { + ajaxStub = sinon.stub() + ajaxBuilderStub = sinon.stub(ajaxLib, 'ajaxBuilder').returns(ajaxStub); + }); + + afterEach(function() { + ajaxBuilderStub.restore(); + deleteAllCookies(); }); - afterEach(() => { - sandbox.restore(); + + it('should get the value from a nid cookie', function() { + storage.setCookie('nid', 'old_nid_cookie', storage.expires) + + const callback = sinon.spy(); + const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; + + ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { + if (successCallbacks && typeof successCallbacks === 'function') { + successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); + } + }); + apiCallback(callback) + + expect(callback.calledOnce).to.be.true; + expect(callback.calledWith('old_nid_cookie')).to.be.true; }); - it('should NOT find navegg id', function () { - let id = naveggIdSubmodule.getId(); + it('should get the value from a nav cookie', function() { + storage.setCookie('navId', 'old_nav_cookie', storage.expires) + + const callback = sinon.spy(); + const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; + + ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { + if (successCallbacks && typeof successCallbacks === 'function') { + successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); + } + }); + apiCallback(callback) - expect(id).to.be.undefined; + expect(callback.calledOnce).to.be.true; + expect(callback.calledWith('old_nav_cookie')).to.be.true; }); - it('getId() should return "test-nid" id from cookie OLD_NAVEGG_ID', function() { - sinon.stub(storage, 'getCookie').withArgs('nid').returns('test-nid'); - let id = naveggIdSubmodule.getId(); - expect(id).to.be.deep.equal({id: 'test-nid'}) - }) + it('should get the value from an old nvg cookie', function() { + storage.setCookie('nvgid', 'old_nvg_cookie', storage.expires) - it('getId() should return "test-nvggid" id from local storage NAVEGG_ID', function() { - storage.getDataFromLocalStorage.callsFake(() => 'test-ninvggidd') + const callback = sinon.spy(); + const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; - let id = naveggIdSubmodule.getId(); - expect(id).to.be.deep.equal({id: 'test-ninvggidd'}) - }) + ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { + if (successCallbacks && typeof successCallbacks === 'function') { + successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); + } + }); + apiCallback(callback) - it('getId() should return "test-nvggid" id from local storage NAV0', function() { - storage.getDataFromLocalStorage.callsFake(() => 'nvgid-nav0') + expect(callback.calledOnce).to.be.true; + expect(callback.calledWith('old_nvg_cookie')).to.be.true; + }); + + it('should return correct value from API response', function(done) { + const callback = sinon.spy(); + const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; - let id = naveggIdSubmodule.getId(); - expect(id).to.be.deep.equal({id: 'nvgid-nav0'}) - }) + ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { + if (successCallbacks && typeof successCallbacks === 'function') { + successCallbacks(JSON.stringify(MOCK_RESPONSE)); + } + }); + apiCallback(callback) - it('getId() should return "test-nvggid" id from local storage NVG0', function() { - storage.getDataFromLocalStorage.callsFake(() => 'nvgid-nvg0') + expect(callback.calledOnce).to.be.true; + expect(callback.calledWith('test_nvggid')).to.be.true; + done(); + }); - let id = naveggIdSubmodule.getId(); - expect(id).to.be.deep.equal({id: 'nvgid-nvg0'}) - }) + it('should return no value from API response', function(done) { + const callback = sinon.spy(); + const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; + + ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { + if (successCallbacks && typeof successCallbacks === 'function') { + successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); + } + }); + apiCallback(callback) + console.log('result: ' + callback.lastCall.lastArg) + expect(callback.calledOnce).to.be.true; + expect(callback.calledWith(undefined)).to.be.true; + done(); + }); }); From 279272b07c6f99fc8c2e03b0e8265cf8d1a7affd Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro Date: Wed, 24 Jul 2024 14:44:17 +0000 Subject: [PATCH 3/5] dev/ final adjustments --- modules/naveggIdSystem.js | 30 +++++++++------- modules/naveggIdSystem.md | 8 +++++ test/spec/modules/naveggIdSystem_spec.js | 45 ++++++++++++------------ 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/modules/naveggIdSystem.js b/modules/naveggIdSystem.js index e6794aab542..f0fa1ae9f79 100644 --- a/modules/naveggIdSystem.js +++ b/modules/naveggIdSystem.js @@ -22,36 +22,32 @@ const BASE_URL = 'https://id.navegg.com/uid/'; export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME}); -/* eslint-disable no-console */ -function getIdFromAPI(toStore, storedName) { +function getIdFromAPI() { const resp = function (callback) { ajaxLib.ajaxBuilder()( BASE_URL, response => { if (response) { let responseObj; - try { responseObj = JSON.parse(response); - console.log(JSON.stringify(responseObj)) } catch (error) { logError(error); - const fallbackValue = getOldCookie(); + const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie(); callback(fallbackValue) } if (responseObj && responseObj[NAVEGG_ID]) { callback(responseObj[NAVEGG_ID]); } else { - const fallbackValue = getOldCookie(); - console.log('fallbackValue ' + fallbackValue) + const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie(); callback(fallbackValue); } } }, error => { logError('Navegg ID fetch encountered an error', error); - const fallbackValue = getOldCookie(); + const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie(); callback(fallbackValue) }, {method: 'GET', withCredentials: false}); @@ -69,19 +65,27 @@ function readNvgIdFromCookie() { * @returns {string | null} */ function readNavIdFromCookie() { - return storage.cookiesAreEnabled ? (storage.findSimilarCookies('nav') ? storage.findSimilarCookies('nav')[0] : null) : null; + return storage.cookiesAreEnabled() ? (storage.findSimilarCookies('nav') ? storage.findSimilarCookies('nav')[0] : null) : null; } /** * @returns {string | null} */ function readOldNaveggIdFromCookie() { - return storage.cookiesAreEnabled ? storage.getCookie(OLD_NAVEGG_ID) : null; + return storage.cookiesAreEnabled() ? storage.getCookie(OLD_NAVEGG_ID) : null; } - +/** + * @returns {string | null} + */ function getOldCookie() { const oldCookie = readOldNaveggIdFromCookie() || readNvgIdFromCookie() || readNavIdFromCookie(); return oldCookie; } +/** + * @returns {string | null} + */ +function getNaveggIdFromLocalStorage() { + return storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage(NAVEGG_ID) : null; +} /** @type {Submodule} */ export const naveggIdSubmodule = { @@ -109,8 +113,8 @@ export const naveggIdSubmodule = { * @param {SubmoduleConfig} config * @return {{id: string | undefined } | undefined} */ - getId({ name, enabledStorageTypes = [], storage: storageConfig = {} }, gdprConsent, storedId) { - const resp = getIdFromAPI(enabledStorageTypes, storageConfig.name) + getId(config, consentData) { + const resp = getIdFromAPI() return {callback: resp} }, eids: { diff --git a/modules/naveggIdSystem.md b/modules/naveggIdSystem.md index f758fbc9d5d..81d9841c78f 100644 --- a/modules/naveggIdSystem.md +++ b/modules/naveggIdSystem.md @@ -10,6 +10,11 @@ pbjs.setConfig({ userSync: { userIds: [{ name: 'naveggId', + storage: { + name : 'nvggid', + type : 'cookie&html5', + expires: 8 + } }] } }); @@ -20,3 +25,6 @@ The below parameters apply only to the naveggID integration. | Param under usersync.userIds[] | Scope | Type | Description | Example | | --- | --- | --- | --- | --- | | name | Required | String | ID of the module - `"naveggId"` | `"naveggId"` | +| storage.name | Required | String | The name of the cookie or html5 local storage where the user ID will be stored. | `"nvggid"` | +| storage.type | Required | String | Must be "`cookie`", "`html5`" or "`cookie&html5`". This is where the results of the user ID will be stored. | `"cookie&html5"` | +| storage.expires | Required | Integer | How long (in days) the user ID information will be stored. | `8` | diff --git a/test/spec/modules/naveggIdSystem_spec.js b/test/spec/modules/naveggIdSystem_spec.js index d4b0fa11f99..4a9195a06b5 100644 --- a/test/spec/modules/naveggIdSystem_spec.js +++ b/test/spec/modules/naveggIdSystem_spec.js @@ -4,7 +4,6 @@ import { server } from 'test/mocks/xhr.js'; import * as ajaxLib from 'src/ajax.js'; const NAVEGGID_CONFIG_COOKIE_HTML5 = { - name: 'naveggId', storage: { name: 'nvggid', type: 'cookie&html5', @@ -12,24 +11,6 @@ const NAVEGGID_CONFIG_COOKIE_HTML5 = { } } -const NAVEGGID_CONFIG_COOKIE = { - name: 'naveggId', - storage: { - name: 'nvggid', - type: 'cookie', - expires: 8 - } -} - -const NAVEGGID_CONFIG_HTML5 = { - name: 'naveggId', - storage: { - name: 'nvggid', - type: 'html5', - expires: 8 - } -} - const MOCK_RESPONSE = { nvggid: 'test_nvggid' } @@ -56,18 +37,39 @@ function deleteAllCookies() { }); } -/* eslint-disable no-console */ +function setLocalStorage() { + storage.setDataInLocalStorage('nvggid', 'localstorage_value'); +} + describe('getId', function () { let ajaxStub, ajaxBuilderStub; beforeEach(function() { - ajaxStub = sinon.stub() + ajaxStub = sinon.stub(); ajaxBuilderStub = sinon.stub(ajaxLib, 'ajaxBuilder').returns(ajaxStub); }); afterEach(function() { ajaxBuilderStub.restore(); deleteAllCookies(); + storage.removeDataFromLocalStorage('nvggid'); + }); + + it('should get the value from the existing localstorage', function() { + setLocalStorage(); + + const callback = sinon.spy(); + const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; + + ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { + if (successCallbacks && typeof successCallbacks === 'function') { + successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); + } + }); + apiCallback(callback) + + expect(callback.calledOnce).to.be.true; + expect(callback.calledWith('localstorage_value')).to.be.true; }); it('should get the value from a nid cookie', function() { @@ -147,7 +149,6 @@ describe('getId', function () { } }); apiCallback(callback) - console.log('result: ' + callback.lastCall.lastArg) expect(callback.calledOnce).to.be.true; expect(callback.calledWith(undefined)).to.be.true; done(); From 13bd384586f3e2d237cb85a6f43eef33f46745c7 Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro Date: Mon, 29 Jul 2024 14:15:27 +0000 Subject: [PATCH 4/5] dev: minor-changes --- modules/naveggIdSystem.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/naveggIdSystem.js b/modules/naveggIdSystem.js index f0fa1ae9f79..cba502a2c37 100644 --- a/modules/naveggIdSystem.js +++ b/modules/naveggIdSystem.js @@ -34,7 +34,7 @@ function getIdFromAPI() { } catch (error) { logError(error); const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie(); - callback(fallbackValue) + callback(fallbackValue); } if (responseObj && responseObj[NAVEGG_ID]) { @@ -48,11 +48,11 @@ function getIdFromAPI() { error => { logError('Navegg ID fetch encountered an error', error); const fallbackValue = getNaveggIdFromLocalStorage() || getOldCookie(); - callback(fallbackValue) + callback(fallbackValue); }, {method: 'GET', withCredentials: false}); }; - return resp + return resp; } /** From 4ac7a9b6140b8d2fb64eb279922b85fef9a7cd3f Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro Date: Thu, 1 Aug 2024 15:01:22 +0000 Subject: [PATCH 5/5] hotfix/ importing only the necessary functions --- modules/naveggIdSystem.js | 8 ++++---- test/spec/modules/naveggIdSystem_spec.js | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/naveggIdSystem.js b/modules/naveggIdSystem.js index cba502a2c37..6f1964b11df 100644 --- a/modules/naveggIdSystem.js +++ b/modules/naveggIdSystem.js @@ -6,9 +6,9 @@ */ import { isStr, isPlainObject, logError } from '../src/utils.js'; import { submodule } from '../src/hook.js'; -import * as ajaxLib from '../src/ajax.js'; -import {getStorageManager} from '../src/storageManager.js'; -import {MODULE_TYPE_UID} from '../src/activities/modules.js'; +import { ajaxBuilder } from '../src/ajax.js'; +import { getStorageManager } from '../src/storageManager.js'; +import { MODULE_TYPE_UID } from '../src/activities/modules.js'; /** * @typedef {import('../modules/userId/index.js').Submodule} Submodule @@ -24,7 +24,7 @@ export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleNam function getIdFromAPI() { const resp = function (callback) { - ajaxLib.ajaxBuilder()( + ajaxBuilder()( BASE_URL, response => { if (response) { diff --git a/test/spec/modules/naveggIdSystem_spec.js b/test/spec/modules/naveggIdSystem_spec.js index 4a9195a06b5..4907a63abde 100644 --- a/test/spec/modules/naveggIdSystem_spec.js +++ b/test/spec/modules/naveggIdSystem_spec.js @@ -1,5 +1,4 @@ import { naveggIdSubmodule, storage, getIdFromAPI } from 'modules/naveggIdSystem.js'; -import * as utils from '../../../src/utils.js'; import { server } from 'test/mocks/xhr.js'; import * as ajaxLib from 'src/ajax.js';