AuthToken Tests now use recently added TestAuthMethod and TestAccount (#226)
This commit is contained in:
1 parent
5ceb335466
commit
41b2d36d98
13 files changed
+207
-176
No files matched your search
@@ -5,70 +5,32 @@ import (
|
||||
"testing"
|
||||
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/watchtower/internal/auth/password"
|
||||
"github.com/hashicorp/watchtower/internal/db"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
iamStore "github.com/hashicorp/watchtower/internal/iam/store"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAuthToken(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, scopeId string) *AuthToken {
|
||||
t.Helper()
|
||||
u := iam.TestUser(t, conn, scopeId)
|
||||
amId := setupAuthMethod(t, conn, scopeId)
|
||||
|
||||
authMethod := password.TestAuthMethods(t, conn, scopeId, 1)[0]
|
||||
// auth account is only used to join auth method to user.
|
||||
// We don't do anything else with the auth account in the test setup.
|
||||
acct := setupAuthAccount(t, conn, scopeId, amId, u.GetPublicId())
|
||||
acct := password.TestAccounts(t, conn, authMethod.GetPublicId(), 1)[0]
|
||||
|
||||
ctx := context.Background()
|
||||
rw := db.New(conn)
|
||||
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
|
||||
u, err := iamRepo.LookupUserWithLogin(ctx, acct.GetPublicId(), iam.WithAutoVivify(true))
|
||||
require.NoError(t, err)
|
||||
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
at, err := repo.CreateAuthToken(ctx, u.GetPublicId(), acct.GetPublicId())
|
||||
require.NoError(t, err)
|
||||
return at
|
||||
}
|
||||
|
||||
// Returns auth method id
|
||||
// TODO: Remove this when the auth method repos are created with the relevant test methods.
|
||||
func setupAuthMethod(t *testing.T, conn *gorm.DB, scope string) string {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
insert := `insert into auth_method
|
||||
(public_id, scope_id)
|
||||
values
|
||||
($1, $2);`
|
||||
amId, err := db.NewPublicId("am")
|
||||
require.NoError(err)
|
||||
_, err = conn.DB().Exec(insert, amId, scope)
|
||||
require.NoError(err)
|
||||
return amId
|
||||
}
|
||||
|
||||
// TODO: Remove this when the auth method repos are created with the relevant test methods.
|
||||
func setupAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId string) *iam.AuthAccount {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
require.NotEmpty(scopeId)
|
||||
require.NotEmpty(authMethodId)
|
||||
require.NotEmpty(userId)
|
||||
|
||||
authAcctId, err := db.NewPublicId("aa")
|
||||
require.NoError(err)
|
||||
|
||||
acct := &iam.AuthAccount{
|
||||
AuthAccount: &iamStore.AuthAccount{
|
||||
PublicId: authAcctId,
|
||||
ScopeId: scopeId,
|
||||
AuthMethodId: authMethodId,
|
||||
IamUserId: userId,
|
||||
},
|
||||
}
|
||||
rw := db.New(conn)
|
||||
err = rw.Create(context.Background(), acct)
|
||||
require.NoError(err)
|
||||
require.NotEmpty(acct.PublicId)
|
||||
return acct
|
||||
}
|
||||
Reference in new issue
Block a user