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
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/hashicorp/watchtower/internal/auth/password"
|
||||
"github.com/hashicorp/watchtower/internal/authtoken/store"
|
||||
"github.com/hashicorp/watchtower/internal/db"
|
||||
"github.com/hashicorp/watchtower/internal/db/timestamp"
|
||||
@@ -24,9 +25,8 @@ func TestAuthToken_DbUpdate(t *testing.T) {
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
u := iam.TestUser(t, conn, org.GetPublicId())
|
||||
amId := setupAuthMethod(t, conn, org.GetPublicId())
|
||||
acct := setupAuthAccount(t, conn, org.GetPublicId(), amId, u.GetPublicId())
|
||||
am := password.TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
acct := password.TestAccounts(t, conn, am.GetPublicId(), 1)[0]
|
||||
|
||||
newAuthTokId, err := newAuthTokenId()
|
||||
require.NoError(t, err)
|
||||
@@ -118,9 +118,8 @@ func TestAuthToken_DbCreate(t *testing.T) {
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org, _ := iam.TestScopes(t, conn)
|
||||
u := iam.TestUser(t, conn, org.GetPublicId())
|
||||
amId := setupAuthMethod(t, conn, org.GetPublicId())
|
||||
acct := setupAuthAccount(t, conn, org.GetPublicId(), amId, u.GetPublicId())
|
||||
am := password.TestAuthMethods(t, conn, org.GetPublicId(), 1)[0]
|
||||
acct := password.TestAccounts(t, conn, am.GetPublicId(), 1)[0]
|
||||
createdAuthToken := TestAuthToken(t, conn, wrapper, org.GetPublicId())
|
||||
|
||||
testAuthTokenId := func() string {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
"github.com/hashicorp/watchtower/internal/auth/password"
|
||||
iamStore "github.com/hashicorp/watchtower/internal/iam/store"
|
||||
"github.com/hashicorp/watchtower/internal/oplog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -132,9 +133,13 @@ func TestRepository_CreateAuthToken(t *testing.T) {
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
org1, _ := iam.TestScopes(t, conn)
|
||||
u1 := iam.TestUser(t, conn, org1.GetPublicId())
|
||||
amId1 := setupAuthMethod(t, conn, org1.GetPublicId())
|
||||
aAcct := setupAuthAccount(t, conn, org1.GetPublicId(), amId1, u1.GetPublicId())
|
||||
am := password.TestAuthMethods(t, conn, org1.GetPublicId(), 1)[0]
|
||||
aAcct := password.TestAccounts(t, conn, am.GetPublicId(), 1)[0]
|
||||
|
||||
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
|
||||
require.NoError(t, err)
|
||||
u1, err := iamRepo.LookupUserWithLogin(context.Background(), aAcct.GetPublicId(), iam.WithAutoVivify(true))
|
||||
require.NoError(t, err)
|
||||
|
||||
org2, _ := iam.TestScopes(t, conn)
|
||||
u2 := iam.TestUser(t, conn, org2.GetPublicId())
|
||||
|
||||
@@ -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