github.com/criteo-forks/consul@v1.4.5-criteonogrpc/command/acl/token/create/token_create_test.go (about)

     1  package tokencreate
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/consul/agent"
     9  	"github.com/hashicorp/consul/api"
    10  	"github.com/hashicorp/consul/logger"
    11  	"github.com/hashicorp/consul/testrpc"
    12  	"github.com/hashicorp/consul/testutil"
    13  	"github.com/mitchellh/cli"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestTokenCreateCommand_noTabs(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
    21  		t.Fatal("help has tabs")
    22  	}
    23  }
    24  
    25  func TestTokenCreateCommand(t *testing.T) {
    26  	t.Parallel()
    27  	assert := assert.New(t)
    28  
    29  	testDir := testutil.TempDir(t, "acl")
    30  	defer os.RemoveAll(testDir)
    31  
    32  	a := agent.NewTestAgent(t, t.Name(), `
    33  	primary_datacenter = "dc1"
    34  	acl {
    35  		enabled = true
    36  		tokens {
    37  			master = "root"
    38  		}
    39  	}`)
    40  
    41  	a.Agent.LogWriter = logger.NewLogWriter(512)
    42  
    43  	defer a.Shutdown()
    44  	testrpc.WaitForLeader(t, a.RPC, "dc1")
    45  
    46  	ui := cli.NewMockUi()
    47  	cmd := New(ui)
    48  
    49  	// Create a policy
    50  	client := a.Client()
    51  
    52  	policy, _, err := client.ACL().PolicyCreate(
    53  		&api.ACLPolicy{Name: "test-policy"},
    54  		&api.WriteOptions{Token: "root"},
    55  	)
    56  	assert.NoError(err)
    57  
    58  	// create with policy by name
    59  	{
    60  		args := []string{
    61  			"-http-addr=" + a.HTTPAddr(),
    62  			"-token=root",
    63  			"-policy-name=" + policy.Name,
    64  			"-description=test token",
    65  		}
    66  
    67  		code := cmd.Run(args)
    68  		assert.Equal(code, 0)
    69  		assert.Empty(ui.ErrorWriter.String())
    70  	}
    71  
    72  	// create with policy by id
    73  	{
    74  		args := []string{
    75  			"-http-addr=" + a.HTTPAddr(),
    76  			"-token=root",
    77  			"-policy-id=" + policy.ID,
    78  			"-description=test token",
    79  		}
    80  
    81  		code := cmd.Run(args)
    82  		assert.Equal(code, 0)
    83  		assert.Empty(ui.ErrorWriter.String())
    84  	}
    85  }