github.com/prebid/prebid-server/v2@v2.18.0/gdpr/consent_parser_test.go (about)

     1  package gdpr
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/prebid/go-gdpr/consentconstants"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestParseConsent(t *testing.T) {
    14  	validTCF1Consent := "BONV8oqONXwgmADACHENAO7pqzAAppY"
    15  	validTCF2Consent := "CPuKGCPPuKGCPNEAAAENCZCAAAAAAAAAAAAAAAAAAAAA"
    16  
    17  	tests := []struct {
    18  		name                    string
    19  		consent                 string
    20  		expectedEncodingVersion uint8
    21  		expectedListVersion     uint16
    22  		expectedSpecVersion     uint16
    23  		expectedError           error
    24  	}{
    25  
    26  		{
    27  			name:                    "valid_consent_with_encoding_version_2",
    28  			consent:                 validTCF2Consent,
    29  			expectedEncodingVersion: 2,
    30  			expectedListVersion:     153,
    31  			expectedSpecVersion:     2,
    32  		},
    33  		{
    34  			name:    "invalid_consent_parsing_error",
    35  			consent: "",
    36  			expectedError: &ErrorMalformedConsent{
    37  				Consent: "",
    38  				Cause:   consentconstants.ErrEmptyDecodedConsent,
    39  			},
    40  		},
    41  		{
    42  			name:    "invalid_consent_version_validation_error",
    43  			consent: validTCF1Consent,
    44  			expectedError: &ErrorMalformedConsent{
    45  				Consent: validTCF1Consent,
    46  				Cause:   errors.New("invalid encoding format version: 1"),
    47  			},
    48  		},
    49  	}
    50  
    51  	for _, tt := range tests {
    52  		t.Run(tt.name, func(t *testing.T) {
    53  			parsedConsent, err := parseConsent(tt.consent)
    54  
    55  			if tt.expectedError != nil {
    56  				assert.Equal(t, tt.expectedError, err)
    57  				assert.Nil(t, parsedConsent)
    58  			} else {
    59  				assert.NoError(t, err)
    60  				assert.NotNil(t, parsedConsent)
    61  				assert.Equal(t, uint8(2), parsedConsent.encodingVersion)
    62  				assert.Equal(t, uint16(153), parsedConsent.listVersion)
    63  				assert.Equal(t, uint16(2), parsedConsent.specVersion)
    64  				assert.Equal(t, tt.expectedEncodingVersion, parsedConsent.consentMeta.Version())
    65  				assert.Equal(t, tt.expectedListVersion, parsedConsent.consentMeta.VendorListVersion())
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestValidateVersions(t *testing.T) {
    72  	tests := []struct {
    73  		name          string
    74  		version       uint8
    75  		expectedError error
    76  	}{
    77  		{
    78  			name:    "valid_consent_version=2",
    79  			version: 2,
    80  		},
    81  		{
    82  			name:          "invalid_consent_version<2",
    83  			version:       1,
    84  			expectedError: errors.New("invalid encoding format version: 1"),
    85  		},
    86  		{
    87  			name:          "invalid_consent_version>2",
    88  			version:       3,
    89  			expectedError: errors.New("invalid encoding format version: 3"),
    90  		},
    91  	}
    92  
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			mcs := mockConsentString{
    96  				version: tt.version,
    97  			}
    98  			err := validateVersions(&mcs)
    99  			if tt.expectedError != nil {
   100  				assert.Equal(t, tt.expectedError, err)
   101  			} else {
   102  				assert.Nil(t, err)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestGetSpecVersion(t *testing.T) {
   109  	tests := []struct {
   110  		name                string
   111  		policyVersion       uint8
   112  		expectedSpecVersion uint16
   113  	}{
   114  		{
   115  			name:                "policy_version_0_gives_spec_version_2",
   116  			policyVersion:       0,
   117  			expectedSpecVersion: 2,
   118  		},
   119  		{
   120  			name:                "policy_version_3_gives_spec_version_2",
   121  			policyVersion:       3,
   122  			expectedSpecVersion: 2,
   123  		},
   124  		{
   125  			name:                "policy_version_4_gives_spec_version_3",
   126  			policyVersion:       4,
   127  			expectedSpecVersion: 3,
   128  		},
   129  		{
   130  			name:                "policy_version_5_gives_spec_version_3",
   131  			policyVersion:       5,
   132  			expectedSpecVersion: 3,
   133  		},
   134  	}
   135  
   136  	for _, tt := range tests {
   137  		t.Run(tt.name, func(t *testing.T) {
   138  			specVersion := getSpecVersion(tt.policyVersion)
   139  			assert.Equal(t, tt.expectedSpecVersion, specVersion)
   140  		})
   141  	}
   142  }
   143  
   144  type mockConsentString struct {
   145  	version       uint8
   146  	policyVersion uint8
   147  }
   148  
   149  func (mcs *mockConsentString) Version() uint8                                  { return mcs.version }
   150  func (mcs *mockConsentString) Created() time.Time                              { return time.Time{} }
   151  func (mcs *mockConsentString) LastUpdated() time.Time                          { return time.Time{} }
   152  func (mcs *mockConsentString) CmpID() uint16                                   { return 0 }
   153  func (mcs *mockConsentString) CmpVersion() uint16                              { return 0 }
   154  func (mcs *mockConsentString) ConsentScreen() uint8                            { return 0 }
   155  func (mcs *mockConsentString) ConsentLanguage() string                         { return "" }
   156  func (mcs *mockConsentString) VendorListVersion() uint16                       { return 0 }
   157  func (mcs *mockConsentString) TCFPolicyVersion() uint8                         { return mcs.policyVersion }
   158  func (mcs *mockConsentString) MaxVendorID() uint16                             { return 0 }
   159  func (mcs *mockConsentString) PurposeAllowed(id consentconstants.Purpose) bool { return false }
   160  func (mcs *mockConsentString) VendorConsent(id uint16) bool                    { return false }