From 64a999c67981b70f3763121010151bcf1c20553a Mon Sep 17 00:00:00 2001 From: Chris Quigley Date: Mon, 16 Dec 2024 16:33:38 -0500 Subject: [PATCH] Unit test and delint/gofmt --- cli/keyring_list.go | 10 +++++++ cli/keyring_list_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ keyring/file.go | 1 - 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 cli/keyring_list_test.go diff --git a/cli/keyring_list.go b/cli/keyring_list.go index b9c1d1c8bc..35e2a2482e 100644 --- a/cli/keyring_list.go +++ b/cli/keyring_list.go @@ -1,3 +1,13 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + package cli import ( diff --git a/cli/keyring_list_test.go b/cli/keyring_list_test.go new file mode 100644 index 0000000000..9edbc65bc8 --- /dev/null +++ b/cli/keyring_list_test.go @@ -0,0 +1,65 @@ +// Copyright 2024 Democratized Data Foundation +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package cli + +import ( + "bytes" + "encoding/hex" + "os" + "regexp" + "testing" + + "github.com/sourcenetwork/defradb/crypto" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestKeyringList(t *testing.T) { + rootdir := t.TempDir() + + err := os.Setenv("DEFRA_KEYRING_SECRET", "password") + require.NoError(t, err) + + keyNames := []string{"keyname1", "keyname2", "keyname3"} + + // Insert the keys into the keyring + for _, keyName := range keyNames { + keyBytes, err := crypto.GenerateAES256() + require.NoError(t, err) + keyHex := hex.EncodeToString(keyBytes) + cmd := NewDefraCommand() + cmd.SetArgs([]string{"keyring", "import", "--rootdir", rootdir, keyName, keyHex}) + err = cmd.Execute() + require.NoError(t, err) + } + + // Run the 'keyring list' command, and require no error on the output + var output bytes.Buffer + cmd := NewDefraCommand() + cmd.SetOut(&output) + cmd.SetArgs([]string{"keyring", "list", "--rootdir", rootdir}) + err = cmd.Execute() + require.NoError(t, err) + + outputString := output.String() + + // Use regex to extract the keys, and compare with the expected values + // We know the format the output should be, which is: "Keys in the keyring:\n- keyname1\n- keyname2\n- keyname3\n" + re := regexp.MustCompile(`-\s([^\n]+)`) + matches := re.FindAllStringSubmatch(outputString, -1) + var extractedKeys []string + for _, match := range matches { + extractedKeys = append(extractedKeys, match[1]) + } + + assert.ElementsMatch(t, keyNames, extractedKeys, "The extracted keys do not match the expected keys.") +} diff --git a/keyring/file.go b/keyring/file.go index f1299d2f4d..d78fe1322c 100644 --- a/keyring/file.go +++ b/keyring/file.go @@ -66,7 +66,6 @@ func (f *fileKeyring) Delete(user string) error { } func (f *fileKeyring) List() ([]string, error) { - files, err := os.ReadDir(f.dir) if err != nil { return nil, err