Skip to content

Commit

Permalink
Unit test and delint/gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBQu committed Dec 16, 2024
1 parent 0b3a9b5 commit 64a999c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cli/keyring_list.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
65 changes: 65 additions & 0 deletions cli/keyring_list_test.go
Original file line number Diff line number Diff line change
@@ -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.")
}
1 change: 0 additions & 1 deletion keyring/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 64a999c

Please sign in to comment.