github.com/prebid/prebid-server/v2@v2.18.0/adapters/infoawarebidder_test.go (about)

     1  package adapters_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/prebid/prebid-server/v2/adapters"
     9  	"github.com/prebid/prebid-server/v2/config"
    10  	"github.com/prebid/prebid-server/v2/errortypes"
    11  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestAppNotSupported(t *testing.T) {
    17  	bidder := &mockBidder{}
    18  	info := config.BidderInfo{
    19  		Capabilities: &config.CapabilitiesInfo{
    20  			Site: &config.PlatformInfo{
    21  				MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
    22  			},
    23  		},
    24  	}
    25  	constrained := adapters.BuildInfoAwareBidder(bidder, info)
    26  	bids, errs := constrained.MakeRequests(&openrtb2.BidRequest{
    27  		Imp: []openrtb2.Imp{{ID: "imp-1", Banner: &openrtb2.Banner{}}},
    28  		App: &openrtb2.App{},
    29  	}, &adapters.ExtraRequestInfo{})
    30  	if !assert.Len(t, errs, 1) {
    31  		return
    32  	}
    33  	assert.EqualError(t, errs[0], "this bidder does not support app requests")
    34  	assert.IsType(t, &errortypes.Warning{}, errs[0])
    35  	assert.Len(t, bids, 0)
    36  }
    37  
    38  func TestSiteNotSupported(t *testing.T) {
    39  	bidder := &mockBidder{}
    40  	info := config.BidderInfo{
    41  		Capabilities: &config.CapabilitiesInfo{
    42  			App: &config.PlatformInfo{
    43  				MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
    44  			},
    45  		},
    46  	}
    47  	constrained := adapters.BuildInfoAwareBidder(bidder, info)
    48  	bids, errs := constrained.MakeRequests(&openrtb2.BidRequest{
    49  		Imp:  []openrtb2.Imp{{ID: "imp-1", Banner: &openrtb2.Banner{}}},
    50  		Site: &openrtb2.Site{},
    51  	}, &adapters.ExtraRequestInfo{})
    52  	if !assert.Len(t, errs, 1) {
    53  		return
    54  	}
    55  	assert.EqualError(t, errs[0], "this bidder does not support site requests")
    56  	assert.IsType(t, &errortypes.Warning{}, errs[0])
    57  	assert.Len(t, bids, 0)
    58  }
    59  
    60  func TestDOOHNotSupported(t *testing.T) {
    61  	bidder := &mockBidder{}
    62  	info := config.BidderInfo{
    63  		Capabilities: &config.CapabilitiesInfo{
    64  			Site: &config.PlatformInfo{
    65  				MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
    66  			},
    67  		},
    68  	}
    69  	constrained := adapters.BuildInfoAwareBidder(bidder, info)
    70  	bids, errs := constrained.MakeRequests(&openrtb2.BidRequest{
    71  		Imp:  []openrtb2.Imp{{ID: "imp-1", Banner: &openrtb2.Banner{}}},
    72  		DOOH: &openrtb2.DOOH{},
    73  	}, &adapters.ExtraRequestInfo{})
    74  	require.Len(t, errs, 1)
    75  	assert.EqualError(t, errs[0], "this bidder does not support dooh requests")
    76  	assert.IsType(t, &errortypes.Warning{}, errs[0])
    77  	assert.Len(t, bids, 0)
    78  }
    79  
    80  func TestImpFiltering(t *testing.T) {
    81  	bidder := &mockBidder{}
    82  	info := config.BidderInfo{
    83  		Capabilities: &config.CapabilitiesInfo{
    84  			Site: &config.PlatformInfo{
    85  				MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeVideo},
    86  			},
    87  			App: &config.PlatformInfo{
    88  				MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeBanner},
    89  			},
    90  			DOOH: &config.PlatformInfo{
    91  				MediaTypes: []openrtb_ext.BidType{openrtb_ext.BidTypeNative},
    92  			},
    93  		},
    94  	}
    95  
    96  	constrained := adapters.BuildInfoAwareBidder(bidder, info)
    97  
    98  	testCases := []struct {
    99  		description    string
   100  		inBidRequest   *openrtb2.BidRequest
   101  		expectedErrors []error
   102  		expectedImpLen int
   103  	}{
   104  		{
   105  			description: "Empty Imp array. MakeRequest() call not expected",
   106  			inBidRequest: &openrtb2.BidRequest{
   107  				Imp:  []openrtb2.Imp{},
   108  				Site: &openrtb2.Site{},
   109  			},
   110  			expectedErrors: []error{
   111  				&errortypes.BadInput{Message: "Bid request didn't contain media types supported by the bidder"},
   112  			},
   113  			expectedImpLen: 0,
   114  		},
   115  		{
   116  			description: "Sole imp in bid request is of wrong media type. MakeRequest() call not expected",
   117  			inBidRequest: &openrtb2.BidRequest{
   118  				Imp: []openrtb2.Imp{{ID: "imp-1", Video: &openrtb2.Video{}}},
   119  				App: &openrtb2.App{},
   120  			},
   121  			expectedErrors: []error{
   122  				&errortypes.BadInput{Message: "request.imp[0] uses video, but this bidder doesn't support it"},
   123  				&errortypes.BadInput{Message: "Bid request didn't contain media types supported by the bidder"},
   124  			},
   125  			expectedImpLen: 0,
   126  		},
   127  		{
   128  			description: "All imps in bid request of wrong media type, MakeRequest() call not expected",
   129  			inBidRequest: &openrtb2.BidRequest{
   130  				Imp: []openrtb2.Imp{
   131  					{ID: "imp-1", Video: &openrtb2.Video{}},
   132  					{ID: "imp-2", Native: &openrtb2.Native{}},
   133  					{ID: "imp-3", Audio: &openrtb2.Audio{}},
   134  				},
   135  				App: &openrtb2.App{},
   136  			},
   137  			expectedErrors: []error{
   138  				&errortypes.BadInput{Message: "request.imp[0] uses video, but this bidder doesn't support it"},
   139  				&errortypes.BadInput{Message: "request.imp[1] uses native, but this bidder doesn't support it"},
   140  				&errortypes.BadInput{Message: "request.imp[2] uses audio, but this bidder doesn't support it"},
   141  				&errortypes.BadInput{Message: "Bid request didn't contain media types supported by the bidder"},
   142  			},
   143  			expectedImpLen: 0,
   144  		},
   145  		{
   146  			description: "Some imps with correct media type, MakeRequest() call expected",
   147  			inBidRequest: &openrtb2.BidRequest{
   148  				Imp: []openrtb2.Imp{
   149  					{
   150  						ID:    "imp-1",
   151  						Video: &openrtb2.Video{},
   152  					},
   153  					{
   154  						Native: &openrtb2.Native{},
   155  					},
   156  					{
   157  						ID:     "imp-2",
   158  						Video:  &openrtb2.Video{},
   159  						Native: &openrtb2.Native{},
   160  					},
   161  					{
   162  						Banner: &openrtb2.Banner{},
   163  					},
   164  				},
   165  				Site: &openrtb2.Site{},
   166  			},
   167  			expectedErrors: []error{
   168  				&errortypes.BadInput{Message: "request.imp[1] uses native, but this bidder doesn't support it"},
   169  				&errortypes.BadInput{Message: "request.imp[2] uses native, but this bidder doesn't support it"},
   170  				&errortypes.BadInput{Message: "request.imp[3] uses banner, but this bidder doesn't support it"},
   171  				&errortypes.BadInput{Message: "request.imp[1] has no supported MediaTypes. It will be ignored"},
   172  				&errortypes.BadInput{Message: "request.imp[3] has no supported MediaTypes. It will be ignored"},
   173  			},
   174  			expectedImpLen: 2,
   175  		},
   176  		{
   177  			description: "All imps with correct media type, MakeRequest() call expected",
   178  			inBidRequest: &openrtb2.BidRequest{
   179  				Imp: []openrtb2.Imp{
   180  					{ID: "imp-1", Native: &openrtb2.Native{}},
   181  					{ID: "imp-2", Native: &openrtb2.Native{}},
   182  				},
   183  				DOOH: &openrtb2.DOOH{},
   184  			},
   185  			expectedErrors: nil,
   186  			expectedImpLen: 2,
   187  		},
   188  	}
   189  
   190  	for _, test := range testCases {
   191  		actualAdapterRequests, actualErrs := constrained.MakeRequests(test.inBidRequest, &adapters.ExtraRequestInfo{})
   192  
   193  		// Assert the request.Imp slice was correctly filtered and if MakeRequest() was called by asserting
   194  		// the corresponding error messages were returned
   195  		for i, expectedErr := range test.expectedErrors {
   196  			assert.EqualError(t, expectedErr, actualErrs[i].Error(), "Test failed. Error[%d] in error list mismatch: %s", i, test.description)
   197  		}
   198  
   199  		// Extra MakeRequests() call check: our mockBidder returns an adapter request for every imp
   200  		assert.Len(t, actualAdapterRequests, test.expectedImpLen, "Test failed. Incorrect length of filtered imps: %s", test.description)
   201  	}
   202  }
   203  
   204  type mockBidder struct {
   205  }
   206  
   207  func (m *mockBidder) MakeRequests(request *openrtb2.BidRequest, reqInfo *adapters.ExtraRequestInfo) ([]*adapters.RequestData, []error) {
   208  	var adapterRequests []*adapters.RequestData
   209  
   210  	for i := 0; i < len(request.Imp); i++ {
   211  		adapterRequests = append(adapterRequests, &adapters.RequestData{})
   212  	}
   213  
   214  	return adapterRequests, nil
   215  }
   216  
   217  func (m *mockBidder) MakeBids(internalRequest *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
   218  	return nil, []error{errors.New("mock MakeBids error")}
   219  }