github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/device_test.go (about) 1 package openrtb_ext 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/prebid/prebid-server/v2/util/jsonutil" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestInvalidDeviceExt(t *testing.T) { 12 var s ExtDevice 13 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100") 14 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":105}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100") 15 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":true,"minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100") 16 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":null,"minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100") 17 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":"75","minheightperc":0}}}`), &s), "request.device.ext.prebid.interstitial.minwidthperc must be a number between 0 and 100") 18 19 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":85}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100") 20 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":85,"minheightperc":-5}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100") 21 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":85,"minheightperc":false}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100") 22 assert.EqualError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":85,"minheightperc":"75"}}}`), &s), "request.device.ext.prebid.interstitial.minheightperc must be a number between 0 and 100") 23 } 24 25 func TestValidDeviceExt(t *testing.T) { 26 var s ExtDevice 27 assert.NoError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{}}`), &s)) 28 assert.Nil(t, s.Prebid.Interstitial) 29 assert.NoError(t, jsonutil.UnmarshalValid([]byte(`{}`), &s)) 30 assert.Nil(t, s.Prebid.Interstitial) 31 assert.NoError(t, jsonutil.UnmarshalValid([]byte(`{"prebid":{"interstitial":{"minwidthperc":75,"minheightperc":60}}}`), &s)) 32 assert.EqualValues(t, 75, s.Prebid.Interstitial.MinWidthPerc) 33 assert.EqualValues(t, 60, s.Prebid.Interstitial.MinHeightPerc) 34 } 35 36 func TestIsKnownIOSAppTrackingStatus(t *testing.T) { 37 valid := []int64{0, 1, 2, 3} 38 invalid := []int64{-1, 4} 39 40 for _, v := range valid { 41 assert.True(t, IsKnownIOSAppTrackingStatus(v)) 42 } 43 44 for _, v := range invalid { 45 assert.False(t, IsKnownIOSAppTrackingStatus(v)) 46 } 47 } 48 49 func TestParseDeviceExtATTS(t *testing.T) { 50 authorized := IOSAppTrackingStatusAuthorized 51 52 tests := []struct { 53 description string 54 givenExt json.RawMessage 55 expectedStatus *IOSAppTrackingStatus 56 expectedError string 57 }{ 58 { 59 description: "Nil", 60 givenExt: nil, 61 expectedStatus: nil, 62 }, 63 { 64 description: "Empty", 65 givenExt: json.RawMessage(``), 66 expectedStatus: nil, 67 }, 68 { 69 description: "Empty Object", 70 givenExt: json.RawMessage(`{}`), 71 expectedStatus: nil, 72 }, 73 { 74 description: "Valid", 75 givenExt: json.RawMessage(`{"atts":3}`), 76 expectedStatus: &authorized, 77 }, 78 { 79 description: "Invalid Value", 80 givenExt: json.RawMessage(`{"atts":5}`), 81 expectedStatus: nil, 82 expectedError: "invalid status", 83 }, 84 { 85 // This test case produces an error with the standard Go library, but jsonparser doesn't 86 // return an error for malformed JSON. It treats this case the same as not being found. 87 description: "Malformed - Standard Test Case", 88 givenExt: json.RawMessage(`malformed`), 89 expectedStatus: nil, 90 }, 91 { 92 description: "Malformed - Wrong Type", 93 givenExt: json.RawMessage(`{"atts":"1"}`), 94 expectedStatus: nil, 95 expectedError: "Value is not a number: 1", 96 }, 97 } 98 99 for _, test := range tests { 100 status, err := ParseDeviceExtATTS(test.givenExt) 101 102 if test.expectedError == "" { 103 assert.NoError(t, err, test.description+":err") 104 } else { 105 assert.EqualError(t, err, test.expectedError, test.description+":err") 106 } 107 108 assert.Equal(t, test.expectedStatus, status, test.description+":status") 109 } 110 }