github.com/prebid/prebid-server/v2@v2.18.0/dsa/writer_test.go (about)

     1  package dsa
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/prebid/prebid-server/v2/config"
     9  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    10  	"github.com/prebid/prebid-server/v2/util/ptrutil"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestWrite(t *testing.T) {
    15  	requestDSAJSON := json.RawMessage(`{"dsa":{"dsarequired":2,"pubrender":1,"datatopub":1,"transparency":[{"domain":"example1.com","dsaparams":[1,2,3]}]}}`)
    16  	defaultDSAJSON := json.RawMessage(`{"dsa":{"dsarequired":3,"pubrender":2,"datatopub":2,"transparency":[{"domain":"example2.com","dsaparams":[4,5,6]}]}}`)
    17  	defaultDSA := &openrtb_ext.ExtRegsDSA{
    18  		DataToPub: ptrutil.ToPtr[int8](2),
    19  		Required:  ptrutil.ToPtr[int8](3),
    20  		PubRender: ptrutil.ToPtr[int8](2),
    21  		Transparency: []openrtb_ext.ExtBidDSATransparency{
    22  			{
    23  				Domain: "example2.com",
    24  				Params: []int{4, 5, 6},
    25  			},
    26  		},
    27  	}
    28  
    29  	tests := []struct {
    30  		name          string
    31  		giveConfig    *config.AccountDSA
    32  		giveGDPR      bool
    33  		giveRequest   *openrtb_ext.RequestWrapper
    34  		expectRequest *openrtb_ext.RequestWrapper
    35  	}{
    36  		{
    37  			name: "request_nil",
    38  			giveConfig: &config.AccountDSA{
    39  				DefaultUnpacked: defaultDSA,
    40  			},
    41  			giveRequest:   nil,
    42  			expectRequest: nil,
    43  		},
    44  		{
    45  			name:       "config_nil",
    46  			giveConfig: nil,
    47  			giveRequest: &openrtb_ext.RequestWrapper{
    48  				BidRequest: &openrtb2.BidRequest{
    49  					Regs: &openrtb2.Regs{
    50  						Ext: nil,
    51  					},
    52  				},
    53  			},
    54  			expectRequest: &openrtb_ext.RequestWrapper{
    55  				BidRequest: &openrtb2.BidRequest{
    56  					Regs: &openrtb2.Regs{
    57  						Ext: nil,
    58  					},
    59  				},
    60  			},
    61  		},
    62  		{
    63  			name: "config_default_nil",
    64  			giveConfig: &config.AccountDSA{
    65  				DefaultUnpacked: nil,
    66  			},
    67  			giveRequest: &openrtb_ext.RequestWrapper{
    68  				BidRequest: &openrtb2.BidRequest{
    69  					Regs: &openrtb2.Regs{
    70  						Ext: nil,
    71  					},
    72  				},
    73  			},
    74  			expectRequest: &openrtb_ext.RequestWrapper{
    75  				BidRequest: &openrtb2.BidRequest{
    76  					Regs: &openrtb2.Regs{
    77  						Ext: nil,
    78  					},
    79  				},
    80  			},
    81  		},
    82  		{
    83  			name: "request_dsa_present",
    84  			giveConfig: &config.AccountDSA{
    85  				DefaultUnpacked: defaultDSA,
    86  			},
    87  			giveRequest: &openrtb_ext.RequestWrapper{
    88  				BidRequest: &openrtb2.BidRequest{
    89  					Regs: &openrtb2.Regs{
    90  						Ext: requestDSAJSON,
    91  					},
    92  				},
    93  			},
    94  			expectRequest: &openrtb_ext.RequestWrapper{
    95  				BidRequest: &openrtb2.BidRequest{
    96  					Regs: &openrtb2.Regs{
    97  						Ext: requestDSAJSON,
    98  					},
    99  				},
   100  			},
   101  		},
   102  		{
   103  			name: "config_default_present_with_gdpr_only_set_and_gdpr_in_scope",
   104  			giveConfig: &config.AccountDSA{
   105  				DefaultUnpacked: defaultDSA,
   106  				GDPROnly:        true,
   107  			},
   108  			giveGDPR: true,
   109  			giveRequest: &openrtb_ext.RequestWrapper{
   110  				BidRequest: &openrtb2.BidRequest{},
   111  			},
   112  			expectRequest: &openrtb_ext.RequestWrapper{
   113  				BidRequest: &openrtb2.BidRequest{
   114  					Regs: &openrtb2.Regs{
   115  						Ext: defaultDSAJSON,
   116  					},
   117  				},
   118  			},
   119  		},
   120  		{
   121  			name: "config_default_present_with_gdpr_only_set_and_gdpr_not_in_scope",
   122  			giveConfig: &config.AccountDSA{
   123  				DefaultUnpacked: defaultDSA,
   124  				GDPROnly:        true,
   125  			},
   126  			giveGDPR: false,
   127  			giveRequest: &openrtb_ext.RequestWrapper{
   128  				BidRequest: &openrtb2.BidRequest{},
   129  			},
   130  			expectRequest: &openrtb_ext.RequestWrapper{
   131  				BidRequest: &openrtb2.BidRequest{},
   132  			},
   133  		},
   134  		{
   135  			name: "config_default_present_with_gdpr_only_not_set_and_gdpr_in_scope",
   136  			giveConfig: &config.AccountDSA{
   137  				DefaultUnpacked: defaultDSA,
   138  				GDPROnly:        false,
   139  			},
   140  			giveGDPR: true,
   141  			giveRequest: &openrtb_ext.RequestWrapper{
   142  				BidRequest: &openrtb2.BidRequest{},
   143  			},
   144  			expectRequest: &openrtb_ext.RequestWrapper{
   145  				BidRequest: &openrtb2.BidRequest{
   146  					Regs: &openrtb2.Regs{
   147  						Ext: defaultDSAJSON,
   148  					},
   149  				},
   150  			},
   151  		},
   152  		{
   153  			name: "config_default_present_with_gdpr_only_not_set_and_gdpr_not_in_scope",
   154  			giveConfig: &config.AccountDSA{
   155  				DefaultUnpacked: defaultDSA,
   156  				GDPROnly:        false,
   157  			},
   158  			giveGDPR: false,
   159  			giveRequest: &openrtb_ext.RequestWrapper{
   160  				BidRequest: &openrtb2.BidRequest{},
   161  			},
   162  			expectRequest: &openrtb_ext.RequestWrapper{
   163  				BidRequest: &openrtb2.BidRequest{
   164  					Regs: &openrtb2.Regs{
   165  						Ext: defaultDSAJSON,
   166  					},
   167  				},
   168  			},
   169  		},
   170  	}
   171  
   172  	for _, tt := range tests {
   173  		t.Run(tt.name, func(t *testing.T) {
   174  			writer := Writer{
   175  				Config:      tt.giveConfig,
   176  				GDPRInScope: tt.giveGDPR,
   177  			}
   178  			err := writer.Write(tt.giveRequest)
   179  
   180  			if tt.giveRequest != nil {
   181  				tt.giveRequest.RebuildRequest()
   182  				assert.Equal(t, tt.expectRequest.BidRequest, tt.giveRequest.BidRequest)
   183  			} else {
   184  				assert.Nil(t, tt.giveRequest)
   185  			}
   186  			assert.Nil(t, err)
   187  		})
   188  	}
   189  }