test(e2e): Add test that creates a user with the right permissions to start a session
This commit is contained in:
1 parent
07b16e877c
commit
6bd168b2a3
4 files changed
+357
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package boundary
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"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)
|
||||
t.Cleanup(func() {
|
||||
_, err := aClient.Delete(ctx, accountId)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
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, loginName string) (string, string) {
|
||||
c, err := loadConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
password, err := base62.Random(16)
|
||||
require.NoError(t, err)
|
||||
os.Setenv("E2E_TEST_ACCOUNT_PASSWORD", password)
|
||||
output := e2e.RunCommand(ctx, "boundary", "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",
|
||||
)
|
||||
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.Cleanup(func() {
|
||||
AuthenticateAdminCli(t)
|
||||
output := e2e.RunCommand(ctx, "boundary", "accounts", "delete", "-id", newAccountId)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
|
||||
t.Logf("Created Account: %s", newAccountId)
|
||||
|
||||
return newAccountId, password
|
||||
}
|
||||
@@ -58,6 +58,7 @@ func CreateNewOrgCli(t testing.TB) string {
|
||||
|
||||
newOrgId := newOrgResult.Item.Id
|
||||
t.Cleanup(func() {
|
||||
AuthenticateAdminCli(t)
|
||||
output := e2e.RunCommand(ctx, "boundary", "scopes", "delete", "-id", newOrgId)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// CreateNewUserCli creates a new user using the go api.
|
||||
// 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
|
||||
func CreateNewUserCli(t testing.TB, scopeId string) string {
|
||||
ctx := context.Background()
|
||||
output := e2e.RunCommand(ctx, "boundary", "users", "create",
|
||||
"-scope-id", scopeId,
|
||||
"-name", "e2e User",
|
||||
"-description", "e2e User",
|
||||
"-format", "json",
|
||||
)
|
||||
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() {
|
||||
AuthenticateAdminCli(t)
|
||||
output := e2e.RunCommand(ctx, "boundary", "users", "delete", "-id", newUserId)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
t.Logf("Created User: %s", newUserId)
|
||||
|
||||
return newUserId
|
||||
}
|
||||
|
||||
// SetAccountToUserCli sets an account to a the specified user using the cli.
|
||||
func SetAccountToUserCli(t testing.TB, userId string, accountId string) {
|
||||
output := e2e.RunCommand(context.Background(), "boundary", "users", "set-accounts",
|
||||
"-id", userId,
|
||||
"-account", accountId,
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
}
|
||||
Reference in new issue
Block a user