2018-10-19 16:04:07 +00:00
|
|
|
package tokenlist
|
|
|
|
|
|
|
|
import (
|
2020-02-15 15:06:05 +00:00
|
|
|
"encoding/json"
|
2018-10-19 16:04:07 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-02-15 15:06:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-19 16:04:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTokenListCommand_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:06:05 +00:00
|
|
|
func TestTokenListCommand_Pretty(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2018-10-19 16:04:07 +00:00
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
tokens {
|
|
|
|
master = "root"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
var tokenIds []string
|
|
|
|
|
|
|
|
// Create a couple tokens to list
|
|
|
|
client := a.Client()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
description := fmt.Sprintf("test token %d", i)
|
|
|
|
|
|
|
|
token, _, err := client.ACL().TokenCreate(
|
|
|
|
&api.ACLToken{Description: description},
|
|
|
|
&api.WriteOptions{Token: "root"},
|
|
|
|
)
|
|
|
|
tokenIds = append(tokenIds, token.AccessorID)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
assert.Equal(code, 0)
|
|
|
|
assert.Empty(ui.ErrorWriter.String())
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
|
|
|
|
for i, v := range tokenIds {
|
|
|
|
assert.Contains(output, fmt.Sprintf("test token %d", i))
|
|
|
|
assert.Contains(output, v)
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 15:06:05 +00:00
|
|
|
|
|
|
|
func TestTokenListCommand_JSON(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:06:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2020-02-15 15:06:05 +00:00
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
tokens {
|
|
|
|
master = "root"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
var tokenIds []string
|
|
|
|
|
|
|
|
// Create a couple tokens to list
|
|
|
|
client := a.Client()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
description := fmt.Sprintf("test token %d", i)
|
|
|
|
|
|
|
|
token, _, err := client.ACL().TokenCreate(
|
|
|
|
&api.ACLToken{Description: description},
|
|
|
|
&api.WriteOptions{Token: "root"},
|
|
|
|
)
|
|
|
|
tokenIds = append(tokenIds, token.AccessorID)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
"-format=json",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
assert.Equal(code, 0)
|
|
|
|
assert.Empty(ui.ErrorWriter.String())
|
|
|
|
|
2020-05-14 21:02:52 +00:00
|
|
|
var jsonOutput []api.ACLTokenListEntry
|
2020-02-15 15:06:05 +00:00
|
|
|
err := json.Unmarshal([]byte(ui.OutputWriter.String()), &jsonOutput)
|
|
|
|
require.NoError(t, err, "token unmarshalling error")
|
2020-05-14 21:02:52 +00:00
|
|
|
|
|
|
|
respIDs := make([]string, 0, len(jsonOutput))
|
|
|
|
for _, obj := range jsonOutput {
|
|
|
|
respIDs = append(respIDs, obj.AccessorID)
|
|
|
|
}
|
|
|
|
require.Subset(t, respIDs, tokenIds)
|
2020-02-15 15:06:05 +00:00
|
|
|
}
|