github.com/prebid/prebid-server/v2@v2.18.0/schain/schainwriter_test.go (about)

     1  package schain
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  	"github.com/prebid/prebid-server/v2/util/jsonutil"
     9  
    10  	"github.com/prebid/openrtb/v20/openrtb2"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestSChainWriter(t *testing.T) {
    15  
    16  	const seller1SChain string = `"schain":{"complete":1,"nodes":[{"asi":"directseller1.com","sid":"00001","rid":"BidRequest1","hp":1}],"ver":"1.0"}`
    17  	const seller2SChain string = `"schain":{"complete":2,"nodes":[{"asi":"directseller2.com","sid":"00002","rid":"BidRequest2","hp":2}],"ver":"2.0"}`
    18  	const seller3SChain string = `"schain":{"complete":3,"nodes":[{"asi":"directseller3.com","sid":"00003","rid":"BidRequest3","hp":3}],"ver":"3.0"}`
    19  	const sellerWildCardSChain string = `"schain":{"complete":1,"nodes":[{"asi":"wildcard1.com","sid":"wildcard1","rid":"WildcardReq1","hp":1}],"ver":"1.0"}`
    20  	const hostNode string = `{"asi":"pbshostcompany.com","sid":"00001","rid":"BidRequest","hp":1}`
    21  	const seller1Node string = `{"asi":"directseller1.com","sid":"00001","rid":"BidRequest1","hp":1}`
    22  
    23  	tests := []struct {
    24  		description    string
    25  		giveRequest    openrtb2.BidRequest
    26  		giveBidder     string
    27  		giveHostSChain *openrtb2.SupplyChainNode
    28  		wantRequest    openrtb2.BidRequest
    29  		wantError      bool
    30  	}{
    31  		{
    32  			description: "nil source, nil ext.prebid.schains and empty host schain",
    33  			giveRequest: openrtb2.BidRequest{
    34  				Ext:    nil,
    35  				Source: nil,
    36  			},
    37  			giveBidder:     "appnexus",
    38  			giveHostSChain: nil,
    39  			wantRequest: openrtb2.BidRequest{
    40  				Ext:    nil,
    41  				Source: nil,
    42  			},
    43  		},
    44  		{
    45  			description: "Use source schain -- no bidder schain or wildcard schain in nil ext.prebid.schains",
    46  			giveRequest: openrtb2.BidRequest{
    47  				Ext: json.RawMessage(`{}`),
    48  				Source: &openrtb2.Source{
    49  					Ext: json.RawMessage(`{` + seller2SChain + `}`),
    50  				},
    51  			},
    52  			giveBidder: "appnexus",
    53  			wantRequest: openrtb2.BidRequest{
    54  				Ext: json.RawMessage(`{}`),
    55  				Source: &openrtb2.Source{
    56  					Ext: json.RawMessage(`{` + seller2SChain + `}`),
    57  				},
    58  			},
    59  		},
    60  		{
    61  			description: "Use source schain -- no bidder schain or wildcard schain in not nil ext.prebid.schains",
    62  			giveRequest: openrtb2.BidRequest{
    63  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `}]}}`),
    64  				Source: &openrtb2.Source{
    65  					Ext: json.RawMessage(`{` + seller2SChain + `}`),
    66  				},
    67  			},
    68  			giveBidder: "rubicon",
    69  			wantRequest: openrtb2.BidRequest{
    70  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `}]}}`),
    71  				Source: &openrtb2.Source{
    72  					Ext: json.RawMessage(`{` + seller2SChain + `}`),
    73  				},
    74  			},
    75  		},
    76  		{
    77  			description: "Use schain for bidder in ext.prebid.schains; ensure other ext.source field values are retained.",
    78  			giveRequest: openrtb2.BidRequest{
    79  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `}]}}`),
    80  				Source: &openrtb2.Source{
    81  					FD:     openrtb2.Int8Ptr(1),
    82  					TID:    "tid data",
    83  					PChain: "pchain data",
    84  					Ext:    json.RawMessage(`{` + seller2SChain + `}`),
    85  				},
    86  			},
    87  			giveBidder: "appnexus",
    88  			wantRequest: openrtb2.BidRequest{
    89  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `}]}}`),
    90  				Source: &openrtb2.Source{
    91  					FD:     openrtb2.Int8Ptr(1),
    92  					TID:    "tid data",
    93  					PChain: "pchain data",
    94  					Ext:    json.RawMessage(`{` + seller1SChain + `}`),
    95  				},
    96  			},
    97  		},
    98  		{
    99  			description: "Use schain for bidder in ext.prebid.schains, nil req.source ",
   100  			giveRequest: openrtb2.BidRequest{
   101  				Ext:    json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `}]}}`),
   102  				Source: nil,
   103  			},
   104  			giveBidder: "appnexus",
   105  			wantRequest: openrtb2.BidRequest{
   106  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `}]}}`),
   107  				Source: &openrtb2.Source{
   108  					Ext: json.RawMessage(`{` + seller1SChain + `}`),
   109  				},
   110  			},
   111  		},
   112  		{
   113  			description: "Use wildcard schain in ext.prebid.schains.",
   114  			giveRequest: openrtb2.BidRequest{
   115  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["*"],` + sellerWildCardSChain + `}]}}`),
   116  				Source: &openrtb2.Source{
   117  					Ext: nil,
   118  				},
   119  			},
   120  			giveBidder: "appnexus",
   121  			wantRequest: openrtb2.BidRequest{
   122  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["*"],` + sellerWildCardSChain + `}]}}`),
   123  				Source: &openrtb2.Source{
   124  					Ext: json.RawMessage(`{` + sellerWildCardSChain + `}`),
   125  				},
   126  			},
   127  		},
   128  		{
   129  			description: "Use schain for bidder in ext.prebid.schains instead of wildcard.",
   130  			giveRequest: openrtb2.BidRequest{
   131  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `},{"bidders":["*"],` + sellerWildCardSChain + `}]}}`),
   132  				Source: &openrtb2.Source{
   133  					Ext: nil,
   134  				},
   135  			},
   136  			giveBidder: "appnexus",
   137  			wantRequest: openrtb2.BidRequest{
   138  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `},{"bidders":["*"],` + sellerWildCardSChain + `}]}}`),
   139  				Source: &openrtb2.Source{
   140  					Ext: json.RawMessage(`{` + seller1SChain + `}`),
   141  				},
   142  			},
   143  		},
   144  		{
   145  			description: "Use source schain -- multiple (two) bidder schains in ext.prebid.schains.",
   146  			giveRequest: openrtb2.BidRequest{
   147  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `},{"bidders":["appnexus"],` + seller2SChain + `}]}}`),
   148  				Source: &openrtb2.Source{
   149  					Ext: json.RawMessage(`{` + seller3SChain + `}`),
   150  				},
   151  			},
   152  			giveBidder: "appnexus",
   153  			wantRequest: openrtb2.BidRequest{
   154  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["appnexus"],` + seller1SChain + `},{"bidders":["appnexus"],` + seller2SChain + `}]}}`),
   155  				Source: &openrtb2.Source{
   156  					Ext: json.RawMessage(`{` + seller3SChain + `}`),
   157  				},
   158  			},
   159  			wantError: true,
   160  		},
   161  		{
   162  			description: "Schain in request, host schain defined, source.ext for bidder request should update with appended host schain",
   163  			giveRequest: openrtb2.BidRequest{
   164  				Ext:    json.RawMessage(`{"prebid":{"schains":[{"bidders":["testbidder"],"schain":{"complete":1,"nodes":[` + seller1Node + `],"ver":"1.0"}}]}}`),
   165  				Source: nil,
   166  			},
   167  			giveBidder: "testbidder",
   168  			giveHostSChain: &openrtb2.SupplyChainNode{
   169  				ASI: "pbshostcompany.com", SID: "00001", RID: "BidRequest", HP: openrtb2.Int8Ptr(1),
   170  			},
   171  			wantRequest: openrtb2.BidRequest{
   172  				Ext: json.RawMessage(`{"prebid":{"schains":[{"bidders":["testbidder"],"schain":{"complete":1,"nodes":[` + seller1Node + `],"ver":"1.0"}}]}}`),
   173  				Source: &openrtb2.Source{
   174  					Ext: json.RawMessage(`{"schain":{"complete":1,"nodes":[` + seller1Node + `,` + hostNode + `],"ver":"1.0"}}`),
   175  				},
   176  			},
   177  		},
   178  		{
   179  			description: "No Schain in request, host schain defined, source.ext for bidder request should have just the host schain",
   180  			giveRequest: openrtb2.BidRequest{
   181  				Ext:    nil,
   182  				Source: nil,
   183  			},
   184  			giveBidder: "testbidder",
   185  			giveHostSChain: &openrtb2.SupplyChainNode{
   186  				ASI: "pbshostcompany.com", SID: "00001", RID: "BidRequest", HP: openrtb2.Int8Ptr(1),
   187  			},
   188  			wantRequest: openrtb2.BidRequest{
   189  				Ext: nil,
   190  				Source: &openrtb2.Source{
   191  					Ext: json.RawMessage(`{"schain":{"complete":0,"nodes":[` + hostNode + `],"ver":"1.0"}}`),
   192  				},
   193  			},
   194  		},
   195  	}
   196  
   197  	for _, tt := range tests {
   198  		// unmarshal ext to get schains object needed to initialize writer
   199  		var reqExt *openrtb_ext.ExtRequest
   200  		if tt.giveRequest.Ext != nil {
   201  			reqExt = &openrtb_ext.ExtRequest{}
   202  			err := jsonutil.UnmarshalValid(tt.giveRequest.Ext, reqExt)
   203  			if err != nil {
   204  				t.Error("Unable to unmarshal request.ext")
   205  			}
   206  		}
   207  
   208  		writer, err := NewSChainWriter(reqExt, tt.giveHostSChain)
   209  
   210  		if tt.wantError {
   211  			assert.NotNil(t, err)
   212  			assert.Nil(t, writer)
   213  		} else {
   214  			assert.Nil(t, err)
   215  			assert.NotNil(t, writer)
   216  
   217  			writer.Write(&tt.giveRequest, tt.giveBidder)
   218  
   219  			assert.Equal(t, tt.wantRequest, tt.giveRequest, tt.description)
   220  		}
   221  	}
   222  }