github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/bidders_validate_test.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/xeipuuv/gojsonschema"
     9  )
    10  
    11  // TestMain does the expensive setup so we don't keep re-reading the files in static/bidder-params for each test.
    12  func TestMain(m *testing.M) {
    13  	bidderParams, err := NewBidderParamsValidator("../static/bidder-params")
    14  	if err != nil {
    15  		os.Exit(1)
    16  	}
    17  	validator = bidderParams
    18  	os.Exit(m.Run())
    19  }
    20  
    21  var validator BidderParamValidator
    22  
    23  // TestBidderParamSchemas makes sure that the validator.Schema() function
    24  // returns valid JSON for all known CoreBidderNames.
    25  func TestBidderParamSchemas(t *testing.T) {
    26  	for _, bidderName := range CoreBidderNames() {
    27  		schema := validator.Schema(bidderName)
    28  		if schema == "" {
    29  			t.Errorf("No schema exists for bidder %s. Does static/bidder-params/%s.json exist?", bidderName, bidderName)
    30  		}
    31  
    32  		if _, err := gojsonschema.NewBytesLoader([]byte(schema)).LoadJSON(); err != nil {
    33  			t.Errorf("static/bidder-params/%s.json does not have a valid json-schema. %v", bidderName, err)
    34  		}
    35  	}
    36  }
    37  
    38  func TestBidderNamesValid(t *testing.T) {
    39  	for _, bidder := range CoreBidderNames() {
    40  		isReserved := IsBidderNameReserved(string(bidder))
    41  		assert.False(t, isReserved, "bidder %v conflicts with a reserved name", bidder)
    42  	}
    43  }
    44  
    45  // TestBidderUniquenessGatekeeping acts as a gatekeeper of bidder name uniqueness. If this test fails
    46  // when you're building a new adapter, please consider choosing a different bidder name to maintain the
    47  // current uniqueness threshold, or else start a discussion in the PR.
    48  func TestBidderUniquenessGatekeeping(t *testing.T) {
    49  	// Get List Of Bidders
    50  	// - Exclude duplicates of adapters for the same bidder, as it's unlikely a publisher will use both.
    51  	var bidders []string
    52  	for _, bidder := range CoreBidderNames() {
    53  		if bidder != BidderSilverPush && bidder != BidderTripleliftNative && bidder != BidderAdkernelAdn && bidder != "freewheel-ssp" && bidder != "yahooAdvertising" {
    54  			bidders = append(bidders, string(bidder))
    55  		}
    56  	}
    57  
    58  	currentThreshold := 6
    59  	measuredThreshold := minUniquePrefixLength(bidders)
    60  
    61  	assert.NotZero(t, measuredThreshold, "BidderMap contains duplicate bidder name values.")
    62  	assert.LessOrEqual(t, measuredThreshold, currentThreshold)
    63  }
    64  
    65  // minUniquePrefixLength measures the minimun amount of characters needed to uniquely identify
    66  // one of the strings, or returns 0 if there are duplicates.
    67  func minUniquePrefixLength(b []string) int {
    68  	targetingKeyMaxLength := 20
    69  	for prefixLength := 1; prefixLength <= targetingKeyMaxLength; prefixLength++ {
    70  		if uniqueForPrefixLength(b, prefixLength) {
    71  			return prefixLength
    72  		}
    73  	}
    74  	return 0
    75  }
    76  
    77  func uniqueForPrefixLength(b []string, prefixLength int) bool {
    78  	m := make(map[string]struct{})
    79  
    80  	if prefixLength <= 0 {
    81  		return false
    82  	}
    83  
    84  	for i, n := range b {
    85  		ns := string(n)
    86  
    87  		if len(ns) > prefixLength {
    88  			ns = ns[0:prefixLength]
    89  		}
    90  
    91  		m[ns] = struct{}{}
    92  
    93  		if len(m) != i+1 {
    94  			return false
    95  		}
    96  	}
    97  
    98  	return true
    99  }