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