github.com/prebid/prebid-server@v0.275.0/openrtb_ext/device_test.go (about)

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