2015-03-29 23:42:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-03-30 18:07:31 +00:00
|
|
|
"fmt"
|
2015-03-30 17:55:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-03-30 18:07:31 +00:00
|
|
|
"path/filepath"
|
2015-03-29 23:42:45 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-03-30 18:07:31 +00:00
|
|
|
tokenDisk "github.com/hashicorp/vault/builtin/token/disk"
|
|
|
|
"github.com/hashicorp/vault/command/token"
|
2015-03-29 23:42:45 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2015-03-30 17:55:41 +00:00
|
|
|
func TestAuth_token(t *testing.T) {
|
|
|
|
testAuthInit(t)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &AuthCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
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 != "foo" {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:42:45 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 17:55:41 +00:00
|
|
|
|
|
|
|
func testAuthInit(t *testing.T) {
|
|
|
|
td, err := ioutil.TempDir("", "vault")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-30 18:07:31 +00:00
|
|
|
// Set the HOME env var so we get that right
|
2015-03-30 17:55:41 +00:00
|
|
|
os.Setenv("HOME", td)
|
2015-03-30 18:07:31 +00:00
|
|
|
|
|
|
|
// 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{})
|
2015-03-30 17:55:41 +00:00
|
|
|
}
|