-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navegg UserID Submodule: conform with pub storage configuration #12032
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
05c25b0
dev/ simple solution
p-ribeiro c29b15b
dev: working script and working tests
p-ribeiro 279272b
dev/ final adjustments
p-ribeiro 486df9d
Merge branch 'prebid:master' into navegg/fixing_saving
p-ribeiro 13bd384
dev: minor-changes
p-ribeiro 4ac7a9b
hotfix/ importing only the necessary functions
p-ribeiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,45 +1,156 @@ | ||
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 = { | ||
storage: { | ||
name: 'nvggid', | ||
type: 'cookie&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'; | ||
}); | ||
} | ||
|
||
function setLocalStorage() { | ||
storage.setDataInLocalStorage('nvggid', 'localstorage_value'); | ||
} | ||
|
||
describe('getId', function () { | ||
let ajaxStub, ajaxBuilderStub; | ||
|
||
beforeEach(function() { | ||
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; | ||
}); | ||
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; | ||
|
||
expect(id).to.be.undefined; | ||
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_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) | ||
|
||
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) | ||
|
||
it('getId() should return "test-nvggid" id from local storage NAVEGG_ID', function() { | ||
storage.getDataFromLocalStorage.callsFake(() => 'test-ninvggidd') | ||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('old_nvg_cookie')).to.be.true; | ||
}); | ||
|
||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'test-ninvggidd'}) | ||
}) | ||
it('should return correct value from API response', function(done) { | ||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
it('getId() should return "test-nvggid" id from local storage NAV0', function() { | ||
storage.getDataFromLocalStorage.callsFake(() => 'nvgid-nav0') | ||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
|
||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'nvgid-nav0'}) | ||
}) | ||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('test_nvggid')).to.be.true; | ||
done(); | ||
}); | ||
|
||
it('getId() should return "test-nvggid" id from local storage NVG0', function() { | ||
storage.getDataFromLocalStorage.callsFake(() => '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; | ||
|
||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'nvgid-nvg0'}) | ||
}) | ||
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(undefined)).to.be.true; | ||
done(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please only import what you need