github.com/prebid/prebid-server/v2@v2.18.0/exchange/gdpr_test.go (about)

     1  package exchange
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	gpplib "github.com/prebid/go-gpp"
     8  	gppConstants "github.com/prebid/go-gpp/constants"
     9  	"github.com/prebid/openrtb/v20/openrtb2"
    10  	"github.com/prebid/prebid-server/v2/gdpr"
    11  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestGetGDPR(t *testing.T) {
    16  	tests := []struct {
    17  		description string
    18  		giveRegs    *openrtb2.Regs
    19  		wantGDPR    gdpr.Signal
    20  		wantError   bool
    21  	}{
    22  		{
    23  			description: "Regs Ext GDPR = 0",
    24  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr": 0}`)},
    25  			wantGDPR:    gdpr.SignalNo,
    26  		},
    27  		{
    28  			description: "Regs Ext GDPR = 1",
    29  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr": 1}`)},
    30  			wantGDPR:    gdpr.SignalYes,
    31  		},
    32  		{
    33  			description: "Regs Ext GDPR = null",
    34  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr": null}`)},
    35  			wantGDPR:    gdpr.SignalAmbiguous,
    36  		},
    37  		{
    38  			description: "Regs is nil",
    39  			giveRegs:    nil,
    40  			wantGDPR:    gdpr.SignalAmbiguous,
    41  		},
    42  		{
    43  			description: "Regs Ext is nil",
    44  			giveRegs:    &openrtb2.Regs{Ext: nil},
    45  			wantGDPR:    gdpr.SignalAmbiguous,
    46  		},
    47  		{
    48  			description: "JSON unmarshal error",
    49  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"`)},
    50  			wantGDPR:    gdpr.SignalAmbiguous,
    51  			wantError:   true,
    52  		},
    53  		{
    54  			description: "Regs Ext GDPR = null, GPPSID has tcf2",
    55  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr": null}`), GPPSID: []int8{2}},
    56  			wantGDPR:    gdpr.SignalYes,
    57  		},
    58  		{
    59  			description: "Regs Ext GDPR = 1, GPPSID has uspv1",
    60  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr": 1}`), GPPSID: []int8{6}},
    61  			wantGDPR:    gdpr.SignalNo,
    62  		},
    63  		{
    64  			description: "Regs Ext GDPR = 0, GPPSID has tcf2",
    65  			giveRegs:    &openrtb2.Regs{Ext: json.RawMessage(`{"gdpr": 0}`), GPPSID: []int8{2}},
    66  			wantGDPR:    gdpr.SignalYes,
    67  		},
    68  		{
    69  			description: "Regs Ext is nil, GPPSID has tcf2",
    70  			giveRegs:    &openrtb2.Regs{GPPSID: []int8{2}},
    71  			wantGDPR:    gdpr.SignalYes,
    72  		},
    73  		{
    74  			description: "Regs Ext is nil, GPPSID has uspv1",
    75  			giveRegs:    &openrtb2.Regs{GPPSID: []int8{6}},
    76  			wantGDPR:    gdpr.SignalNo,
    77  		},
    78  	}
    79  
    80  	for _, tt := range tests {
    81  		t.Run(tt.description, func(t *testing.T) {
    82  			req := openrtb_ext.RequestWrapper{
    83  				BidRequest: &openrtb2.BidRequest{
    84  					Regs: tt.giveRegs,
    85  				},
    86  			}
    87  			result, err := getGDPR(&req)
    88  			assert.Equal(t, tt.wantGDPR, result)
    89  
    90  			if tt.wantError {
    91  				assert.NotNil(t, err)
    92  			} else {
    93  				assert.Nil(t, err)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestGetConsent(t *testing.T) {
   100  	tests := []struct {
   101  		description string
   102  		giveUser    *openrtb2.User
   103  		giveGPP     gpplib.GppContainer
   104  		wantConsent string
   105  		wantError   bool
   106  	}{
   107  		{
   108  			description: "User Ext Consent is not empty",
   109  			giveUser:    &openrtb2.User{Ext: json.RawMessage(`{"consent": "BOS2bx5OS2bx5ABABBAAABoAAAAAFA"}`)},
   110  			wantConsent: "BOS2bx5OS2bx5ABABBAAABoAAAAAFA",
   111  		},
   112  		{
   113  			description: "User Ext Consent is empty",
   114  			giveUser:    &openrtb2.User{Ext: json.RawMessage(`{"consent": ""}`)},
   115  			wantConsent: "",
   116  		},
   117  		{
   118  			description: "User Ext is nil",
   119  			giveUser:    &openrtb2.User{Ext: nil},
   120  			wantConsent: "",
   121  		},
   122  		{
   123  			description: "User is nil",
   124  			giveUser:    nil,
   125  			wantConsent: "",
   126  		},
   127  		{
   128  			description: "JSON unmarshal error",
   129  			giveUser:    &openrtb2.User{Ext: json.RawMessage(`{`)},
   130  			wantConsent: "",
   131  			wantError:   true,
   132  		},
   133  		{
   134  			description: "User Ext is nil, GPP has no GDPR",
   135  			giveUser:    &openrtb2.User{Ext: nil},
   136  			giveGPP:     gpplib.GppContainer{Version: 1, SectionTypes: []gppConstants.SectionID{6}, Sections: []gpplib.Section{&upsv1Section}},
   137  			wantConsent: "",
   138  		},
   139  		{
   140  			description: "User Ext is nil, GPP has GDPR",
   141  			giveUser:    &openrtb2.User{Ext: nil},
   142  			giveGPP:     gpplib.GppContainer{Version: 1, SectionTypes: []gppConstants.SectionID{2}, Sections: []gpplib.Section{&tcf1Section}},
   143  			wantConsent: "BOS2bx5OS2bx5ABABBAAABoAAAAAFA",
   144  		},
   145  		{
   146  			description: "User Ext has GDPR, GPP has GDPR",
   147  			giveUser:    &openrtb2.User{Ext: json.RawMessage(`{"consent": "BSOMECONSENT"}`)},
   148  			giveGPP:     gpplib.GppContainer{Version: 1, SectionTypes: []gppConstants.SectionID{2}, Sections: []gpplib.Section{&tcf1Section}},
   149  			wantConsent: "BOS2bx5OS2bx5ABABBAAABoAAAAAFA",
   150  		},
   151  	}
   152  
   153  	for _, tt := range tests {
   154  		t.Run(tt.description, func(t *testing.T) {
   155  			req := openrtb_ext.RequestWrapper{
   156  				BidRequest: &openrtb2.BidRequest{
   157  					User: tt.giveUser,
   158  				},
   159  			}
   160  
   161  			result, err := getConsent(&req, tt.giveGPP)
   162  			assert.Equal(t, tt.wantConsent, result, tt.description)
   163  
   164  			if tt.wantError {
   165  				assert.NotNil(t, err, tt.description)
   166  			} else {
   167  				assert.Nil(t, err, tt.description)
   168  			}
   169  		})
   170  	}
   171  }
   172  
   173  var upsv1Section mockGPPSection = mockGPPSection{sectionID: 6, value: "1YNY"}
   174  var tcf1Section mockGPPSection = mockGPPSection{sectionID: 2, value: "BOS2bx5OS2bx5ABABBAAABoAAAAAFA"}
   175  
   176  type mockGPPSection struct {
   177  	sectionID gppConstants.SectionID
   178  	value     string
   179  }
   180  
   181  func (ms mockGPPSection) GetID() gppConstants.SectionID {
   182  	return ms.sectionID
   183  }
   184  
   185  func (ms mockGPPSection) GetValue() string {
   186  	return ms.value
   187  }
   188  
   189  func (ms mockGPPSection) Encode(bool) []byte {
   190  	return nil
   191  }