github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/convert_up_test.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestConvertUpTo26(t *testing.T) {
    12  	testCases := []struct {
    13  		description     string
    14  		givenRequest    openrtb2.BidRequest
    15  		expectedRequest openrtb2.BidRequest
    16  		expectedErr     string
    17  	}{
    18  		{
    19  			description: "Malformed",
    20  			givenRequest: openrtb2.BidRequest{
    21  				Ext: json.RawMessage(`malformed`),
    22  			},
    23  			expectedErr: "req.ext is invalid: expect { or n, but found m",
    24  		},
    25  		{
    26  			description: "2.4 -> 2.6",
    27  			givenRequest: openrtb2.BidRequest{
    28  				ID:   "anyID",
    29  				Imp:  []openrtb2.Imp{{Ext: json.RawMessage(`{"prebid":{"is_rewarded_inventory":1}}`)}},
    30  				Ext:  json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"2"}}`),
    31  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr":1,"us_privacy":"3"}`)},
    32  				User: &openrtb2.User{Ext: json.RawMessage(`{"consent":"1","eids":[{"source":"42"}]}`)},
    33  			},
    34  			expectedRequest: openrtb2.BidRequest{
    35  				ID:     "anyID",
    36  				Imp:    []openrtb2.Imp{{Rwdd: 1}},
    37  				Source: &openrtb2.Source{SChain: &openrtb2.SupplyChain{Complete: 1, Nodes: []openrtb2.SupplyChainNode{}, Ver: "2"}},
    38  				Regs:   &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(1), USPrivacy: "3"},
    39  				User:   &openrtb2.User{Consent: "1", EIDs: []openrtb2.EID{{Source: "42"}}},
    40  			},
    41  		},
    42  		{
    43  			description: "2.4 -> 2.6 + Other Ext Fields",
    44  			givenRequest: openrtb2.BidRequest{
    45  				ID:   "anyID",
    46  				Imp:  []openrtb2.Imp{{Ext: json.RawMessage(`{"prebid":{"is_rewarded_inventory":1},"other":"otherImp"}`)}},
    47  				Ext:  json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"2"},"other":"otherExt"}`),
    48  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr":1,"other":"otherRegs","us_privacy":"3"}`)},
    49  				User: &openrtb2.User{Ext: json.RawMessage(`{"consent":"1","eids":[{"source":"42"}],"other":"otherUser"}`)},
    50  			},
    51  			expectedRequest: openrtb2.BidRequest{
    52  				ID:     "anyID",
    53  				Imp:    []openrtb2.Imp{{Rwdd: 1, Ext: json.RawMessage(`{"other":"otherImp"}`)}},
    54  				Ext:    json.RawMessage(`{"other":"otherExt"}`),
    55  				Source: &openrtb2.Source{SChain: &openrtb2.SupplyChain{Complete: 1, Nodes: []openrtb2.SupplyChainNode{}, Ver: "2"}},
    56  				Regs:   &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(1), USPrivacy: "3", Ext: json.RawMessage(`{"other":"otherRegs"}`)},
    57  				User:   &openrtb2.User{Consent: "1", EIDs: []openrtb2.EID{{Source: "42"}}, Ext: json.RawMessage(`{"other":"otherUser"}`)},
    58  			},
    59  		},
    60  		{
    61  			description: "2.5 -> 2.6",
    62  			givenRequest: openrtb2.BidRequest{
    63  				ID:     "anyID",
    64  				Imp:    []openrtb2.Imp{{Ext: json.RawMessage(`{"prebid":{"is_rewarded_inventory":1}}`)}},
    65  				Source: &openrtb2.Source{Ext: json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"2"}}`)},
    66  				Regs:   &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr":1,"us_privacy":"3"}`)},
    67  				User:   &openrtb2.User{Ext: json.RawMessage(`{"consent":"1","eids":[{"source":"42"}]}`)},
    68  			},
    69  			expectedRequest: openrtb2.BidRequest{
    70  				ID:     "anyID",
    71  				Imp:    []openrtb2.Imp{{Rwdd: 1}},
    72  				Source: &openrtb2.Source{SChain: &openrtb2.SupplyChain{Complete: 1, Nodes: []openrtb2.SupplyChainNode{}, Ver: "2"}},
    73  				Regs:   &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(1), USPrivacy: "3"},
    74  				User:   &openrtb2.User{Consent: "1", EIDs: []openrtb2.EID{{Source: "42"}}},
    75  			},
    76  		},
    77  		{
    78  			description: "2.5 -> 2.6 + Other Ext Fields",
    79  			givenRequest: openrtb2.BidRequest{
    80  				ID:     "anyID",
    81  				Imp:    []openrtb2.Imp{{Ext: json.RawMessage(`{"prebid":{"is_rewarded_inventory":1},"other":"otherImp"}`)}},
    82  				Ext:    json.RawMessage(`{"other":"otherExt"}`),
    83  				Source: &openrtb2.Source{Ext: json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"2"},"other":"otherSource"}`)},
    84  				Regs:   &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr":1,"us_privacy":"3","other":"otherRegs"}`)},
    85  				User:   &openrtb2.User{Ext: json.RawMessage(`{"consent":"1","eids":[{"source":"42"}],"other":"otherUser"}`)},
    86  			},
    87  			expectedRequest: openrtb2.BidRequest{
    88  				ID:     "anyID",
    89  				Imp:    []openrtb2.Imp{{Rwdd: 1, Ext: json.RawMessage(`{"other":"otherImp"}`)}},
    90  				Ext:    json.RawMessage(`{"other":"otherExt"}`),
    91  				Source: &openrtb2.Source{SChain: &openrtb2.SupplyChain{Complete: 1, Nodes: []openrtb2.SupplyChainNode{}, Ver: "2"}, Ext: json.RawMessage(`{"other":"otherSource"}`)},
    92  				Regs:   &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(1), USPrivacy: "3", Ext: json.RawMessage(`{"other":"otherRegs"}`)},
    93  				User:   &openrtb2.User{Consent: "1", EIDs: []openrtb2.EID{{Source: "42"}}, Ext: json.RawMessage(`{"other":"otherUser"}`)},
    94  			},
    95  		},
    96  	}
    97  
    98  	for _, test := range testCases {
    99  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   100  		err := ConvertUpTo26(w)
   101  		if len(test.expectedErr) > 0 {
   102  			assert.EqualError(t, err, test.expectedErr, test.description)
   103  		} else {
   104  			assert.NoError(t, w.RebuildRequest(), test.description)
   105  			assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   106  		}
   107  	}
   108  }
   109  
   110  func TestConvertUpEnsureExt(t *testing.T) {
   111  	testCases := []struct {
   112  		description  string
   113  		givenRequest openrtb2.BidRequest
   114  		expectedErr  string
   115  	}{
   116  		{
   117  			description:  "Empty",
   118  			givenRequest: openrtb2.BidRequest{},
   119  		},
   120  		{
   121  			description:  "Ext",
   122  			givenRequest: openrtb2.BidRequest{Ext: json.RawMessage("malformed")},
   123  			expectedErr:  "req.ext is invalid: expect { or n, but found m",
   124  		},
   125  		{
   126  			description:  "Source.Ext",
   127  			givenRequest: openrtb2.BidRequest{Source: &openrtb2.Source{Ext: json.RawMessage("malformed")}},
   128  			expectedErr:  "req.source.ext is invalid: expect { or n, but found m",
   129  		},
   130  		{
   131  			description:  "Regs.Ext",
   132  			givenRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage("malformed")}},
   133  			expectedErr:  "req.regs.ext is invalid: expect { or n, but found m",
   134  		},
   135  		{
   136  			description:  "User.Ext",
   137  			givenRequest: openrtb2.BidRequest{User: &openrtb2.User{Ext: json.RawMessage("malformed")}},
   138  			expectedErr:  "req.user.ext is invalid: expect { or n, but found m",
   139  		},
   140  		{
   141  			description:  "Imp.Ext",
   142  			givenRequest: openrtb2.BidRequest{Imp: []openrtb2.Imp{{Ext: json.RawMessage("malformed")}}},
   143  			expectedErr:  "imp[0].imp.ext is invalid: expect { or n, but found m",
   144  		},
   145  	}
   146  
   147  	for _, test := range testCases {
   148  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   149  		err := convertUpEnsureExt(w)
   150  		if len(test.expectedErr) > 0 {
   151  			assert.EqualError(t, err, test.expectedErr, test.description)
   152  		} else {
   153  			assert.NoError(t, err, test.description)
   154  		}
   155  	}
   156  }
   157  
   158  func TestMoveSupplyChainFrom24To25(t *testing.T) {
   159  	var (
   160  		schain1 = json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"1"}}`)
   161  		schain2 = json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"2"}}`)
   162  	)
   163  
   164  	testCases := []struct {
   165  		description     string
   166  		givenRequest    openrtb2.BidRequest
   167  		expectedRequest openrtb2.BidRequest
   168  	}{
   169  		{
   170  			description:     "Not Present",
   171  			givenRequest:    openrtb2.BidRequest{},
   172  			expectedRequest: openrtb2.BidRequest{},
   173  		},
   174  		{
   175  			description:     "2.4 Migrated To 2.5 - Source Doesn't Exist",
   176  			givenRequest:    openrtb2.BidRequest{Ext: schain1},
   177  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1}},
   178  		},
   179  		{
   180  			description:     "2.4 Migrated To 2.5 - Source Exists",
   181  			givenRequest:    openrtb2.BidRequest{Source: &openrtb2.Source{}, Ext: schain1},
   182  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1}},
   183  		},
   184  		{
   185  			description:     "2.4 Dropped",
   186  			givenRequest:    openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1}, Ext: schain2},
   187  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1}},
   188  		},
   189  		{
   190  			description:     "2.5 Left Alone",
   191  			givenRequest:    openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1}},
   192  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1}},
   193  		},
   194  	}
   195  
   196  	for _, test := range testCases {
   197  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   198  		moveSupplyChainFrom24To25(w)
   199  		assert.NoError(t, w.RebuildRequest(), test.description)
   200  		assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   201  	}
   202  }
   203  
   204  func TestConvertSupplyChainFrom25To26(t *testing.T) {
   205  	var (
   206  		schain1     = &openrtb2.SupplyChain{Complete: 1, Nodes: []openrtb2.SupplyChainNode{}, Ver: "1"}
   207  		schain1Json = json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"1"}}`)
   208  		schain2Json = json.RawMessage(`{"schain":{"complete":1,"nodes":[],"ver":"2"}}`)
   209  	)
   210  
   211  	testCases := []struct {
   212  		description     string
   213  		givenRequest    openrtb2.BidRequest
   214  		expectedRequest openrtb2.BidRequest
   215  	}{
   216  		{
   217  			description:     "Not Present",
   218  			givenRequest:    openrtb2.BidRequest{},
   219  			expectedRequest: openrtb2.BidRequest{},
   220  		},
   221  		{
   222  			description:     "2.5 Migrated To 2.6",
   223  			givenRequest:    openrtb2.BidRequest{Source: &openrtb2.Source{Ext: schain1Json}},
   224  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{SChain: schain1}},
   225  		},
   226  		{
   227  			description:     "2.5 Dropped",
   228  			givenRequest:    openrtb2.BidRequest{Source: &openrtb2.Source{SChain: schain1, Ext: schain2Json}},
   229  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{SChain: schain1}},
   230  		},
   231  		{
   232  			description:     "2.6 Left Alone",
   233  			givenRequest:    openrtb2.BidRequest{Source: &openrtb2.Source{SChain: schain1}},
   234  			expectedRequest: openrtb2.BidRequest{Source: &openrtb2.Source{SChain: schain1}},
   235  		},
   236  	}
   237  
   238  	for _, test := range testCases {
   239  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   240  		moveSupplyChainFrom25To26(w)
   241  		assert.NoError(t, w.RebuildRequest(), test.description)
   242  		assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   243  	}
   244  }
   245  
   246  func TestMoveGDPRFrom25To26(t *testing.T) {
   247  	testCases := []struct {
   248  		description     string
   249  		givenRequest    openrtb2.BidRequest
   250  		expectedRequest openrtb2.BidRequest
   251  	}{
   252  		{
   253  			description:     "Not Present",
   254  			givenRequest:    openrtb2.BidRequest{},
   255  			expectedRequest: openrtb2.BidRequest{},
   256  		},
   257  		{
   258  			description:     "2.5 Migrated To 2.6",
   259  			givenRequest:    openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr":0}`)}},
   260  			expectedRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(0)}},
   261  		},
   262  		{
   263  			description:     "2.5 Dropped",
   264  			givenRequest:    openrtb2.BidRequest{Regs: &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(0), Ext: json.RawMessage(`{"gdpr":1}`)}},
   265  			expectedRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(0)}},
   266  		},
   267  		{
   268  			description:     "2.6 Left Alone",
   269  			givenRequest:    openrtb2.BidRequest{Regs: &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(0)}},
   270  			expectedRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{GDPR: openrtb2.Int8Ptr(0)}},
   271  		},
   272  	}
   273  
   274  	for _, test := range testCases {
   275  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   276  		moveGDPRFrom25To26(w)
   277  		assert.NoError(t, w.RebuildRequest(), test.description)
   278  		assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   279  	}
   280  }
   281  
   282  func TestMoveConsentFrom25To26(t *testing.T) {
   283  	testCases := []struct {
   284  		description     string
   285  		givenRequest    openrtb2.BidRequest
   286  		expectedRequest openrtb2.BidRequest
   287  	}{
   288  		{
   289  			description:     "Not Present",
   290  			givenRequest:    openrtb2.BidRequest{},
   291  			expectedRequest: openrtb2.BidRequest{},
   292  		},
   293  		{
   294  			description:     "2.5 Migrated To 2.6",
   295  			givenRequest:    openrtb2.BidRequest{User: &openrtb2.User{Ext: json.RawMessage(`{"consent":"1"}`)}},
   296  			expectedRequest: openrtb2.BidRequest{User: &openrtb2.User{Consent: "1"}},
   297  		},
   298  		{
   299  			description:     "2.5 Dropped",
   300  			givenRequest:    openrtb2.BidRequest{User: &openrtb2.User{Consent: "1", Ext: json.RawMessage(`{"consent":2}`)}},
   301  			expectedRequest: openrtb2.BidRequest{User: &openrtb2.User{Consent: "1"}},
   302  		},
   303  		{
   304  			description:     "2.6 Left Alone",
   305  			givenRequest:    openrtb2.BidRequest{User: &openrtb2.User{Consent: "1"}},
   306  			expectedRequest: openrtb2.BidRequest{User: &openrtb2.User{Consent: "1"}},
   307  		},
   308  	}
   309  
   310  	for _, test := range testCases {
   311  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   312  		moveConsentFrom25To26(w)
   313  		assert.NoError(t, w.RebuildRequest(), test.description)
   314  		assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   315  	}
   316  }
   317  
   318  func TestMoveUSPrivacyFrom25To26(t *testing.T) {
   319  	testCases := []struct {
   320  		description     string
   321  		givenRequest    openrtb2.BidRequest
   322  		expectedRequest openrtb2.BidRequest
   323  	}{
   324  		{
   325  			description:     "Not Present",
   326  			givenRequest:    openrtb2.BidRequest{},
   327  			expectedRequest: openrtb2.BidRequest{},
   328  		},
   329  		{
   330  			description:     "2.5 Migrated To 2.6",
   331  			givenRequest:    openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"us_privacy":"1"}`)}},
   332  			expectedRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{USPrivacy: "1"}},
   333  		},
   334  		{
   335  			description:     "2.5 Dropped",
   336  			givenRequest:    openrtb2.BidRequest{Regs: &openrtb2.Regs{USPrivacy: "1", Ext: json.RawMessage(`{"us_privacy":"2"}`)}},
   337  			expectedRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{USPrivacy: "1"}},
   338  		},
   339  		{
   340  			description:     "2.6 Left Alone",
   341  			givenRequest:    openrtb2.BidRequest{Regs: &openrtb2.Regs{USPrivacy: "1"}},
   342  			expectedRequest: openrtb2.BidRequest{Regs: &openrtb2.Regs{USPrivacy: "1"}},
   343  		},
   344  	}
   345  
   346  	for _, test := range testCases {
   347  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   348  		moveUSPrivacyFrom25To26(w)
   349  		assert.NoError(t, w.RebuildRequest(), test.description)
   350  		assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   351  	}
   352  }
   353  
   354  func TestMoveEIDFrom25To26(t *testing.T) {
   355  	var (
   356  		eid1     = []openrtb2.EID{{Source: "1"}}
   357  		eid1Json = json.RawMessage(`{"eids":[{"source":"1"}]}`)
   358  		eid2Json = json.RawMessage(`{"eids":[{"source":"2"}]}`)
   359  	)
   360  
   361  	testCases := []struct {
   362  		description     string
   363  		givenRequest    openrtb2.BidRequest
   364  		expectedRequest openrtb2.BidRequest
   365  	}{
   366  		{
   367  			description:     "Not Present",
   368  			givenRequest:    openrtb2.BidRequest{},
   369  			expectedRequest: openrtb2.BidRequest{},
   370  		},
   371  		{
   372  			description:     "2.5 Migrated To 2.6",
   373  			givenRequest:    openrtb2.BidRequest{User: &openrtb2.User{Ext: eid1Json}},
   374  			expectedRequest: openrtb2.BidRequest{User: &openrtb2.User{EIDs: eid1}},
   375  		},
   376  		{
   377  			description:     "2.5 Dropped",
   378  			givenRequest:    openrtb2.BidRequest{User: &openrtb2.User{EIDs: eid1, Ext: eid2Json}},
   379  			expectedRequest: openrtb2.BidRequest{User: &openrtb2.User{EIDs: eid1}},
   380  		},
   381  		{
   382  			description:     "2.6 Left Alone",
   383  			givenRequest:    openrtb2.BidRequest{User: &openrtb2.User{EIDs: eid1}},
   384  			expectedRequest: openrtb2.BidRequest{User: &openrtb2.User{EIDs: eid1}},
   385  		},
   386  	}
   387  
   388  	for _, test := range testCases {
   389  		w := &RequestWrapper{BidRequest: &test.givenRequest}
   390  		moveEIDFrom25To26(w)
   391  		assert.NoError(t, w.RebuildRequest(), test.description)
   392  		assert.Equal(t, test.expectedRequest, *w.BidRequest, test.description)
   393  	}
   394  }
   395  
   396  func TestMoveRewardedFromPrebidExtTo26(t *testing.T) {
   397  	var (
   398  		rwdd1Json = json.RawMessage(`{"prebid":{"is_rewarded_inventory":1}}`)
   399  		rwdd2Json = json.RawMessage(`{"prebid":{"is_rewarded_inventory":2}}`)
   400  	)
   401  
   402  	testCases := []struct {
   403  		description string
   404  		givenImp    openrtb2.Imp
   405  		expectedImp openrtb2.Imp
   406  	}{
   407  		{
   408  			description: "Not Present - No Ext",
   409  			givenImp:    openrtb2.Imp{},
   410  			expectedImp: openrtb2.Imp{},
   411  		},
   412  		{
   413  			description: "Not Present - Empty Ext",
   414  			givenImp:    openrtb2.Imp{Ext: json.RawMessage(`{}`)},
   415  			expectedImp: openrtb2.Imp{Ext: json.RawMessage(`{}`)},
   416  		},
   417  		{
   418  			description: "Not Present - Null Prebid Ext",
   419  			givenImp:    openrtb2.Imp{Ext: json.RawMessage(`{"prebid":null}`)},
   420  			expectedImp: openrtb2.Imp{}, // empty prebid object pruned by RebuildImp
   421  		},
   422  		{
   423  			description: "Not Present - Empty Prebid Ext",
   424  			givenImp:    openrtb2.Imp{Ext: json.RawMessage(`{"prebid":{}}`)},
   425  			expectedImp: openrtb2.Imp{}, // empty prebid object pruned by RebuildImp
   426  		},
   427  		{
   428  			description: "Prebid Ext Migrated To 2.6",
   429  			givenImp:    openrtb2.Imp{Ext: rwdd1Json},
   430  			expectedImp: openrtb2.Imp{Rwdd: 1},
   431  		},
   432  		{
   433  			description: "2.5 Dropped",
   434  			givenImp:    openrtb2.Imp{Rwdd: 1, Ext: rwdd2Json},
   435  			expectedImp: openrtb2.Imp{Rwdd: 1},
   436  		},
   437  		{
   438  			description: "2.6 Left Alone",
   439  			givenImp:    openrtb2.Imp{Rwdd: 1},
   440  			expectedImp: openrtb2.Imp{Rwdd: 1},
   441  		},
   442  	}
   443  
   444  	for _, test := range testCases {
   445  		w := &ImpWrapper{Imp: &test.givenImp}
   446  		moveRewardedFromPrebidExtTo26(w)
   447  		assert.NoError(t, w.RebuildImp(), test.description)
   448  		assert.Equal(t, test.expectedImp, *w.Imp, test.description)
   449  	}
   450  }