github.com/prebid/prebid-server/v2@v2.18.0/currency/validation_test.go (about)

     1  package currency
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/prebid-server/v2/errortypes"
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestValidateCustomRates(t *testing.T) {
    12  	boolTrue := true
    13  	boolFalse := false
    14  
    15  	testCases := []struct {
    16  		desc               string
    17  		inBidReqCurrencies *openrtb_ext.ExtRequestCurrency
    18  		outCurrencyError   error
    19  	}{
    20  		{
    21  			desc:               "nil input, no errors expected",
    22  			inBidReqCurrencies: nil,
    23  			outCurrencyError:   nil,
    24  		},
    25  		{
    26  			desc: "empty custom currency rates but UsePBSRates is set to false, we don't return error nor warning",
    27  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    28  				ConversionRates: map[string]map[string]float64{},
    29  				UsePBSRates:     &boolFalse,
    30  			},
    31  			outCurrencyError: nil,
    32  		},
    33  		{
    34  			desc: "empty custom currency rates but UsePBSRates is set to true, no need to return error because we can use PBS rates",
    35  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    36  				ConversionRates: map[string]map[string]float64{},
    37  				UsePBSRates:     &boolTrue,
    38  			},
    39  			outCurrencyError: nil,
    40  		},
    41  		{
    42  			desc: "UsePBSRates is nil and defaults to true, bidExt fromCurrency is invalid, expect bad input error",
    43  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    44  				ConversionRates: map[string]map[string]float64{
    45  					"FOO": {
    46  						"GBP": 1.2,
    47  						"MXN": 0.05,
    48  						"JPY": 0.95,
    49  					},
    50  				},
    51  			},
    52  			outCurrencyError: &errortypes.BadInput{Message: "currency code FOO is not recognized or malformed"},
    53  		},
    54  		{
    55  			desc: "UsePBSRates set to false, bidExt fromCurrency is invalid, expect bad input error",
    56  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    57  				ConversionRates: map[string]map[string]float64{
    58  					"FOO": {
    59  						"GBP": 1.2,
    60  						"MXN": 0.05,
    61  						"JPY": 0.95,
    62  					},
    63  				},
    64  				UsePBSRates: &boolFalse,
    65  			},
    66  			outCurrencyError: &errortypes.BadInput{Message: "currency code FOO is not recognized or malformed"},
    67  		},
    68  		{
    69  			desc: "UsePBSRates set to false, some of the bidExt 'to' Currencies are invalid, expect bad input error when parsing the first invalid currency code",
    70  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    71  				ConversionRates: map[string]map[string]float64{
    72  					"USD": {
    73  						"FOO": 10.0,
    74  						"MXN": 0.05,
    75  					},
    76  				},
    77  				UsePBSRates: &boolFalse,
    78  			},
    79  			outCurrencyError: &errortypes.BadInput{Message: "currency code FOO is not recognized or malformed"},
    80  		},
    81  		{
    82  			desc: "UsePBSRates set to false, some of the bidExt 'from' and 'to' currencies are invalid, expect bad input error when parsing the first invalid currency code",
    83  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    84  				ConversionRates: map[string]map[string]float64{
    85  					"FOO": {
    86  						"MXN": 0.05,
    87  						"CAD": 0.95,
    88  					},
    89  				},
    90  				UsePBSRates: &boolFalse,
    91  			},
    92  			outCurrencyError: &errortypes.BadInput{Message: "currency code FOO is not recognized or malformed"},
    93  		},
    94  		{
    95  			desc: "All 3-digit currency codes exist, expect no error",
    96  			inBidReqCurrencies: &openrtb_ext.ExtRequestCurrency{
    97  				ConversionRates: map[string]map[string]float64{
    98  					"USD": {
    99  						"MXN": 0.05,
   100  					},
   101  					"MXN": {
   102  						"JPY": 10.0,
   103  						"EUR": 10.95,
   104  					},
   105  				},
   106  				UsePBSRates: &boolFalse,
   107  			},
   108  		},
   109  	}
   110  
   111  	for _, tc := range testCases {
   112  		actualErr := ValidateCustomRates(tc.inBidReqCurrencies)
   113  
   114  		assert.Equal(t, tc.outCurrencyError, actualErr, tc.desc)
   115  	}
   116  }