Skip to content

Commit

Permalink
Identity link id system - handle empty response (#5279)
Browse files Browse the repository at this point in the history
* IdentityLinkIdSystem - handle empty response

* IdentityLinkIdSystem - add tests

* IdentityLinkIdSystem - rename describe in tests
  • Loading branch information
mamatic authored May 25, 2020
1 parent 2691784 commit 52b8944
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/identityLinkIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function getEnvelope(url, callback) {
utils.logError(error);
}
}
callback(responseObj.envelope);
callback((responseObj && responseObj.envelope) ? responseObj.envelope : '');
},
error: error => {
utils.logError(`identityLink: ID fetch encountered an error`, error);
Expand Down
92 changes: 92 additions & 0 deletions test/spec/modules/identityLinkIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {identityLinkSubmodule} from 'modules/identityLinkIdSystem.js';
import * as utils from 'src/utils.js';
import {server} from 'test/mocks/xhr.js';

const pid = '14';
const defaultConfigParams = {pid: pid};
const responseHeader = {'Content-Type': 'application/json'}

describe('IdentityLinkId tests', function () {
let logErrorStub;

beforeEach(function () {
logErrorStub = sinon.stub(utils, 'logError');
});

afterEach(function () {
logErrorStub.restore();
});

it('should log an error if no configParams were passed when getId', function () {
identityLinkSubmodule.getId();
expect(logErrorStub.calledOnce).to.be.true;
});

it('should log an error if pid configParam was not passed when getId', function () {
identityLinkSubmodule.getId({});
expect(logErrorStub.calledOnce).to.be.true;
});

it('should call the LiveRamp envelope endpoint', function () {
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request.url).to.be.eq('https://api.rlcdn.com/api/identity/envelope?pid=14');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should call the LiveRamp envelope endpoint with consent string', function () {
let callBackSpy = sinon.spy();
let consentData = {
gdprApplies: true,
consentString: 'BOkIpDSOkIpDSADABAENCc-AAAApOAFAAMAAsAMIAcAA_g'
};
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams, consentData).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request.url).to.be.eq('https://api.rlcdn.com/api/identity/envelope?pid=14&ct=1&cv=BOkIpDSOkIpDSADABAENCc-AAAApOAFAAMAAsAMIAcAA_g');
request.respond(
200,
responseHeader,
JSON.stringify({})
);
expect(callBackSpy.calledOnce).to.be.true;
});

it('should not throw Uncaught TypeError when envelope endpoint returns empty response', function () {
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request.url).to.be.eq('https://api.rlcdn.com/api/identity/envelope?pid=14');
request.respond(
204,
responseHeader,
''
);
expect(callBackSpy.calledOnce).to.be.true;
expect(request.response).to.equal('');
expect(logErrorStub.calledOnce).to.not.be.true;
});

it('should log an error and continue to callback if ajax request errors', function () {
let callBackSpy = sinon.spy();
let submoduleCallback = identityLinkSubmodule.getId(defaultConfigParams).callback;
submoduleCallback(callBackSpy);
let request = server.requests[0];
expect(request.url).to.be.eq('https://api.rlcdn.com/api/identity/envelope?pid=14');
request.respond(
503,
responseHeader,
'Unavailable'
);
expect(logErrorStub.calledOnce).to.be.true;
expect(callBackSpy.calledOnce).to.be.true;
});
});

0 comments on commit 52b8944

Please sign in to comment.