github.com/prebid/prebid-server/v2@v2.18.0/gdpr/basic_enforcement_test.go (about) 1 package gdpr 2 3 import ( 4 "testing" 5 6 "github.com/prebid/go-gdpr/consentconstants" 7 "github.com/prebid/go-gdpr/vendorconsent" 8 tcf2 "github.com/prebid/go-gdpr/vendorconsent/tcf2" 9 "github.com/prebid/prebid-server/v2/openrtb_ext" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestBasicLegalBasis(t *testing.T) { 15 var ( 16 appnexus = string(openrtb_ext.BidderAppnexus) 17 appnexusID = uint16(32) 18 ) 19 20 noConsents := "CPerMsAPerMsAAAAAAENCfCAAAAAAAAAAAAAAAAAAAAA" 21 purpose2Consent := "CPerMsAPerMsAAAAAAENCfCAAEAAAAAAAAAAAAAAAAAA" 22 purpose2LI := "CPerMsAPerMsAAAAAAENCfCAAAAAAEAAAAAAAAAAAAAA" 23 vendor32Consent := "CPerMsAPerMsAAAAAAENCfCAAAAAAAAAAAAAAQAAAAAEAAAAAAAA" 24 purpose2AndVendor32Consent := "CPerMsAPerMsAAAAAAENCfCAAEAAAAAAAAAAAQAAAAAEAAAAAAAA" 25 26 tests := []struct { 27 description string 28 config purposeConfig 29 consent string 30 overrides Overrides 31 wantResult bool 32 }{ 33 { 34 description: "enforce purpose & vendors are off", 35 consent: noConsents, 36 config: purposeConfig{ 37 PurposeID: consentconstants.Purpose(2), 38 EnforcePurpose: false, 39 EnforceVendors: false, 40 }, 41 wantResult: true, 42 }, 43 { 44 description: "enforce purpose on, purpose consent N", 45 consent: noConsents, 46 config: purposeConfig{ 47 PurposeID: consentconstants.Purpose(2), 48 EnforcePurpose: true, 49 EnforceVendors: false, 50 }, 51 wantResult: false, 52 }, 53 { 54 description: "enforce purpose on, purpose consent Y", 55 consent: purpose2Consent, 56 config: purposeConfig{ 57 PurposeID: consentconstants.Purpose(2), 58 EnforcePurpose: true, 59 EnforceVendors: false, 60 }, 61 wantResult: true, 62 }, 63 { 64 description: "enforce purpose on, purpose consent Y, enforce vendors off but overrides treats it as on", 65 consent: purpose2Consent, 66 config: purposeConfig{ 67 PurposeID: consentconstants.Purpose(2), 68 EnforcePurpose: true, 69 EnforceVendors: false, 70 }, 71 overrides: Overrides{enforceVendors: true}, 72 wantResult: false, 73 }, 74 { 75 description: "enforce purpose on, purpose consent Y, vendor consent Y, enforce vendors off but overrides treats it as on", 76 consent: purpose2AndVendor32Consent, 77 config: purposeConfig{ 78 PurposeID: consentconstants.Purpose(2), 79 EnforcePurpose: true, 80 EnforceVendors: false, 81 }, 82 overrides: Overrides{enforceVendors: true}, 83 wantResult: true, 84 }, 85 { 86 description: "enforce purpose on, purpose LI Transparency Y", 87 consent: purpose2LI, 88 config: purposeConfig{ 89 PurposeID: consentconstants.Purpose(2), 90 EnforcePurpose: true, 91 EnforceVendors: false, 92 }, 93 wantResult: false, 94 }, 95 { 96 description: "enforce purpose on, purpose LI Transparency Y but overrides allow it", 97 consent: purpose2LI, 98 config: purposeConfig{ 99 PurposeID: consentconstants.Purpose(2), 100 EnforcePurpose: true, 101 EnforceVendors: false, 102 }, 103 overrides: Overrides{allowLITransparency: true}, 104 wantResult: true, 105 }, 106 { 107 description: "enforce vendors on, vendor consent N", 108 consent: noConsents, 109 config: purposeConfig{ 110 PurposeID: consentconstants.Purpose(2), 111 EnforcePurpose: false, 112 EnforceVendors: true, 113 }, 114 wantResult: false, 115 }, 116 { 117 description: "enforce vendors on, vendor consent Y", 118 consent: vendor32Consent, 119 config: purposeConfig{ 120 PurposeID: consentconstants.Purpose(2), 121 EnforcePurpose: false, 122 EnforceVendors: true, 123 }, 124 wantResult: true, 125 }, 126 { 127 description: "enforce vendors on, vendor consent Y, enforce purpose off but overrides treats it as on", 128 consent: vendor32Consent, 129 config: purposeConfig{ 130 PurposeID: consentconstants.Purpose(2), 131 EnforcePurpose: false, 132 EnforceVendors: true, 133 }, 134 overrides: Overrides{enforcePurpose: true}, 135 wantResult: false, 136 }, 137 { 138 description: "enforce vendors on, purpose consent Y, vendor consent Y, enforce purpose off but overrides treats it as on", 139 consent: purpose2AndVendor32Consent, 140 config: purposeConfig{ 141 PurposeID: consentconstants.Purpose(2), 142 EnforcePurpose: false, 143 EnforceVendors: true, 144 }, 145 overrides: Overrides{enforcePurpose: true}, 146 wantResult: true, 147 }, 148 { 149 description: "enforce vendors on, vendor consent N, bidder is a basic vendor", 150 consent: noConsents, 151 config: purposeConfig{ 152 PurposeID: consentconstants.Purpose(2), 153 EnforcePurpose: false, 154 EnforceVendors: true, 155 BasicEnforcementVendorsMap: map[string]struct{}{appnexus: {}}, 156 }, 157 wantResult: true, 158 }, 159 { 160 description: "enforce purpose & vendors are on", 161 consent: noConsents, 162 config: purposeConfig{ 163 PurposeID: consentconstants.Purpose(2), 164 EnforcePurpose: true, 165 EnforceVendors: true, 166 }, 167 wantResult: false, 168 }, 169 { 170 description: "enforce purpose & vendors are on, bidder is a vendor exception", 171 consent: noConsents, 172 config: purposeConfig{ 173 PurposeID: consentconstants.Purpose(2), 174 EnforcePurpose: true, 175 EnforceVendors: true, 176 VendorExceptionMap: map[string]struct{}{appnexus: {}}, 177 }, 178 wantResult: true, 179 }, 180 { 181 description: "enforce purpose & vendors are on, bidder is a vendor exception but overrides disallow them", 182 consent: noConsents, 183 config: purposeConfig{ 184 PurposeID: consentconstants.Purpose(2), 185 EnforcePurpose: true, 186 EnforceVendors: true, 187 VendorExceptionMap: map[string]struct{}{appnexus: {}}, 188 }, 189 overrides: Overrides{blockVendorExceptions: true}, 190 wantResult: false, 191 }, 192 { 193 description: "enforce purpose & vendors are on, purpose consent Y, vendor consent N", 194 consent: purpose2Consent, 195 config: purposeConfig{ 196 PurposeID: consentconstants.Purpose(2), 197 EnforcePurpose: true, 198 EnforceVendors: true, 199 }, 200 wantResult: false, 201 }, 202 { 203 description: "enforce purpose & vendors are on, purpose consent Y, vendor consent N, bidder is a basic vendor", 204 consent: purpose2Consent, 205 config: purposeConfig{ 206 PurposeID: consentconstants.Purpose(2), 207 EnforcePurpose: true, 208 EnforceVendors: true, 209 BasicEnforcementVendorsMap: map[string]struct{}{appnexus: {}}, 210 }, 211 wantResult: true, 212 }, 213 { 214 description: "enforce purpose & vendors are on, purpose consent Y, vendor consent Y", 215 consent: purpose2AndVendor32Consent, 216 config: purposeConfig{ 217 PurposeID: consentconstants.Purpose(2), 218 EnforcePurpose: true, 219 EnforceVendors: true, 220 }, 221 wantResult: true, 222 }, 223 } 224 225 for _, tt := range tests { 226 // convert consent string to TCF2 object 227 parsedConsent, err := vendorconsent.ParseString(tt.consent) 228 if err != nil { 229 t.Fatalf("Failed to parse consent %s: %s\n", tt.consent, tt.description) 230 } 231 consentMeta, ok := parsedConsent.(tcf2.ConsentMetadata) 232 if !ok { 233 t.Fatalf("Failed to convert consent %s: %s\n", tt.consent, tt.description) 234 } 235 236 enforcer := BasicEnforcement{cfg: tt.config} 237 238 vendorInfo := VendorInfo{vendorID: appnexusID, vendor: nil} 239 result := enforcer.LegalBasis(vendorInfo, appnexus, consentMeta, tt.overrides) 240 241 assert.Equal(t, tt.wantResult, result, tt.description) 242 } 243 }