Skip to content

Commit

Permalink
Merged in RVR-1883-Add-Basic-Access-Authentication (pull request preb…
Browse files Browse the repository at this point in the history
…id#17)

RVR-1883 Add Basic Access Authentication

* RVR-1914 - Rename functions

* RVR-1914 - Set default total_duration to null in bid response

* RVR-1883 - Use RIVR_CLIENT_AUTH_TOKEN global variable for Auth token

* RVR-1883 - Restore stub after every test not just at the end

* RVR-1883 - Remove commented code
  • Loading branch information
AlessandroDG committed Oct 19, 2018
1 parent d28b84d commit 6326a91
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 54 deletions.
60 changes: 33 additions & 27 deletions modules/rivrAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,49 @@ let rivrAnalytics = Object.assign(adapter({analyticsType}), {

export function sendAuction() {
console.log('Function called: ============= sendAuction');
removeEmptyProperties(rivrAnalytics.context.auctionObject)
let auctionObject = rivrAnalytics.context.auctionObject;
let req = Object.assign({}, {Auction: auctionObject});
rivrAnalytics.context.auctionObject = fulfillAuctionObject();
logInfo('sending request to analytics => ', req);
ajax(
`http://${rivrAnalytics.context.host}/${rivrAnalytics.context.clientID}/auctions`,
() => {},
JSON.stringify(req),
{
contentType: 'application/json',
customHeaders: {
'Authorization': 'Basic b3V0ZXJwYXNzaXZlOkQ3OVZ5YXI1eVZXUEVBaHI='
}
}
);
};

function sendImpressions() {
console.log('Function called: ============= sendImpressions');
let impressions = rivrAnalytics.context.queue.popAll();
if (impressions.length !== 0) {
let impressionsReq = Object.assign({}, {impressions});
logInfo('sending impressions request to analytics => ', impressionsReq);
console.log('Function called: ============= sendAuction rivrAnalytics.context.authToken', rivrAnalytics.context.authToken);
if (rivrAnalytics.context.authToken) {
removeEmptyProperties(rivrAnalytics.context.auctionObject)
let auctionObject = rivrAnalytics.context.auctionObject;
let req = Object.assign({}, {Auction: auctionObject});
rivrAnalytics.context.auctionObject = fulfillAuctionObject();
logInfo('sending request to analytics => ', req);
ajax(
`http://${rivrAnalytics.context.host}/impressions`,
`http://${rivrAnalytics.context.host}/${rivrAnalytics.context.clientID}/auctions`,
() => {},
JSON.stringify(impressionsReq),
JSON.stringify(req),
{
contentType: 'application/json',
customHeaders: {
'Authorization': 'Basic b3V0ZXJwYXNzaXZlOkQ3OVZ5YXI1eVZXUEVBaHI='
'Authorization': 'Basic ' + rivrAnalytics.context.authToken
}
}
);
}
};

function sendImpressions() {
console.log('Function called: ============= sendImpressions');
if (rivrAnalytics.context.authToken) {
let impressions = rivrAnalytics.context.queue.popAll();
if (impressions.length !== 0) {
let impressionsReq = Object.assign({}, {impressions});
logInfo('sending impressions request to analytics => ', impressionsReq);
ajax(
`http://${rivrAnalytics.context.host}/impressions`,
() => {},
JSON.stringify(impressionsReq),
{
contentType: 'application/json',
customHeaders: {
'Authorization': 'Basic ' + rivrAnalytics.context.authToken
}
}
);
}
}
};

function trackAuctionInit(args) {
rivrAnalytics.context.auctionTimeStart = Date.now();
rivrAnalytics.context.auctionObject.id = args.auctionId;
Expand Down Expand Up @@ -518,6 +523,7 @@ rivrAnalytics.enableAnalytics = (config) => {
auctionObject: {},
adUnits: copiedUnits,
clientID: config.options.clientID,
authToken: config.options.authToken,
queue: new ExpiringQueue(sendImpressions, sendAuction, config.options.queueTimeout || DEFAULT_QUEUE_TIMEOUT)
};
let bannersIds = config.options.bannersIds
Expand Down
77 changes: 50 additions & 27 deletions test/spec/modules/rivrAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,19 @@ describe('', () => {
let timer;

before(() => {
ajaxStub = sandbox.stub(ajax, 'ajax');
timer = sandbox.useFakeTimers(0);
});

beforeEach(() => {
ajaxStub = sandbox.stub(ajax, 'ajax');
sandbox.stub(events, 'getEvents').callsFake(() => {
return []
});
});

afterEach(() => {
events.getEvents.restore();
ajaxStub.restore();
});

it('should be configurable', () => {
Expand Down Expand Up @@ -315,43 +316,65 @@ describe('', () => {
});
});

it('sends request after timeout', () => {
let impressions = analyticsAdapter.context.auctionObject.imp;
let responses = analyticsAdapter.context.auctionObject.bidResponses;
let requests = analyticsAdapter.context.auctionObject.bidRequests;
describe('when authToken is defined', () => {
it('sends request after timeout', () => {
analyticsAdapter.context.authToken = 'anAuthToken';
let impressions = analyticsAdapter.context.auctionObject.imp;
let responses = analyticsAdapter.context.auctionObject.bidResponses;
let requests = analyticsAdapter.context.auctionObject.bidRequests;

expect(impressions.length).to.be.eql(1);
expect(responses.length).to.be.eql(3);
expect(requests.length).to.be.eql(3);
expect(ajaxStub.calledOnce).to.be.equal(false);
expect(impressions.length).to.be.eql(1);
expect(responses.length).to.be.eql(3);
expect(requests.length).to.be.eql(3);
expect(ajaxStub.notCalled).to.be.equal(true);

timer.tick(4500);
timer.tick(4500);

let impressionsAfterSend = analyticsAdapter.context.auctionObject.imp;
let responsesAfterSend = analyticsAdapter.context.auctionObject.bidResponses;
let requestsAfterSend = analyticsAdapter.context.auctionObject.bidRequests;
let impressionsAfterSend = analyticsAdapter.context.auctionObject.imp;
let responsesAfterSend = analyticsAdapter.context.auctionObject.bidResponses;
let requestsAfterSend = analyticsAdapter.context.auctionObject.bidRequests;

expect(ajaxStub.calledOnce).to.be.equal(true);
expect(impressionsAfterSend.length).to.be.eql(0);
expect(responsesAfterSend.length).to.be.eql(0);
expect(requestsAfterSend.length).to.be.eql(0);
expect(ajaxStub.calledOnce).to.be.equal(true);
expect(impressionsAfterSend.length).to.be.eql(0);
expect(responsesAfterSend.length).to.be.eql(0);
expect(requestsAfterSend.length).to.be.eql(0);

analyticsAdapter.context.authToken = undefined;
});
});

describe('sendAuction', () => {
it('clears empty payload properties', () => {
analyticsAdapter.context.auctionObject.nullProperty = null;
analyticsAdapter.context.auctionObject.notNullProperty = 'aValue';
describe('when authToken is defined', () => {
it('fires call clearing empty payload properties', () => {
analyticsAdapter.context.authToken = 'anAuthToken';
analyticsAdapter.context.auctionObject.nullProperty = null;
analyticsAdapter.context.auctionObject.notNullProperty = 'aValue';

sendAuction();

expect(ajaxStub.getCall(0).args[0]).to.match(/http:\/\/tracker.rivr.simplaex.com\/(\w+)\/auctions/);

const payload = JSON.parse(ajaxStub.getCall(0).args[2]);

expect(payload.Auction.notNullProperty).to.be.equal('aValue');
expect(payload.nullProperty).to.be.equal(undefined);

analyticsAdapter.context.authToken = undefined;
});
});

sendAuction();
describe('when authToken is not defined', () => {
it('does not fire call', () => {
analyticsAdapter.context.authToken = undefined;
analyticsAdapter.context.auctionObject.nullProperty = null;
analyticsAdapter.context.auctionObject.notNullProperty = 'aValue';

// sendAuction is called automatically. This is the reason why we are testing the second call here.
// Understand how to avoid it and isolate the test.
expect(ajaxStub.getCall(1).args[0]).to.match(/http:\/\/tracker.rivr.simplaex.com\/(\w+)\/auctions/);
expect(ajaxStub.callCount).to.be.equal(0);

const payload = JSON.parse(ajaxStub.getCall(1).args[2]);
sendAuction();

expect(payload.Auction.notNullProperty).to.be.equal('aValue');
expect(payload.nullProperty).to.be.equal(undefined);
expect(ajaxStub.callCount).to.be.equal(0);
});
});
});
});
Expand Down

0 comments on commit 6326a91

Please sign in to comment.