From 6f3e3236818450276296029242aedb4cc92aeac3 Mon Sep 17 00:00:00 2001 From: "dhruv.sonone" Date: Fri, 30 Aug 2024 19:42:47 +0530 Subject: [PATCH] Adding UTs --- exchange/events.go | 9 +- exchange/events_test.go | 296 ++++++++++++++++++ .../events-vast-account-off-request-on.json | 4 +- .../events-vast-account-on-request-off.json | 4 +- injector/injector.go | 2 +- injector/injector_test.go | 2 +- 6 files changed, 306 insertions(+), 11 deletions(-) diff --git a/exchange/events.go b/exchange/events.go index a4729bc9433..920f89bcd01 100644 --- a/exchange/events.go +++ b/exchange/events.go @@ -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 @@ -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: diff --git a/exchange/events_test.go b/exchange/events_test.go index a4ed587cd67..5f24676a534 100644 --- a/exchange/events_test.go +++ b/exchange/events_test.go @@ -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" ) @@ -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: "", + }, + }, + bidderName: "bidder1", + expectedPbsBid: &entities.PbsOrtbBid{ + BidType: openrtb_ext.BidTypeBanner, + Bid: &openrtb2.Bid{ + ID: "bid1", + AdM: "", + }, + }, + }, + { + 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: "prebid.org wrapper", + }, + }, + }, + { + 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: `iabtechlabhttp://example.com/errorhttp://example.com/track/impression`, + }, + }, + bidderName: "bidder2", + expectedPbsBid: &entities.PbsOrtbBid{ + BidType: openrtb_ext.BidTypeVideo, + Bid: &openrtb2.Bid{ + ID: "bid2", + NURL: "http://nurl.com", + AdM: "", + }, + }, + }, + } + 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) + }) + } +} diff --git a/exchange/exchangetest/events-vast-account-off-request-on.json b/exchange/exchangetest/events-vast-account-off-request-on.json index 2929e31ec5f..560f4f0e2c7 100644 --- a/exchange/exchangetest/events-vast-account-off-request-on.json +++ b/exchange/exchangetest/events-vast-account-off-request-on.json @@ -145,7 +145,7 @@ "bid": [ { "id": "winning-bid", - "adm": "", + "adm": "prebid.org wrapper", "nurl": "http://domain.com/winning-bid", "impid": "my-imp-id", "price": 0.71, @@ -172,7 +172,7 @@ }, { "id": "losing-bid", - "adm": "", + "adm": "prebid.org wrapper", "nurl": "http://domain.com/losing-bid", "impid": "my-imp-id", "price": 0.21, diff --git a/exchange/exchangetest/events-vast-account-on-request-off.json b/exchange/exchangetest/events-vast-account-on-request-off.json index a6e8c76ccfe..882f1e8c623 100644 --- a/exchange/exchangetest/events-vast-account-on-request-off.json +++ b/exchange/exchangetest/events-vast-account-on-request-off.json @@ -139,7 +139,7 @@ "bid": [ { "id": "winning-bid", - "adm": "", + "adm": "prebid.org wrapper", "nurl": "http://domain.com/winning-bid", "impid": "my-imp-id", "price": 0.71, @@ -165,7 +165,7 @@ }, { "id": "losing-bid", - "adm": "", + "adm": "prebid.org wrapper", "nurl": "http://domain.com/losing-bid", "impid": "my-imp-id", "price": 0.21, diff --git a/injector/injector.go b/injector/injector.go index 8f902273257..219481a39e0 100644 --- a/injector/injector.go +++ b/injector/injector.go @@ -11,7 +11,7 @@ import ( ) const ( - emptyAdmResponse = `prebid.org wrapper` + emptyAdmResponse = `prebid.org wrapper` ) const ( diff --git a/injector/injector_test.go b/injector/injector_test.go index 2c9dceba154..fa424a1e674 100644 --- a/injector/injector_test.go +++ b/injector/injector_test.go @@ -75,7 +75,7 @@ func TestInjectTracker(t *testing.T) { vastXML: "", NURL: "www.nurl.com", }, - want: `prebid.org wrapper`, + want: `prebid.org wrapper`, wantError: nil, }, {