Skip to content

Commit

Permalink
Adding UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pubmatic-Dhruv-Sonone committed Aug 30, 2024
1 parent 65c9114 commit 6f3e323
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 11 deletions.
9 changes: 4 additions & 5 deletions exchange/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ func (ev *eventTracking) modifyBidVAST(pbsBid *entities.PbsOrtbBid, bidderName o
if pbsBid.BidType != openrtb_ext.BidTypeVideo || len(bid.AdM) == 0 && len(bid.NURL) == 0 {
return
}
vastXML := makeVAST(bid)

ev.InjectTrackers(pbsBid, bidderName)
bidID := bid.ID
if len(pbsBid.GeneratedBidID) > 0 {
bidID = pbsBid.GeneratedBidID
}
if newVastXML, ok := events.ModifyVastXmlString(ev.externalURL, vastXML, bidID, bidderName.String(), ev.accountID, ev.auctionTimestampMs, ev.integrationType); ok {
if newVastXML, ok := events.ModifyVastXmlString(ev.externalURL, pbsBid.Bid.AdM, bidID, bidderName.String(), ev.accountID, ev.auctionTimestampMs, ev.integrationType); ok {
bid.AdM = newVastXML
}

ev.InjectTrackers(pbsBid, bidderName)
}

// modifyBidJSON injects "wurl" (win) event url if needed, otherwise returns original json
Expand Down Expand Up @@ -163,7 +162,7 @@ func convertToVastEvent(events config.Events) injector.VASTEvents {

for _, event := range events.VASTEvents {
switch event.CreateElement {
// TODO: Prebid currently uses config.ExternalURL for impression tracking, decprecated in favor of config.Event
// TODO: Prebid currently uses config.ExternalURL for impression tracking, deprecated in favor of config.Event
// case config.ImpressionVASTElement:
// ve.Impressions = appendURLs(ve.Impressions, event, events.DefaultURL)
case config.ErrorVASTElement:
Expand Down
296 changes: 296 additions & 0 deletions exchange/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"testing"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/exchange/entities"
"github.com/prebid/prebid-server/v2/injector"
"github.com/prebid/prebid-server/v2/macros"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -192,3 +195,296 @@ func Test_isEventAllowed(t *testing.T) {
})
}
}

func TestConvertToVastEvent(t *testing.T) {
tests := []struct {
name string
input config.Events
expected injector.VASTEvents
}{
{
name: "Error event",
input: config.Events{
DefaultURL: "http://default.url",
VASTEvents: []config.VASTEvent{
{
CreateElement: config.ErrorVASTElement,
URLs: []string{"http://error.url"},
ExcludeDefaultURL: true,
},
},
},
expected: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
Errors: []string{"http://error.url"},
},
},
{
name: "NonLinearTracking event",
input: config.Events{
DefaultURL: "http://default.url",
VASTEvents: []config.VASTEvent{
{
CreateElement: config.NonLinearClickTrackingVASTElement,
URLs: []string{"http://tracking.url"},
ExcludeDefaultURL: true,
},
},
},
expected: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
NonLinearClickTracking: []string{"http://tracking.url"},
},
},
{
name: "CompanionClickThrough event",
input: config.Events{
DefaultURL: "http://default.url",
VASTEvents: []config.VASTEvent{
{
CreateElement: config.CompanionClickThroughVASTElement,
URLs: []string{"http://tracking.url"},
ExcludeDefaultURL: false,
},
},
},
expected: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
CompanionClickThrough: []string{"http://tracking.url", "http://default.url"},
},
},
{
name: "Tracking event",
input: config.Events{
DefaultURL: "http://default.url",
VASTEvents: []config.VASTEvent{
{
CreateElement: config.TrackingVASTElement,
Type: "start",
URLs: []string{"http://tracking.url"},
},
},
},
expected: injector.VASTEvents{
TrackingEvents: map[string][]string{
"start": {"http://tracking.url", "http://default.url"},
},
},
},
{
name: "Click tracking event",
input: config.Events{
DefaultURL: "http://default.url",
VASTEvents: []config.VASTEvent{
{
CreateElement: config.ClickTrackingVASTElement,
URLs: []string{"http://clicktracking.url"},
},
},
},
expected: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
VideoClicks: []string{"http://clicktracking.url", "http://default.url"},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToVastEvent(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}

func TestAppendURLs(t *testing.T) {
tests := []struct {
name string
urls []string
event config.VASTEvent
defaultURL string
expectedURLs []string
}{
{
name: "No URLs in event, include default URL",
urls: []string{},
event: config.VASTEvent{
URLs: []string{},
ExcludeDefaultURL: false,
},
defaultURL: "http://default.url",
expectedURLs: []string{"http://default.url"},
},
{
name: "No URLs in event, exclude default URL",
urls: []string{},
event: config.VASTEvent{
URLs: []string{},
ExcludeDefaultURL: true,
},
defaultURL: "http://default.url",
expectedURLs: []string{},
},
{
name: "URLs in event, include default URL",
urls: []string{},
event: config.VASTEvent{
URLs: []string{"http://event.url"},
ExcludeDefaultURL: false,
},
defaultURL: "http://default.url",
expectedURLs: []string{"http://event.url", "http://default.url"},
},
{
name: "URLs in event, exclude default URL",
urls: []string{},
event: config.VASTEvent{
URLs: []string{"http://event.url"},
ExcludeDefaultURL: true,
},
defaultURL: "http://default.url",
expectedURLs: []string{"http://event.url"},
},
{
name: "Existing URLs, URLs in event, include default URL",
urls: []string{"http://existing.url"},
event: config.VASTEvent{
URLs: []string{"http://event.url"},
ExcludeDefaultURL: false,
},
defaultURL: "http://default.url",
expectedURLs: []string{"http://existing.url", "http://event.url", "http://default.url"},
},
{
name: "Existing URLs, URLs in event, exclude default URL",
urls: []string{"http://existing.url"},
event: config.VASTEvent{
URLs: []string{"http://event.url"},
ExcludeDefaultURL: true,
},
defaultURL: "http://default.url",
expectedURLs: []string{"http://existing.url", "http://event.url"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := appendURLs(tt.urls, tt.event, tt.defaultURL)
assert.Equal(t, tt.expectedURLs, result)
})
}
}

func TestModifyBidVAST(t *testing.T) {
tests := []struct {
name string
ev *eventTracking
pbsBid *entities.PbsOrtbBid
bidderName openrtb_ext.BidderName
expectedPbsBid *entities.PbsOrtbBid
}{
{
name: "Non-video bid type",
ev: &eventTracking{
externalURL: "http://example.com",
accountID: "account1",
auctionTimestampMs: 1234567890,
integrationType: "integration1",
macroProvider: macros.NewProvider(&openrtb_ext.RequestWrapper{
BidRequest: &openrtb2.BidRequest{},
}),
events: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
CompanionClickThrough: []string{"http://tracking.url", "http://default.url"},
},
},
pbsBid: &entities.PbsOrtbBid{
BidType: openrtb_ext.BidTypeBanner,
Bid: &openrtb2.Bid{
ID: "bid1",
AdM: "<Ad></Ad>",
},
},
bidderName: "bidder1",
expectedPbsBid: &entities.PbsOrtbBid{
BidType: openrtb_ext.BidTypeBanner,
Bid: &openrtb2.Bid{
ID: "bid1",
AdM: "<Ad></Ad>",
},
},
},
{
name: "Video bid type with No AdM",
ev: &eventTracking{
externalURL: "http://example.com",
accountID: "account1",
auctionTimestampMs: 1234567890,
integrationType: "integration1",
macroProvider: macros.NewProvider(&openrtb_ext.RequestWrapper{
BidRequest: &openrtb2.BidRequest{},
}),
events: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
CompanionClickThrough: []string{"http://tracking.url", "http://default.url"},
},
},
pbsBid: &entities.PbsOrtbBid{
BidType: openrtb_ext.BidTypeVideo,
Bid: &openrtb2.Bid{
ID: "bid2",
NURL: "http://nurl.com",
AdM: "",
},
},
bidderName: "bidder2",
expectedPbsBid: &entities.PbsOrtbBid{
BidType: openrtb_ext.BidTypeVideo,
Bid: &openrtb2.Bid{
ID: "bid2",
NURL: "http://nurl.com",
AdM: "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[http://nurl.com]]></VASTAdTagURI><Impression><![CDATA[http://example.com/event?t=imp&b=bid2&a=account1&bidder=bidder2&f=b&int=integration1&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
},
},
},
{
name: "Video bid type with AdM",
ev: &eventTracking{
externalURL: "http://example.com",
accountID: "account1",
auctionTimestampMs: 1234567890,
integrationType: "integration1",
macroProvider: macros.NewProvider(&openrtb_ext.RequestWrapper{
BidRequest: &openrtb2.BidRequest{},
}),
events: injector.VASTEvents{
TrackingEvents: make(map[string][]string),
CompanionClickThrough: []string{"http://tracking.url", "http://default.url"},
},
},
pbsBid: &entities.PbsOrtbBid{
BidType: openrtb_ext.BidTypeVideo,
Bid: &openrtb2.Bid{
ID: "bid2",
NURL: "http://nurl.com",
AdM: `<VAST version="4.0" xmlns="http://www.iab.com/VAST"><Ad id="20011" sequence="1" conditionalAd="false"><Wrapper followAdditionalWrappers="0" allowMultipleAds="1" fallbackOnNoAd="0"><AdSystem version="4.0">iabtechlab</AdSystem><Error>http://example.com/error</Error><Impression id="Impression-ID">http://example.com/track/impression</Impression><Creatives><Creative id="5480" sequence="1" adId="2447226"><CompanionAds><Companion id="1232" width="100" height="150" assetWidth="250" assetHeight="200" expandedWidth="350" expandedHeight="250" apiFramework="VPAID" adSlotID="3214" pxratio="1400"><StaticResource creativeType="image/png"><![CDATA[https://www.iab.com/wp-content/uploads/2014/09/iab-tech-lab-6-644x290.png]]></StaticResource><CompanionClickThrough><![CDATA[https://iabtechlab.com]]></CompanionClickThrough></Companion></CompanionAds></Creative></Creatives><VASTAdTagURI><![CDATA[https://raw.githubusercontent.com/InteractiveAdvertisingBureau/VAST_Samples/master/VAST%204.0%20Samples/Inline_Companion_Tag-test.xml]]></VASTAdTagURI></Wrapper></Ad></VAST>`,
},
},
bidderName: "bidder2",
expectedPbsBid: &entities.PbsOrtbBid{
BidType: openrtb_ext.BidTypeVideo,
Bid: &openrtb2.Bid{
ID: "bid2",
NURL: "http://nurl.com",
AdM: "<VAST version=\"4.0\" xmlns=\"http://www.iab.com/VAST\"><Ad id=\"20011\" sequence=\"1\" conditionalAd=\"false\"><Wrapper followAdditionalWrappers=\"0\" allowMultipleAds=\"1\" fallbackOnNoAd=\"0\"><AdSystem version=\"4.0\"><![CDATA[iabtechlab]]></AdSystem><Error><![CDATA[http://example.com/error]]></Error><Impression id=\"Impression-ID\"><![CDATA[http://example.com/track/impression]]></Impression><Impression><![CDATA[http://example.com/event?t=imp&b=bid2&a=account1&bidder=bidder2&f=b&int=integration1&ts=1234567890]]></Impression><Creatives><Creative id=\"5480\" sequence=\"1\" adId=\"2447226\"><CompanionAds><Companion id=\"1232\" width=\"100\" height=\"150\" assetWidth=\"250\" assetHeight=\"200\" expandedWidth=\"350\" expandedHeight=\"250\" apiFramework=\"VPAID\" adSlotID=\"3214\" pxratio=\"1400\"><StaticResource creativeType=\"image/png\"><![CDATA[https://www.iab.com/wp-content/uploads/2014/09/iab-tech-lab-6-644x290.png]]></StaticResource><CompanionClickThrough><![CDATA[https://iabtechlab.com]]></CompanionClickThrough><CompanionClickThrough><![CDATA[http://tracking.url]]></CompanionClickThrough><CompanionClickThrough><![CDATA[http://default.url]]></CompanionClickThrough></Companion></CompanionAds></Creative></Creatives><VASTAdTagURI><![CDATA[https://raw.githubusercontent.com/InteractiveAdvertisingBureau/VAST_Samples/master/VAST%204.0%20Samples/Inline_Companion_Tag-test.xml]]></VASTAdTagURI></Wrapper></Ad></VAST>",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.ev.modifyBidVAST(tt.pbsBid, tt.bidderName)
assert.Equal(t, tt.expectedPbsBid, tt.pbsBid)
})
}
}
4 changes: 2 additions & 2 deletions exchange/exchangetest/events-vast-account-off-request-on.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"bid": [
{
"id": "winning-bid",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem><![CDATA[prebid.org wrapper]]></AdSystem><VASTAdTagURI><![CDATA[http://domain.com/winning-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=bid-appnexus-1&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[http://domain.com/winning-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=bid-appnexus-1&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"nurl": "http://domain.com/winning-bid",
"impid": "my-imp-id",
"price": 0.71,
Expand All @@ -172,7 +172,7 @@
},
{
"id": "losing-bid",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem><![CDATA[prebid.org wrapper]]></AdSystem><VASTAdTagURI><![CDATA[http://domain.com/losing-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=bid-appnexus-2&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[http://domain.com/losing-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=bid-appnexus-2&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"nurl": "http://domain.com/losing-bid",
"impid": "my-imp-id",
"price": 0.21,
Expand Down
4 changes: 2 additions & 2 deletions exchange/exchangetest/events-vast-account-on-request-off.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"bid": [
{
"id": "winning-bid",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem><![CDATA[prebid.org wrapper]]></AdSystem><VASTAdTagURI><![CDATA[http://domain.com/winning-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=winning-bid&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[http://domain.com/winning-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=winning-bid&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"nurl": "http://domain.com/winning-bid",
"impid": "my-imp-id",
"price": 0.71,
Expand All @@ -165,7 +165,7 @@
},
{
"id": "losing-bid",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem><![CDATA[prebid.org wrapper]]></AdSystem><VASTAdTagURI><![CDATA[http://domain.com/losing-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=losing-bid&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"adm": "<VAST version=\"3.0\"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[http://domain.com/losing-bid]]></VASTAdTagURI><Impression><![CDATA[http://localhost/event?t=imp&b=losing-bid&a=testaccount&bidder=appnexus&f=b&int=testIntegrationType&ts=1234567890]]></Impression><Creatives></Creatives></Wrapper></Ad></VAST>",
"nurl": "http://domain.com/losing-bid",
"impid": "my-imp-id",
"price": 0.21,
Expand Down
2 changes: 1 addition & 1 deletion injector/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
emptyAdmResponse = `<VAST version="3.0"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[%s]]></VASTAdTagURI><Creatives></Creatives></Wrapper></Ad></VAST>`
emptyAdmResponse = `<VAST version="3.0"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[%s]]></VASTAdTagURI><Impression></Impression><Creatives></Creatives></Wrapper></Ad></VAST>`
)

const (
Expand Down
2 changes: 1 addition & 1 deletion injector/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestInjectTracker(t *testing.T) {
vastXML: "",
NURL: "www.nurl.com",
},
want: `<VAST version="3.0"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[www.nurl.com]]></VASTAdTagURI><Creatives></Creatives></Wrapper></Ad></VAST>`,
want: `<VAST version="3.0"><Ad><Wrapper><AdSystem>prebid.org wrapper</AdSystem><VASTAdTagURI><![CDATA[www.nurl.com]]></VASTAdTagURI><Impression></Impression><Creatives></Creatives></Wrapper></Ad></VAST>`,
wantError: nil,
},
{
Expand Down

0 comments on commit 6f3e323

Please sign in to comment.