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

     1  package privacy
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/prebid-server/v2/config"
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  	"github.com/prebid/prebid-server/v2/util/ptrutil"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestNewActivityControl(t *testing.T) {
    13  	testCases := []struct {
    14  		name            string
    15  		privacyConf     config.AccountPrivacy
    16  		activityControl ActivityControl
    17  	}{
    18  		{
    19  			name:            "empty",
    20  			privacyConf:     config.AccountPrivacy{},
    21  			activityControl: ActivityControl{plans: nil},
    22  		},
    23  		{
    24  			name: "specified_and_correct",
    25  			privacyConf: config.AccountPrivacy{
    26  				AllowActivities: &config.AllowActivities{
    27  					SyncUser:                 getTestActivityConfig(true),
    28  					FetchBids:                getTestActivityConfig(true),
    29  					EnrichUserFPD:            getTestActivityConfig(true),
    30  					ReportAnalytics:          getTestActivityConfig(true),
    31  					TransmitUserFPD:          getTestActivityConfig(true),
    32  					TransmitPreciseGeo:       getTestActivityConfig(false),
    33  					TransmitUniqueRequestIds: getTestActivityConfig(true),
    34  					TransmitTids:             getTestActivityConfig(true),
    35  				},
    36  				IPv6Config: config.IPv6{AnonKeepBits: 32},
    37  				IPv4Config: config.IPv4{AnonKeepBits: 16},
    38  			},
    39  			activityControl: ActivityControl{
    40  				plans: map[Activity]ActivityPlan{
    41  					ActivitySyncUser:                 getTestActivityPlan(ActivityAllow),
    42  					ActivityFetchBids:                getTestActivityPlan(ActivityAllow),
    43  					ActivityEnrichUserFPD:            getTestActivityPlan(ActivityAllow),
    44  					ActivityReportAnalytics:          getTestActivityPlan(ActivityAllow),
    45  					ActivityTransmitUserFPD:          getTestActivityPlan(ActivityAllow),
    46  					ActivityTransmitPreciseGeo:       getTestActivityPlan(ActivityDeny),
    47  					ActivityTransmitUniqueRequestIDs: getTestActivityPlan(ActivityAllow),
    48  					ActivityTransmitTIDs:             getTestActivityPlan(ActivityAllow),
    49  				},
    50  				IPv6Config: config.IPv6{AnonKeepBits: 32},
    51  				IPv4Config: config.IPv4{AnonKeepBits: 16},
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, test := range testCases {
    57  		t.Run(test.name, func(t *testing.T) {
    58  			actualAC := NewActivityControl(&test.privacyConf)
    59  			assert.Equal(t, test.activityControl, actualAC)
    60  		})
    61  	}
    62  }
    63  
    64  func TestCfgToDefaultResult(t *testing.T) {
    65  	testCases := []struct {
    66  		name            string
    67  		activityDefault *bool
    68  		expectedResult  bool
    69  	}{
    70  		{
    71  			name:            "nil",
    72  			activityDefault: nil,
    73  			expectedResult:  true,
    74  		},
    75  		{
    76  			name:            "true",
    77  			activityDefault: ptrutil.ToPtr(true),
    78  			expectedResult:  true,
    79  		},
    80  		{
    81  			name:            "false",
    82  			activityDefault: ptrutil.ToPtr(false),
    83  			expectedResult:  false,
    84  		},
    85  	}
    86  
    87  	for _, test := range testCases {
    88  		t.Run(test.name, func(t *testing.T) {
    89  			actualResult := cfgToDefaultResult(test.activityDefault)
    90  			assert.Equal(t, test.expectedResult, actualResult)
    91  		})
    92  	}
    93  }
    94  
    95  func TestActivityControlAllow(t *testing.T) {
    96  	testCases := []struct {
    97  		name            string
    98  		activityControl ActivityControl
    99  		activity        Activity
   100  		target          Component
   101  		activityResult  bool
   102  	}{
   103  		{
   104  			name:            "plans_is_nil",
   105  			activityControl: ActivityControl{plans: nil},
   106  			activity:        ActivityFetchBids,
   107  			target:          Component{Type: "bidder", Name: "bidderA"},
   108  			activityResult:  true,
   109  		},
   110  		{
   111  			name: "activity_not_defined",
   112  			activityControl: ActivityControl{plans: map[Activity]ActivityPlan{
   113  				ActivitySyncUser: getTestActivityPlan(ActivityAllow)}},
   114  			activity:       ActivityFetchBids,
   115  			target:         Component{Type: "bidder", Name: "bidderA"},
   116  			activityResult: true,
   117  		},
   118  		{
   119  			name: "activity_defined_but_not_found_default_returned",
   120  			activityControl: ActivityControl{plans: map[Activity]ActivityPlan{
   121  				ActivityFetchBids: getTestActivityPlan(ActivityAllow)}},
   122  			activity:       ActivityFetchBids,
   123  			target:         Component{Type: "bidder", Name: "bidderB"},
   124  			activityResult: true,
   125  		},
   126  		{
   127  			name: "activity_defined_and_allowed",
   128  			activityControl: ActivityControl{plans: map[Activity]ActivityPlan{
   129  				ActivityFetchBids: getTestActivityPlan(ActivityAllow)}},
   130  			activity:       ActivityFetchBids,
   131  			target:         Component{Type: "bidder", Name: "bidderA"},
   132  			activityResult: true,
   133  		},
   134  	}
   135  
   136  	for _, test := range testCases {
   137  		t.Run(test.name, func(t *testing.T) {
   138  			actualResult := test.activityControl.Allow(test.activity, test.target, ActivityRequest{})
   139  			assert.Equal(t, test.activityResult, actualResult)
   140  
   141  		})
   142  	}
   143  }
   144  
   145  func TestActivityRequest(t *testing.T) {
   146  	t.Run("empty", func(t *testing.T) {
   147  		r := ActivityRequest{}
   148  		assert.False(t, r.IsPolicies())
   149  		assert.False(t, r.IsBidRequest())
   150  	})
   151  
   152  	t.Run("policies", func(t *testing.T) {
   153  		r := NewRequestFromPolicies(Policies{})
   154  		assert.True(t, r.IsPolicies())
   155  		assert.False(t, r.IsBidRequest())
   156  	})
   157  
   158  	t.Run("request", func(t *testing.T) {
   159  		r := NewRequestFromBidRequest(openrtb_ext.RequestWrapper{})
   160  		assert.False(t, r.IsPolicies())
   161  		assert.True(t, r.IsBidRequest())
   162  	})
   163  }
   164  
   165  func getTestActivityConfig(allow bool) config.Activity {
   166  	return config.Activity{
   167  		Default: ptrutil.ToPtr(true),
   168  		Rules: []config.ActivityRule{
   169  			{
   170  				Allow: allow,
   171  				Condition: config.ActivityCondition{
   172  					ComponentName: []string{"bidderA"},
   173  					ComponentType: []string{"bidder"},
   174  				},
   175  			},
   176  		},
   177  	}
   178  }
   179  
   180  func getTestActivityPlan(result ActivityResult) ActivityPlan {
   181  	return ActivityPlan{
   182  		defaultResult: true,
   183  		rules: []Rule{
   184  			ConditionRule{
   185  				result:        result,
   186  				componentName: []string{"bidderA"},
   187  				componentType: []string{"bidder"},
   188  			},
   189  		},
   190  	}
   191  }