| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io/ioutil" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestClient_WAFs(t *testing.T) { |
| 10 |
t.Parallel() |
| 11 |
|
| 12 |
var err error |
| 13 |
var tv *Version |
| 14 |
record(t, "wafs/version", func(c *Client) { |
| 15 |
tv = testVersion(t, c) |
| 16 |
}) |
| 17 |
|
| 18 |
// Enable logging on the service - we cannot create wafs without logging |
| 19 |
// enabled |
| 20 |
record(t, "wafs/logging/create", func(c *Client) { |
| 21 |
_, err = c.CreateSyslog(&CreateSyslogInput{ |
| 22 |
Service: testServiceID, |
| 23 |
Version: tv.Number, |
| 24 |
Name: "test-syslog", |
| 25 |
Address: "example.com", |
| 26 |
Hostname: "example.com", |
| 27 |
Port: 1234, |
| 28 |
Token: "abcd1234", |
| 29 |
Format: "format", |
| 30 |
FormatVersion: 2, |
| 31 |
MessageType: "classic", |
| 32 |
}) |
| 33 |
}) |
| 34 |
if err != nil { |
| 35 |
t.Fatal(err) |
| 36 |
} |
| 37 |
defer func() { |
| 38 |
record(t, "wafs/logging/cleanup", func(c *Client) { |
| 39 |
c.DeleteSyslog(&DeleteSyslogInput{ |
| 40 |
Service: testServiceID, |
| 41 |
Version: tv.Number, |
| 42 |
Name: "test-syslog", |
| 43 |
}) |
| 44 |
}) |
| 45 |
}() |
| 46 |
|
| 47 |
// Create a condition - we cannot create a waf without attaching a condition |
| 48 |
var condition *Condition |
| 49 |
record(t, "wafs/condition/create", func(c *Client) { |
| 50 |
condition, err = c.CreateCondition(&CreateConditionInput{ |
| 51 |
Service: testServiceID, |
| 52 |
Version: tv.Number, |
| 53 |
Name: "test-waf-condition", |
| 54 |
Statement: "req.url~+\"index.html\"", |
| 55 |
Type: "PREFETCH", // This must be a prefetch condition |
| 56 |
Priority: 1, |
| 57 |
}) |
| 58 |
}) |
| 59 |
if err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
defer func() { |
| 63 |
record(t, "wafs/condition/cleanup", func(c *Client) { |
| 64 |
c.DeleteCondition(&DeleteConditionInput{ |
| 65 |
Service: testServiceID, |
| 66 |
Version: tv.Number, |
| 67 |
Name: "test-waf-condition", |
| 68 |
}) |
| 69 |
}) |
| 70 |
}() |
| 71 |
|
| 72 |
// Create a response object |
| 73 |
var ro *ResponseObject |
| 74 |
record(t, "wafs/response_object/create", func(c *Client) { |
| 75 |
ro, err = c.CreateResponseObject(&CreateResponseObjectInput{ |
| 76 |
Service: testServiceID, |
| 77 |
Version: tv.Number, |
| 78 |
Name: "test-response-object", |
| 79 |
Status: 200, |
| 80 |
Response: "Ok", |
| 81 |
Content: "abcd", |
| 82 |
ContentType: "text/plain", |
| 83 |
}) |
| 84 |
}) |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
defer func() { |
| 89 |
record(t, "wafs/response_object/cleanup", func(c *Client) { |
| 90 |
c.DeleteResponseObject(&DeleteResponseObjectInput{ |
| 91 |
Service: testServiceID, |
| 92 |
Version: tv.Number, |
| 93 |
Name: ro.Name, |
| 94 |
}) |
| 95 |
}) |
| 96 |
}() |
| 97 |
|
| 98 |
// Create |
| 99 |
var waf *WAF |
| 100 |
record(t, "wafs/create", func(c *Client) { |
| 101 |
waf, err = c.CreateWAF(&CreateWAFInput{ |
| 102 |
Service: testServiceID, |
| 103 |
Version: tv.Number, |
| 104 |
PrefetchCondition: condition.Name, |
| 105 |
Response: ro.Name, |
| 106 |
}) |
| 107 |
}) |
| 108 |
if err != nil { |
| 109 |
t.Fatal(err) |
| 110 |
} |
| 111 |
|
| 112 |
// List |
| 113 |
var wafs []*WAF |
| 114 |
record(t, "wafs/list", func(c *Client) { |
| 115 |
wafs, err = c.ListWAFs(&ListWAFsInput{ |
| 116 |
Service: testServiceID, |
| 117 |
Version: tv.Number, |
| 118 |
}) |
| 119 |
}) |
| 120 |
if err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
if len(wafs) < 1 { |
| 124 |
t.Errorf("bad wafs: %v", wafs) |
| 125 |
} |
| 126 |
|
| 127 |
// Ensure deleted |
| 128 |
defer func() { |
| 129 |
record(t, "wafs/cleanup", func(c *Client) { |
| 130 |
c.DeleteWAF(&DeleteWAFInput{ |
| 131 |
Service: testServiceID, |
| 132 |
Version: tv.Number, |
| 133 |
ID: waf.ID, |
| 134 |
}) |
| 135 |
}) |
| 136 |
}() |
| 137 |
|
| 138 |
// Get |
| 139 |
var nwaf *WAF |
| 140 |
record(t, "wafs/get", func(c *Client) { |
| 141 |
nwaf, err = c.GetWAF(&GetWAFInput{ |
| 142 |
Service: testServiceID, |
| 143 |
Version: tv.Number, |
| 144 |
ID: waf.ID, |
| 145 |
}) |
| 146 |
}) |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
if nwaf.ID != waf.ID { |
| 151 |
t.Errorf("expected %q to be %q", nwaf.ID, waf.ID) |
| 152 |
} |
| 153 |
|
| 154 |
// Update |
| 155 |
// Create a new response object to attach |
| 156 |
var nro *ResponseObject |
| 157 |
record(t, "wafs/response_object/create_another", func(c *Client) { |
| 158 |
nro, err = c.CreateResponseObject(&CreateResponseObjectInput{ |
| 159 |
Service: testServiceID, |
| 160 |
Version: tv.Number, |
| 161 |
Name: "test-response-object-2", |
| 162 |
Status: 200, |
| 163 |
Response: "Ok", |
| 164 |
Content: "efgh", |
| 165 |
ContentType: "text/plain", |
| 166 |
}) |
| 167 |
}) |
| 168 |
if err != nil { |
| 169 |
t.Fatal(err) |
| 170 |
} |
| 171 |
defer func() { |
| 172 |
record(t, "wafs/response_object/cleanup_another", func(c *Client) { |
| 173 |
c.DeleteResponseObject(&DeleteResponseObjectInput{ |
| 174 |
Service: testServiceID, |
| 175 |
Version: tv.Number, |
| 176 |
Name: nro.Name, |
| 177 |
}) |
| 178 |
}) |
| 179 |
}() |
| 180 |
var uwaf *WAF |
| 181 |
record(t, "wafs/update", func(c *Client) { |
| 182 |
uwaf, err = c.UpdateWAF(&UpdateWAFInput{ |
| 183 |
Service: testServiceID, |
| 184 |
Version: tv.Number, |
| 185 |
ID: waf.ID, |
| 186 |
Response: nro.Name, |
| 187 |
}) |
| 188 |
}) |
| 189 |
if err != nil { |
| 190 |
t.Fatal(err) |
| 191 |
} |
| 192 |
if uwaf.Response != "test-response-object-2" { |
| 193 |
t.Errorf("bad name: %q", uwaf.Response) |
| 194 |
} |
| 195 |
|
| 196 |
// Delete |
| 197 |
record(t, "wafs/delete", func(c *Client) { |
| 198 |
err = c.DeleteWAF(&DeleteWAFInput{ |
| 199 |
Service: testServiceID, |
| 200 |
Version: tv.Number, |
| 201 |
ID: waf.ID, |
| 202 |
}) |
| 203 |
}) |
| 204 |
if err != nil { |
| 205 |
t.Fatal(err) |
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
func TestClient_ListWAFs_validation(t *testing.T) { |
| 211 |
var err error |
| 212 |
_, err = testClient.ListWAFs(&ListWAFsInput{ |
| 213 |
Service: "", |
| 214 |
}) |
| 215 |
if err != ErrMissingService { |
| 216 |
t.Errorf("bad error: %s", err) |
| 217 |
} |
| 218 |
|
| 219 |
_, err = testClient.ListWAFs(&ListWAFsInput{ |
| 220 |
Service: "foo", |
| 221 |
Version: 0, |
| 222 |
}) |
| 223 |
if err != ErrMissingVersion { |
| 224 |
t.Errorf("bad error: %s", err) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func TestClient_CreateWAF_validation(t *testing.T) { |
| 229 |
var err error |
| 230 |
_, err = testClient.CreateWAF(&CreateWAFInput{ |
| 231 |
Service: "", |
| 232 |
}) |
| 233 |
if err != ErrMissingService { |
| 234 |
t.Errorf("bad error: %s", err) |
| 235 |
} |
| 236 |
|
| 237 |
_, err = testClient.CreateWAF(&CreateWAFInput{ |
| 238 |
Service: "foo", |
| 239 |
Version: 0, |
| 240 |
}) |
| 241 |
if err != ErrMissingVersion { |
| 242 |
t.Errorf("bad error: %s", err) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func TestClient_GetWAF_validation(t *testing.T) { |
| 247 |
var err error |
| 248 |
_, err = testClient.GetWAF(&GetWAFInput{ |
| 249 |
Service: "", |
| 250 |
}) |
| 251 |
if err != ErrMissingService { |
| 252 |
t.Errorf("bad error: %s", err) |
| 253 |
} |
| 254 |
|
| 255 |
_, err = testClient.GetWAF(&GetWAFInput{ |
| 256 |
Service: "foo", |
| 257 |
Version: 0, |
| 258 |
}) |
| 259 |
if err != ErrMissingVersion { |
| 260 |
t.Errorf("bad error: %s", err) |
| 261 |
} |
| 262 |
|
| 263 |
_, err = testClient.GetWAF(&GetWAFInput{ |
| 264 |
Service: "foo", |
| 265 |
Version: 1, |
| 266 |
ID: "", |
| 267 |
}) |
| 268 |
if err != ErrMissingWAFID { |
| 269 |
t.Errorf("bad error: %s", err) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// |
| 274 |
// func TestClient_UpdateWAF_validation(t *testing.T) { |
| 275 |
// var err error |
| 276 |
// _, err = testClient.UpdateWAF(&UpdateWAFInput{ |
| 277 |
// Service: "", |
| 278 |
// }) |
| 279 |
// if err != ErrMissingService { |
| 280 |
// t.Errorf("bad error: %s", err) |
| 281 |
// } |
| 282 |
// |
| 283 |
// _, err = testClient.UpdateWAF(&UpdateWAFInput{ |
| 284 |
// Service: "foo", |
| 285 |
// Version: 0, |
| 286 |
// }) |
| 287 |
// if err != ErrMissingVersion { |
| 288 |
// t.Errorf("bad error: %s", err) |
| 289 |
// } |
| 290 |
// |
| 291 |
// _, err = testClient.UpdateWAF(&UpdateWAFInput{ |
| 292 |
// Service: "foo", |
| 293 |
// Version: 1, |
| 294 |
// WAFID: "", |
| 295 |
// }) |
| 296 |
// if err != ErrMissingWAFID { |
| 297 |
// t.Errorf("bad error: %s", err) |
| 298 |
// } |
| 299 |
// } |
| 300 |
// |
| 301 |
// func TestClient_DeleteWAF_validation(t *testing.T) { |
| 302 |
// var err error |
| 303 |
// err = testClient.DeleteWAF(&DeleteWAFInput{ |
| 304 |
// Service: "", |
| 305 |
// }) |
| 306 |
// if err != ErrMissingService { |
| 307 |
// t.Errorf("bad error: %s", err) |
| 308 |
// } |
| 309 |
// |
| 310 |
// err = testClient.DeleteWAF(&DeleteWAFInput{ |
| 311 |
// Service: "foo", |
| 312 |
// Version: 0, |
| 313 |
// }) |
| 314 |
// if err != ErrMissingVersion { |
| 315 |
// t.Errorf("bad error: %s", err) |
| 316 |
// } |
| 317 |
// |
| 318 |
// err = testClient.DeleteWAF(&DeleteWAFInput{ |
| 319 |
// Service: "foo", |
| 320 |
// Version: 1, |
| 321 |
// WAFID: "", |
| 322 |
// }) |
| 323 |
// if err != ErrMissingWAFID { |
| 324 |
// t.Errorf("bad error: %s", err) |
| 325 |
// } |
| 326 |
// } |
| 327 |
|
| 328 |
func TestUpdateWAFRuleStatusesInput_validate(t *testing.T) { |
| 329 |
tests := []struct { |
| 330 |
description string |
| 331 |
input UpdateWAFRuleStatusInput |
| 332 |
expected error |
| 333 |
}{ |
| 334 |
{ |
| 335 |
description: "Accepts valid input", |
| 336 |
input: UpdateWAFRuleStatusInput{ |
| 337 |
ID: "as098k-8104", |
| 338 |
RuleID: 8104, |
| 339 |
Service: "108asj1", |
| 340 |
WAF: "as098k", |
| 341 |
Status: "block", |
| 342 |
}, |
| 343 |
expected: nil, |
| 344 |
}, |
| 345 |
{ |
| 346 |
description: "Rejects input with missing int field", |
| 347 |
input: UpdateWAFRuleStatusInput{ |
| 348 |
ID: "as098k-8104", |
| 349 |
Service: "108asj1", |
| 350 |
WAF: "as098k", |
| 351 |
Status: "block", |
| 352 |
}, |
| 353 |
expected: ErrMissingRuleID, |
| 354 |
}, |
| 355 |
{ |
| 356 |
description: "Rejects input with missing string field", |
| 357 |
input: UpdateWAFRuleStatusInput{ |
| 358 |
ID: "as098k-8104", |
| 359 |
RuleID: 8104, |
| 360 |
WAF: "as098k", |
| 361 |
Status: "block", |
| 362 |
}, |
| 363 |
expected: ErrMissingService, |
| 364 |
}, |
| 365 |
} |
| 366 |
for _, testcase := range tests { |
| 367 |
err := testcase.input.validate() |
| 368 |
if err != testcase.expected { |
| 369 |
t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err) |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
func TestUpdateWAFRuleTagStatusInput_validate(t *testing.T) { |
| 375 |
tests := []struct { |
| 376 |
description string |
| 377 |
input UpdateWAFRuleTagStatusInput |
| 378 |
expected error |
| 379 |
}{ |
| 380 |
{ |
| 381 |
description: "Accepts valid input", |
| 382 |
input: UpdateWAFRuleTagStatusInput{ |
| 383 |
Tag: "lala tag la", |
| 384 |
Service: "108asj1", |
| 385 |
WAF: "as098k", |
| 386 |
Status: "block", |
| 387 |
}, |
| 388 |
expected: nil, |
| 389 |
}, |
| 390 |
{ |
| 391 |
description: "Rejects input with missing string field", |
| 392 |
input: UpdateWAFRuleTagStatusInput{ |
| 393 |
Service: "108asj1", |
| 394 |
WAF: "as098k", |
| 395 |
Status: "block", |
| 396 |
}, |
| 397 |
expected: ErrMissingTag, |
| 398 |
}, |
| 399 |
} |
| 400 |
for _, testcase := range tests { |
| 401 |
err := testcase.input.validate() |
| 402 |
if err != testcase.expected { |
| 403 |
t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err) |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
func TestGetWAFRuleStatusesInput_formatFilters(t *testing.T) { |
| 409 |
tests := []struct { |
| 410 |
description string |
| 411 |
filters GetWAFRuleStatusesFilters |
| 412 |
expected map[string]string |
| 413 |
}{ |
| 414 |
{ |
| 415 |
description: "converts both strings and ints to strings", |
| 416 |
filters: GetWAFRuleStatusesFilters{ |
| 417 |
Status: "log", |
| 418 |
Accuracy: 10, |
| 419 |
Version: "180ad", |
| 420 |
}, |
| 421 |
expected: map[string]string{ |
| 422 |
"filter[status]": "log", |
| 423 |
"filter[rule][accuracy]": "10", |
| 424 |
"filter[rule][version]": "180ad", |
| 425 |
}, |
| 426 |
}, |
| 427 |
{ |
| 428 |
description: "converts arrays to strings", |
| 429 |
filters: GetWAFRuleStatusesFilters{ |
| 430 |
Status: "log", |
| 431 |
Version: "181ad", |
| 432 |
Tags: []int{18, 1, 1093, 86308}, |
| 433 |
}, |
| 434 |
expected: map[string]string{ |
| 435 |
"filter[status]": "log", |
| 436 |
"filter[rule][version]": "181ad", |
| 437 |
"include": "18,1,1093,86308", |
| 438 |
}, |
| 439 |
}, |
| 440 |
} |
| 441 |
for _, testcase := range tests { |
| 442 |
input := GetWAFRuleStatusesInput{ |
| 443 |
Filters: testcase.filters, |
| 444 |
} |
| 445 |
answer := input.formatFilters() |
| 446 |
if len(answer) != len(testcase.expected) { |
| 447 |
t.Errorf("In test %s: Expected map with %d entries,got one with %d", testcase.description, len(testcase.expected), len(answer)) |
| 448 |
} |
| 449 |
for key, value := range testcase.expected { |
| 450 |
if answer[key] != value { |
| 451 |
t.Errorf("In test %s: Expected %s key to have value %s, got %s", testcase.description, key, value, answer[key]) |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
func TestGetPages(t *testing.T) { |
| 458 |
tests := []struct { |
| 459 |
description string |
| 460 |
input string |
| 461 |
expectedPages paginationInfo |
| 462 |
expectedErr error |
| 463 |
}{ |
| 464 |
{ |
| 465 |
description: "returns the next page", |
| 466 |
input: `{"links": {"next": "https://google.com/2"}, "data": []}`, |
| 467 |
expectedPages: paginationInfo{ |
| 468 |
Next: "https://google.com/2", |
| 469 |
}, |
| 470 |
}, |
| 471 |
{ |
| 472 |
description: "returns multiple pages", |
| 473 |
input: `{"links": {"next": "https://google.com/2", "first": "https://google.com/1"}, "data": []}`, |
| 474 |
expectedPages: paginationInfo{ |
| 475 |
First: "https://google.com/1", |
| 476 |
Next: "https://google.com/2", |
| 477 |
}, |
| 478 |
}, |
| 479 |
{ |
| 480 |
description: "returns no pages", |
| 481 |
input: `{"data": []}`, |
| 482 |
expectedPages: paginationInfo{}, |
| 483 |
}, |
| 484 |
} |
| 485 |
for _, testcase := range tests { |
| 486 |
pages, reader, err := getPages(bytes.NewReader([]byte(testcase.input))) |
| 487 |
if pages != testcase.expectedPages { |
| 488 |
t.Errorf("Test %s: Expected pages %+v, got %+v", testcase.description, testcase.expectedPages, pages) |
| 489 |
} |
| 490 |
|
| 491 |
// we expect to be able to get the original input out again |
| 492 |
resultBytes, _ := ioutil.ReadAll(reader) |
| 493 |
if string(resultBytes) != testcase.input { |
| 494 |
t.Errorf("Test %s: Expected body %s, got %s", testcase.description, testcase.input, string(resultBytes)) |
| 495 |
} |
| 496 |
if err != testcase.expectedErr { |
| 497 |
t.Errorf("Test %s: Expected error %v, got %v", testcase.description, testcase.expectedErr, err) |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
func TestUpdateWAFConfigSetInput_validate(t *testing.T) { |
| 503 |
tests := []struct { |
| 504 |
description string |
| 505 |
input UpdateWAFConfigSetInput |
| 506 |
expected error |
| 507 |
}{ |
| 508 |
{ |
| 509 |
description: "Accepts valid input", |
| 510 |
input: UpdateWAFConfigSetInput{ |
| 511 |
WAFList: []ConfigSetWAFs{{ID: "derpID"}}, |
| 512 |
ConfigSetID: "derpConfigSet", |
| 513 |
}, |
| 514 |
expected: nil, |
| 515 |
}, |
| 516 |
} |
| 517 |
for _, testcase := range tests { |
| 518 |
err := testcase.input.validate() |
| 519 |
if err != testcase.expected { |
| 520 |
t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err) |
| 521 |
} |
| 522 |
} |
| 523 |
} |