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

     1  package privacy
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/openrtb/v20/openrtb2"
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestComponentEnforcementRuleEvaluate(t *testing.T) {
    12  	testCases := []struct {
    13  		name           string
    14  		componentRule  ConditionRule
    15  		target         Component
    16  		activityResult ActivityResult
    17  	}{
    18  		{
    19  			name: "activity_is_allowed",
    20  			componentRule: ConditionRule{
    21  				result:        ActivityAllow,
    22  				componentName: []string{"bidderA"},
    23  				componentType: []string{"bidder"},
    24  			},
    25  			target:         Component{Type: "bidder", Name: "bidderA"},
    26  			activityResult: ActivityAllow,
    27  		},
    28  		{
    29  			name: "activity_is_not_allowed",
    30  			componentRule: ConditionRule{
    31  				result:        ActivityDeny,
    32  				componentName: []string{"bidderA"},
    33  				componentType: []string{"bidder"},
    34  			},
    35  			target:         Component{Type: "bidder", Name: "bidderA"},
    36  			activityResult: ActivityDeny,
    37  		},
    38  		{
    39  			name: "abstain_both_clauses_do_not_match",
    40  			componentRule: ConditionRule{
    41  				result:        ActivityAllow,
    42  				componentName: []string{"bidderA"},
    43  				componentType: []string{"bidder"},
    44  			},
    45  			target:         Component{Type: "bidder", Name: "bidderB"},
    46  			activityResult: ActivityAbstain,
    47  		},
    48  		{
    49  			name: "abstain_gppsid",
    50  			componentRule: ConditionRule{
    51  				result: ActivityAllow,
    52  				gppSID: []int8{1},
    53  			},
    54  			target:         Component{Type: "bidder", Name: "bidderB"},
    55  			activityResult: ActivityAbstain,
    56  		},
    57  		{
    58  			name: "activity_is_not_allowed_componentName_only",
    59  			componentRule: ConditionRule{
    60  				result:        ActivityAllow,
    61  				componentName: []string{"bidderA"},
    62  			},
    63  			target:         Component{Type: "bidder", Name: "bidderA"},
    64  			activityResult: ActivityAllow,
    65  		},
    66  		{
    67  			name: "activity_is_allowed_componentType_only",
    68  			componentRule: ConditionRule{
    69  				result:        ActivityAllow,
    70  				componentType: []string{"bidder"},
    71  			},
    72  			target:         Component{Type: "bidder", Name: "bidderB"},
    73  			activityResult: ActivityAllow,
    74  		},
    75  		{
    76  			name: "no-conditions-allow",
    77  			componentRule: ConditionRule{
    78  				result: ActivityAllow,
    79  			},
    80  			target:         Component{Type: "bidder", Name: "bidderA"},
    81  			activityResult: ActivityAllow,
    82  		},
    83  		{
    84  			name: "no-conditions-deny",
    85  			componentRule: ConditionRule{
    86  				result: ActivityDeny,
    87  			},
    88  			target:         Component{Type: "bidder", Name: "bidderA"},
    89  			activityResult: ActivityDeny,
    90  		},
    91  	}
    92  
    93  	for _, test := range testCases {
    94  		t.Run(test.name, func(t *testing.T) {
    95  			actualResult := test.componentRule.Evaluate(test.target, ActivityRequest{})
    96  			assert.Equal(t, test.activityResult, actualResult)
    97  		})
    98  	}
    99  }
   100  
   101  func TestEvaluateGPPSID(t *testing.T) {
   102  	testCases := []struct {
   103  		name         string
   104  		sidCondition []int8
   105  		sidRequest   []int8
   106  		expected     bool
   107  	}{
   108  		{
   109  			name:         "condition-nil-request-nil",
   110  			sidCondition: nil,
   111  			sidRequest:   nil,
   112  			expected:     true,
   113  		},
   114  		{
   115  			name:         "condition-empty-request-nil",
   116  			sidCondition: []int8{},
   117  			sidRequest:   nil,
   118  			expected:     true,
   119  		},
   120  		{
   121  			name:         "condition-nil-request-empty",
   122  			sidCondition: nil,
   123  			sidRequest:   []int8{},
   124  			expected:     true,
   125  		},
   126  		{
   127  			name:         "condition-empty-request-empty",
   128  			sidCondition: []int8{},
   129  			sidRequest:   []int8{},
   130  			expected:     true,
   131  		},
   132  		{
   133  			name:         "condition-one-request-nil",
   134  			sidCondition: []int8{1},
   135  			sidRequest:   nil,
   136  			expected:     false,
   137  		},
   138  		{
   139  			name:         "condition-many-request-nil",
   140  			sidCondition: []int8{1, 2},
   141  			sidRequest:   nil,
   142  			expected:     false,
   143  		},
   144  		{
   145  			name:         "condition-one-request-empty",
   146  			sidCondition: []int8{1},
   147  			sidRequest:   []int8{},
   148  			expected:     false,
   149  		},
   150  		{
   151  			name:         "condition-many-request-empty",
   152  			sidCondition: []int8{1, 2},
   153  			sidRequest:   []int8{},
   154  			expected:     false,
   155  		},
   156  		{
   157  			name:         "condition-one-request-one-match",
   158  			sidCondition: []int8{1},
   159  			sidRequest:   []int8{1},
   160  			expected:     true,
   161  		},
   162  		{
   163  			name:         "condition-one-request-one-nomatch",
   164  			sidCondition: []int8{1},
   165  			sidRequest:   []int8{2},
   166  			expected:     false,
   167  		},
   168  		{
   169  			name:         "condition-one-request-many-match",
   170  			sidCondition: []int8{1},
   171  			sidRequest:   []int8{1, 2},
   172  			expected:     true,
   173  		},
   174  		{
   175  			name:         "condition-one-request-many-nomatch",
   176  			sidCondition: []int8{3},
   177  			sidRequest:   []int8{1, 2},
   178  			expected:     false,
   179  		},
   180  		{
   181  			name:         "condition-nil-request-one",
   182  			sidCondition: nil,
   183  			sidRequest:   []int8{1},
   184  			expected:     true,
   185  		},
   186  		{
   187  			name:         "condition-nil-request-many",
   188  			sidCondition: nil,
   189  			sidRequest:   []int8{1, 2},
   190  			expected:     true,
   191  		},
   192  		{
   193  			name:         "condition-empty-request-one",
   194  			sidCondition: []int8{},
   195  			sidRequest:   []int8{1},
   196  			expected:     true,
   197  		},
   198  		{
   199  			name:         "condition-empty-request-many",
   200  			sidCondition: []int8{},
   201  			sidRequest:   []int8{1, 2},
   202  			expected:     true,
   203  		},
   204  		{
   205  			name:         "condition-many-request-one-match",
   206  			sidCondition: []int8{1, 2},
   207  			sidRequest:   []int8{1},
   208  			expected:     true,
   209  		},
   210  		{
   211  			name:         "condition-many-request-one-nomatch",
   212  			sidCondition: []int8{1, 2},
   213  			sidRequest:   []int8{3},
   214  			expected:     false,
   215  		},
   216  		{
   217  			name:         "condition-many-request-many-match",
   218  			sidCondition: []int8{1, 2},
   219  			sidRequest:   []int8{1, 2},
   220  			expected:     true,
   221  		},
   222  		{
   223  			name:         "condition-many-request-many-nomatch",
   224  			sidCondition: []int8{1, 2},
   225  			sidRequest:   []int8{3, 4},
   226  			expected:     false,
   227  		},
   228  		{
   229  			name:         "condition-many-request-many-mixed",
   230  			sidCondition: []int8{1, 2},
   231  			sidRequest:   []int8{2, 3},
   232  			expected:     true,
   233  		},
   234  	}
   235  
   236  	for _, test := range testCases {
   237  		t.Run(test.name, func(t *testing.T) {
   238  			actualResult := evaluateGPPSID(test.sidCondition, NewRequestFromPolicies(Policies{GPPSID: test.sidRequest}))
   239  			assert.Equal(t, test.expected, actualResult)
   240  		})
   241  	}
   242  }
   243  
   244  func TestGetGPPSID(t *testing.T) {
   245  	testCases := []struct {
   246  		name     string
   247  		request  ActivityRequest
   248  		expected []int8
   249  	}{
   250  		{
   251  			name:     "empty",
   252  			request:  ActivityRequest{},
   253  			expected: nil,
   254  		},
   255  		{
   256  			name:     "policies",
   257  			request:  ActivityRequest{policies: &Policies{GPPSID: []int8{1}}},
   258  			expected: []int8{1},
   259  		},
   260  		{
   261  			name:     "request-regs",
   262  			request:  ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{GPPSID: []int8{1}}}}},
   263  			expected: []int8{1},
   264  		},
   265  		{
   266  			name:     "request-regs-nil",
   267  			request:  ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: nil}}},
   268  			expected: nil,
   269  		},
   270  	}
   271  
   272  	for _, test := range testCases {
   273  		t.Run(test.name, func(t *testing.T) {
   274  			actualResult := getGPPSID(test.request)
   275  			assert.Equal(t, test.expected, actualResult)
   276  		})
   277  	}
   278  }