github.com/prebid/prebid-server/v2@v2.18.0/macros/provider_test.go (about)

     1  package macros
     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 TestGetMacro(t *testing.T) {
    13  	type args struct {
    14  		key            string
    15  		reqWrapper     *openrtb_ext.RequestWrapper
    16  		seat           string
    17  		vastEvent      string
    18  		eventType      string
    19  		vastCreativeID string
    20  	}
    21  	tests := []struct {
    22  		name string
    23  		args args
    24  		want string
    25  	}{
    26  		{
    27  			name: " Macro present, get PBS-APPBUNDLE key",
    28  			args: args{
    29  				key: MacroKeyAppBundle,
    30  				reqWrapper: &openrtb_ext.RequestWrapper{
    31  					BidRequest: &openrtb2.BidRequest{
    32  						ID: "123",
    33  						App: &openrtb2.App{
    34  							Bundle: "test",
    35  						},
    36  					},
    37  				},
    38  			},
    39  			want: "test",
    40  		},
    41  		{
    42  			name: " Macro does present, get PBS-APPBUNDLE key",
    43  			args: args{
    44  				key: MacroKeyAppBundle,
    45  				reqWrapper: &openrtb_ext.RequestWrapper{
    46  					BidRequest: &openrtb2.BidRequest{
    47  						ID: "123",
    48  					},
    49  				},
    50  			},
    51  			want: "",
    52  		},
    53  		{
    54  			name: "Invalid Macro key",
    55  			args: args{
    56  				key: "PBS-NOEXISTENTKEY",
    57  				reqWrapper: &openrtb_ext.RequestWrapper{
    58  					BidRequest: &openrtb2.BidRequest{
    59  						Ext: []byte(`{"prebid":{"channel": {"name":"test1"},"macros":{"CUSTOMMACR3":"a"}}}`),
    60  					},
    61  				},
    62  			},
    63  			want: "",
    64  		},
    65  	}
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			macroProvider := NewProvider(tt.args.reqWrapper)
    69  			macroProvider.PopulateBidMacros(&entities.PbsOrtbBid{Bid: bid}, tt.args.seat)
    70  			macroProvider.PopulateEventMacros(tt.args.vastCreativeID, tt.args.eventType, tt.args.vastEvent)
    71  			got := macroProvider.GetMacro(tt.args.key)
    72  			assert.Equal(t, tt.want, got, tt.name)
    73  		})
    74  	}
    75  }
    76  
    77  func TestPopulateRequestMacros(t *testing.T) {
    78  	type args struct {
    79  		reqWrapper *openrtb_ext.RequestWrapper
    80  	}
    81  	tests := []struct {
    82  		name string
    83  		args args
    84  		want map[string]string
    85  	}{
    86  		{
    87  			name: "No request level macros present",
    88  			args: args{
    89  				reqWrapper: &openrtb_ext.RequestWrapper{
    90  					BidRequest: &openrtb2.BidRequest{},
    91  				},
    92  			},
    93  			want: map[string]string{MacroKeyBidID: "", MacroKeyAppBundle: "", MacroKeyDomain: "", MacroKeyPubDomain: "", MacroKeyPageURL: "", MacroKeyAccountID: "", MacroKeyLmtTracking: "", MacroKeyConsent: "", MacroKeyBidder: "", MacroKeyIntegration: "", MacroKeyVastCRTID: "", MacroKeyAuctionID: "", MacroKeyChannel: "", MacroKeyEventType: "", MacroKeyVastEvent: ""},
    94  		},
    95  		{
    96  			name: " AUCTIONID, AppBundle, PageURL present key",
    97  			args: args{
    98  				reqWrapper: &openrtb_ext.RequestWrapper{
    99  					BidRequest: &openrtb2.BidRequest{
   100  						ID: "123",
   101  						App: &openrtb2.App{
   102  							Bundle: "testBundle",
   103  						},
   104  						Site: &openrtb2.Site{
   105  							Page: "testPage",
   106  						},
   107  					},
   108  				},
   109  			},
   110  			want: map[string]string{MacroKeyBidID: "", MacroKeyAppBundle: "testBundle", MacroKeyDomain: "", MacroKeyPubDomain: "", MacroKeyPageURL: "testPage", MacroKeyAccountID: "", MacroKeyLmtTracking: "", MacroKeyConsent: "", MacroKeyBidder: "", MacroKeyIntegration: "", MacroKeyVastCRTID: "", MacroKeyAuctionID: "123", MacroKeyChannel: "", MacroKeyEventType: "", MacroKeyVastEvent: ""},
   111  		},
   112  		{
   113  			name: " AppDomain, PubDomain, present key",
   114  			args: args{
   115  				reqWrapper: &openrtb_ext.RequestWrapper{
   116  					BidRequest: &openrtb2.BidRequest{
   117  						App: &openrtb2.App{
   118  							Domain: "testDomain",
   119  						},
   120  						Site: &openrtb2.Site{
   121  							Publisher: &openrtb2.Publisher{
   122  								Domain: "pubDomain",
   123  							},
   124  						},
   125  					},
   126  				},
   127  			},
   128  			want: map[string]string{MacroKeyBidID: "", MacroKeyAppBundle: "", MacroKeyDomain: "testDomain", MacroKeyPubDomain: "pubDomain", MacroKeyPageURL: "", MacroKeyAccountID: "", MacroKeyLmtTracking: "", MacroKeyConsent: "", MacroKeyBidder: "", MacroKeyIntegration: "", MacroKeyVastCRTID: "", MacroKeyAuctionID: "", MacroKeyChannel: "", MacroKeyEventType: "", MacroKeyVastEvent: ""},
   129  		},
   130  		{
   131  			name: " Integration, Consent present key",
   132  			args: args{
   133  				reqWrapper: &openrtb_ext.RequestWrapper{
   134  					BidRequest: &openrtb2.BidRequest{
   135  						User: &openrtb2.User{Ext: []byte(`{"consent":"1" }`)},
   136  						Ext:  []byte(`{"prebid":{"integration":"testIntegration"}}`),
   137  					},
   138  				},
   139  			},
   140  			want: map[string]string{MacroKeyBidID: "", MacroKeyAppBundle: "", MacroKeyDomain: "", MacroKeyPubDomain: "", MacroKeyPageURL: "", MacroKeyAccountID: "", MacroKeyLmtTracking: "", MacroKeyConsent: "1", MacroKeyBidder: "", MacroKeyIntegration: "testIntegration", MacroKeyVastCRTID: "", MacroKeyAuctionID: "", MacroKeyChannel: "", MacroKeyEventType: "", MacroKeyVastEvent: ""},
   141  		},
   142  		{
   143  			name: " PBS-CHANNEL, LIMITADTRACKING present key",
   144  			args: args{
   145  				reqWrapper: &openrtb_ext.RequestWrapper{
   146  					BidRequest: &openrtb2.BidRequest{
   147  						Device: &openrtb2.Device{
   148  							Lmt: &lmt,
   149  						},
   150  						Ext: []byte(`{"prebid":{"channel": {"name":"test1"}}}`),
   151  					},
   152  				},
   153  			},
   154  			want: map[string]string{MacroKeyBidID: "", MacroKeyAppBundle: "", MacroKeyDomain: "", MacroKeyPubDomain: "", MacroKeyPageURL: "", MacroKeyAccountID: "", MacroKeyLmtTracking: "10", MacroKeyConsent: "", MacroKeyBidder: "", MacroKeyIntegration: "", MacroKeyVastCRTID: "", MacroKeyAuctionID: "", MacroKeyChannel: "test1", MacroKeyEventType: "", MacroKeyVastEvent: ""},
   155  		},
   156  		{
   157  			name: " custom macros present key",
   158  			args: args{
   159  				reqWrapper: &openrtb_ext.RequestWrapper{
   160  					BidRequest: &openrtb2.BidRequest{
   161  						Ext: []byte(`{"prebid":{"macros":{"CUSTOMMACR1":"value1"}}}`),
   162  					},
   163  				},
   164  			},
   165  			want: map[string]string{"PBS-MACRO-CUSTOMMACR1": "value1", MacroKeyBidID: "", MacroKeyAppBundle: "", MacroKeyDomain: "", MacroKeyPubDomain: "", MacroKeyPageURL: "", MacroKeyAccountID: "", MacroKeyLmtTracking: "", MacroKeyConsent: "", MacroKeyBidder: "", MacroKeyIntegration: "", MacroKeyVastCRTID: "", MacroKeyAuctionID: "", MacroKeyChannel: "", MacroKeyEventType: "", MacroKeyVastEvent: ""},
   166  		},
   167  		{
   168  			name: " All request macros present key",
   169  			args: args{
   170  				reqWrapper: &openrtb_ext.RequestWrapper{
   171  					BidRequest: &openrtb2.BidRequest{
   172  						ID: "123",
   173  						Site: &openrtb2.Site{
   174  							Domain: "testdomain",
   175  							Publisher: &openrtb2.Publisher{
   176  								Domain: "publishertestdomain",
   177  								ID:     "testpublisherID",
   178  							},
   179  							Page: "pageurltest",
   180  						},
   181  						App: &openrtb2.App{
   182  							Domain: "testdomain",
   183  							Bundle: "testbundle",
   184  							Publisher: &openrtb2.Publisher{
   185  								Domain: "publishertestdomain",
   186  								ID:     "testpublisherID",
   187  							},
   188  						},
   189  						Device: &openrtb2.Device{
   190  							Lmt: &lmt,
   191  						},
   192  						User: &openrtb2.User{Ext: []byte(`{"consent":"1" }`)},
   193  						Ext:  []byte(`{"prebid":{"channel": {"name":"test1"},"macros":{"CUSTOMMACR1":"value1"}}}`),
   194  					},
   195  				},
   196  			},
   197  			want: map[string]string{"PBS-MACRO-CUSTOMMACR1": "value1", MacroKeyBidID: "", MacroKeyAppBundle: "testbundle", MacroKeyDomain: "testdomain", MacroKeyPubDomain: "publishertestdomain", MacroKeyPageURL: "pageurltest", MacroKeyAccountID: "testpublisherID", MacroKeyLmtTracking: "10", MacroKeyConsent: "1", MacroKeyBidder: "", MacroKeyIntegration: "", MacroKeyVastCRTID: "", MacroKeyAuctionID: "123", MacroKeyChannel: "test1", MacroKeyEventType: "", MacroKeyVastEvent: ""},
   198  		},
   199  	}
   200  	for _, tt := range tests {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			b := &MacroProvider{
   203  				macros: map[string]string{},
   204  			}
   205  			b.populateRequestMacros(tt.args.reqWrapper)
   206  			output := map[string]string{}
   207  			for key := range tt.want {
   208  				output[key] = b.GetMacro(key)
   209  			}
   210  			assert.Equal(t, tt.want, output, tt.name)
   211  		})
   212  	}
   213  }
   214  
   215  func TestPopulateBidMacros(t *testing.T) {
   216  
   217  	type args struct {
   218  		bid  *entities.PbsOrtbBid
   219  		seat string
   220  	}
   221  	tests := []struct {
   222  		name      string
   223  		args      args
   224  		wantBidID string
   225  		wantSeat  string
   226  	}{
   227  		{
   228  			name: "Bid ID set, no generatedbid id, no seat",
   229  			args: args{
   230  				bid: &entities.PbsOrtbBid{
   231  					Bid: &openrtb2.Bid{
   232  						ID: "bid123",
   233  					},
   234  				},
   235  			},
   236  			wantBidID: "bid123",
   237  			wantSeat:  "",
   238  		},
   239  		{
   240  			name: "Bid ID set, no generatedbid id, seat set",
   241  			args: args{
   242  				bid: &entities.PbsOrtbBid{
   243  					Bid: &openrtb2.Bid{
   244  						ID: "bid123",
   245  					},
   246  				},
   247  				seat: "testSeat",
   248  			},
   249  			wantBidID: "bid123",
   250  			wantSeat:  "testSeat",
   251  		},
   252  		{
   253  			name: "Bid ID set, generatedbid id set, no seat",
   254  			args: args{
   255  				bid: &entities.PbsOrtbBid{
   256  					GeneratedBidID: "generatedbid123",
   257  					Bid: &openrtb2.Bid{
   258  						ID: "bid123",
   259  					},
   260  				},
   261  			},
   262  			wantBidID: "generatedbid123",
   263  			wantSeat:  "",
   264  		},
   265  		{
   266  			name: "Bid ID set, generatedbid id set, seat set",
   267  			args: args{
   268  				bid: &entities.PbsOrtbBid{
   269  					GeneratedBidID: "generatedbid123",
   270  					Bid: &openrtb2.Bid{
   271  						ID: "bid123",
   272  					},
   273  				},
   274  				seat: "testseat",
   275  			},
   276  			wantBidID: "generatedbid123",
   277  			wantSeat:  "testseat",
   278  		},
   279  		{
   280  			name: "Bid ID not set, generatedbid id set, seat set",
   281  			args: args{
   282  				seat: "test-seat",
   283  				bid: &entities.PbsOrtbBid{
   284  					GeneratedBidID: "generatedbid123",
   285  					Bid:            &openrtb2.Bid{},
   286  				},
   287  			},
   288  			wantBidID: "generatedbid123",
   289  			wantSeat:  "test-seat",
   290  		},
   291  		{
   292  			name: "Bid ID not set, generatedbid id not set, seat set",
   293  			args: args{
   294  				seat: "test-seat",
   295  				bid:  &entities.PbsOrtbBid{},
   296  			},
   297  			wantBidID: "",
   298  			wantSeat:  "test-seat",
   299  		},
   300  	}
   301  	for _, tt := range tests {
   302  		t.Run(tt.name, func(t *testing.T) {
   303  			b := &MacroProvider{
   304  				macros: map[string]string{},
   305  			}
   306  			b.PopulateBidMacros(tt.args.bid, tt.args.seat)
   307  			assert.Equal(t, tt.wantBidID, b.GetMacro(MacroKeyBidID), tt.name)
   308  			assert.Equal(t, tt.wantSeat, b.GetMacro(MacroKeyBidder), tt.name)
   309  		})
   310  	}
   311  }
   312  func TestPopulateEventMacros(t *testing.T) {
   313  
   314  	type args struct {
   315  		vastCreativeID string
   316  		eventType      string
   317  		vastEvent      string
   318  	}
   319  	tests := []struct {
   320  		name               string
   321  		args               args
   322  		wantVastCreativeID string
   323  		wantEventType      string
   324  		wantVastEvent      string
   325  	}{
   326  		{
   327  			name:               "creativeId not set, eventType not set, vastEvent not set",
   328  			args:               args{},
   329  			wantVastCreativeID: "",
   330  			wantEventType:      "",
   331  			wantVastEvent:      "",
   332  		},
   333  		{
   334  			name: "creativeId set, eventType not set, vastEvent not set",
   335  			args: args{
   336  				vastCreativeID: "123",
   337  			},
   338  			wantVastCreativeID: "123",
   339  			wantEventType:      "",
   340  			wantVastEvent:      "",
   341  		},
   342  		{
   343  			name: "creativeId not set, eventType  set, vastEvent not set",
   344  			args: args{
   345  				eventType: "win",
   346  			},
   347  			wantVastCreativeID: "",
   348  			wantEventType:      "win",
   349  			wantVastEvent:      "",
   350  		},
   351  		{
   352  			name: "creativeId not set, eventType not set, vastEvent set",
   353  			args: args{
   354  				vastEvent: "firstQuartile",
   355  			},
   356  			wantVastCreativeID: "",
   357  			wantEventType:      "",
   358  			wantVastEvent:      "firstQuartile",
   359  		},
   360  		{
   361  			name: "creativeId not set, eventType  set, vastEvent set",
   362  			args: args{
   363  				vastEvent: "firstQuartile",
   364  				eventType: "win",
   365  			},
   366  			wantVastCreativeID: "",
   367  			wantEventType:      "win",
   368  			wantVastEvent:      "firstQuartile",
   369  		},
   370  		{
   371  			name: "creativeId  set, eventType not set, vastEvent set",
   372  			args: args{
   373  				vastEvent:      "firstQuartile",
   374  				vastCreativeID: "123",
   375  			},
   376  			wantVastCreativeID: "123",
   377  			wantEventType:      "",
   378  			wantVastEvent:      "firstQuartile",
   379  		},
   380  		{
   381  			name: "creativeId set, eventType set, vastEvent not set",
   382  			args: args{
   383  				eventType:      "win",
   384  				vastCreativeID: "123",
   385  			},
   386  			wantVastCreativeID: "123",
   387  			wantEventType:      "win",
   388  			wantVastEvent:      "",
   389  		},
   390  		{
   391  			name: "creativeId set, eventType set, vastEvent set",
   392  			args: args{
   393  				vastEvent:      "firstQuartile",
   394  				eventType:      "win",
   395  				vastCreativeID: "123",
   396  			},
   397  			wantVastCreativeID: "123",
   398  			wantEventType:      "win",
   399  			wantVastEvent:      "firstQuartile",
   400  		},
   401  	}
   402  	for _, tt := range tests {
   403  		t.Run(tt.name, func(t *testing.T) {
   404  			b := &MacroProvider{
   405  				macros: map[string]string{},
   406  			}
   407  			b.PopulateEventMacros(tt.args.vastCreativeID, tt.args.eventType, tt.args.vastEvent)
   408  			assert.Equal(t, tt.wantVastCreativeID, b.GetMacro(MacroKeyVastCRTID), tt.name)
   409  			assert.Equal(t, tt.wantVastEvent, b.GetMacro(MacroKeyVastEvent), tt.name)
   410  			assert.Equal(t, tt.wantEventType, b.GetMacro(MacroKeyEventType), tt.name)
   411  		})
   412  	}
   413  }
   414  
   415  func TestTruncate(t *testing.T) {
   416  	type args struct {
   417  		text  string
   418  		width uint
   419  	}
   420  	tests := []struct {
   421  		name string
   422  		args args
   423  		want string
   424  	}{
   425  		{
   426  			name: "text is empty",
   427  			args: args{
   428  				text:  "",
   429  				width: customMacroLength,
   430  			},
   431  			want: "",
   432  		},
   433  		{
   434  			name: "width less than 100 chars",
   435  			args: args{
   436  				text:  "abcdef",
   437  				width: customMacroLength,
   438  			},
   439  			want: "abcdef",
   440  		},
   441  		{
   442  			name: "width exactly 100 chars",
   443  			args: args{
   444  				text:  "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv",
   445  				width: customMacroLength,
   446  			},
   447  			want: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv",
   448  		},
   449  		{
   450  			name: "width greater than 100 chars",
   451  			args: args{
   452  				text:  "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
   453  				width: customMacroLength,
   454  			},
   455  			want: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv",
   456  		},
   457  	}
   458  	for _, tt := range tests {
   459  		t.Run(tt.name, func(t *testing.T) {
   460  			got := truncate(tt.args.text, tt.args.width)
   461  			assert.Equal(t, tt.want, got, tt.name)
   462  		})
   463  	}
   464  }