github.com/prebid/prebid-server@v0.275.0/adapters/infoawarebidder_test.go (about)

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