|
| 1 | +import {expect} from 'chai'; |
| 2 | +import {spec} from 'modules/orbidderBidAdapter'; |
| 3 | +import {newBidder} from 'src/adapters/bidderFactory'; |
| 4 | +import * as ajax from 'src/ajax'; |
| 5 | + |
| 6 | +describe('orbidderBidAdapter', () => { |
| 7 | + const adapter = newBidder(spec); |
| 8 | + const bidRequest = { |
| 9 | + bidId: 'd66fa86787e0b0ca900a96eacfd5f0bb', |
| 10 | + auctionId: 'ccc4c7cdfe11cfbd74065e6dd28413d8', |
| 11 | + transactionId: 'd58851660c0c4461e4aa06344fc9c0c6', |
| 12 | + adUnitCode: 'adunit-code', |
| 13 | + sizes: [[300, 250], [300, 600]], |
| 14 | + params: { |
| 15 | + 'foo': 'bar' |
| 16 | + } |
| 17 | + }; |
| 18 | + |
| 19 | + const buildRequest = function (buildRequest) { |
| 20 | + return spec.buildRequests( |
| 21 | + [buildRequest], |
| 22 | + { |
| 23 | + refererInfo: { |
| 24 | + referer: 'http://localhost:9876/' |
| 25 | + } |
| 26 | + })[0]; |
| 27 | + } |
| 28 | + |
| 29 | + describe('inherited functions', () => { |
| 30 | + it('exists and is a function', () => { |
| 31 | + expect(adapter.callBids).to.exist.and.to.be.a('function'); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('isBidRequestValid', () => { |
| 36 | + it('should return true when required params found', () => { |
| 37 | + expect(spec.isBidRequestValid(bidRequest)).to.equal(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return false when required params are not passed', () => { |
| 41 | + let bidRequest = Object.assign({}, bidRequest); |
| 42 | + delete bidRequest.params; |
| 43 | + expect(spec.isBidRequestValid(bidRequest)).to.equal(false); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('buildRequests', () => { |
| 48 | + const request = buildRequest(bidRequest); |
| 49 | + |
| 50 | + it('sends bid request to endpoint via https using post', () => { |
| 51 | + expect(request.method).to.equal('POST'); |
| 52 | + expect(request.url.indexOf('https://')).to.equal(0); |
| 53 | + expect(request.url).to.equal(`${spec.orbidderHost}/bid`); |
| 54 | + }); |
| 55 | + |
| 56 | + it('sends correct bid parameters', () => { |
| 57 | + // we add one, because we add referer information from bidderRequest object |
| 58 | + expect(Object.keys(request.data).length).to.equal(Object.keys(bidRequest).length + 1); |
| 59 | + expect(request.data.pageUrl).to.equal('http://localhost:9876/'); |
| 60 | + // expect(request.data.referrer).to.equal(''); |
| 61 | + Object.keys(bidRequest).forEach((key) => { |
| 62 | + expect(bidRequest[key]).to.equal(request.data[key]); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('handles empty gdpr object', () => { |
| 67 | + let bidRequest = Object.assign({}, bidRequest); |
| 68 | + bidRequest.gdprConsent = {}; |
| 69 | + |
| 70 | + const request = buildRequest(bidRequest); |
| 71 | + expect(request.data.gdprConsent.consentRequired).to.be.equal(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('handles non-existent gdpr object', () => { |
| 75 | + let bidRequest = Object.assign({}, bidRequest); |
| 76 | + bidRequest.gdprConsent = null; |
| 77 | + |
| 78 | + const request = buildRequest(bidRequest); |
| 79 | + expect(request.data.gdprConsent).to.be.undefined; |
| 80 | + }); |
| 81 | + |
| 82 | + it('handles properly filled gdpr object where gdpr applies', () => { |
| 83 | + const consentString = 'someWeirdString'; |
| 84 | + const bidRequest = Object.assign({}, bidRequest); |
| 85 | + bidRequest.gdprConsent = { |
| 86 | + gdprApplies: true, |
| 87 | + consentString: 'someWeirdString' |
| 88 | + }; |
| 89 | + |
| 90 | + const request = buildRequest(bidRequest); |
| 91 | + const gdprConsent = request.data.gdprConsent; |
| 92 | + expect(gdprConsent.consentRequired).to.be.equal(true); |
| 93 | + expect(gdprConsent.consentString).to.be.equal(consentString); |
| 94 | + }); |
| 95 | + |
| 96 | + it('handles properly filled gdpr object where gdpr does not apply', () => { |
| 97 | + const consentString = 'someWeirdString'; |
| 98 | + const bidRequest = Object.assign({}, bidRequest); |
| 99 | + bidRequest.gdprConsent = { |
| 100 | + gdprApplies: false, |
| 101 | + consentString: 'someWeirdString' |
| 102 | + }; |
| 103 | + |
| 104 | + const request = buildRequest(bidRequest); |
| 105 | + const gdprConsent = request.data.gdprConsent; |
| 106 | + expect(gdprConsent.consentRequired).to.be.equal(false); |
| 107 | + expect(gdprConsent.consentString).to.be.equal(consentString); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('onBidWon', () => { |
| 112 | + let ajaxStub; |
| 113 | + const winObj = { |
| 114 | + test: 1, |
| 115 | + pageUrl: 'www.someurl.de', |
| 116 | + referrer: 'www.somereferrer.de' |
| 117 | + }; |
| 118 | + |
| 119 | + beforeEach(() => { |
| 120 | + ajaxStub = sinon.stub(ajax, 'ajax'); |
| 121 | + }); |
| 122 | + |
| 123 | + afterEach(() => { |
| 124 | + ajaxStub.restore(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('calls orbidder\'s win endpoint', () => { |
| 128 | + spec.onBidWon(winObj); |
| 129 | + expect(ajaxStub.calledOnce).to.equal(true); |
| 130 | + expect(ajaxStub.firstCall.args[0].indexOf('https://')).to.equal(0); |
| 131 | + expect(ajaxStub.firstCall.args[0]).to.equal(`${spec.orbidderHost}/win`); |
| 132 | + expect(ajaxStub.firstCall.args[2]).to.equal(JSON.stringify(winObj)); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('interpretResponse', () => { |
| 137 | + it('should get correct bid response', () => { |
| 138 | + const serverResponse = [ |
| 139 | + { |
| 140 | + 'width': 300, |
| 141 | + 'height': 250, |
| 142 | + 'creativeId': '29681110', |
| 143 | + 'ad': '<!-- Creative -->', |
| 144 | + 'cpm': 0.5, |
| 145 | + 'requestId': '30b31c1838de1e', |
| 146 | + 'ttl': 60, |
| 147 | + 'netRevenue': true, |
| 148 | + 'currency': 'EUR' |
| 149 | + } |
| 150 | + ]; |
| 151 | + |
| 152 | + const expectedResponse = [ |
| 153 | + { |
| 154 | + 'requestId': '30b31c1838de1e', |
| 155 | + 'cpm': 0.5, |
| 156 | + 'creativeId': '29681110', |
| 157 | + 'width': 300, |
| 158 | + 'height': 250, |
| 159 | + 'ttl': 60, |
| 160 | + 'currency': 'EUR', |
| 161 | + 'ad': '<!-- Creative -->', |
| 162 | + 'netRevenue': true |
| 163 | + } |
| 164 | + ]; |
| 165 | + |
| 166 | + const result = spec.interpretResponse({body: serverResponse}); |
| 167 | + |
| 168 | + expect(result.length).to.equal(expectedResponse.length); |
| 169 | + Object.keys(expectedResponse[0]).forEach((key) => { |
| 170 | + expect(result[0][key]).to.equal(expectedResponse[0][key]); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('handles broken server response', () => { |
| 175 | + const serverResponse = [ |
| 176 | + { |
| 177 | + 'ad': '<!-- Creative -->', |
| 178 | + 'cpm': 0.5, |
| 179 | + 'requestId': '30b31c1838de1e', |
| 180 | + 'ttl': 60 |
| 181 | + } |
| 182 | + ]; |
| 183 | + const result = spec.interpretResponse({body: serverResponse}); |
| 184 | + |
| 185 | + expect(result.length).to.equal(0); |
| 186 | + }); |
| 187 | + |
| 188 | + it('handles nobid responses', () => { |
| 189 | + const serverResponse = []; |
| 190 | + const result = spec.interpretResponse({body: serverResponse}); |
| 191 | + |
| 192 | + expect(result.length).to.equal(0); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments