open-vault/command/auth_test.go

136 lines
3.3 KiB
Go
Raw Normal View History

2015-03-29 23:42:45 +00:00
package command
import (
2015-04-02 00:01:10 +00:00
"strings"
2015-03-29 23:42:45 +00:00
"testing"
2017-09-05 03:59:24 +00:00
"github.com/mitchellh/cli"
2017-09-05 03:59:24 +00:00
credToken "github.com/hashicorp/vault/builtin/credential/token"
credUserpass "github.com/hashicorp/vault/builtin/credential/userpass"
"github.com/hashicorp/vault/command/token"
2015-03-29 23:42:45 +00:00
)
2017-09-05 03:59:24 +00:00
func testAuthCommand(tb testing.TB) (*cli.MockUi, *AuthCommand) {
tb.Helper()
2015-03-30 17:55:41 +00:00
2017-09-05 03:59:24 +00:00
ui := cli.NewMockUi()
return ui, &AuthCommand{
BaseCommand: &BaseCommand{
UI: ui,
2015-03-30 17:55:41 +00:00
2017-09-05 03:59:24 +00:00
// Override to our own token helper
tokenHelper: token.NewTestingTokenHelper(),
},
2017-09-08 01:56:39 +00:00
Handlers: map[string]LoginHandler{
2017-09-05 03:59:24 +00:00
"token": &credToken.CLIHandler{},
"userpass": &credUserpass.CLIHandler{},
},
}
2017-09-05 03:59:24 +00:00
}
2017-09-05 03:59:24 +00:00
func TestAuthCommand_Run(t *testing.T) {
t.Parallel()
2017-09-08 01:56:39 +00:00
// TODO: remove in 0.9.0
t.Run("deprecated_methods", func(t *testing.T) {
2017-09-05 03:59:24 +00:00
t.Parallel()
2017-09-05 03:59:24 +00:00
client, closer := testVaultServer(t)
defer closer()
2017-09-05 03:59:24 +00:00
ui, cmd := testAuthCommand(t)
cmd.client = client
2017-09-08 01:56:39 +00:00
// vault auth -methods -> vault auth list
code := cmd.Run([]string{"-methods"})
2017-09-05 03:59:24 +00:00
if exp := 0; code != exp {
2017-09-08 01:56:39 +00:00
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
2017-09-05 03:59:24 +00:00
}
2017-09-08 01:56:39 +00:00
stdout, stderr := ui.OutputWriter.String(), ui.ErrorWriter.String()
2017-09-05 03:59:24 +00:00
2017-09-08 01:56:39 +00:00
if expected := "WARNING!"; !strings.Contains(stderr, expected) {
t.Errorf("expected %q to contain %q", stderr, expected)
2017-09-05 03:59:24 +00:00
}
2017-09-08 01:56:39 +00:00
if expected := "token/"; !strings.Contains(stdout, expected) {
t.Errorf("expected %q to contain %q", stdout, expected)
2017-09-05 03:59:24 +00:00
}
})
2015-04-06 16:40:47 +00:00
2017-09-08 01:56:39 +00:00
t.Run("deprecated_method_help", func(t *testing.T) {
2017-09-05 03:59:24 +00:00
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
ui, cmd := testAuthCommand(t)
cmd.client = client
2017-09-08 01:56:39 +00:00
// vault auth -method=foo -method-help -> vault auth help foo
2017-09-05 03:59:24 +00:00
code := cmd.Run([]string{
2017-09-08 01:56:39 +00:00
"-method=userpass",
"-method-help",
2017-09-05 03:59:24 +00:00
})
if exp := 0; code != exp {
2017-09-08 01:56:39 +00:00
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
2017-09-05 03:59:24 +00:00
}
2017-09-08 01:56:39 +00:00
stdout, stderr := ui.OutputWriter.String(), ui.ErrorWriter.String()
2017-09-05 03:59:24 +00:00
2017-09-08 01:56:39 +00:00
if expected := "WARNING!"; !strings.Contains(stderr, expected) {
t.Errorf("expected %q to contain %q", stderr, expected)
2017-09-05 03:59:24 +00:00
}
2017-09-08 01:56:39 +00:00
if expected := "vault login"; !strings.Contains(stdout, expected) {
t.Errorf("expected %q to contain %q", stdout, expected)
2017-09-05 03:59:24 +00:00
}
})
2015-04-06 16:40:47 +00:00
2017-09-08 01:56:39 +00:00
t.Run("deprecated_login", func(t *testing.T) {
2017-09-05 03:59:24 +00:00
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
2017-09-08 01:56:39 +00:00
if err := client.Sys().EnableAuth("my-auth", "userpass", ""); err != nil {
2017-09-05 03:59:24 +00:00
t.Fatal(err)
}
2017-09-08 01:56:39 +00:00
if _, err := client.Logical().Write("auth/my-auth/users/test", map[string]interface{}{
2017-09-05 03:59:24 +00:00
"password": "test",
"policies": "default",
}); err != nil {
t.Fatal(err)
}
ui, cmd := testAuthCommand(t)
cmd.client = client
2017-09-08 01:56:39 +00:00
// vault auth ARGS -> vault login ARGS
2017-09-05 03:59:24 +00:00
code := cmd.Run([]string{
"-method", "userpass",
2017-09-08 01:56:39 +00:00
"-path", "my-auth",
2017-09-05 03:59:24 +00:00
"username=test",
"password=test",
})
if exp := 0; code != exp {
2017-09-08 01:56:39 +00:00
t.Errorf("expected %d to be %d: %s", code, exp, ui.ErrorWriter.String())
2017-09-05 03:59:24 +00:00
}
2017-09-08 01:56:39 +00:00
stdout, stderr := ui.OutputWriter.String(), ui.ErrorWriter.String()
2015-04-06 16:40:47 +00:00
2017-09-08 01:56:39 +00:00
if expected := "WARNING!"; !strings.Contains(stderr, expected) {
t.Errorf("expected %q to contain %q", stderr, expected)
2017-09-05 03:59:24 +00:00
}
2017-09-08 01:56:39 +00:00
if expected := "Success! You are now authenticated."; !strings.Contains(stdout, expected) {
t.Errorf("expected %q to contain %q", stdout, expected)
2017-09-05 03:59:24 +00:00
}
})
2017-09-05 03:59:24 +00:00
t.Run("no_tabs", func(t *testing.T) {
t.Parallel()
2015-04-06 16:40:47 +00:00
2017-09-05 03:59:24 +00:00
_, cmd := testAuthCommand(t)
assertNoTabs(t, cmd)
})
2015-04-06 16:40:47 +00:00
}