Skip to content

Commit

Permalink
DigiTrust Facade init GH Issue 3911 (prebid#3918)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
goosemanjack authored and sa1omon committed Nov 28, 2019
1 parent 58230ef commit 5f74875
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
36 changes: 30 additions & 6 deletions modules/digiTrustIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;';
}

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
5 changes: 4 additions & 1 deletion modules/digiTrustIdSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ 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;
}
else {
// Digitrust init failed
}
*/
}
},
storage: {
Expand Down
50 changes: 50 additions & 0 deletions test/spec/modules/digitrustIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 5f74875

Please sign in to comment.