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