github.com/prebid/prebid-server/v2@v2.18.0/adapters/orbidder/orbidder_test.go (about) 1 package orbidder 2 3 import ( 4 "encoding/json" 5 "errors" 6 "testing" 7 8 "github.com/prebid/openrtb/v20/openrtb2" 9 "github.com/stretchr/testify/mock" 10 11 "github.com/prebid/prebid-server/v2/adapters" 12 "github.com/prebid/prebid-server/v2/adapters/adapterstest" 13 "github.com/prebid/prebid-server/v2/config" 14 "github.com/prebid/prebid-server/v2/openrtb_ext" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestUnmarshalOrbidderExtImp(t *testing.T) { 19 ext := json.RawMessage(`{"accountId":"orbidder-test", "placementId":"center-banner", "bidfloor": 0.1}`) 20 impExt := new(openrtb_ext.ExtImpOrbidder) 21 22 assert.NoError(t, json.Unmarshal(ext, impExt)) 23 assert.Equal(t, &openrtb_ext.ExtImpOrbidder{ 24 AccountId: "orbidder-test", 25 PlacementId: "center-banner", 26 BidFloor: 0.1, 27 }, impExt) 28 } 29 30 func TestPreprocessExtensions(t *testing.T) { 31 for name, tc := range testCasesExtension { 32 t.Run(name, func(t *testing.T) { 33 imp := tc.imp 34 err := preprocessExtensions(&imp) 35 tc.assertError(t, err) 36 }) 37 } 38 } 39 40 func TestJsonSamples(t *testing.T) { 41 bidder, buildErr := Builder(openrtb_ext.BidderOrbidder, config.Adapter{ 42 Endpoint: "https://orbidder-test"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"}) 43 44 if buildErr != nil { 45 t.Fatalf("Builder returned unexpected error %v", buildErr) 46 } 47 48 adapterstest.RunJSONBidderTest(t, "orbiddertest", bidder) 49 } 50 51 var testCasesCurrency = map[string]struct { 52 imp openrtb2.Imp 53 setMock func(m *mock.Mock) 54 expectedImp openrtb2.Imp 55 assertError func(t assert.TestingT, err error, msgAndArgs ...interface{}) bool 56 }{ 57 "EUR: no bidfloor, no currency": { 58 imp: openrtb2.Imp{ 59 BidFloor: 0, 60 BidFloorCur: "", 61 }, 62 setMock: func(m *mock.Mock) {}, 63 expectedImp: openrtb2.Imp{ 64 BidFloor: 0, 65 BidFloorCur: "EUR", 66 }, 67 assertError: assert.NoError, 68 }, 69 "EUR: bidfloor, no currency": { 70 imp: openrtb2.Imp{ 71 BidFloor: 1, 72 BidFloorCur: "", 73 }, 74 setMock: func(m *mock.Mock) {}, 75 expectedImp: openrtb2.Imp{ 76 BidFloor: 1, 77 BidFloorCur: "EUR", 78 }, 79 assertError: assert.NoError, 80 }, 81 "EUR: bidfloor and currency": { 82 imp: openrtb2.Imp{ 83 BidFloor: 1, 84 BidFloorCur: "EUR", 85 }, 86 setMock: func(m *mock.Mock) {}, 87 expectedImp: openrtb2.Imp{ 88 BidFloor: 1, 89 BidFloorCur: "EUR", 90 }, 91 assertError: assert.NoError, 92 }, 93 "USD: bidfloor with currency": { 94 imp: openrtb2.Imp{ 95 BidFloor: 1, 96 BidFloorCur: "USD", 97 }, 98 setMock: func(m *mock.Mock) { 99 m.On("GetRate", "USD", "EUR").Return(2.5, nil) 100 }, 101 expectedImp: openrtb2.Imp{ 102 BidFloor: 2.5, 103 BidFloorCur: "EUR", 104 }, 105 assertError: assert.NoError, 106 }, 107 "USD: no bidfloor": { 108 imp: openrtb2.Imp{ 109 BidFloor: 0, 110 BidFloorCur: "USD", 111 }, 112 setMock: func(m *mock.Mock) {}, 113 expectedImp: openrtb2.Imp{ 114 BidFloor: 0, 115 BidFloorCur: "EUR", 116 }, 117 assertError: assert.NoError, 118 }, 119 "ABC: invalid currency code": { 120 imp: openrtb2.Imp{ 121 BidFloor: 1, 122 BidFloorCur: "ABC", 123 }, 124 setMock: func(m *mock.Mock) { 125 m.On("GetRate", "ABC", "EUR").Return(0.0, errors.New("currency conversion error")) 126 }, 127 expectedImp: openrtb2.Imp{ 128 BidFloor: 1, 129 BidFloorCur: "ABC", 130 }, 131 assertError: assert.Error, 132 }, 133 } 134 135 var testCasesExtension = map[string]struct { 136 imp openrtb2.Imp 137 assertError func(t assert.TestingT, err error, msgAndArgs ...interface{}) bool 138 }{ 139 "Valid Orbidder Extension": { 140 imp: openrtb2.Imp{ 141 Ext: json.RawMessage(`{"bidder":{"accountId":"orbidder-test", "placementId":"center-banner", "bidfloor": 0.1}}`), 142 }, 143 assertError: assert.NoError, 144 }, 145 "Invalid Orbidder Extension": { 146 imp: openrtb2.Imp{ 147 Ext: json.RawMessage(`{"there's'":{"something":"strange", "in the":"neighbourhood", "who you gonna call?": 0.1}}`), 148 }, 149 assertError: assert.Error, 150 }, 151 } 152 153 func TestPreprocessBidFloorCurrency(t *testing.T) { 154 for name, tc := range testCasesCurrency { 155 t.Run(name, func(t *testing.T) { 156 imp := tc.imp 157 mockConversions := &mockCurrencyConversion{} 158 tc.setMock(&mockConversions.Mock) 159 extraRequestInfo := adapters.ExtraRequestInfo{ 160 CurrencyConversions: mockConversions, 161 } 162 err := preprocessBidFloorCurrency(&imp, &extraRequestInfo) 163 assert.True(t, mockConversions.AssertExpectations(t)) 164 tc.assertError(t, err) 165 assert.Equal(t, tc.expectedImp, imp) 166 }) 167 } 168 } 169 170 type mockCurrencyConversion struct { 171 mock.Mock 172 } 173 174 func (m *mockCurrencyConversion) GetRate(from string, to string) (float64, error) { 175 args := m.Called(from, to) 176 return args.Get(0).(float64), args.Error(1) 177 } 178 179 func (m *mockCurrencyConversion) GetRates() *map[string]map[string]float64 { 180 args := m.Called() 181 return args.Get(0).(*map[string]map[string]float64) 182 }