github.com/prebid/prebid-server/v2@v2.18.0/exchange/events_test.go (about)

     1  package exchange
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/openrtb/v20/openrtb2"
     7  	"github.com/prebid/prebid-server/v2/exchange/entities"
     8  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_eventsData_makeBidExtEvents(t *testing.T) {
    13  	type args struct {
    14  		enabledForAccount bool
    15  		enabledForRequest bool
    16  		bidType           openrtb_ext.BidType
    17  		generatedBidId    string
    18  	}
    19  	tests := []struct {
    20  		name string
    21  		args args
    22  		want *openrtb_ext.ExtBidPrebidEvents
    23  	}{
    24  		{
    25  			name: "banner: events enabled for request, disabled for account",
    26  			args: args{enabledForAccount: false, enabledForRequest: true, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
    27  			want: &openrtb_ext.ExtBidPrebidEvents{
    28  				Win: "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890",
    29  				Imp: "http://localhost/event?t=imp&b=BID-1&a=123456&bidder=openx&ts=1234567890",
    30  			},
    31  		},
    32  		{
    33  			name: "banner: events enabled for account, disabled for request",
    34  			args: args{enabledForAccount: true, enabledForRequest: false, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
    35  			want: &openrtb_ext.ExtBidPrebidEvents{
    36  				Win: "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890",
    37  				Imp: "http://localhost/event?t=imp&b=BID-1&a=123456&bidder=openx&ts=1234567890",
    38  			},
    39  		},
    40  		{
    41  			name: "banner: events disabled for account and request",
    42  			args: args{enabledForAccount: false, enabledForRequest: false, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
    43  			want: nil,
    44  		},
    45  		{
    46  			name: "video: events enabled for account and request",
    47  			args: args{enabledForAccount: true, enabledForRequest: true, bidType: openrtb_ext.BidTypeVideo, generatedBidId: ""},
    48  			want: nil,
    49  		},
    50  		{
    51  			name: "video: events disabled for account and request",
    52  			args: args{enabledForAccount: false, enabledForRequest: false, bidType: openrtb_ext.BidTypeVideo, generatedBidId: ""},
    53  			want: nil,
    54  		},
    55  		{
    56  			name: "banner: use generated bid id",
    57  			args: args{enabledForAccount: false, enabledForRequest: true, bidType: openrtb_ext.BidTypeBanner, generatedBidId: "randomId"},
    58  			want: &openrtb_ext.ExtBidPrebidEvents{
    59  				Win: "http://localhost/event?t=win&b=randomId&a=123456&bidder=openx&ts=1234567890",
    60  				Imp: "http://localhost/event?t=imp&b=randomId&a=123456&bidder=openx&ts=1234567890",
    61  			},
    62  		},
    63  	}
    64  	for _, tt := range tests {
    65  		t.Run(tt.name, func(t *testing.T) {
    66  			evData := &eventTracking{
    67  				enabledForAccount:  tt.args.enabledForAccount,
    68  				enabledForRequest:  tt.args.enabledForRequest,
    69  				accountID:          "123456",
    70  				auctionTimestampMs: 1234567890,
    71  				externalURL:        "http://localhost",
    72  			}
    73  			bid := &entities.PbsOrtbBid{Bid: &openrtb2.Bid{ID: "BID-1"}, BidType: tt.args.bidType, GeneratedBidID: tt.args.generatedBidId}
    74  			assert.Equal(t, tt.want, evData.makeBidExtEvents(bid, openrtb_ext.BidderOpenx))
    75  		})
    76  	}
    77  }
    78  
    79  func Test_eventsData_modifyBidJSON(t *testing.T) {
    80  	type args struct {
    81  		enabledForAccount bool
    82  		enabledForRequest bool
    83  		bidType           openrtb_ext.BidType
    84  		generatedBidId    string
    85  	}
    86  	tests := []struct {
    87  		name      string
    88  		args      args
    89  		jsonBytes []byte
    90  		want      []byte
    91  	}{
    92  		{
    93  			name:      "banner: events enabled for request, disabled for account",
    94  			args:      args{enabledForAccount: false, enabledForRequest: true, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
    95  			jsonBytes: []byte(`{"ID": "something"}`),
    96  			want:      []byte(`{"ID": "something", "wurl": "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890"}`),
    97  		},
    98  		{
    99  			name:      "banner: events enabled for account, disabled for request",
   100  			args:      args{enabledForAccount: true, enabledForRequest: false, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
   101  			jsonBytes: []byte(`{"ID": "something"}`),
   102  			want:      []byte(`{"ID": "something", "wurl": "http://localhost/event?t=win&b=BID-1&a=123456&bidder=openx&ts=1234567890"}`),
   103  		},
   104  		{
   105  			name:      "banner: events disabled for account and request",
   106  			args:      args{enabledForAccount: false, enabledForRequest: false, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
   107  			jsonBytes: []byte(`{"ID": "something"}`),
   108  			want:      []byte(`{"ID": "something"}`),
   109  		},
   110  		{
   111  			name:      "video: events disabled for account and request",
   112  			args:      args{enabledForAccount: false, enabledForRequest: false, bidType: openrtb_ext.BidTypeVideo, generatedBidId: ""},
   113  			jsonBytes: []byte(`{"ID": "something"}`),
   114  			want:      []byte(`{"ID": "something"}`),
   115  		},
   116  		{
   117  			name:      "video: events enabled for account and request",
   118  			args:      args{enabledForAccount: true, enabledForRequest: true, bidType: openrtb_ext.BidTypeVideo, generatedBidId: ""},
   119  			jsonBytes: []byte(`{"ID": "something"}`),
   120  			want:      []byte(`{"ID": "something"}`),
   121  		},
   122  		{
   123  			name:      "banner: broken json expected to fail patching",
   124  			args:      args{enabledForAccount: false, enabledForRequest: true, bidType: openrtb_ext.BidTypeBanner, generatedBidId: ""},
   125  			jsonBytes: []byte(`broken json`),
   126  			want:      nil,
   127  		},
   128  		{
   129  			name:      "banner: generate bid id enabled",
   130  			args:      args{enabledForAccount: false, enabledForRequest: true, bidType: openrtb_ext.BidTypeBanner, generatedBidId: "randomID"},
   131  			jsonBytes: []byte(`{"ID": "something"}`),
   132  			want:      []byte(`{"ID": "something", "wurl":"http://localhost/event?t=win&b=randomID&a=123456&bidder=openx&ts=1234567890"}`),
   133  		},
   134  	}
   135  	for _, tt := range tests {
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			evData := &eventTracking{
   138  				enabledForAccount:  tt.args.enabledForAccount,
   139  				enabledForRequest:  tt.args.enabledForRequest,
   140  				accountID:          "123456",
   141  				auctionTimestampMs: 1234567890,
   142  				externalURL:        "http://localhost",
   143  			}
   144  			bid := &entities.PbsOrtbBid{Bid: &openrtb2.Bid{ID: "BID-1"}, BidType: tt.args.bidType, GeneratedBidID: tt.args.generatedBidId}
   145  			modifiedJSON, err := evData.modifyBidJSON(bid, openrtb_ext.BidderOpenx, tt.jsonBytes)
   146  			if tt.want != nil {
   147  				assert.NoError(t, err, "Unexpected error")
   148  				assert.JSONEq(t, string(tt.want), string(modifiedJSON))
   149  			} else {
   150  				assert.Error(t, err)
   151  				assert.Equal(t, string(tt.jsonBytes), string(modifiedJSON), "Expected original json on failure to modify")
   152  			}
   153  		})
   154  	}
   155  }
   156  
   157  func Test_isEventAllowed(t *testing.T) {
   158  	type args struct {
   159  		enabledForAccount bool
   160  		enabledForRequest bool
   161  	}
   162  	tests := []struct {
   163  		name string
   164  		args args
   165  		want bool
   166  	}{
   167  		{
   168  			name: "enabled for account",
   169  			args: args{enabledForAccount: true, enabledForRequest: false},
   170  			want: true,
   171  		},
   172  		{
   173  			name: "enabled for request",
   174  			args: args{enabledForAccount: false, enabledForRequest: true},
   175  			want: true,
   176  		},
   177  		{
   178  			name: "disabled for account and request",
   179  			args: args{enabledForAccount: false, enabledForRequest: false},
   180  			want: false,
   181  		},
   182  	}
   183  
   184  	for _, tt := range tests {
   185  		t.Run(tt.name, func(t *testing.T) {
   186  			evData := &eventTracking{
   187  				enabledForAccount: tt.args.enabledForAccount,
   188  				enabledForRequest: tt.args.enabledForRequest,
   189  			}
   190  			isEventAllowed := evData.isEventAllowed()
   191  			assert.Equal(t, tt.want, isEventAllowed)
   192  		})
   193  	}
   194  }