Skip to content

Commit

Permalink
refact(e2e): Add err to CreateAccountApi
Browse files Browse the repository at this point in the history
  • Loading branch information
moduli committed Apr 1, 2024
1 parent 4e995fc commit 3929fe5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
21 changes: 15 additions & 6 deletions testing/internal/e2e/boundary/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package boundary
import (
"context"
"encoding/json"
"fmt"
"testing"

"github.com/hashicorp/boundary/api"
Expand All @@ -15,21 +16,29 @@ import (
"github.com/stretchr/testify/require"
)

// CreateNewAccountApi creates a new account using the Go api.
// CreateAccountApi creates a new account using the Go api.
// Returns the id of the new account as well as the password that was generated
func CreateNewAccountApi(t testing.TB, ctx context.Context, client *api.Client, authMethodId string, loginName string) (accountId string, password string) {
func CreateAccountApi(t testing.TB, ctx context.Context, client *api.Client, authMethodId string, loginName string) (string, string, error) {
name, err := base62.Random(16)
if err != nil {
return "", "", err
}

aClient := accounts.NewClient(client)
password, err := base62.Random(16)
require.NoError(t, err)
newAccountResult, err := aClient.Create(ctx, authMethodId,
createAccountResult, err := aClient.Create(ctx, authMethodId,
accounts.WithPasswordAccountLoginName(loginName),
accounts.WithPasswordAccountPassword(password),
accounts.WithName(fmt.Sprintf("e2e Account %s", name)),
)
require.NoError(t, err)
if err != nil {
return "", "", err
}

accountId = newAccountResult.Item.Id
accountId := createAccountResult.Item.Id
t.Logf("Create Account: %s", accountId)
return
return accountId, password, nil
}

// CreateNewAccountCli creates a new account using the cli.
Expand Down
16 changes: 10 additions & 6 deletions testing/internal/e2e/tests/base/paginate_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestCliPaginateAccounts(t *testing.T) {
// Create enough accounts to overflow a single page.
var accountIds []string
for i := 0; i < c.MaxPageSize+1; i++ {
accId, _ := boundary.CreateNewAccountApi(t, ctx, client, amId, "testuser-"+strconv.Itoa(i))
accId, _, err := boundary.CreateAccountApi(t, ctx, client, amId, "testuser-"+strconv.Itoa(i))
require.NoError(t, err)
accountIds = append(accountIds, accId)
}

Expand Down Expand Up @@ -82,7 +83,8 @@ func TestCliPaginateAccounts(t *testing.T) {
assert.Empty(t, initialAccounts.ListToken)

// Create a new account and destroy one of the other accounts
newAccountId, _ := boundary.CreateNewAccountApi(t, ctx, client, amId, "newuser")
accountId, _, err := boundary.CreateAccountApi(t, ctx, client, amId, "newuser")
require.NoError(t, err)
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"accounts", "delete",
Expand All @@ -109,7 +111,7 @@ func TestCliPaginateAccounts(t *testing.T) {
// The first item should be the most recently created, which
// should be our new account
firstItem := newAccounts.Items[0]
assert.Equal(t, newAccountId, firstItem.Id)
assert.Equal(t, accountId, firstItem.Id)
assert.Empty(t, newAccounts.ResponseType)
assert.Empty(t, newAccounts.RemovedIds)
assert.Empty(t, newAccounts.ListToken)
Expand Down Expand Up @@ -153,7 +155,8 @@ func TestApiPaginateAccounts(t *testing.T) {
// Create enough accounts to overflow a single page.
var accountIds []string
for i := 0; i < c.MaxPageSize+1; i++ {
accId, _ := boundary.CreateNewAccountApi(t, ctx, client, amId, "testuser-"+strconv.Itoa(i))
accId, _, err := boundary.CreateAccountApi(t, ctx, client, amId, "testuser-"+strconv.Itoa(i))
require.NoError(t, err)
accountIds = append(accountIds, accId)
}

Expand All @@ -178,7 +181,8 @@ func TestApiPaginateAccounts(t *testing.T) {
assert.Len(t, mapSliceItems, c.MaxPageSize+1)

// Create a new account and destroy one of the other accounts
newAccountId, _ := boundary.CreateNewAccountApi(t, ctx, client, amId, "newuser")
accountId, _, err := boundary.CreateAccountApi(t, ctx, client, amId, "newuser")
require.NoError(t, err)
_, err = acClient.Delete(ctx, initialAccounts.Items[0].Id)
require.NoError(t, err)

Expand All @@ -194,7 +198,7 @@ func TestApiPaginateAccounts(t *testing.T) {
// The first item should be the most recently created, which
// should be our new account
firstItem := newAccounts.Items[0]
assert.Equal(t, newAccountId, firstItem.Id)
assert.Equal(t, accountId, firstItem.Id)
assert.Equal(t, "complete", newAccounts.ResponseType)
// Note that the removed IDs may contain entries from other tests,
// so just check that there is at least 1 entry and that our entry
Expand Down
7 changes: 4 additions & 3 deletions testing/internal/e2e/tests/base/session_cancel_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ func TestApiCreateGroup(t *testing.T) {
require.NoError(t, err)

acctName := "e2e-account"
newAcctId, _ := boundary.CreateNewAccountApi(t, ctx, client, bc.AuthMethodId, acctName)
accountId, _, err := boundary.CreateAccountApi(t, ctx, client, bc.AuthMethodId, acctName)
require.NoError(t, err)
t.Cleanup(func() {
aClient := accounts.NewClient(client)
_, err := aClient.Delete(ctx, newAcctId)
_, err := aClient.Delete(ctx, accountId)
require.NoError(t, err)
})
userId, err := boundary.CreateUserApi(t, ctx, client, "global")
Expand All @@ -216,7 +217,7 @@ func TestApiCreateGroup(t *testing.T) {
require.NoError(t, err)
})
uClient := users.NewClient(client)
_, err = uClient.SetAccounts(ctx, userId, 0, []string{newAcctId}, users.WithAutomaticVersioning(true))
_, err = uClient.SetAccounts(ctx, userId, 0, []string{accountId}, users.WithAutomaticVersioning(true))
require.NoError(t, err)

gClient := groups.NewClient(client)
Expand Down
7 changes: 4 additions & 3 deletions testing/internal/e2e/tests/base/session_cancel_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ func TestApiCreateUser(t *testing.T) {
require.NoError(t, err)

acctName := "e2e-account"
newAcctId, _ := boundary.CreateNewAccountApi(t, ctx, client, bc.AuthMethodId, acctName)
accountId, _, err := boundary.CreateAccountApi(t, ctx, client, bc.AuthMethodId, acctName)
require.NoError(t, err)
t.Cleanup(func() {
aClient := accounts.NewClient(client)
_, err := aClient.Delete(ctx, newAcctId)
_, err := aClient.Delete(ctx, accountId)
require.NoError(t, err)
})
userId, err := boundary.CreateUserApi(t, ctx, client, "global")
Expand All @@ -209,7 +210,7 @@ func TestApiCreateUser(t *testing.T) {
require.NoError(t, err)
})
uClient := users.NewClient(client)
_, err = uClient.SetAccounts(ctx, userId, 0, []string{newAcctId}, users.WithAutomaticVersioning(true))
_, err = uClient.SetAccounts(ctx, userId, 0, []string{accountId}, users.WithAutomaticVersioning(true))
require.NoError(t, err)

rClient := roles.NewClient(client)
Expand Down

0 comments on commit 3929fe5

Please sign in to comment.