-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] allow list operators/accounts/users to show its output as JSON (…
…#692) Fixes #660 Signed-off-by: Alberto Ricart <[email protected]>
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type entryJSON struct { | ||
Name string `json:"name"` | ||
PublicKey string `json:"public_key"` | ||
} | ||
|
||
func Test_ListOperatorsJSON(t *testing.T) { | ||
ts := NewTestStore(t, "O") | ||
defer ts.Done(t) | ||
ts.AddOperator(t, "OO") | ||
|
||
cmd := createListOperatorsCmd() | ||
cmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON") | ||
|
||
_, stderr, err := ExecuteCmd(cmd, "--json") | ||
require.NoError(t, err) | ||
|
||
var entries []entryJSON | ||
require.NoError(t, json.Unmarshal([]byte(stderr), &entries)) | ||
assert.Len(t, entries, 2) | ||
assert.Equal(t, entries[0].Name, "O") | ||
assert.NotEmpty(t, entries[0].PublicKey) | ||
assert.Equal(t, entries[1].Name, "OO") | ||
assert.NotEmpty(t, entries[1].PublicKey) | ||
} | ||
|
||
func Test_ListAccountsJSON(t *testing.T) { | ||
ts := NewTestStore(t, "O") | ||
defer ts.Done(t) | ||
|
||
ts.AddAccount(t, "A") | ||
ts.AddAccount(t, "B") | ||
|
||
cmd := createListAccountsCmd() | ||
cmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON") | ||
|
||
_, stderr, err := ExecuteCmd(cmd, "--json") | ||
require.NoError(t, err) | ||
|
||
var entries []entryJSON | ||
require.NoError(t, json.Unmarshal([]byte(stderr), &entries)) | ||
assert.Len(t, entries, 2) | ||
assert.Equal(t, entries[0].Name, "A") | ||
assert.Equal(t, entries[0].PublicKey, ts.GetAccountPublicKey(t, "A")) | ||
assert.Equal(t, entries[1].Name, "B") | ||
assert.Equal(t, entries[1].PublicKey, ts.GetAccountPublicKey(t, "B")) | ||
} | ||
|
||
func Test_ListUsersJSON(t *testing.T) { | ||
ts := NewTestStore(t, "O") | ||
defer ts.Done(t) | ||
|
||
ts.AddAccount(t, "A") | ||
ts.AddUser(t, "A", "U") | ||
ts.AddUser(t, "A", "UU") | ||
|
||
cmd := createListUsersCmd() | ||
cmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON") | ||
|
||
_, stderr, err := ExecuteCmd(cmd, "--json") | ||
require.NoError(t, err) | ||
|
||
var entries []entryJSON | ||
require.NoError(t, json.Unmarshal([]byte(stderr), &entries)) | ||
assert.Len(t, entries, 2) | ||
assert.Equal(t, entries[0].Name, "U") | ||
assert.Equal(t, entries[0].PublicKey, ts.GetUserPublicKey(t, "A", "U")) | ||
assert.Equal(t, entries[1].Name, "UU") | ||
assert.Equal(t, entries[1].PublicKey, ts.GetUserPublicKey(t, "A", "UU")) | ||
} |