Skip to content

Commit

Permalink
add new global config params
Browse files Browse the repository at this point in the history
  • Loading branch information
kodi committed Mar 4, 2022
1 parent 2c3fcdb commit 5cd36d5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
17 changes: 15 additions & 2 deletions modules/cwireBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getRefererInfo} from '../src/refererDetection.js';
import {getStorageManager} from '../src/storageManager.js';
import {config} from '../src/config.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {OUTSTREAM} from '../src/video.js';
import {
Expand All @@ -13,6 +14,7 @@ import {
isNumber,
logError,
logWarn,
isEmpty,
parseSizesInput,
} from '../src/utils.js';
import {Renderer} from '../src/Renderer.js';
Expand Down Expand Up @@ -162,9 +164,19 @@ export const spec = {

let refgroups = [];

const cwCreativeId = getQueryVariable(CW_CREATIVE_QUERY);
const cwCreativeId = parseInt(getQueryVariable(CW_CREATIVE_QUERY), 10) || null;
const cwCreativeIdFromConfig = config.getConfig('cwcreative');
const refGroupsFromConfig = config.getConfig('refgroups');
const cwApiKeyFromConfig = config.getConfig('cwapikey');
const rgQuery = getQueryVariable(CW_GROUPS_QUERY);

if (!isEmpty(refGroupsFromConfig)) {
refgroups = refGroupsFromConfig.split(',');
}

if (rgQuery !== null) {
// override if query param is present
refgroups = [];
refgroups = rgQuery.split(',');
}

Expand All @@ -173,8 +185,9 @@ export const spec = {
const payload = {
cwid: localStorageCWID,
refgroups,
cwcreative: cwCreativeId,
cwcreative: cwCreativeId || cwCreativeIdFromConfig || null,
slots: slots,
cwapikey: cwApiKeyFromConfig || null,
httpRef: referer || '',
pageViewId: CW_PAGE_VIEW_ID,
};
Expand Down
18 changes: 16 additions & 2 deletions modules/cwireBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Module Name: C-WIRE Bid Adapter
Module Type: Adagio Adapter
Maintainer: dragan@cwire.ch
Maintainer: publishers@cwire.ch

## Description

Expand All @@ -17,8 +17,22 @@ Below, the list of C-WIRE params and where they can be set.
| ---------- | ------------- | ------------- | ---- | ---------|
| pageId | | x | number | YES |
| placementId | | x | number | YES |
| refgroups | x | | string | NO |
| cwcreative | x | | integer | NO |
| cwapikey | x | | string | NO |
| adUnitElementId | | x | string | NO |


### Global configuration
```javascript
pbjs.setConfig({
cwcreative: 42, // optional - id of creative to force
refgroups: 'test-user', // optional - name of group or coma separated list of groups to force
cwapikey: 'b08d7fd1-d088-4e17-af29-24decff7582c', // optional - api key for integration testing
});
````


### adUnit configuration

```javascript
Expand All @@ -40,4 +54,4 @@ var adUnits = [
}]
}
];
```
```
68 changes: 64 additions & 4 deletions test/spec/modules/cwireBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as utils from '../../../src/utils.js';
import { config } from '../../../src/config.js';
import {
spec,
CW_PAGE_VIEW_ID,
Expand Down Expand Up @@ -85,6 +86,7 @@ describe('C-WIRE bid adapter', () => {

afterEach(() => {
sandbox.restore();
config.resetConfig();
});

// START TESTING
Expand Down Expand Up @@ -138,11 +140,39 @@ describe('C-WIRE bid adapter', () => {

describe('C-WIRE - buildRequests()', function () {
it('creates a valid request', function () {
const bid01 = new BidRequestBuilder({
mediaTypes: {
banner: {
sizes: [[1, 1]],
}
}
}).withParams().build();

config.setConfig({
cwcreative: 54321,
cwapikey: 'xxx-xxx-yyy-zzz-uuid',
refgroups: 'group_1',
});

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01], bidderRequest01);

expect(requests.data.slots.length).to.equal(1);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.slots[0].sizes[0]).to.equal('1x1');
expect(requests.data.cwcreative).to.equal(54321);
expect(requests.data.cwapikey).to.equal('xxx-xxx-yyy-zzz-uuid');
expect(requests.data.refgroups[0]).to.equal('group_1');
});

it('creates a valid request - config is overriden by URL params', function () {
// for whatever reason stub for getWindowLocation does not work
// so this was the closest way to test for get params
const params = sandbox.stub(utils, 'getParameterByName');
params.withArgs('cwgroups').returns('group_1');
params.withArgs('cwcreative').returns('54321');
params.withArgs('cwgroups').returns('group_2');
params.withArgs('cwcreative').returns('654321');

const bid01 = new BidRequestBuilder({
mediaTypes: {
Expand All @@ -152,15 +182,45 @@ describe('C-WIRE bid adapter', () => {
}
}).withParams().build();

config.setConfig({
cwcreative: 54321,
cwapikey: 'xxx-xxx-yyy-zzz',
refgroups: 'group_1',
});

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01], bidderRequest01);

expect(requests.data.slots.length).to.equal(1);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.slots[0].sizes[0]).to.equal('1x1');
expect(requests.data.cwcreative).to.equal('54321');
expect(requests.data.refgroups[0]).to.equal('group_1');
expect(requests.data.cwcreative).to.equal(654321);
expect(requests.data.cwapikey).to.equal('xxx-xxx-yyy-zzz');
expect(requests.data.refgroups[0]).to.equal('group_2');
});

it('creates a valid request - if config not set null or empty array is sent', function () {
const bid01 = new BidRequestBuilder({
mediaTypes: {
banner: {
sizes: [[1, 1]],
}
}
}).withParams().build();

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01], bidderRequest01);

expect(requests.data.slots.length).to.equal(1);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.slots[0].sizes[0]).to.equal('1x1');
expect(requests.data.cwcreative).to.equal(null);
expect(requests.data.cwapikey).to.equal(null);
expect(requests.data.refgroups.length).to.equal(0);
});
});

Expand Down

0 comments on commit 5cd36d5

Please sign in to comment.