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

     1  package ccpa
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // RegExt.SetUSPrivacy() is the new ConsentWriter
    13  func TestConsentWriter(t *testing.T) {
    14  	consent := "anyConsent"
    15  	testCases := []struct {
    16  		description   string
    17  		request       *openrtb2.BidRequest
    18  		expected      *openrtb2.BidRequest
    19  		expectedError bool
    20  	}{
    21  		{
    22  			description: "Nil Request",
    23  			request:     nil,
    24  			expected:    nil,
    25  		},
    26  		{
    27  			description: "Success",
    28  			request:     &openrtb2.BidRequest{},
    29  			expected: &openrtb2.BidRequest{
    30  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"us_privacy":"anyConsent"}`)},
    31  			},
    32  		},
    33  		{
    34  			description: "Error With Regs.Ext - Does Not Mutate",
    35  			request: &openrtb2.BidRequest{
    36  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`malformed}`)},
    37  			},
    38  			expectedError: false,
    39  			expected: &openrtb2.BidRequest{
    40  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`malformed}`)},
    41  			},
    42  		},
    43  	}
    44  
    45  	for _, test := range testCases {
    46  
    47  		reqWrapper := &openrtb_ext.RequestWrapper{BidRequest: test.request}
    48  		var err error
    49  		regsExt, err1 := reqWrapper.GetRegExt()
    50  		if err1 == nil {
    51  			regsExt.SetUSPrivacy(consent)
    52  			if reqWrapper.BidRequest != nil {
    53  				err = reqWrapper.RebuildRequest()
    54  			}
    55  		}
    56  		assertError(t, test.expectedError, err, test.description)
    57  		assert.Equal(t, test.expected, reqWrapper.BidRequest, test.description)
    58  	}
    59  }
    60  
    61  func TestConsentWriterLegacy(t *testing.T) {
    62  	consent := "anyConsent"
    63  	testCases := []struct {
    64  		description   string
    65  		request       *openrtb2.BidRequest
    66  		expected      *openrtb2.BidRequest
    67  		expectedError bool
    68  	}{
    69  		{
    70  			description: "Nil Request",
    71  			request:     nil,
    72  			expected:    nil,
    73  		},
    74  		{
    75  			description: "Success",
    76  			request:     &openrtb2.BidRequest{},
    77  			expected: &openrtb2.BidRequest{
    78  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"us_privacy":"anyConsent"}`)},
    79  			},
    80  		},
    81  		{
    82  			description: "Error With Regs.Ext - Does Not Mutate",
    83  			request: &openrtb2.BidRequest{
    84  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`malformed}`)},
    85  			},
    86  			expectedError: true,
    87  			expected: &openrtb2.BidRequest{
    88  				Regs: &openrtb2.Regs{Ext: json.RawMessage(`malformed}`)},
    89  			},
    90  		},
    91  	}
    92  
    93  	for _, test := range testCases {
    94  		writer := ConsentWriter{consent}
    95  
    96  		err := writer.Write(test.request)
    97  
    98  		assertError(t, test.expectedError, err, test.description)
    99  		assert.Equal(t, test.expected, test.request, test.description)
   100  	}
   101  }