From 10fdae3cf6f2687b23904e50dc935486de579ef9 Mon Sep 17 00:00:00 2001 From: sj-williams Date: Fri, 6 Dec 2024 14:44:46 +0000 Subject: [PATCH 1/2] =?UTF-8?q?test:=20=F0=9F=92=8D=20check=20for=20secret?= =?UTF-8?q?=20json=20missing=20data=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/decodeSecret/decodeSecret.go | 5 ++++- pkg/decodeSecret/decodeSecret_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/decodeSecret/decodeSecret.go b/pkg/decodeSecret/decodeSecret.go index f4708399..efb504c3 100644 --- a/pkg/decodeSecret/decodeSecret.go +++ b/pkg/decodeSecret/decodeSecret.go @@ -77,7 +77,10 @@ func (sd *secretDecoder) processJson(jsn string, rawPrint bool) (string, error) return "", err } - data := result["data"].(map[string]interface{}) + data, valid := result["data"].(map[string]interface{}) + if !valid { + return "", errors.New("unable to decode secret, does it have a data key?") + } err = decodeKeys(data) if err != nil { diff --git a/pkg/decodeSecret/decodeSecret_test.go b/pkg/decodeSecret/decodeSecret_test.go index 9d572154..ed626e7d 100644 --- a/pkg/decodeSecret/decodeSecret_test.go +++ b/pkg/decodeSecret/decodeSecret_test.go @@ -106,4 +106,17 @@ func TestFormatJson(t *testing.T) { if err == nil { t.Fatal("Expected error and received nil") } + +} + +func TestJsonNoDataKey(t *testing.T) { + jsn := `{ "key1": "1", "key2": "2" }` + sd := secretDecoder{} + _, err := sd.processJson(jsn, false) + if err == nil { + t.Error("Expected an error when no data key is present") + } + if err.Error() != "unable to decode secret, does it have a data key?" { + t.Errorf("Expected error message 'unable to decode secret, does it have a data key?', got '%s'", err.Error()) + } } From a6e1280dbd0976185a233dc966ac35376fbbfcde Mon Sep 17 00:00:00 2001 From: sj-williams Date: Fri, 6 Dec 2024 16:37:54 +0000 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=F0=9F=A4=96=20tidy=20test=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/decodeSecret/decodeSecret_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/decodeSecret/decodeSecret_test.go b/pkg/decodeSecret/decodeSecret_test.go index ed626e7d..1c62088c 100644 --- a/pkg/decodeSecret/decodeSecret_test.go +++ b/pkg/decodeSecret/decodeSecret_test.go @@ -112,11 +112,12 @@ func TestFormatJson(t *testing.T) { func TestJsonNoDataKey(t *testing.T) { jsn := `{ "key1": "1", "key2": "2" }` sd := secretDecoder{} + expectedMsg := "unable to decode secret, does it have a data key?" _, err := sd.processJson(jsn, false) if err == nil { t.Error("Expected an error when no data key is present") } - if err.Error() != "unable to decode secret, does it have a data key?" { - t.Errorf("Expected error message 'unable to decode secret, does it have a data key?', got '%s'", err.Error()) + if err.Error() != expectedMsg { + t.Errorf("Expected error message '%s', got '%s'", expectedMsg, err.Error()) } }