|
1 | 1 | import { expect } from 'chai'; |
2 | | -import { spec } from 'modules/beopBidAdapter.js'; |
| 2 | +import { spec, __storage } from 'modules/beopBidAdapter.js'; |
3 | 3 | import { newBidder } from 'src/adapters/bidderFactory.js'; |
4 | 4 | import { config } from 'src/config.js'; |
5 | 5 | import { setConfig as setCurrencyConfig } from '../../../modules/currency.js'; |
@@ -380,15 +380,114 @@ describe('BeOp Bid Adapter tests', () => { |
380 | 380 |
|
381 | 381 | describe('Ensure first party cookie is well managed', function () { |
382 | 382 | const bidRequests = []; |
| 383 | + let sandbox; |
383 | 384 |
|
384 | | - it(`should generate a new uuid`, function () { |
| 385 | + beforeEach(function () { |
| 386 | + sandbox = sinon.createSandbox(); |
| 387 | + }); |
| 388 | + |
| 389 | + afterEach(function () { |
| 390 | + sandbox.restore(); |
| 391 | + }); |
| 392 | + |
| 393 | + it('should set fg to a 24-char hex ObjectID (caudid) when no cookie present', function () { |
| 394 | + sandbox.stub(__storage, 'cookiesAreEnabled').returns(true); |
| 395 | + sandbox.stub(__storage, 'getCookie').returns(undefined); |
385 | 396 | const bid = Object.assign({}, validBid); |
386 | | - bidRequests.push(bid); |
387 | | - const request = spec.buildRequests(bidRequests, {}); |
| 397 | + const request = spec.buildRequests([bid], {}); |
388 | 398 | const payload = JSON.parse(request.data); |
389 | 399 | expect(payload.fg).to.exist; |
390 | | - }) |
391 | | - }) |
| 400 | + expect(payload.fg).to.match(/^[0-9a-f]{24}$/); |
| 401 | + }); |
| 402 | + |
| 403 | + it('should set both caudid and caudid_date cookies when generating a new id', function () { |
| 404 | + sandbox.stub(__storage, 'cookiesAreEnabled').returns(true); |
| 405 | + sandbox.stub(__storage, 'getCookie').returns(undefined); |
| 406 | + const setCookieSpy = sandbox.stub(__storage, 'setCookie'); |
| 407 | + |
| 408 | + const bid = Object.assign({}, validBid); |
| 409 | + const request = spec.buildRequests([bid], {}); |
| 410 | + const payload = JSON.parse(request.data); |
| 411 | + |
| 412 | + expect(setCookieSpy.calledTwice).to.be.true; |
| 413 | + expect(setCookieSpy.firstCall.args[0]).to.equal('caudid'); |
| 414 | + expect(setCookieSpy.firstCall.args[1]).to.match(/^[0-9a-f]{24}$/); |
| 415 | + expect(setCookieSpy.secondCall.args[0]).to.equal('caudid_date'); |
| 416 | + expect(setCookieSpy.secondCall.args[1]).to.match(/^\d+$/); |
| 417 | + expect(Number(setCookieSpy.secondCall.args[1])).to.be.closeTo(Date.now(), 5000); |
| 418 | + expect(payload.fg).to.equal(setCookieSpy.firstCall.args[1]); |
| 419 | + }); |
| 420 | + |
| 421 | + it('should always produce 24-char caudid when clock is in the past (timestamp padding)', function () { |
| 422 | + sandbox.stub(__storage, 'cookiesAreEnabled').returns(true); |
| 423 | + sandbox.stub(__storage, 'getCookie').returns(undefined); |
| 424 | + const setCookieSpy = sandbox.stub(__storage, 'setCookie'); |
| 425 | + // Unix epoch 0 → hex "0"; without padding that would yield 1 + 16 = 17 chars and fail validIdRegExp |
| 426 | + sandbox.stub(Date, 'now').returns(0); |
| 427 | + |
| 428 | + const bid = Object.assign({}, validBid); |
| 429 | + const request = spec.buildRequests([bid], {}); |
| 430 | + const payload = JSON.parse(request.data); |
| 431 | + |
| 432 | + const caudid = setCookieSpy.firstCall.args[1]; |
| 433 | + expect(caudid).to.have.lengthOf(24); |
| 434 | + expect(caudid).to.match(/^[0-9a-f]{24}$/); |
| 435 | + expect(caudid.substring(0, 8)).to.equal('00000000'); |
| 436 | + expect(payload.fg).to.equal(caudid); |
| 437 | + }); |
| 438 | + |
| 439 | + it('should not set cookies when a valid caudid cookie already exists', function () { |
| 440 | + const existingCaudid = '674a1b2c3d4e5f6789abcdef'; |
| 441 | + sandbox.stub(__storage, 'cookiesAreEnabled').returns(true); |
| 442 | + sandbox.stub(__storage, 'getCookie').callsFake((name) => |
| 443 | + name === 'caudid' ? existingCaudid : undefined |
| 444 | + ); |
| 445 | + const setCookieSpy = sandbox.stub(__storage, 'setCookie'); |
| 446 | + |
| 447 | + const bid = Object.assign({}, validBid); |
| 448 | + const request = spec.buildRequests([bid], {}); |
| 449 | + const payload = JSON.parse(request.data); |
| 450 | + |
| 451 | + expect(setCookieSpy.called).to.be.false; |
| 452 | + expect(payload.fg).to.equal(existingCaudid); |
| 453 | + }); |
| 454 | + |
| 455 | + it('should regenerate caudid and set both cookies when existing caudid is invalid format', function () { |
| 456 | + sandbox.stub(__storage, 'cookiesAreEnabled').returns(true); |
| 457 | + sandbox.stub(__storage, 'getCookie').callsFake((name) => |
| 458 | + name === 'caudid' ? 'invalid-uuid-format' : undefined |
| 459 | + ); |
| 460 | + const setCookieSpy = sandbox.stub(__storage, 'setCookie'); |
| 461 | + |
| 462 | + const bid = Object.assign({}, validBid); |
| 463 | + const request = spec.buildRequests([bid], {}); |
| 464 | + const payload = JSON.parse(request.data); |
| 465 | + |
| 466 | + expect(setCookieSpy.calledTwice).to.be.true; |
| 467 | + expect(setCookieSpy.firstCall.args[0]).to.equal('caudid'); |
| 468 | + expect(setCookieSpy.firstCall.args[1]).to.match(/^[0-9a-f]{24}$/); |
| 469 | + expect(setCookieSpy.secondCall.args[0]).to.equal('caudid_date'); |
| 470 | + expect(payload.fg).to.equal(setCookieSpy.firstCall.args[1]); |
| 471 | + }); |
| 472 | + |
| 473 | + it('should clear both caudid and caudid_date when cookies are disabled', function () { |
| 474 | + sandbox.stub(__storage, 'cookiesAreEnabled').returns(false); |
| 475 | + const setCookieSpy = sandbox.stub(__storage, 'setCookie'); |
| 476 | + |
| 477 | + const bid = Object.assign({}, validBid); |
| 478 | + const request = spec.buildRequests([bid], {}); |
| 479 | + const payload = JSON.parse(request.data); |
| 480 | + |
| 481 | + expect(setCookieSpy.calledTwice).to.be.true; |
| 482 | + expect(setCookieSpy.firstCall.args[0]).to.equal('caudid'); |
| 483 | + expect(setCookieSpy.firstCall.args[1]).to.equal(''); |
| 484 | + expect(setCookieSpy.firstCall.args[2]).to.equal(0); |
| 485 | + expect(setCookieSpy.secondCall.args[0]).to.equal('caudid_date'); |
| 486 | + expect(setCookieSpy.secondCall.args[1]).to.equal(''); |
| 487 | + expect(setCookieSpy.secondCall.args[2]).to.equal(0); |
| 488 | + expect(payload.fg).to.equal(''); |
| 489 | + }); |
| 490 | + }); |
392 | 491 | describe('slot name normalization', function () { |
393 | 492 | it('should preserve non-GPT adUnitCode unchanged (case-sensitive)', function () { |
394 | 493 | const bid = Object.assign({}, validBid); |
|
0 commit comments