forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbidders_test.go
284 lines (262 loc) · 7.09 KB
/
bidders_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package openrtb_ext
import (
"encoding/json"
"errors"
"os"
"strings"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
)
func TestBidderParamValidatorValidate(t *testing.T) {
testSchemaLoader := gojsonschema.NewStringLoader(`{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Test Params",
"description": "Test Description",
"type": "object",
"properties": {
"placementId": {
"type": "integer",
"description": "An ID which identifies this placement of the impression."
},
"optionalText": {
"type": "string",
"description": "Optional text for testing."
}
},
"required": ["placementId"]
}`)
testSchema, err := gojsonschema.NewSchema(testSchemaLoader)
if !assert.NoError(t, err) {
t.FailNow()
}
testBidderName := BidderName("foo")
testValidator := bidderParamValidator{
parsedSchemas: map[BidderName]*gojsonschema.Schema{
testBidderName: testSchema,
},
}
testCases := []struct {
description string
ext json.RawMessage
expectedError string
}{
{
description: "Valid",
ext: json.RawMessage(`{"placementId":123}`),
expectedError: "",
},
{
description: "Invalid - Wrong Type",
ext: json.RawMessage(`{"placementId":"stringInsteadOfInt"}`),
expectedError: "placementId: Invalid type. Expected: integer, given: string",
},
{
description: "Invalid - Empty Object",
ext: json.RawMessage(`{}`),
expectedError: "(root): placementId is required",
},
{
description: "Malformed",
ext: json.RawMessage(`malformedJSON`),
expectedError: "invalid character 'm' looking for beginning of value",
},
}
for _, test := range testCases {
err := testValidator.Validate(testBidderName, test.ext)
if test.expectedError == "" {
assert.NoError(t, err, test.description)
} else {
assert.EqualError(t, err, test.expectedError, test.description)
}
}
}
func TestBidderParamValidatorSchema(t *testing.T) {
testValidator := bidderParamValidator{
schemaContents: map[BidderName]string{
BidderName("foo"): "foo content",
BidderName("bar"): "bar content",
},
}
result := testValidator.Schema(BidderName("bar"))
assert.Equal(t, "bar content", result)
}
func TestIsBidderNameReserved(t *testing.T) {
testCases := []struct {
bidder string
expected bool
}{
{"all", true},
{"aLl", true},
{"ALL", true},
{"context", true},
{"CONTEXT", true},
{"conTExt", true},
{"data", true},
{"DATA", true},
{"DaTa", true},
{"general", true},
{"gEnErAl", true},
{"GENERAL", true},
{"gpid", true},
{"GPID", true},
{"GPid", true},
{"prebid", true},
{"PREbid", true},
{"PREBID", true},
{"skadn", true},
{"skADN", true},
{"SKADN", true},
{"tid", true},
{"TId", true},
{"Tid", true},
{"TiD", true},
{"notreserved", false},
}
for _, test := range testCases {
result := IsBidderNameReserved(test.bidder)
assert.Equal(t, test.expected, result, test.bidder)
}
}
func TestSetAliasBidderName(t *testing.T) {
parentBidder := BidderName("pBidder")
existingCoreBidderNames := coreBidderNames
testCases := []struct {
aliasBidderName string
err error
}{
{"aBidder", nil},
{"all", errors.New("alias all is a reserved bidder name and cannot be used")},
}
for _, test := range testCases {
err := SetAliasBidderName(test.aliasBidderName, parentBidder)
if err != nil {
assert.Equal(t, test.err, err)
} else {
assert.Contains(t, CoreBidderNames(), BidderName(test.aliasBidderName))
assert.Contains(t, aliasBidderToParent, BidderName(test.aliasBidderName))
assert.Contains(t, bidderNameLookup, strings.ToLower(test.aliasBidderName))
}
}
//reset package variables to not interfere with other test cases. Example - TestBidderParamSchemas
coreBidderNames = existingCoreBidderNames
aliasBidderToParent = map[BidderName]BidderName{}
}
type mockParamsHelper struct {
fs fstest.MapFS
absFilePath string
absPathErr error
schemaLoaderErr error
readFileErr error
}
func (m *mockParamsHelper) readDir(name string) ([]os.DirEntry, error) {
return m.fs.ReadDir(name)
}
func (m *mockParamsHelper) readFile(name string) ([]byte, error) {
if m.readFileErr != nil {
return nil, m.readFileErr
}
return m.fs.ReadFile(name)
}
func (m *mockParamsHelper) newReferenceLoader(source string) gojsonschema.JSONLoader {
return nil
}
func (m *mockParamsHelper) newSchema(l gojsonschema.JSONLoader) (*gojsonschema.Schema, error) {
return nil, m.schemaLoaderErr
}
func (m *mockParamsHelper) abs(path string) (string, error) {
return m.absFilePath, m.absPathErr
}
func TestNewBidderParamsValidator(t *testing.T) {
testCases := []struct {
description string
paramsValidator mockParamsHelper
dir string
expectedErr error
}{
{
description: "Valid case",
paramsValidator: mockParamsHelper{
fs: fstest.MapFS{
"test/appnexus.json": {
Data: []byte("{}"),
},
},
},
dir: "test",
},
{
description: "failed to read directory",
paramsValidator: mockParamsHelper{},
dir: "t",
expectedErr: errors.New("Failed to read JSON schemas from directory t. open t: file does not exist"),
},
{
description: "file name does not match the bidder name",
paramsValidator: mockParamsHelper{
fs: fstest.MapFS{
"test/anyBidder.json": {
Data: []byte("{}"),
},
},
},
dir: "test",
expectedErr: errors.New("File test/anyBidder.json does not match a valid BidderName."),
},
{
description: "abs file path error",
paramsValidator: mockParamsHelper{
fs: fstest.MapFS{
"test/appnexus.json": {
Data: []byte("{}"),
},
},
absFilePath: "test/app.json",
absPathErr: errors.New("any abs error"),
},
dir: "test",
expectedErr: errors.New("Failed to get an absolute representation of the path: test/app.json, any abs error"),
},
{
description: "schema loader error",
paramsValidator: mockParamsHelper{
fs: fstest.MapFS{
"test/appnexus.json": {
Data: []byte("{}"),
},
},
schemaLoaderErr: errors.New("any schema loader error"),
},
dir: "test",
expectedErr: errors.New("Failed to load json schema at : any schema loader error"),
},
{
description: "read file error",
paramsValidator: mockParamsHelper{
fs: fstest.MapFS{
"test/appnexus.json": {
Data: []byte("{}"),
},
},
readFileErr: errors.New("any read file error"),
},
dir: "test",
expectedErr: errors.New("Failed to read file test/appnexus.json: any read file error"),
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
aliasBidderToParent = map[BidderName]BidderName{"rubicon": "appnexus"}
paramsValidator = &test.paramsValidator
bidderValidator, err := NewBidderParamsValidator(test.dir)
if test.expectedErr == nil {
assert.NoError(t, err)
assert.Contains(t, bidderValidator.Schema("appnexus"), "{}")
assert.Contains(t, bidderValidator.Schema("rubicon"), "{}")
} else {
assert.Equal(t, err, test.expectedErr)
}
})
}
}