implement SetTableName patttern that allows table name to be set back to the default for the storage type. (#196)

This commit is contained in:
Jim authored and GitHub committed 2020-07-17 16:20:56 -04:00
1 parent fa5f45260b
commit c83e90ed38
20 files changed
+768 -46

No files matched your search

+9 -3
View File
@@ -1,16 +1,22 @@
package authtoken
const defaultAuthTokenTableName = "auth_token"
// TableName returns the table name for the auth token.
func (s *AuthToken) TableName() string {
if s.tableName != "" {
return s.tableName
}
return "auth_token"
return defaultAuthTokenTableName
}
// SetTableName sets the table name.
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (s *AuthToken) SetTableName(n string) {
if n != "" {
switch n {
case "":
s.tableName = defaultAuthTokenTableName
default:
s.tableName = n
}
}
+45
View File
@@ -0,0 +1,45 @@
package authtoken
import (
"testing"
"github.com/hashicorp/watchtower/internal/authtoken/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthToken_SetTableName(t *testing.T) {
defaultTableName := defaultAuthTokenTableName
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocAuthToken()
require.Equal(defaultTableName, def.TableName())
s := &AuthToken{
AuthToken: &store.AuthToken{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}