|
| 1 | +import { expect } from 'chai'; |
| 2 | +import find from 'core-js-pure/features/array/find.js'; |
| 3 | +import { config } from 'src/config.js'; |
| 4 | +import { newStorageManager } from 'src/storageManager.js'; |
| 5 | +import { init, requestBidsHook, setSubmoduleRegistry } from 'modules/userId/index.js'; |
| 6 | +import { zeotapIdPlusSubmodule } from 'modules/zeotapIdPlusIdSystem.js'; |
| 7 | + |
| 8 | +const storage = newStorageManager(); |
| 9 | + |
| 10 | +const ZEOTAP_COOKIE_NAME = 'IDP'; |
| 11 | +const ZEOTAP_COOKIE = 'THIS-IS-A-DUMMY-COOKIE'; |
| 12 | +const ENCODED_ZEOTAP_COOKIE = btoa(JSON.stringify(ZEOTAP_COOKIE)); |
| 13 | + |
| 14 | +function getConfigMock() { |
| 15 | + return { |
| 16 | + userSync: { |
| 17 | + syncDelay: 0, |
| 18 | + userIds: [{ |
| 19 | + name: 'zeotapIdPlus' |
| 20 | + }] |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function getAdUnitMock(code = 'adUnit-code') { |
| 26 | + return { |
| 27 | + code, |
| 28 | + mediaTypes: {banner: {}, native: {}}, |
| 29 | + sizes: [ |
| 30 | + [300, 200], |
| 31 | + [300, 600] |
| 32 | + ], |
| 33 | + bids: [{ |
| 34 | + bidder: 'sampleBidder', |
| 35 | + params: { placementId: 'banner-only-bidder' } |
| 36 | + }] |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function unsetCookie() { |
| 41 | + storage.setCookie(ZEOTAP_COOKIE_NAME, ''); |
| 42 | +} |
| 43 | + |
| 44 | +function unsetLocalStorage() { |
| 45 | + storage.setDataInLocalStorage(ZEOTAP_COOKIE_NAME, ''); |
| 46 | +} |
| 47 | + |
| 48 | +describe('Zeotap ID System', function() { |
| 49 | + describe('test method: getId', function() { |
| 50 | + afterEach(() => { |
| 51 | + unsetCookie(); |
| 52 | + unsetLocalStorage(); |
| 53 | + }) |
| 54 | + |
| 55 | + it('provides the stored Zeotap id if a cookie exists', function() { |
| 56 | + storage.setCookie( |
| 57 | + ZEOTAP_COOKIE_NAME, |
| 58 | + ENCODED_ZEOTAP_COOKIE, |
| 59 | + (new Date(Date.now() + 5000).toUTCString()), |
| 60 | + ); |
| 61 | + let id = zeotapIdPlusSubmodule.getId(); |
| 62 | + expect(id).to.deep.equal({ |
| 63 | + id: ENCODED_ZEOTAP_COOKIE |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('provides the stored Zeotap id if cookie is absent but present in local storage', function() { |
| 68 | + storage.setDataInLocalStorage(ZEOTAP_COOKIE_NAME, ENCODED_ZEOTAP_COOKIE); |
| 69 | + let id = zeotapIdPlusSubmodule.getId(); |
| 70 | + expect(id).to.deep.equal({ |
| 71 | + id: ENCODED_ZEOTAP_COOKIE |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('returns undefined if both cookie and local storage are empty', function() { |
| 76 | + let id = zeotapIdPlusSubmodule.getId(); |
| 77 | + expect(id).to.be.undefined |
| 78 | + }) |
| 79 | + }); |
| 80 | + |
| 81 | + describe('test method: decode', function() { |
| 82 | + it('provides the Zeotap ID (IDP) from a stored object', function() { |
| 83 | + let zeotapId = { |
| 84 | + id: ENCODED_ZEOTAP_COOKIE, |
| 85 | + }; |
| 86 | + |
| 87 | + expect(zeotapIdPlusSubmodule.decode(zeotapId)).to.deep.equal({ |
| 88 | + IDP: ZEOTAP_COOKIE |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('provides the Zeotap ID (IDP) from a stored string', function() { |
| 93 | + let zeotapId = ENCODED_ZEOTAP_COOKIE; |
| 94 | + |
| 95 | + expect(zeotapIdPlusSubmodule.decode(zeotapId)).to.deep.equal({ |
| 96 | + IDP: ZEOTAP_COOKIE |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('requestBids hook', function() { |
| 102 | + let adUnits; |
| 103 | + |
| 104 | + beforeEach(function() { |
| 105 | + adUnits = [getAdUnitMock()]; |
| 106 | + storage.setCookie( |
| 107 | + ZEOTAP_COOKIE_NAME, |
| 108 | + ENCODED_ZEOTAP_COOKIE, |
| 109 | + (new Date(Date.now() + 5000).toUTCString()), |
| 110 | + ); |
| 111 | + setSubmoduleRegistry([zeotapIdPlusSubmodule]); |
| 112 | + init(config); |
| 113 | + config.setConfig(getConfigMock()); |
| 114 | + }); |
| 115 | + |
| 116 | + afterEach(function() { |
| 117 | + unsetCookie(); |
| 118 | + unsetLocalStorage(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('when a stored Zeotap ID exists it is added to bids', function(done) { |
| 122 | + requestBidsHook(function() { |
| 123 | + adUnits.forEach(unit => { |
| 124 | + unit.bids.forEach(bid => { |
| 125 | + expect(bid).to.have.deep.nested.property('userId.IDP'); |
| 126 | + expect(bid.userId.IDP).to.equal(ZEOTAP_COOKIE); |
| 127 | + const zeotapIdAsEid = find(bid.userIdAsEids, e => e.source == 'zeotap.com'); |
| 128 | + expect(zeotapIdAsEid).to.deep.equal({ |
| 129 | + source: 'zeotap.com', |
| 130 | + uids: [{ |
| 131 | + id: ZEOTAP_COOKIE, |
| 132 | + atype: 1, |
| 133 | + }] |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + done(); |
| 138 | + }, { adUnits }); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments