github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/alternatebiddercodes_test.go (about) 1 package openrtb_ext 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestAlternateBidderCodes_IsValidBidderCode(t *testing.T) { 11 type fields struct { 12 Enabled bool 13 Bidders map[string]ExtAdapterAlternateBidderCodes 14 } 15 type args struct { 16 bidder string 17 alternateBidder string 18 } 19 tests := []struct { 20 name string 21 fields fields 22 args args 23 wantIsValid bool 24 wantErr error 25 }{ 26 { 27 name: "alternateBidder is not set/blank (default non-extra bid case)", 28 wantIsValid: true, 29 }, 30 { 31 name: "alternateBidder and bidder are same (default non-extra bid case with seat's alternateBidder explicitly set)", 32 args: args{ 33 bidder: "pubmatic", 34 alternateBidder: "pubmatic", 35 }, 36 wantIsValid: true, 37 }, 38 { 39 name: "alternateBidder and bidder are the same under Unicode case-folding (default non-extra bid case with seat's alternateBidder explicitly set)", 40 args: args{ 41 bidder: "pubmatic", 42 alternateBidder: "pubmatic", 43 }, 44 wantIsValid: true, 45 }, 46 { 47 name: "account.alternatebiddercodes config not defined (default, reject bid)", 48 args: args{ 49 bidder: "pubmatic", 50 alternateBidder: "groupm", 51 }, 52 wantIsValid: false, 53 wantErr: errors.New(`alternateBidderCodes disabled for "pubmatic", rejecting bids for "groupm"`), 54 }, 55 { 56 name: "account.alternatebiddercodes config enabled but adapter config not defined", 57 args: args{ 58 bidder: "pubmatic", 59 alternateBidder: "groupm", 60 }, 61 fields: fields{Enabled: true}, 62 wantIsValid: false, 63 wantErr: errors.New(`alternateBidderCodes not defined for adapter "pubmatic", rejecting bids for "groupm"`), 64 }, 65 { 66 name: "account.alternatebiddercodes config enabled but adapter config is not available", 67 args: args{ 68 bidder: "pubmatic", 69 alternateBidder: "groupm", 70 }, 71 fields: fields{ 72 Enabled: true, 73 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 74 "appnexus": {}, 75 }, 76 }, 77 wantIsValid: false, 78 wantErr: errors.New(`alternateBidderCodes not defined for adapter "pubmatic", rejecting bids for "groupm"`), 79 }, 80 { 81 name: "account.alternatebiddercodes config enabled but adapter config is disabled", 82 args: args{ 83 bidder: "pubmatic", 84 alternateBidder: "groupm", 85 }, 86 fields: fields{ 87 Enabled: true, 88 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 89 "pubmatic": {Enabled: false}, 90 }, 91 }, 92 wantIsValid: false, 93 wantErr: errors.New(`alternateBidderCodes disabled for "pubmatic", rejecting bids for "groupm"`), 94 }, 95 { 96 name: "account.alternatebiddercodes and adapter config enabled but adapter config does not have allowedBidderCodes defined", 97 args: args{ 98 bidder: "pubmatic", 99 alternateBidder: "groupm", 100 }, 101 fields: fields{ 102 Enabled: true, 103 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 104 "pubmatic": {Enabled: true}, 105 }, 106 }, 107 wantIsValid: true, 108 }, 109 { 110 name: "bidder is different in casing than the entry in account.alternatebiddercodes but they match because our case insensitive comparison", 111 args: args{ 112 bidder: "PUBmatic", 113 alternateBidder: "groupm", 114 }, 115 fields: fields{ 116 Enabled: true, 117 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 118 "pubmatic": {Enabled: true}, 119 }, 120 }, 121 wantIsValid: true, 122 }, 123 { 124 name: "allowedBidderCodes is *", 125 args: args{ 126 bidder: "pubmatic", 127 alternateBidder: "groupm", 128 }, 129 fields: fields{ 130 Enabled: true, 131 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 132 "pubmatic": { 133 Enabled: true, 134 AllowedBidderCodes: []string{"*"}, 135 }, 136 }, 137 }, 138 wantIsValid: true, 139 }, 140 { 141 name: "allowedBidderCodes is in the list", 142 args: args{ 143 bidder: "pubmatic", 144 alternateBidder: "groupm", 145 }, 146 fields: fields{ 147 Enabled: true, 148 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 149 "pubmatic": { 150 Enabled: true, 151 AllowedBidderCodes: []string{"groupm"}, 152 }, 153 }, 154 }, 155 wantIsValid: true, 156 }, 157 { 158 name: "allowedBidderCodes is not in the list", 159 args: args{ 160 bidder: "pubmatic", 161 alternateBidder: "groupm", 162 }, 163 fields: fields{ 164 Enabled: true, 165 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 166 "pubmatic": { 167 Enabled: true, 168 AllowedBidderCodes: []string{"xyz"}, 169 }, 170 }, 171 }, 172 wantIsValid: false, 173 wantErr: errors.New(`invalid biddercode "groupm" sent by adapter "pubmatic"`), 174 }, 175 { 176 name: "account.alternatebiddercodes and adapter config enabled but adapter config has allowedBidderCodes list empty", 177 args: args{ 178 bidder: "pubmatic", 179 alternateBidder: "groupm", 180 }, 181 fields: fields{ 182 Enabled: true, 183 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 184 "pubmatic": {Enabled: true, AllowedBidderCodes: []string{}}, 185 }, 186 }, 187 wantIsValid: false, 188 wantErr: errors.New(`invalid biddercode "groupm" sent by adapter "pubmatic"`), 189 }, 190 } 191 for _, tt := range tests { 192 t.Run(tt.name, func(t *testing.T) { 193 a := &ExtAlternateBidderCodes{ 194 Enabled: tt.fields.Enabled, 195 Bidders: tt.fields.Bidders, 196 } 197 gotIsValid, gotErr := a.IsValidBidderCode(tt.args.bidder, tt.args.alternateBidder) 198 assert.Equal(t, tt.wantIsValid, gotIsValid) 199 assert.Equal(t, tt.wantErr, gotErr) 200 }) 201 } 202 } 203 204 func TestIsBidderInAlternateBidderCodes(t *testing.T) { 205 type testInput struct { 206 bidder string 207 bidderCodes *ExtAlternateBidderCodes 208 } 209 type testOutput struct { 210 adapterCfg ExtAdapterAlternateBidderCodes 211 found bool 212 } 213 testCases := []struct { 214 desc string 215 in testInput 216 expected testOutput 217 }{ 218 { 219 desc: "empty bidder", 220 in: testInput{ 221 bidderCodes: &ExtAlternateBidderCodes{}, 222 }, 223 expected: testOutput{ 224 adapterCfg: ExtAdapterAlternateBidderCodes{}, 225 found: false, 226 }, 227 }, 228 { 229 desc: "nil ExtAlternateBidderCodes", 230 in: testInput{ 231 bidder: "appnexus", 232 bidderCodes: nil, 233 }, 234 expected: testOutput{ 235 adapterCfg: ExtAdapterAlternateBidderCodes{}, 236 found: false, 237 }, 238 }, 239 { 240 desc: "nil ExtAlternateBidderCodes.Bidder map", 241 in: testInput{ 242 bidder: "appnexus", 243 bidderCodes: &ExtAlternateBidderCodes{}, 244 }, 245 expected: testOutput{ 246 adapterCfg: ExtAdapterAlternateBidderCodes{}, 247 found: false, 248 }, 249 }, 250 { 251 desc: "nil ExtAlternateBidderCodes.Bidder map", 252 in: testInput{ 253 bidder: "appnexus", 254 bidderCodes: &ExtAlternateBidderCodes{ 255 Bidders: nil, 256 }, 257 }, 258 expected: testOutput{ 259 adapterCfg: ExtAdapterAlternateBidderCodes{}, 260 found: false, 261 }, 262 }, 263 { 264 desc: "bidder arg identical to entry in Bidders map", 265 in: testInput{ 266 bidder: "appnexus", 267 bidderCodes: &ExtAlternateBidderCodes{ 268 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 269 "appnexus": { 270 Enabled: true, 271 AllowedBidderCodes: []string{"abcCode"}, 272 }, 273 }, 274 }, 275 }, 276 expected: testOutput{ 277 adapterCfg: ExtAdapterAlternateBidderCodes{ 278 Enabled: true, 279 AllowedBidderCodes: []string{"abcCode"}, 280 }, 281 found: true, 282 }, 283 }, 284 { 285 desc: "bidder arg matches an entry in Bidders map with case insensitive comparisson", 286 in: testInput{ 287 bidder: "appnexus", 288 bidderCodes: &ExtAlternateBidderCodes{ 289 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 290 "AppNexus": {AllowedBidderCodes: []string{"adnxsCode"}}, 291 "PubMatic": {AllowedBidderCodes: []string{"pubCode"}}, 292 "Rubicon": {AllowedBidderCodes: []string{"rCode"}}, 293 }, 294 }, 295 }, 296 expected: testOutput{ 297 adapterCfg: ExtAdapterAlternateBidderCodes{ 298 AllowedBidderCodes: []string{"adnxsCode"}, 299 }, 300 found: true, 301 }, 302 }, 303 { 304 desc: "bidder arg doesn't match any entry in map", 305 in: testInput{ 306 bidder: "unknown", 307 bidderCodes: &ExtAlternateBidderCodes{ 308 Bidders: map[string]ExtAdapterAlternateBidderCodes{ 309 "AppNexus": {AllowedBidderCodes: []string{"adnxsCode"}}, 310 "PubMatic": {AllowedBidderCodes: []string{"pubCode"}}, 311 "Rubicon": {AllowedBidderCodes: []string{"rCode"}}, 312 }, 313 }, 314 }, 315 expected: testOutput{ 316 adapterCfg: ExtAdapterAlternateBidderCodes{}, 317 found: false, 318 }, 319 }, 320 } 321 for _, tc := range testCases { 322 t.Run(tc.desc, func(t *testing.T) { 323 adapterCfg, found := tc.in.bidderCodes.IsBidderInAlternateBidderCodes(tc.in.bidder) 324 assert.Equal(t, tc.expected.adapterCfg, adapterCfg) 325 assert.Equal(t, tc.expected.found, found) 326 }) 327 } 328 }