136 lines
2.4 KiB
Go
136 lines
2.4 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
tokenDisk "github.com/hashicorp/vault/builtin/token/disk"
|
|
"github.com/hashicorp/vault/command/token"
|
|
"github.com/hashicorp/vault/http"
|
|
"github.com/hashicorp/vault/vault"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestAuth_methods(t *testing.T) {
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
ln, addr := http.TestServer(t, core)
|
|
defer ln.Close()
|
|
|
|
testAuthInit(t)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &AuthCommand{
|
|
Meta: Meta{
|
|
ClientToken: token,
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-address", addr,
|
|
"-methods",
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
output := ui.OutputWriter.String()
|
|
if !strings.Contains(output, "token") {
|
|
t.Fatalf("bad: %#v", output)
|
|
}
|
|
}
|
|
|
|
func TestAuth_token(t *testing.T) {
|
|
core, _, token := vault.TestCoreUnsealed(t)
|
|
ln, addr := http.TestServer(t, core)
|
|
defer ln.Close()
|
|
|
|
testAuthInit(t)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &AuthCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-address", addr,
|
|
token,
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
helper, err := c.TokenHelper()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual, err := helper.Get()
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if actual != token {
|
|
t.Fatalf("bad: %s", actual)
|
|
}
|
|
}
|
|
|
|
func TestAuth_argsWithMethod(t *testing.T) {
|
|
ui := new(cli.MockUi)
|
|
c := &AuthCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-method=foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestAuth_tooManyArgs(t *testing.T) {
|
|
ui := new(cli.MockUi)
|
|
c := &AuthCommand{
|
|
Meta: Meta{
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"foo",
|
|
"bar",
|
|
}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func testAuthInit(t *testing.T) {
|
|
td, err := ioutil.TempDir("", "vault")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
// Set the HOME env var so we get that right
|
|
os.Setenv("HOME", td)
|
|
|
|
// Write a .vault config to use our custom token helper
|
|
config := fmt.Sprintf(
|
|
"token_helper = \"%s\"\n", token.TestProcessPath(t))
|
|
ioutil.WriteFile(filepath.Join(td, ".vault"), []byte(config), 0644)
|
|
}
|
|
|
|
func TestHelperProcess(t *testing.T) {
|
|
token.TestHelperProcessCLI(t, &tokenDisk.Command{})
|
|
}
|