forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_test.go
109 lines (96 loc) · 4.32 KB
/
device_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
package openrtb_ext
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInvalidDeviceExt(t *testing.T) {
var s ExtDevice
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":105}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":true,"minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":null,"minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":"75","minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":85}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":85,"minheightperc":-5}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":85,"minheightperc":false}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100")
assert.EqualError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":85,"minheightperc":"75"}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100")
}
func TestValidDeviceExt(t *testing.T) {
var s ExtDevice
assert.NoError(t, json.Unmarshal([]byte(`{"prebid":{}}`), &s))
assert.Nil(t, s.Prebid.Interstitial)
assert.NoError(t, json.Unmarshal([]byte(`{}`), &s))
assert.Nil(t, s.Prebid.Interstitial)
assert.NoError(t, json.Unmarshal([]byte(`{"prebid":{"interstitial":{"minwidthperc":75,"minheightperc":60}}}`), &s))
assert.EqualValues(t, 75, s.Prebid.Interstitial.MinWidthPerc)
assert.EqualValues(t, 60, s.Prebid.Interstitial.MinHeightPerc)
}
func TestIsKnownIOSAppTrackingStatus(t *testing.T) {
valid := []int64{0, 1, 2, 3}
invalid := []int64{-1, 4}
for _, v := range valid {
assert.True(t, IsKnownIOSAppTrackingStatus(v))
}
for _, v := range invalid {
assert.False(t, IsKnownIOSAppTrackingStatus(v))
}
}
func TestParseDeviceExtATTS(t *testing.T) {
authorized := IOSAppTrackingStatusAuthorized
tests := []struct {
description string
givenExt json.RawMessage
expectedStatus *IOSAppTrackingStatus
expectedError string
}{
{
description: "Nil",
givenExt: nil,
expectedStatus: nil,
},
{
description: "Empty",
givenExt: json.RawMessage(``),
expectedStatus: nil,
},
{
description: "Empty Object",
givenExt: json.RawMessage(`{}`),
expectedStatus: nil,
},
{
description: "Valid",
givenExt: json.RawMessage(`{"atts":3}`),
expectedStatus: &authorized,
},
{
description: "Invalid Value",
givenExt: json.RawMessage(`{"atts":5}`),
expectedStatus: nil,
expectedError: "invalid status",
},
{
// This test case produces an error with the standard Go library, but jsonparser doesn't
// return an error for malformed JSON. It treats this case the same as not being found.
description: "Malformed - Standard Test Case",
givenExt: json.RawMessage(`malformed`),
expectedStatus: nil,
},
{
description: "Malformed - Wrong Type",
givenExt: json.RawMessage(`{"atts":"1"}`),
expectedStatus: nil,
expectedError: "Value is not a number: 1",
},
}
for _, test := range tests {
status, err := ParseDeviceExtATTS(test.givenExt)
if test.expectedError == "" {
assert.NoError(t, err, test.description+":err")
} else {
assert.EqualError(t, err, test.expectedError, test.description+":err")
}
assert.Equal(t, test.expectedStatus, status, test.description+":status")
}
}