forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendorlist-fetching_test.go
290 lines (257 loc) · 8.39 KB
/
vendorlist-fetching_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
285
286
287
288
289
290
package gdpr
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/prebid/prebid-server/config"
)
func TestVendorFetch(t *testing.T) {
vendorListOne := mockVendorListData(t, 1, map[uint16]*purposes{
32: {
purposes: []int{1, 2},
},
})
vendorListTwo := mockVendorListData(t, 2, map[uint16]*purposes{
32: {
purposes: []int{1, 2, 3},
},
})
server := httptest.NewServer(http.HandlerFunc(mockServer(2, map[int]string{
1: vendorListOne,
2: vendorListTwo,
})))
defer server.Close()
fetcher := newVendorListFetcher(context.Background(), testConfig(), server.Client(), testURLMaker(server), 1)
list, err := fetcher(context.Background(), 1)
assertNilErr(t, err)
vendor := list.Vendor(32)
assertBoolsEqual(t, true, vendor.Purpose(1))
assertBoolsEqual(t, false, vendor.Purpose(3))
assertBoolsEqual(t, false, vendor.Purpose(4))
list, err = fetcher(context.Background(), 2)
assertNilErr(t, err)
vendor = list.Vendor(32)
assertBoolsEqual(t, true, vendor.Purpose(1))
assertBoolsEqual(t, true, vendor.Purpose(3))
}
func TestLazyFetch(t *testing.T) {
firstVendorList := mockVendorListData(t, 1, map[uint16]*purposes{
32: {
purposes: []int{1, 2},
},
})
secondVendorList := mockVendorListData(t, 2, map[uint16]*purposes{
3: {
purposes: []int{1},
},
})
server := httptest.NewServer(http.HandlerFunc(mockServer(1, map[int]string{
1: firstVendorList,
2: secondVendorList,
})))
defer server.Close()
fetcher := newVendorListFetcher(context.Background(), testConfig(), server.Client(), testURLMaker(server), 1)
list, err := fetcher(context.Background(), 2)
assertNilErr(t, err)
vendor := list.Vendor(3)
assertBoolsEqual(t, true, vendor.Purpose(1))
assertBoolsEqual(t, false, vendor.Purpose(2))
}
func TestInitialTimeout(t *testing.T) {
list := mockVendorListData(t, 1, map[uint16]*purposes{
32: {
purposes: []int{1, 2},
},
})
server := httptest.NewServer(http.HandlerFunc(mockServer(1, map[int]string{
1: list,
})))
defer server.Close()
ctx, cancel := context.WithDeadline(context.Background(), time.Time{})
defer cancel()
fetcher := newVendorListFetcher(ctx, testConfig(), server.Client(), testURLMaker(server), 1)
_, err := fetcher(context.Background(), 1) // This should do a lazy fetch, even though the initial call failed
assertNilErr(t, err)
}
func TestFetchThrottling(t *testing.T) {
vendorListTwo := mockVendorListData(t, 2, map[uint16]*purposes{
32: {
purposes: []int{1, 2},
},
})
vendorListThree := mockVendorListData(t, 3, map[uint16]*purposes{
32: {
purposes: []int{1, 2},
},
})
server := httptest.NewServer(http.HandlerFunc(mockServer(1, map[int]string{
1: "{}",
2: vendorListTwo,
3: vendorListThree,
})))
defer server.Close()
fetcher := newVendorListFetcher(context.Background(), testConfig(), server.Client(), testURLMaker(server), 1)
_, err := fetcher(context.Background(), 2)
assertNilErr(t, err)
_, err = fetcher(context.Background(), 3)
assertErr(t, err, false)
}
func TestMalformedVendorlistFetch(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(mockServer(1, map[int]string{1: "{}"})))
defer server.Close()
fetcher := newVendorListFetcher(context.Background(), testConfig(), server.Client(), testURLMaker(server), 1)
_, err := fetcher(context.Background(), 1)
assertErr(t, err, false)
}
func TestMissingVendorlistFetch(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(mockServer(1, map[int]string{1: "{}"})))
defer server.Close()
fetcher := newVendorListFetcher(context.Background(), testConfig(), server.Client(), testURLMaker(server), 1)
_, err := fetcher(context.Background(), 2)
assertErr(t, err, false)
}
func TestVendorListMaker(t *testing.T) {
assertStringsEqual(t, "https://vendorlist.consensu.org/vendorlist.json", vendorListURLMaker(0, 1))
assertStringsEqual(t, "https://vendorlist.consensu.org/v-2/vendorlist.json", vendorListURLMaker(2, 1))
assertStringsEqual(t, "https://vendorlist.consensu.org/v-12/vendorlist.json", vendorListURLMaker(12, 1))
assertStringsEqual(t, "https://vendorlist.consensu.org/v2/vendor-list.json", vendorListURLMaker(0, 2))
assertStringsEqual(t, "https://vendorlist.consensu.org/v2/archives/vendor-list-v7.json", vendorListURLMaker(7, 2))
}
// mockServer returns a handler which returns the given response for each global vendor list version.
// The latestVersion param can be used to mock "updates" which occur after PBS has been turned on.
// For example, if latestVersion is 3, but the responses map has data at "4", the server will return
// version "3" when asked for the latest version.
//
// This will help test lazy-fetches for versions which aren't there on app startup.
//
// If the "version" query param doesn't exist, it returns a 400.
//
// If the "version" query param points to a version which doesn't exist, it returns a 403.
// Don't ask why... that's just what the official page is doing. See https://vendorlist.consensu.org/v-9999/vendorlist.json
func mockServer(latestVersion int, responses map[int]string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
version := req.URL.Query().Get("version")
versionInt, err := strconv.Atoi(version)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Request had invalid version: " + version))
return
}
if versionInt == 0 {
versionInt = latestVersion
}
response, ok := responses[versionInt]
if !ok {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Version not found: " + version))
return
}
w.Write([]byte(response))
}
}
func mockVendorListData(t *testing.T, version uint16, vendors map[uint16]*purposes) string {
type vendorContract struct {
ID uint16 `json:"id"`
Purposes []int `json:"purposeIds"`
}
type vendorListContract struct {
Version uint16 `json:"vendorListVersion"`
Vendors []vendorContract `json:"vendors"`
}
buildVendors := func(input map[uint16]*purposes) []vendorContract {
vendors := make([]vendorContract, 0, len(input))
for id, purpose := range input {
vendors = append(vendors, vendorContract{
ID: id,
Purposes: purpose.purposes,
})
}
return vendors
}
obj := vendorListContract{
Version: version,
Vendors: buildVendors(vendors),
}
data, err := json.Marshal(obj)
assertNilErr(t, err)
return string(data)
}
type purposeMap map[uint16]*purposes
func mockVendorListDataTCF2(t *testing.T, version uint16, basicPurposes purposeMap, legitInterests purposeMap, flexPurposes purposeMap, specialPurposes purposeMap) string {
type vendorContract struct {
ID uint16 `json:"id"`
Purposes []int `json:"purposes"`
LegIntPurposes []int `json:"legIntPurposes"`
FlexiblePurposes []int `json:"flexiblePurposes"`
SpecialPurposes []int `json:"specialPurposes"`
}
type vendorListContract struct {
Version uint16 `json:"vendorListVersion"`
Vendors map[string]vendorContract `json:"vendors"`
}
vendors := make(map[string]vendorContract, len(basicPurposes))
for id, purpose := range basicPurposes {
sid := strconv.Itoa(int(id))
vendor, ok := vendors[sid]
if !ok {
vendor = vendorContract{ID: id}
}
vendor.Purposes = purpose.purposes
vendors[sid] = vendor
}
for id, purpose := range legitInterests {
sid := strconv.Itoa(int(id))
vendor, ok := vendors[sid]
if !ok {
vendor = vendorContract{ID: id}
}
vendor.LegIntPurposes = purpose.purposes
vendors[sid] = vendor
}
for id, purpose := range flexPurposes {
sid := strconv.Itoa(int(id))
vendor, ok := vendors[sid]
if !ok {
vendor = vendorContract{ID: id}
}
vendor.FlexiblePurposes = purpose.purposes
vendors[sid] = vendor
}
for id, purpose := range specialPurposes {
sid := strconv.Itoa(int(id))
vendor, ok := vendors[sid]
if !ok {
vendor = vendorContract{ID: id}
}
vendor.SpecialPurposes = purpose.purposes
vendors[sid] = vendor
}
obj := vendorListContract{
Version: version,
Vendors: vendors,
}
data, err := json.Marshal(obj)
assertNilErr(t, err)
return string(data)
}
func testURLMaker(server *httptest.Server) func(uint16, uint8) string {
url := server.URL
return func(version uint16, TCFVer uint8) string {
return url + "?version=" + strconv.Itoa(int(version))
}
}
func testConfig() config.GDPR {
return config.GDPR{
Timeouts: config.GDPRTimeouts{
InitVendorlistFetch: 60 * 1000,
ActiveVendorlistFetch: 1000 * 5,
},
}
}
type purposes struct {
purposes []int
}