github.com/prebid/prebid-server/v2@v2.18.0/config/events_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestIsValidCreateElement(t *testing.T) {
    10  	testCases := []struct {
    11  		description   string
    12  		createElement VASTEventElement
    13  		valid         bool
    14  	}{
    15  		{
    16  			description:   "Empty create element",
    17  			createElement: "",
    18  			valid:         false,
    19  		},
    20  		{
    21  			description:   "Invalid create element",
    22  			createElement: "invalid_element",
    23  			valid:         false,
    24  		},
    25  		{
    26  			description:   "Valid create element",
    27  			createElement: "tracking",
    28  			valid:         true,
    29  		},
    30  		{
    31  			description:   "Case sensitivity of create element",
    32  			createElement: "Tracking",
    33  			valid:         false,
    34  		},
    35  	}
    36  	for _, test := range testCases {
    37  		isValid := test.createElement.isValid()
    38  		assert.Equal(t, test.valid, isValid, test.description)
    39  	}
    40  
    41  }
    42  
    43  func TestIsValidTrackingEventType(t *testing.T) {
    44  	testCases := []struct {
    45  		description string
    46  		vastEvent   VASTEvent
    47  		valid       bool
    48  	}{
    49  		{
    50  			description: "Empty type",
    51  			vastEvent:   VASTEvent{},
    52  			valid:       false,
    53  		},
    54  		{
    55  			description: "Empty type for tracking event",
    56  			vastEvent: VASTEvent{
    57  				CreateElement: TrackingVASTElement,
    58  			},
    59  			valid: false,
    60  		},
    61  		{
    62  			description: "Invalid type for tracking event",
    63  			vastEvent: VASTEvent{
    64  				CreateElement: TrackingVASTElement,
    65  				Type:          "invalid_type",
    66  			},
    67  			valid: false,
    68  		},
    69  		{
    70  			description: "Valid type for tracking event",
    71  			vastEvent: VASTEvent{
    72  				CreateElement: TrackingVASTElement,
    73  				Type:          MidPoint,
    74  			},
    75  			valid: true,
    76  		},
    77  		{
    78  			description: "Case sensitivity of type for tracking event",
    79  			vastEvent: VASTEvent{
    80  				CreateElement: TrackingVASTElement,
    81  				Type:          "COMplete",
    82  			},
    83  			valid: false,
    84  		},
    85  	}
    86  	for _, test := range testCases {
    87  		isValid := test.vastEvent.Type.isValid()
    88  		assert.Equal(t, test.valid, isValid, test.description)
    89  	}
    90  }
    91  
    92  func TestIsValidURL(t *testing.T) {
    93  	testCases := []struct {
    94  		description string
    95  		url         string
    96  		valid       bool
    97  	}{
    98  		{
    99  			description: "Empty Url",
   100  			url:         "",
   101  			valid:       false,
   102  		},
   103  		{
   104  			description: "Capital Domain name",
   105  			url:         "http://PREBID.ORG",
   106  			valid:       true,
   107  		},
   108  		{
   109  			description: "Url with Macros",
   110  			url:         "https://PBS_HOST/event?t=##PBS-EVENTTYPE##&vtype=##PBS-VASTEVENT##&b=##PBS-BIDID##&f=i&a=##PBS-ACCOUNTID##&ts=##PBS-TIMESTAMP##&bidder=##PBS-BIDDER##&int=##PBS-INTEGRATION##&mt=##PBS-MEDIATYPE##&ch=##PBS-CHANNEL##&aid=##PBS-AUCTIONID##&l=##PBS-LINEID##",
   111  			valid:       true,
   112  		},
   113  		{
   114  			description: "Invalid http syntax",
   115  			url:         "HTTP://PREBID.ORG",
   116  			valid:       false,
   117  		},
   118  		{
   119  			description: "Invalid protocol",
   120  			url:         "HTTPE://PREBID.ORG",
   121  			valid:       false,
   122  		},
   123  		{
   124  			description: "Double colon after domain name",
   125  			url:         "https://prebid.org::8000",
   126  			valid:       false,
   127  		},
   128  		{
   129  			description: "Url with Port",
   130  			url:         "https://prebid.org:8000",
   131  			valid:       true,
   132  		},
   133  		{
   134  			description: "Url with invalid Port",
   135  			url:         "https://prebid.org:100000",
   136  			valid:       false,
   137  		},
   138  	}
   139  	for _, test := range testCases {
   140  		isValid := isValidURL(test.url)
   141  		assert.Equal(t, test.valid, isValid, test.description)
   142  	}
   143  }
   144  
   145  func TestIsTrackingEvent(t *testing.T) {
   146  	testCases := []struct {
   147  		description string
   148  		vastEvent   VASTEvent
   149  		valid       bool
   150  	}{
   151  		{
   152  			description: "Empty Tracking Event",
   153  			vastEvent:   VASTEvent{},
   154  			valid:       false,
   155  		},
   156  		{
   157  			description: "Valid Tracking Event",
   158  			vastEvent: VASTEvent{
   159  				CreateElement: "tracking",
   160  			},
   161  			valid: true,
   162  		},
   163  		{
   164  			description: "Case Sensitivity in Tracking Event",
   165  			vastEvent: VASTEvent{
   166  				CreateElement: "Tracking",
   167  			},
   168  			valid: false,
   169  		},
   170  	}
   171  	for _, test := range testCases {
   172  		isValid := test.vastEvent.isTrackingEvent()
   173  		assert.Equal(t, test.valid, isValid, test.description)
   174  	}
   175  }
   176  
   177  func TestValidateVASTEvent(t *testing.T) {
   178  	testCases := []struct {
   179  		description string
   180  		vastEvent   VASTEvent
   181  		index       int
   182  		expectErr   bool
   183  	}{
   184  		{
   185  			description: "At least one URL - Default URL",
   186  			vastEvent: VASTEvent{
   187  				CreateElement:     "tracking",
   188  				Type:              "start",
   189  				ExcludeDefaultURL: false,
   190  				URLs:              nil,
   191  			},
   192  			index:     0,
   193  			expectErr: false,
   194  		},
   195  		{
   196  			description: "URLs has at least one URL, default URL is excluded",
   197  			vastEvent: VASTEvent{
   198  				CreateElement:     "impression",
   199  				ExcludeDefaultURL: true,
   200  				URLs:              []string{"http://mytracker.comm"},
   201  			},
   202  			index:     0,
   203  			expectErr: false,
   204  		},
   205  		{
   206  			description: "No URLs and default URL Excluded",
   207  			vastEvent: VASTEvent{
   208  				CreateElement:     "impression",
   209  				ExcludeDefaultURL: true,
   210  				URLs:              nil,
   211  			},
   212  			index:     0,
   213  			expectErr: true,
   214  		},
   215  		{
   216  			description: "Invalid Create Elemment",
   217  			vastEvent:   VASTEvent{},
   218  			index:       0,
   219  			expectErr:   true,
   220  		},
   221  		{
   222  			description: "Invalid Type",
   223  			vastEvent: VASTEvent{
   224  				CreateElement: "tracking",
   225  				Type:          "invalid_type",
   226  			},
   227  			index:     0,
   228  			expectErr: true,
   229  		},
   230  		{
   231  			description: "Invalid URLs",
   232  			vastEvent: VASTEvent{
   233  				CreateElement: "impression",
   234  				URLs:          []string{"http://url.com?k1=v1&k2=###PBS-MACRO##", "httpE://invalid.url"},
   235  			},
   236  			index:     0,
   237  			expectErr: true,
   238  		},
   239  		{
   240  			description: "Valid type but create element is other than tracking",
   241  			vastEvent: VASTEvent{
   242  				CreateElement: "impression",
   243  				Type:          "start",
   244  			},
   245  			index:     0,
   246  			expectErr: true,
   247  		},
   248  	}
   249  	for _, test := range testCases {
   250  		err := test.vastEvent.validate()
   251  		assert.Equal(t, !test.expectErr, err == nil, test.description)
   252  	}
   253  }
   254  
   255  func TestValidate(t *testing.T) {
   256  	testCases := []struct {
   257  		description string
   258  		events      Events
   259  		expectErr   bool
   260  	}{
   261  		{
   262  			description: "Empty default URL",
   263  			events: Events{
   264  				Enabled: true,
   265  			},
   266  			expectErr: true,
   267  		},
   268  		{
   269  			description: "Events are disabled. Skips validations",
   270  			events: Events{
   271  				Enabled:    false,
   272  				DefaultURL: "",
   273  			},
   274  			expectErr: false,
   275  		},
   276  		{
   277  			description: "No VAST Events and default URL present",
   278  			events: Events{
   279  				Enabled:    true,
   280  				DefaultURL: "http://prebid.org",
   281  			},
   282  			expectErr: false,
   283  		},
   284  		{
   285  			description: "Invalid VAST Event",
   286  			events: Events{
   287  				Enabled:    true,
   288  				DefaultURL: "http://prebid.org",
   289  				VASTEvents: []VASTEvent{
   290  					{},
   291  				},
   292  			},
   293  			expectErr: true,
   294  		},
   295  	}
   296  	for _, test := range testCases {
   297  		errs := test.events.validate(make([]error, 0))
   298  		assert.Equal(t, !test.expectErr, len(errs) == 0, test.description)
   299  	}
   300  }
   301  
   302  func TestValidateVASTEvents(t *testing.T) {
   303  	testCases := []struct {
   304  		description string
   305  		events      []VASTEvent
   306  		expectErr   bool
   307  	}{
   308  		{
   309  			description: "No Vast Events",
   310  			events:      nil,
   311  			expectErr:   false,
   312  		},
   313  		{
   314  
   315  			description: "Invalid Event Object",
   316  			events: []VASTEvent{
   317  				{
   318  					CreateElement: "impression",
   319  				},
   320  				{},
   321  			},
   322  			expectErr: true,
   323  		},
   324  	}
   325  	for _, test := range testCases {
   326  		err := validateVASTEvents(test.events)
   327  		assert.Equal(t, !test.expectErr, err == nil, test.description)
   328  	}
   329  }