Files
threshold/testing/internal/e2e/boundary/account.go
T
Michael LiandJohan Brandhorst-Satzkorn 45171a3f56 refact(e2e): Changes to support an automated migration test (#2676)
* refact(e2e): Move cleanup steps into each test

The migration test needed to create some resources, but doesn't care about cleaning up because the test would take care of spinning down the docker container that it's using. To maintain flexibility going forward, we'll establish that tests are responsible for cleaning up (rather than the methods they use)

* refact(e2e): Use new env var for skip check

Currently, the e2e tests use the E2E_PASSWORD_AUTH_METHOD_ID environment variable to determine if the test should be skipped. However, the new migration test does not require a pre-existing boundary cluster and would not have this environment variable. As a result, we needed an alternative way to check if an e2e test should be skipped when running tests in ci.

* refact(e2e): Create more common boundary methods

* fix(e2e): Set to string value

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2022-12-06 15:59:17 -05:00

65 lines
2.0 KiB
Go

package boundary
import (
"context"
"encoding/json"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/accounts"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/go-secure-stdlib/base62"
"github.com/stretchr/testify/require"
)
// CreateNewAccountApi 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, loginName string) (accountId string, password string) {
c, err := loadConfig()
require.NoError(t, err)
aClient := accounts.NewClient(client)
password, err = base62.Random(16)
require.NoError(t, err)
newAccountResult, err := aClient.Create(ctx, c.AuthMethodId,
accounts.WithPasswordAccountLoginName(loginName),
accounts.WithPasswordAccountPassword(password),
)
require.NoError(t, err)
accountId = newAccountResult.Item.Id
t.Logf("Create Account: %s", accountId)
return
}
// CreateNewAccountCli creates a new account using the cli.
// Returns the id of the new account as well as the password that was generated
func CreateNewAccountCli(t testing.TB, ctx context.Context, loginName string) (string, string) {
c, err := loadConfig()
require.NoError(t, err)
password, err := base62.Random(16)
require.NoError(t, err)
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"accounts", "create", "password",
"-auth-method-id", c.AuthMethodId,
"-login-name", loginName,
"-password", "env://E2E_TEST_ACCOUNT_PASSWORD",
"-name", "e2e Account "+loginName,
"-description", "e2e Account",
"-format", "json",
),
e2e.WithEnv("E2E_TEST_ACCOUNT_PASSWORD", password),
)
require.NoError(t, output.Err, string(output.Stderr))
var newAccountResult accounts.AccountCreateResult
err = json.Unmarshal(output.Stdout, &newAccountResult)
require.NoError(t, err)
newAccountId := newAccountResult.Item.Id
t.Logf("Created Account: %s", newAccountId)
return newAccountId, password
}