github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/deal_tier_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/prebid/prebid-server/v2/errortypes"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestReadDealTiersFromImp(t *testing.T) {
    13  	testCases := []struct {
    14  		description       string
    15  		impExt            json.RawMessage
    16  		expectedResult    DealTierBidderMap
    17  		expectedErrorType error
    18  	}{
    19  		{
    20  			description:    "nil",
    21  			impExt:         nil,
    22  			expectedResult: DealTierBidderMap{},
    23  		},
    24  		{
    25  			description:    "none",
    26  			impExt:         json.RawMessage(``),
    27  			expectedResult: DealTierBidderMap{},
    28  		},
    29  		{
    30  			description:    "empty_object",
    31  			impExt:         json.RawMessage(`{}`),
    32  			expectedResult: DealTierBidderMap{},
    33  		},
    34  		{
    35  			description:    "imp.ext_no_prebid_but_with_other_params",
    36  			impExt:         json.RawMessage(`{"appnexus": {"dealTier": {"minDealTier": 5, "prefix": "anyPrefix"}, "placementId": 12345}, "tid": "1234"}`),
    37  			expectedResult: DealTierBidderMap{},
    38  		},
    39  		{
    40  			description:    "imp.ext.prebid_nil",
    41  			impExt:         json.RawMessage(`{"prebid": null}`),
    42  			expectedResult: DealTierBidderMap{},
    43  		},
    44  		{
    45  			description:    "imp.ext.prebid_empty",
    46  			impExt:         json.RawMessage(`{"prebid": {}}`),
    47  			expectedResult: DealTierBidderMap{},
    48  		},
    49  		{
    50  			description:    "imp.ext.prebid_no bidder but with other params",
    51  			impExt:         json.RawMessage(`{"prebid": {"supportdeals": true}}`),
    52  			expectedResult: DealTierBidderMap{},
    53  		},
    54  		{
    55  			description:    "imp.ext.prebid.bidder_one",
    56  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"appnexus": {"dealTier": {"minDealTier": 5, "prefix": "anyPrefix"}, "placementId": 12345}}}}`),
    57  			expectedResult: DealTierBidderMap{BidderAppnexus: {Prefix: "anyPrefix", MinDealTier: 5}},
    58  		},
    59  		{
    60  			description:    "imp.ext.prebid.bidder_one_but_not_found_in_the_adapter_bidder_list",
    61  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"unknown": {"dealTier": {"minDealTier": 5, "prefix": "anyPrefix"}, "placementId": 12345}}}}`),
    62  			expectedResult: DealTierBidderMap{"unknown": {Prefix: "anyPrefix", MinDealTier: 5}},
    63  		},
    64  		{
    65  			description:    "imp.ext.prebid.bidder_one_but_not_found_in_the_adapter_bidder_list_with_case_insensitive",
    66  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"UnKnOwn": {"dealTier": {"minDealTier": 5, "prefix": "anyPrefix"}, "placementId": 12345}}}}`),
    67  			expectedResult: DealTierBidderMap{"UnKnOwn": {Prefix: "anyPrefix", MinDealTier: 5}},
    68  		},
    69  		{
    70  			description:    "imp.ext.prebid.bidder_one_but_case_is_different_from_the_adapter_bidder_list",
    71  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"APpNExUS": {"dealTier": {"minDealTier": 5, "prefix": "anyPrefix"}, "placementId": 12345}}}}`),
    72  			expectedResult: DealTierBidderMap{BidderAppnexus: {Prefix: "anyPrefix", MinDealTier: 5}},
    73  		},
    74  		{
    75  			description:    "imp.ext.prebid.bidder_one_with_other_params",
    76  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"appnexus": {"dealTier": {"minDealTier": 5, "prefix": "anyPrefix"}, "placementId": 12345}}, "supportdeals": true}, "tid": "1234"}`),
    77  			expectedResult: DealTierBidderMap{BidderAppnexus: {Prefix: "anyPrefix", MinDealTier: 5}},
    78  		},
    79  		{
    80  			description:    "imp.ext.prebid.bidder_multiple",
    81  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"appnexus": {"dealTier": {"minDealTier": 5, "prefix": "appnexusPrefix"}, "placementId": 12345}, "rubicon": {"dealTier": {"minDealTier": 8, "prefix": "rubiconPrefix"}, "placementId": 12345}}}}`),
    82  			expectedResult: DealTierBidderMap{BidderAppnexus: {Prefix: "appnexusPrefix", MinDealTier: 5}, BidderRubicon: {Prefix: "rubiconPrefix", MinDealTier: 8}},
    83  		},
    84  		{
    85  			description:    "imp.ext.prebid.bidder_one_without_deal_tier",
    86  			impExt:         json.RawMessage(`{"prebid": {"bidder": {"appnexus": {"placementId": 12345}}}}`),
    87  			expectedResult: DealTierBidderMap{},
    88  		},
    89  		{
    90  			description:       "imp.ext.prebid.bidder_error",
    91  			impExt:            json.RawMessage(`{"prebid": {"bidder": {"appnexus": {"dealTier": "wrong type", "placementId": 12345}}}}`),
    92  			expectedErrorType: &errortypes.FailedToUnmarshal{},
    93  		},
    94  	}
    95  
    96  	for _, test := range testCases {
    97  		t.Run(test.description, func(t *testing.T) {
    98  
    99  			imp := openrtb2.Imp{Ext: test.impExt}
   100  			result, err := ReadDealTiersFromImp(imp)
   101  
   102  			assert.Equal(t, test.expectedResult, result)
   103  
   104  			if test.expectedErrorType != nil {
   105  				assert.IsType(t, test.expectedErrorType, err)
   106  			} else {
   107  				assert.NoError(t, err)
   108  			}
   109  		})
   110  	}
   111  
   112  	t.Run("imp.ext.prebid.bidder_dedupe", func(t *testing.T) {
   113  		impExt := json.RawMessage(`{"prebid": {"bidder": {"APPNEXUS": {"dealTier": {"minDealTier": 100}},"APpNExUS": {"dealTier": {"minDealTier": 5}}}}}`)
   114  		imp := openrtb2.Imp{Ext: impExt}
   115  		result, err := ReadDealTiersFromImp(imp)
   116  
   117  		assert.Len(t, result, 1)
   118  		assert.NotNil(t, result["appnexus"])
   119  		assert.NoError(t, err)
   120  	})
   121  }