github.com/prebid/prebid-server@v0.275.0/schain/schainwriter_test.go (about)

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