Skip to content

Commit

Permalink
Reinstate testing token helper but in original command/token package
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhjp committed Mar 4, 2024
1 parent 07027b3 commit 7a5f78e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions command/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/hashicorp/cli"
"github.com/hashicorp/vault/api/tokenhelper"
"github.com/hashicorp/vault/command/token"
)

func testAuthCommand(tb testing.TB) (*cli.MockUi, *AuthCommand) {
Expand All @@ -19,7 +19,7 @@ func testAuthCommand(tb testing.TB) (*cli.MockUi, *AuthCommand) {
UI: ui,

// Override to our own token helper
tokenHelper: tokenhelper.NewTestingTokenHelper(),
tokenHelper: token.NewTestingTokenHelper(),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions command/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

"github.com/hashicorp/cli"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/api/tokenhelper"
credToken "github.com/hashicorp/vault/builtin/credential/token"
credUserpass "github.com/hashicorp/vault/builtin/credential/userpass"
"github.com/hashicorp/vault/command/token"
"github.com/hashicorp/vault/helper/testhelpers"
"github.com/hashicorp/vault/vault"
)
Expand All @@ -33,7 +33,7 @@ func testLoginCommand(tb testing.TB) (*cli.MockUi, *LoginCommand) {
UI: ui,

// Override to our own token helper
tokenHelper: tokenhelper.NewTestingTokenHelper(),
tokenHelper: token.NewTestingTokenHelper(),
},
Handlers: map[string]LoginHandler{
"token": &credToken.CLIHandler{},
Expand Down
47 changes: 47 additions & 0 deletions command/token/helper_testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package token

import (
"sync"

"github.com/hashicorp/vault/api/tokenhelper"
)

var _ tokenhelper.TokenHelper = (*TestingTokenHelper)(nil)

// TestingTokenHelper implements token.TokenHelper which runs entirely
// in-memory. This should not be used outside of testing.
type TestingTokenHelper struct {
lock sync.RWMutex
token string
}

func NewTestingTokenHelper() *TestingTokenHelper {
return &TestingTokenHelper{}
}

func (t *TestingTokenHelper) Erase() error {
t.lock.Lock()
defer t.lock.Unlock()
t.token = ""
return nil
}

func (t *TestingTokenHelper) Get() (string, error) {
t.lock.RLock()
defer t.lock.RUnlock()
return t.token, nil
}

func (t *TestingTokenHelper) Path() string {
return ""
}

func (t *TestingTokenHelper) Store(token string) error {
t.lock.Lock()
defer t.lock.Unlock()
t.token = token
return nil
}

0 comments on commit 7a5f78e

Please sign in to comment.