2022-10-24 16:08:09 -04:00
|
|
|
package boundary
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api"
|
|
|
|
|
"github.com/hashicorp/boundary/api/users"
|
|
|
|
|
"github.com/hashicorp/boundary/testing/internal/e2e"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-25 09:59:03 -04:00
|
|
|
// CreateNewUserCli creates a new user using the Go api.
|
2022-10-24 16:08:09 -04:00
|
|
|
// Returns the id of the new user
|
|
|
|
|
func CreateNewUserApi(t testing.TB, ctx context.Context, client *api.Client, scopeId string) string {
|
|
|
|
|
uClient := users.NewClient(client)
|
|
|
|
|
newUserResult, err := uClient.Create(ctx, scopeId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
newUserId := newUserResult.Item.Id
|
|
|
|
|
t.Logf("Created User: %s", newUserId)
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
_, err := uClient.Delete(ctx, newUserId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return newUserId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateNewUserCli creates a new user using the cli.
|
|
|
|
|
// Returns the id of the new user
|
2022-10-25 15:46:10 -04:00
|
|
|
func CreateNewUserCli(t testing.TB, ctx context.Context, scopeId string) string {
|
2022-10-25 15:28:39 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
|
|
|
|
e2e.WithArgs(
|
|
|
|
|
"users", "create",
|
|
|
|
|
"-scope-id", scopeId,
|
|
|
|
|
"-name", "e2e User",
|
|
|
|
|
"-description", "e2e User",
|
|
|
|
|
"-format", "json",
|
|
|
|
|
),
|
2022-10-24 16:08:09 -04:00
|
|
|
)
|
|
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
|
|
|
|
|
var newUserResult users.UserCreateResult
|
|
|
|
|
err := json.Unmarshal(output.Stdout, &newUserResult)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
newUserId := newUserResult.Item.Id
|
|
|
|
|
t.Cleanup(func() {
|
2022-10-25 15:46:10 -04:00
|
|
|
AuthenticateAdminCli(t, context.Background())
|
2022-10-25 15:28:39 -04:00
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
2022-11-02 20:05:15 -04:00
|
|
|
e2e.WithArgs("users", "delete", "-id", newUserId, "-format", "json"),
|
2022-10-25 15:28:39 -04:00
|
|
|
)
|
2022-11-02 20:05:15 -04:00
|
|
|
// Only allow an error if it's due to a Resource Not Found (404)
|
|
|
|
|
// This was needed since there's a test that already deletes the user and would run into an
|
|
|
|
|
// error during cleanup
|
|
|
|
|
if output.Err != nil {
|
|
|
|
|
var response e2e.CliError
|
|
|
|
|
err := json.Unmarshal(output.Stderr, &response)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, 404, int(response.Status))
|
|
|
|
|
}
|
2022-10-24 16:08:09 -04:00
|
|
|
})
|
|
|
|
|
t.Logf("Created User: %s", newUserId)
|
|
|
|
|
|
|
|
|
|
return newUserId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAccountToUserCli sets an account to a the specified user using the cli.
|
2022-10-25 15:46:10 -04:00
|
|
|
func SetAccountToUserCli(t testing.TB, ctx context.Context, userId string, accountId string) {
|
|
|
|
|
output := e2e.RunCommand(ctx, "boundary",
|
2022-10-25 15:28:39 -04:00
|
|
|
e2e.WithArgs(
|
|
|
|
|
"users", "set-accounts",
|
|
|
|
|
"-id", userId,
|
|
|
|
|
"-account", accountId,
|
|
|
|
|
),
|
2022-10-24 16:08:09 -04:00
|
|
|
)
|
|
|
|
|
require.NoError(t, output.Err, string(output.Stderr))
|
|
|
|
|
}
|