forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadriverIdSystem_spec.js
More file actions
87 lines (74 loc) · 3.27 KB
/
adriverIdSystem_spec.js
File metadata and controls
87 lines (74 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { adriverIdSubmodule, storage } from 'modules/adriverIdSystem.js';
import { server } from 'test/mocks/xhr.js';
import * as utils from '../../../src/utils.js';
describe('AdriverIdSystem', function () {
describe('getId', function() {
let setCookieStub, setLocalStorageStub, removeFromLocalStorageStub, logErrorStub;
beforeEach(function() {
setCookieStub = sinon.stub(storage, 'setCookie');
setLocalStorageStub = sinon.stub(storage, 'setDataInLocalStorage');
removeFromLocalStorageStub = sinon.stub(storage, 'removeDataFromLocalStorage');
logErrorStub = sinon.stub(utils, 'logError');
});
afterEach(function () {
setCookieStub.restore();
setLocalStorageStub.restore();
removeFromLocalStorageStub.restore();
logErrorStub.restore();
});
it('should log an error and continue to callback if request errors', function () {
const config = {
params: {}
};
const callbackSpy = sinon.spy();
const callback = adriverIdSubmodule.getId(config).callback;
callback(callbackSpy);
const request = server.requests[0];
expect(request.url).to.include('https://ad.adriver.ru/cgi-bin/json.cgi');
request.respond(503, null, 'Unavailable');
expect(logErrorStub.calledOnce).to.be.true;
});
it('test call user sync url with the right params', function() {
const config = {
params: {}
};
const callbackSpy = sinon.spy();
const callback = adriverIdSubmodule.getId(config).callback;
callback(callbackSpy);
const request = server.requests[0];
expect(request.url).to.include('https://ad.adriver.ru/cgi-bin/json.cgi');
request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ adrcid: 'testAdriverId' }));
expect(callbackSpy.lastCall.lastArg).to.deep.equal('testAdriverId');
});
const responses = [
{ adrcid: 'adrcidValue' },
{ adrcid: undefined }
]
responses.forEach(response => describe('test user sync response behavior', function () {
const config = {
params: {}
};
it('should save adrcid if it exists', function () {
const result = adriverIdSubmodule.getId(config);
result.callback((id) => {
expect(id).to.be.deep.equal(response.adrcid ? response.adrcid : undefined);
});
const request = server.requests[0];
request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ adrcid: response.adrcid }));
const expectedExpiration = new Date();
expectedExpiration.setTime(expectedExpiration.getTime() + 86400 * 1825 * 1000);
const minimalDate = new Date(0).toString();
function dateStringFor(date, maxDeltaMs = 2000) {
return sinon.match((val) => Math.abs(date.getTime() - new Date(val).getTime()) <= maxDeltaMs)
}
if (response.adrcid) {
expect(setCookieStub.calledWith('adrcid', response.adrcid, dateStringFor(expectedExpiration))).to.be.true;
expect(setLocalStorageStub.calledWith('adrcid', response.adrcid)).to.be.true;
} else {
expect(setCookieStub.calledWith('adrcid', '', minimalDate)).to.be.false;
expect(removeFromLocalStorageStub.calledWith('adrcid')).to.be.false;
}
});
}));
});
});