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

     1  package dsa
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/prebid/openrtb/v20/openrtb2"
     9  	"github.com/prebid/prebid-server/v2/exchange/entities"
    10  	"github.com/prebid/prebid-server/v2/openrtb_ext"
    11  	"github.com/prebid/prebid-server/v2/util/ptrutil"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestValidate(t *testing.T) {
    16  	var (
    17  		validBehalf   = strings.Repeat("a", 100)
    18  		invalidBehalf = strings.Repeat("a", 101)
    19  		validPaid     = strings.Repeat("a", 100)
    20  		invalidPaid   = strings.Repeat("a", 101)
    21  	)
    22  
    23  	tests := []struct {
    24  		name        string
    25  		giveRequest *openrtb_ext.RequestWrapper
    26  		giveBid     *entities.PbsOrtbBid
    27  		wantError   error
    28  	}{
    29  		{
    30  			name:        "nil",
    31  			giveRequest: nil,
    32  			giveBid:     nil,
    33  			wantError:   nil,
    34  		},
    35  		{
    36  			name:        "request_nil",
    37  			giveRequest: nil,
    38  			giveBid: &entities.PbsOrtbBid{
    39  				Bid: &openrtb2.Bid{
    40  					Ext: json.RawMessage(`{"dsa":{"behalf":"` + validBehalf + `","paid":"` + validPaid + `","adrender":1}}`),
    41  				},
    42  			},
    43  			wantError: nil,
    44  		},
    45  		{
    46  			name: "not_required_and_bid_is_nil",
    47  			giveRequest: &openrtb_ext.RequestWrapper{
    48  				BidRequest: &openrtb2.BidRequest{
    49  					Regs: &openrtb2.Regs{
    50  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 0}}`),
    51  					},
    52  				},
    53  			},
    54  			giveBid:   nil,
    55  			wantError: nil,
    56  		},
    57  		{
    58  			name: "not_required_and_bid_dsa_is_valid",
    59  			giveRequest: &openrtb_ext.RequestWrapper{
    60  				BidRequest: &openrtb2.BidRequest{
    61  					Regs: &openrtb2.Regs{
    62  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 0,"pubrender":0}}`),
    63  					},
    64  				},
    65  			},
    66  			giveBid: &entities.PbsOrtbBid{
    67  				Bid: &openrtb2.Bid{
    68  					Ext: json.RawMessage(`{"dsa":{"behalf":"` + validBehalf + `","paid":"` + validPaid + `","adrender":1}}`),
    69  				},
    70  			},
    71  			wantError: nil,
    72  		},
    73  		{
    74  			name: "required_and_bid_is_nil",
    75  			giveRequest: &openrtb_ext.RequestWrapper{
    76  				BidRequest: &openrtb2.BidRequest{
    77  					Regs: &openrtb2.Regs{
    78  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2}}`),
    79  					},
    80  				},
    81  			},
    82  			giveBid:   nil,
    83  			wantError: ErrDsaMissing,
    84  		},
    85  		{
    86  			name: "required_and_bid_dsa_has_invalid_behalf",
    87  			giveRequest: &openrtb_ext.RequestWrapper{
    88  				BidRequest: &openrtb2.BidRequest{
    89  					Regs: &openrtb2.Regs{
    90  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2}}`),
    91  					},
    92  				},
    93  			},
    94  			giveBid: &entities.PbsOrtbBid{
    95  				Bid: &openrtb2.Bid{
    96  					Ext: json.RawMessage(`{"dsa":{"behalf":"` + invalidBehalf + `"}}`),
    97  				},
    98  			},
    99  			wantError: ErrBehalfTooLong,
   100  		},
   101  		{
   102  			name: "required_and_bid_dsa_has_invalid_paid",
   103  			giveRequest: &openrtb_ext.RequestWrapper{
   104  				BidRequest: &openrtb2.BidRequest{
   105  					Regs: &openrtb2.Regs{
   106  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2}}`),
   107  					},
   108  				},
   109  			},
   110  			giveBid: &entities.PbsOrtbBid{
   111  				Bid: &openrtb2.Bid{
   112  					Ext: json.RawMessage(`{"dsa":{"paid":"` + invalidPaid + `"}}`),
   113  				},
   114  			},
   115  			wantError: ErrPaidTooLong,
   116  		},
   117  		{
   118  			name: "required_and_neither_will_render",
   119  			giveRequest: &openrtb_ext.RequestWrapper{
   120  				BidRequest: &openrtb2.BidRequest{
   121  					Regs: &openrtb2.Regs{
   122  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2,"pubrender": 0}}`),
   123  					},
   124  				},
   125  			},
   126  			giveBid: &entities.PbsOrtbBid{
   127  				Bid: &openrtb2.Bid{
   128  					Ext: json.RawMessage(`{"dsa":{"adrender": 0}}`),
   129  				},
   130  			},
   131  			wantError: ErrNeitherWillRender,
   132  		},
   133  		{
   134  			name: "required_and_both_will_render",
   135  			giveRequest: &openrtb_ext.RequestWrapper{
   136  				BidRequest: &openrtb2.BidRequest{
   137  					Regs: &openrtb2.Regs{
   138  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2,"pubrender": 2}}`),
   139  					},
   140  				},
   141  			},
   142  			giveBid: &entities.PbsOrtbBid{
   143  				Bid: &openrtb2.Bid{
   144  					Ext: json.RawMessage(`{"dsa":{"adrender": 1}}`),
   145  				},
   146  			},
   147  			wantError: ErrBothWillRender,
   148  		},
   149  		{
   150  			name: "required_and_bid_dsa_is_valid",
   151  			giveRequest: &openrtb_ext.RequestWrapper{
   152  				BidRequest: &openrtb2.BidRequest{
   153  					Regs: &openrtb2.Regs{
   154  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2,"pubrender": 0}}`),
   155  					},
   156  				},
   157  			},
   158  			giveBid: &entities.PbsOrtbBid{
   159  				Bid: &openrtb2.Bid{
   160  					Ext: json.RawMessage(`{"dsa":{"behalf":"` + validBehalf + `","paid":"` + validPaid + `","adrender":1}}`),
   161  				},
   162  			},
   163  			wantError: nil,
   164  		},
   165  		{
   166  			name: "required_and_bid_dsa_is_valid_no_pubrender",
   167  			giveRequest: &openrtb_ext.RequestWrapper{
   168  				BidRequest: &openrtb2.BidRequest{
   169  					Regs: &openrtb2.Regs{
   170  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2}}`),
   171  					},
   172  				},
   173  			},
   174  			giveBid: &entities.PbsOrtbBid{
   175  				Bid: &openrtb2.Bid{
   176  					Ext: json.RawMessage(`{"dsa":{"behalf":"` + validBehalf + `","paid":"` + validPaid + `","adrender":2}}`),
   177  				},
   178  			},
   179  			wantError: nil,
   180  		},
   181  		{
   182  			name: "required_and_bid_dsa_is_valid_no_adrender",
   183  			giveRequest: &openrtb_ext.RequestWrapper{
   184  				BidRequest: &openrtb2.BidRequest{
   185  					Regs: &openrtb2.Regs{
   186  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2, "pubrender": 0}}`),
   187  					},
   188  				},
   189  			},
   190  			giveBid: &entities.PbsOrtbBid{
   191  				Bid: &openrtb2.Bid{
   192  					Ext: json.RawMessage(`{"dsa":{"behalf":"` + validBehalf + `","paid":"` + validPaid + `"}}`),
   193  				},
   194  			},
   195  			wantError: nil,
   196  		},
   197  	}
   198  
   199  	for _, tt := range tests {
   200  		t.Run(tt.name, func(t *testing.T) {
   201  			err := Validate(tt.giveRequest, tt.giveBid)
   202  			if tt.wantError != nil {
   203  				assert.Equal(t, err, tt.wantError)
   204  			} else {
   205  				assert.NoError(t, err)
   206  			}
   207  		})
   208  	}
   209  }
   210  
   211  func TestDSARequired(t *testing.T) {
   212  	tests := []struct {
   213  		name         string
   214  		giveReqDSA   *openrtb_ext.ExtRegsDSA
   215  		wantRequired bool
   216  	}{
   217  		{
   218  			name:         "nil",
   219  			giveReqDSA:   nil,
   220  			wantRequired: false,
   221  		},
   222  		{
   223  			name: "nil_required",
   224  			giveReqDSA: &openrtb_ext.ExtRegsDSA{
   225  				Required: nil,
   226  			},
   227  			wantRequired: false,
   228  		},
   229  		{
   230  			name: "not_required",
   231  			giveReqDSA: &openrtb_ext.ExtRegsDSA{
   232  				Required: ptrutil.ToPtr[int8](0),
   233  			},
   234  			wantRequired: false,
   235  		},
   236  		{
   237  			name: "not_required_supported",
   238  			giveReqDSA: &openrtb_ext.ExtRegsDSA{
   239  				Required: ptrutil.ToPtr[int8](1),
   240  			},
   241  			wantRequired: false,
   242  		},
   243  		{
   244  			name: "required",
   245  			giveReqDSA: &openrtb_ext.ExtRegsDSA{
   246  				Required: ptrutil.ToPtr[int8](2),
   247  			},
   248  			wantRequired: true,
   249  		},
   250  		{
   251  			name: "required_online_platform",
   252  			giveReqDSA: &openrtb_ext.ExtRegsDSA{
   253  				Required: ptrutil.ToPtr[int8](3),
   254  			},
   255  			wantRequired: true,
   256  		},
   257  	}
   258  
   259  	for _, tt := range tests {
   260  		t.Run(tt.name, func(t *testing.T) {
   261  			required := dsaRequired(tt.giveReqDSA)
   262  			assert.Equal(t, tt.wantRequired, required)
   263  		})
   264  	}
   265  }
   266  
   267  func TestGetReqDSA(t *testing.T) {
   268  	tests := []struct {
   269  		name        string
   270  		giveRequest *openrtb_ext.RequestWrapper
   271  		expectedDSA *openrtb_ext.ExtRegsDSA
   272  	}{
   273  		{
   274  			name:        "req_is_nil",
   275  			giveRequest: nil,
   276  			expectedDSA: nil,
   277  		},
   278  		{
   279  			name: "bidrequest_is_nil",
   280  			giveRequest: &openrtb_ext.RequestWrapper{
   281  				BidRequest: nil,
   282  			},
   283  			expectedDSA: nil,
   284  		},
   285  		{
   286  			name: "req.regs_is_nil",
   287  			giveRequest: &openrtb_ext.RequestWrapper{
   288  				BidRequest: &openrtb2.BidRequest{
   289  					Regs: nil,
   290  				},
   291  			},
   292  			expectedDSA: nil,
   293  		},
   294  		{
   295  			name: "req.regs.ext_is_nil",
   296  			giveRequest: &openrtb_ext.RequestWrapper{
   297  				BidRequest: &openrtb2.BidRequest{
   298  					Regs: &openrtb2.Regs{
   299  						Ext: nil,
   300  					},
   301  				},
   302  			},
   303  			expectedDSA: nil,
   304  		},
   305  		{
   306  			name: "req.regs.ext_is_empty",
   307  			giveRequest: &openrtb_ext.RequestWrapper{
   308  				BidRequest: &openrtb2.BidRequest{
   309  					Regs: &openrtb2.Regs{
   310  						Ext: json.RawMessage(`{}`),
   311  					},
   312  				},
   313  			},
   314  			expectedDSA: nil,
   315  		},
   316  		{
   317  			name: "req.regs.ext_dsa_is_populated",
   318  			giveRequest: &openrtb_ext.RequestWrapper{
   319  				BidRequest: &openrtb2.BidRequest{
   320  					Regs: &openrtb2.Regs{
   321  						Ext: json.RawMessage(`{"dsa": {"dsarequired": 2}}`),
   322  					},
   323  				},
   324  			},
   325  			expectedDSA: &openrtb_ext.ExtRegsDSA{
   326  				Required: ptrutil.ToPtr[int8](2),
   327  			},
   328  		},
   329  	}
   330  
   331  	for _, tt := range tests {
   332  		t.Run(tt.name, func(t *testing.T) {
   333  			dsa := getReqDSA(tt.giveRequest)
   334  			assert.Equal(t, tt.expectedDSA, dsa)
   335  		})
   336  	}
   337  }
   338  
   339  func TestGetBidDSA(t *testing.T) {
   340  	tests := []struct {
   341  		name        string
   342  		bid         *entities.PbsOrtbBid
   343  		expectedDSA *openrtb_ext.ExtBidDSA
   344  	}{
   345  		{
   346  			name:        "bid_is_nil",
   347  			bid:         nil,
   348  			expectedDSA: nil,
   349  		},
   350  		{
   351  			name: "bid.bid_is_nil",
   352  			bid: &entities.PbsOrtbBid{
   353  				Bid: nil,
   354  			},
   355  			expectedDSA: nil,
   356  		},
   357  		{
   358  			name: "bid.bid.ext_is_nil",
   359  			bid: &entities.PbsOrtbBid{
   360  				Bid: &openrtb2.Bid{
   361  					Ext: nil,
   362  				},
   363  			},
   364  			expectedDSA: nil,
   365  		},
   366  		{
   367  			name: "bid.bid.ext_is_empty",
   368  			bid: &entities.PbsOrtbBid{
   369  				Bid: &openrtb2.Bid{
   370  					Ext: json.RawMessage(`{}`),
   371  				},
   372  			},
   373  			expectedDSA: nil,
   374  		},
   375  		{
   376  			name: "bid.bid.ext.dsa_is_populated",
   377  			bid: &entities.PbsOrtbBid{
   378  				Bid: &openrtb2.Bid{
   379  					Ext: json.RawMessage(`{"dsa": {"behalf":"test1","paid":"test2","adrender":1}}`),
   380  				},
   381  			},
   382  			expectedDSA: &openrtb_ext.ExtBidDSA{
   383  				Behalf:   "test1",
   384  				Paid:     "test2",
   385  				AdRender: ptrutil.ToPtr[int8](1),
   386  			},
   387  		},
   388  	}
   389  
   390  	for _, tt := range tests {
   391  		t.Run(tt.name, func(t *testing.T) {
   392  			dsa := getBidDSA(tt.bid)
   393  			assert.Equal(t, tt.expectedDSA, dsa)
   394  		})
   395  	}
   396  }