From fc988a30a81f2174af3ad7f0c968d23de4ab99b2 Mon Sep 17 00:00:00 2001 From: Chris Cole Date: Tue, 23 Jul 2019 13:57:03 -0700 Subject: [PATCH] DigiTrust Facade init GH Issue 3911 (#3918) * GH Issue 3911 Fix to init the DigiTrust facade object if the userId framework would not normally execute the call due to an ID being found. * Fixed superfluous trailing arg issue. * Addition of unit test for digiTrustIdSystem. Fix init error in facade callback. * Uncommenting init code. * Reverting package-lock.json file. * Removed extraneous function parameter. * Removing unused code. Fixing file casing issue causing unit test to fail. * Adding support for SameSite=none to cookie * Tweaking unit test. * Removing Promise from unit test as IE doesn't like it. * Cleanup of unused code lines * Minor comment changes. * Commenting out unit tests to see if this fixes Safari timeout issue. * Reenable test. Fixing where done was not being called. * Whitespace changes for lint * Capture and clear fallback timer to fix unit tests. * Removing unused function * Comment improvements. Retry CircleCI for unassociated failure. * Removed old call to attachIdSystem. --- modules/digiTrustIdSystem.js | 36 ++++++++++++--- modules/digiTrustIdSystem.md | 5 ++- test/spec/modules/digitrustIdSystem_spec.js | 50 +++++++++++++++++++++ 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 test/spec/modules/digitrustIdSystem_spec.js diff --git a/modules/digiTrustIdSystem.js b/modules/digiTrustIdSystem.js index 1db65031848..17f6fd9f737 100644 --- a/modules/digiTrustIdSystem.js +++ b/modules/digiTrustIdSystem.js @@ -9,11 +9,12 @@ * @requires module:modules/userId */ -// import { config } from 'src/config'; import * as utils from '../src/utils' import { ajax } from '../src/ajax'; import { submodule } from '../src/hook'; -// import { getGlobal } from 'src/prebidGlobal'; + +var fallbackTimeout = 1550; // timeout value that allows userId system to execute first +var fallbackTimer = 0; // timer Id for fallback init so we don't double call /** * Checks to see if the DigiTrust framework is initialized. @@ -81,7 +82,7 @@ function writeDigiId(id) { var date = new Date(); date.setTime(date.getTime() + 604800000); var exp = 'expires=' + date.toUTCString(); - document.cookie = key + '=' + encId(id) + '; ' + exp + '; path=/;'; + document.cookie = key + '=' + encId(id) + '; ' + exp + '; path=/;SameSite=none;'; } /** @@ -90,6 +91,10 @@ function writeDigiId(id) { */ function initDigitrustFacade(config) { var _savedId = null; // closure variable for storing Id to avoid additional requests + + clearTimeout(fallbackTimer); + fallbackTimer = 0; + var facade = { isClient: true, isMock: true, @@ -162,6 +167,10 @@ function initDigitrustFacade(config) { } } + if (config && isFunc(config.callback)) { + facade._internals.initCallback = config.callback; + } + if (window && window.DigiTrust == null) { window.DigiTrust = facade; } @@ -306,11 +315,12 @@ var testHook = {}; * Exposes the test hook object by attaching to the digitrustIdModule. * This method is called in the unit tests to surface internals. */ -function surfaceTestHook() { - digitrustIdModule['_testHook'] = testHook; +export function surfaceTestHook() { + digiTrustIdSubmodule['_testHook'] = testHook; + return testHook; } -testHook.initDigitrustFacade = initDigitrustFacade; +testHook.initDigitrustFacade = initDigitrustFacade; // expose for unit tests /** @type {Submodule} */ export const digiTrustIdSubmodule = { @@ -336,4 +346,18 @@ export const digiTrustIdSubmodule = { _testInit: surfaceTestHook }; +// check for fallback init of DigiTrust +function fallbackInit() { + if (resultHandler.retryId == 0 && !isInitialized()) { + // this triggers an init + var conf = { + member: 'fallback', + callback: noop + }; + getDigiTrustId(conf); + } +} + +fallbackTimer = setTimeout(fallbackInit, fallbackTimeout); + submodule('userId', digiTrustIdSubmodule); diff --git a/modules/digiTrustIdSystem.md b/modules/digiTrustIdSystem.md index 6ddbad4aea4..c0b274d3292 100644 --- a/modules/digiTrustIdSystem.md +++ b/modules/digiTrustIdSystem.md @@ -24,7 +24,9 @@ for further instructions. site: 'example_site_id' }, callback: function (digiTrustResult) { - // This callback method is optional + // This callback method is optional and used for error handling + // in many if not most cases. + /* if (digiTrustResult.success) { // Success in Digitrust init; // 'DigiTrust Id (encrypted): ' + digiTrustResult.identity.id; @@ -32,6 +34,7 @@ for further instructions. else { // Digitrust init failed } + */ } }, storage: { diff --git a/test/spec/modules/digitrustIdSystem_spec.js b/test/spec/modules/digitrustIdSystem_spec.js new file mode 100644 index 00000000000..55035bc4b4e --- /dev/null +++ b/test/spec/modules/digitrustIdSystem_spec.js @@ -0,0 +1,50 @@ +import { + digiTrustIdSubmodule, + surfaceTestHook +} from 'modules/digiTrustIdSystem'; + +let assert = require('chai').assert; +let expect = require('chai').expect; + +var testHook = null; + +describe('DigiTrust Id System', function () { + it('Should create the test hook', function (done) { + testHook = surfaceTestHook(); + assert.isNotNull(testHook, 'The test hook failed to surface'); + var conf = { + init: { + member: 'unit_test', + site: 'foo' + }, + callback: function (result) { + } + }; + testHook.initDigitrustFacade(conf); + window.DigiTrust.getUser(conf); + expect(window.DigiTrust).to.exist; + expect(window.DigiTrust.isMock).to.be.true; + done(); + }); + + it('Should report as client', function (done) { + delete window.DigiTrust; + testHook = surfaceTestHook(); + + var conf = { + init: { + member: 'unit_test', + site: 'foo' + }, + callback: function (result) { + expect(window.DigiTrust).to.exist; + expect(result).to.exist; + expect(window.DigiTrust.isMock).to.be.true; + } + }; + testHook.initDigitrustFacade(conf); + expect(window.DigiTrust).to.exist; + expect(window.DigiTrust.isClient).to.be.true; + done(); + }); +});