github.com/prebid/prebid-server/v2@v2.18.0/adapters/consumable/params_test.go (about)

     1  package consumable
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  )
     9  
    10  // This file actually intends to test static/bidder-params/consumable.json
    11  //
    12  // These also validate the format of the external API: request.imp[i].ext.prebid.bidder.consumable
    13  
    14  // TestValidParams makes sure that the 33across schema accepts all imp.ext fields which we intend to support.
    15  func TestValidParams(t *testing.T) {
    16  	validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    17  	if err != nil {
    18  		t.Fatalf("Failed to fetch the json-schemas. %v", err)
    19  	}
    20  
    21  	for _, validParam := range validParams {
    22  		if err := validator.Validate(openrtb_ext.BidderConsumable, json.RawMessage(validParam)); err != nil {
    23  			t.Errorf("Schema rejected Consumable params: %s", validParam)
    24  		}
    25  	}
    26  }
    27  
    28  // TestInvalidParams makes sure that the Consumable schema rejects all the imp.ext fields we don't support.
    29  func TestInvalidParams(t *testing.T) {
    30  	validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    31  	if err != nil {
    32  		t.Fatalf("Failed to fetch the json-schemas. %v", err)
    33  	}
    34  
    35  	for _, invalidParam := range invalidParams {
    36  		if err := validator.Validate(openrtb_ext.BidderConsumable, json.RawMessage(invalidParam)); err == nil {
    37  			t.Errorf("Schema allowed unexpected Consumable params: %s", invalidParam)
    38  		}
    39  	}
    40  }
    41  
    42  var validParams = []string{
    43  	`{"networkId": 22, "siteId": 1, "unitId": 101, "unitName": "unit-1"}`,
    44  	`{"networkId": 22, "siteId": 1, "unitId": 101, "unitName": "-unit-1"}`, // unitName can start with a dash
    45  	`{"networkId": 22, "siteId": 1, "unitId": 101}`,                        // unitName can be omitted (although prebid.js doesn't allow that)
    46  	`{"placementId": "abcdjk232"}`,
    47  }
    48  
    49  var invalidParams = []string{
    50  	`{"networkId": 22, "siteId": 1, "unitId": 101, "unitName": "--unit-1"}`, // unitName cannot start --
    51  	`{"networkId": 22, "siteId": 1, "unitId": 101, "unitName": "unit 1"}`,   // unitName cannot contain spaces
    52  	`{"networkId": 22, "siteId": 1, "unitId": 101, "unitName": "1unit-1"}`,  // unitName cannot start with a digit
    53  	`{"networkId": "22", "siteId": 1, "unitId": 101, "unitName": 11}`,       // networkId must be a number
    54  	`{"networkId": 22, "siteId": "1", "unitId": 101, "unitName": 11}`,       // siteId must be a number
    55  	`{"networkId": 22, "siteId": 1, "unitId": "101", "unitName": 11}`,       // unitId must be a number
    56  	`{"networkId": 22, "siteId": 1, "unitId": 101, "unitName": 11}`,         // unitName must be a string
    57  	`{"siteId": 1, "unitId": 101, "unitName": 11}`,                          // networkId must be present
    58  	`{"networkId": 22, "unitId": 101, "unitName": 11}`,                      // siteId must be present
    59  	`{"siteId": 1, "networkId": 22, "unitName": 11}`,                        // unitId must be present
    60  	`{"placementId": "---abjk;jkewj;k;jwejklfs}`,                            // placementId must be alphanumeric
    61  }