From 086b4df7cc8f53e8040b318f977b3d8fae79fed7 Mon Sep 17 00:00:00 2001 From: Philip Hayton Date: Mon, 20 Nov 2023 11:37:55 +0000 Subject: [PATCH] fix(ignore): apply sorting when displaying all ignores (#1407) * fix(ignore): apply sorting when displaying all ignores * test: add some coverage for ignore * chore: pr feedback --- e2e/ignore/.snapshots/TestShowAll-show-all | 42 +++++++++++++++++ .../TestShowIndividual-show-individual | 10 ++++ e2e/ignore/ignore_show_test.go | 46 +++++++++++++++++++ e2e/ignore/testdata/test.ignore | 38 +++++++++++++++ internal/commands/ignore.go | 16 +++++-- 5 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 e2e/ignore/.snapshots/TestShowAll-show-all create mode 100644 e2e/ignore/.snapshots/TestShowIndividual-show-individual create mode 100644 e2e/ignore/ignore_show_test.go create mode 100644 e2e/ignore/testdata/test.ignore diff --git a/e2e/ignore/.snapshots/TestShowAll-show-all b/e2e/ignore/.snapshots/TestShowAll-show-all new file mode 100644 index 000000000..5066f0c9c --- /dev/null +++ b/e2e/ignore/.snapshots/TestShowAll-show-all @@ -0,0 +1,42 @@ + +-- + +5b926801859e02e36224dc3a648a2fda_0 +├─ Ignored At: 2023-10-13T14:57:07Z +├─ Author: gotbadger +├─ False positive? Yes +└─ Comment: variable is safe + +dc369468d8a5d5bb522d023ae7485376_0 +├─ Ignored At: 2023-11-13T14:45:21Z +├─ Author: gotbadger +├─ False positive? Yes +└─ Comment: secret placeholder + +fff78d76f2c44a5768dcb4d55a3bd276_0 +├─ Ignored At: 2023-11-13T14:48:31Z +├─ Author: gotbadger +├─ False positive? No +└─ Comment: data hash + +23d5ed741c83e2e54cc77bdf597bd15d_0 +├─ Ignored At: 2023-11-13T14:52:08Z +├─ Author: gotbadger +├─ False positive? Yes +└─ Comment: token generation + +68a86d90f28db878612eb5f699c06543_0 +├─ Ignored At: 2023-11-13T15:00:28Z +├─ Author: gotbadger +├─ False positive? No +└─ Comment: low risk + +6c3dd0e4ba8f4c427dffaea6c6696986_0 +├─ Ignored At: 2023-11-15T11:51:54Z +├─ Author: gotbadger +├─ False positive? Yes +└─ Comment: token gen + + + + diff --git a/e2e/ignore/.snapshots/TestShowIndividual-show-individual b/e2e/ignore/.snapshots/TestShowIndividual-show-individual new file mode 100644 index 000000000..0657dc1e5 --- /dev/null +++ b/e2e/ignore/.snapshots/TestShowIndividual-show-individual @@ -0,0 +1,10 @@ + +-- + +68a86d90f28db878612eb5f699c06543_0 +├─ Ignored At: 2023-11-13T15:00:28Z +├─ Author: gotbadger +├─ False positive? No +└─ Comment: low risk + + diff --git a/e2e/ignore/ignore_show_test.go b/e2e/ignore/ignore_show_test.go new file mode 100644 index 000000000..995d13651 --- /dev/null +++ b/e2e/ignore/ignore_show_test.go @@ -0,0 +1,46 @@ +package ignore_test + +import ( + "path/filepath" + "testing" + + "github.com/bearer/bearer/e2e/internal/testhelper" +) + +func newIgnoreTest(name string, arguments []string) testhelper.TestCase { + arguments = append([]string{ + "ignore"}, + arguments..., + ) + return testhelper.NewTestCase(name, arguments, testhelper.TestCaseOptions{ + DisplayProgressBar: true, + DisplayStdErr: true, + IgnoreForce: true, + }) +} + +func TestShowAll(t *testing.T) { + tests := []testhelper.TestCase{ + newIgnoreTest("show-all", []string{ + "show", + "--all", + "--ignore-file", + filepath.Join("e2e", "ignore", "testdata/test.ignore"), + }), + } + + testhelper.RunTests(t, tests) +} + +func TestShowIndividual(t *testing.T) { + tests := []testhelper.TestCase{ + newIgnoreTest("show-individual", []string{ + "show", + "68a86d90f28db878612eb5f699c06543_0", + "--ignore-file", + filepath.Join("e2e", "ignore", "testdata/test.ignore"), + }), + } + + testhelper.RunTests(t, tests) +} diff --git a/e2e/ignore/testdata/test.ignore b/e2e/ignore/testdata/test.ignore new file mode 100644 index 000000000..335035a27 --- /dev/null +++ b/e2e/ignore/testdata/test.ignore @@ -0,0 +1,38 @@ +{ + "23d5ed741c83e2e54cc77bdf597bd15d_0": { + "author": "gotbadger", + "comment": "token generation", + "false_positive": true, + "ignored_at": "2023-11-13T14:52:08Z" + }, + "5b926801859e02e36224dc3a648a2fda_0": { + "author": "gotbadger", + "comment": "variable is safe", + "false_positive": true, + "ignored_at": "2023-10-13T14:57:07Z" + }, + "68a86d90f28db878612eb5f699c06543_0": { + "author": "gotbadger", + "comment": "low risk", + "false_positive": false, + "ignored_at": "2023-11-13T15:00:28Z" + }, + "6c3dd0e4ba8f4c427dffaea6c6696986_0": { + "author": "gotbadger", + "comment": "token gen", + "false_positive": true, + "ignored_at": "2023-11-15T11:51:54Z" + }, + "dc369468d8a5d5bb522d023ae7485376_0": { + "author": "gotbadger", + "comment": "secret placeholder", + "false_positive": true, + "ignored_at": "2023-11-13T14:45:21Z" + }, + "fff78d76f2c44a5768dcb4d55a3bd276_0": { + "author": "gotbadger", + "comment": "data hash", + "false_positive": false, + "ignored_at": "2023-11-13T14:48:31Z" + } +} \ No newline at end of file diff --git a/internal/commands/ignore.go b/internal/commands/ignore.go index 4564d7024..590df5ecf 100644 --- a/internal/commands/ignore.go +++ b/internal/commands/ignore.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "sort" "strings" "github.com/spf13/cobra" @@ -107,9 +108,18 @@ $ bearer ignore show `, cmd.Print("\n") if options.IgnoreShowOptions.All { - // show all fingerprints - for fingerprintId, fingerprint := range ignoredFingerprints { - cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, fingerprint, options.GeneralOptions.NoColor)) + // show all fingerprints sorted by date + keys := make([]string, 0, len(ignoredFingerprints)) + for key := range ignoredFingerprints { + keys = append(keys, key) + } + + sort.SliceStable(keys, func(i, j int) bool { + return ignoredFingerprints[keys[i]].IgnoredAt < ignoredFingerprints[keys[j]].IgnoredAt + }) + + for _, k := range keys { + cmd.Print(ignore.DisplayIgnoredEntryTextString(k, ignoredFingerprints[k], options.GeneralOptions.NoColor)) cmd.Print("\n\n") } } else {