| 524 | } |
| 525 | |
| 526 | func TestSearchAudit(t *testing.T) { |
| 527 | t.Parallel() |
| 528 | testCases := []struct { |
| 529 | Name string |
| 530 | Query string |
| 531 | Expected database.GetAuditLogsOffsetParams |
| 532 | ExpectedCountParams database.CountAuditLogsParams |
| 533 | ExpectedErrorContains string |
| 534 | }{ |
| 535 | { |
| 536 | Name: "Empty", |
| 537 | Query: "", |
| 538 | Expected: database.GetAuditLogsOffsetParams{}, |
| 539 | }, |
| 540 | // Failures |
| 541 | { |
| 542 | Name: "ExtraColon", |
| 543 | Query: `search:name:extra`, |
| 544 | ExpectedErrorContains: "can only contain 1 ':'", |
| 545 | }, |
| 546 | { |
| 547 | Name: "ExtraKeys", |
| 548 | Query: `foo:bar`, |
| 549 | ExpectedErrorContains: `"foo" is not a valid query param`, |
| 550 | }, |
| 551 | { |
| 552 | Name: "Dates", |
| 553 | Query: "date_from:2006", |
| 554 | ExpectedErrorContains: "valid date format", |
| 555 | }, |
| 556 | { |
| 557 | Name: "ResourceTarget", |
| 558 | Query: "resource_target:foo", |
| 559 | Expected: database.GetAuditLogsOffsetParams{ |
| 560 | ResourceTarget: "foo", |
| 561 | }, |
| 562 | ExpectedCountParams: database.CountAuditLogsParams{ |
| 563 | ResourceTarget: "foo", |
| 564 | }, |
| 565 | }, |
| 566 | { |
| 567 | Name: "RequestID", |
| 568 | Query: "request_id:foo", |
| 569 | ExpectedErrorContains: "valid uuid", |
| 570 | }, |
| 571 | } |
| 572 | |
| 573 | for _, c := range testCases { |
| 574 | t.Run(c.Name, func(t *testing.T) { |
| 575 | t.Parallel() |
| 576 | // Do not use a real database, this is only used for an |
| 577 | // organization lookup. |
| 578 | db, _ := dbtestutil.NewDB(t) |
| 579 | values, countValues, errs := searchquery.AuditLogs(context.Background(), db, c.Query) |
| 580 | if c.ExpectedErrorContains != "" { |
| 581 | require.True(t, len(errs) > 0, "expect some errors") |
| 582 | var s strings.Builder |
| 583 | for _, err := range errs { |