github.com/prebid/prebid-server/v2@v2.18.0/privacy/lmt/policy_test.go (about)

     1  package lmt
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/openrtb/v20/openrtb2"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestReadFromRequest(t *testing.T) {
    11  	var one int8 = 1
    12  
    13  	testCases := []struct {
    14  		description    string
    15  		request        *openrtb2.BidRequest
    16  		expectedPolicy Policy
    17  	}{
    18  		{
    19  			description: "Nil Request",
    20  			request:     nil,
    21  			expectedPolicy: Policy{
    22  				Signal:         0,
    23  				SignalProvided: false,
    24  			},
    25  		},
    26  		{
    27  			description: "Nil Device",
    28  			request: &openrtb2.BidRequest{
    29  				Device: nil,
    30  			},
    31  			expectedPolicy: Policy{
    32  				Signal:         0,
    33  				SignalProvided: false,
    34  			},
    35  		},
    36  		{
    37  			description: "Nil Device.Lmt",
    38  			request: &openrtb2.BidRequest{
    39  				Device: &openrtb2.Device{
    40  					Lmt: nil,
    41  				},
    42  			},
    43  			expectedPolicy: Policy{
    44  				Signal:         0,
    45  				SignalProvided: false,
    46  			},
    47  		},
    48  		{
    49  			description: "Enabled",
    50  			request: &openrtb2.BidRequest{
    51  				Device: &openrtb2.Device{
    52  					Lmt: &one,
    53  				},
    54  			},
    55  			expectedPolicy: Policy{
    56  				Signal:         1,
    57  				SignalProvided: true,
    58  			},
    59  		},
    60  	}
    61  
    62  	for _, test := range testCases {
    63  		p := ReadFromRequest(test.request)
    64  		assert.Equal(t, test.expectedPolicy, p, test.description)
    65  	}
    66  }
    67  
    68  func TestCanEnforce(t *testing.T) {
    69  	testCases := []struct {
    70  		description string
    71  		policy      Policy
    72  		expected    bool
    73  	}{
    74  		{
    75  			description: "Signal Not Provided - Zero",
    76  			policy: Policy{
    77  				Signal:         0,
    78  				SignalProvided: false,
    79  			},
    80  			expected: false,
    81  		},
    82  		{
    83  			description: "Signal Not Provided - One",
    84  			policy: Policy{
    85  				Signal:         1,
    86  				SignalProvided: false,
    87  			},
    88  			expected: false,
    89  		},
    90  		{
    91  			description: "Signal Not Provided - Other",
    92  			policy: Policy{
    93  				Signal:         42,
    94  				SignalProvided: false,
    95  			},
    96  			expected: false,
    97  		},
    98  		{
    99  			description: "Signal Provided - Zero",
   100  			policy: Policy{
   101  				Signal:         0,
   102  				SignalProvided: true,
   103  			},
   104  			expected: true,
   105  		},
   106  		{
   107  			description: "Signal Provided - One",
   108  			policy: Policy{
   109  				Signal:         1,
   110  				SignalProvided: true,
   111  			},
   112  			expected: true,
   113  		},
   114  		{
   115  			description: "Signal Provided - Other",
   116  			policy: Policy{
   117  				Signal:         42,
   118  				SignalProvided: true,
   119  			},
   120  			expected: true,
   121  		},
   122  	}
   123  
   124  	for _, test := range testCases {
   125  		result := test.policy.CanEnforce()
   126  		assert.Equal(t, test.expected, result, test.description)
   127  	}
   128  }
   129  
   130  func TestShouldEnforce(t *testing.T) {
   131  	testCases := []struct {
   132  		description string
   133  		policy      Policy
   134  		expected    bool
   135  	}{
   136  		{
   137  			description: "Signal Not Provided - Zero",
   138  			policy: Policy{
   139  				Signal:         0,
   140  				SignalProvided: false,
   141  			},
   142  			expected: false,
   143  		},
   144  		{
   145  			description: "Signal Not Provided - One",
   146  			policy: Policy{
   147  				Signal:         1,
   148  				SignalProvided: false,
   149  			},
   150  			expected: false,
   151  		},
   152  		{
   153  			description: "Signal Not Provided - Other",
   154  			policy: Policy{
   155  				Signal:         42,
   156  				SignalProvided: false,
   157  			},
   158  			expected: false,
   159  		},
   160  		{
   161  			description: "Signal Provided - Zero",
   162  			policy: Policy{
   163  				Signal:         0,
   164  				SignalProvided: true,
   165  			},
   166  			expected: false,
   167  		},
   168  		{
   169  			description: "Signal Provided - One",
   170  			policy: Policy{
   171  				Signal:         1,
   172  				SignalProvided: true,
   173  			},
   174  			expected: true,
   175  		},
   176  		{
   177  			description: "Signal Provided - Other",
   178  			policy: Policy{
   179  				Signal:         42,
   180  				SignalProvided: true,
   181  			},
   182  			expected: false,
   183  		},
   184  	}
   185  
   186  	for _, test := range testCases {
   187  		result := test.policy.ShouldEnforce("")
   188  		assert.Equal(t, test.expected, result, test.description)
   189  	}
   190  }