From 02dc9be85fd878440f7e1d064046ee80e0df27c7 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 31 Oct 2022 10:36:18 -0400 Subject: [PATCH] refact(e2e): Create role functions --- testing/internal/e2e/boundary/role.go | 56 +++++++++++++++++++ .../tests/static/session_cancel_user_test.go | 39 ++----------- 2 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 testing/internal/e2e/boundary/role.go diff --git a/testing/internal/e2e/boundary/role.go b/testing/internal/e2e/boundary/role.go new file mode 100644 index 0000000000..4debcdfa35 --- /dev/null +++ b/testing/internal/e2e/boundary/role.go @@ -0,0 +1,56 @@ +package boundary + +import ( + "context" + "encoding/json" + "testing" + + "github.com/hashicorp/boundary/api/roles" + "github.com/hashicorp/boundary/testing/internal/e2e" + "github.com/stretchr/testify/require" +) + +// CreateNewRoleCli creates a new role using the cli. +// Returns the id of the new role. +func CreateNewRoleCli(t testing.TB, ctx context.Context, scopeId string) string { + output := e2e.RunCommand(ctx, "boundary", + e2e.WithArgs( + "roles", "create", + "-scope-id", scopeId, + "-name", "e2e Role", + "-format", "json", + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) + var newRoleResult roles.RoleCreateResult + err := json.Unmarshal(output.Stdout, &newRoleResult) + require.NoError(t, err) + + newRoleId := newRoleResult.Item.Id + t.Logf("Created Role: %s", newRoleId) + return newRoleId +} + +// AddGrantToRoleCli adds a grant/permission to a role using the cli +func AddGrantToRoleCli(t testing.TB, ctx context.Context, roleId string, grant string) { + output := e2e.RunCommand(ctx, "boundary", + e2e.WithArgs( + "roles", "add-grants", + "-id", roleId, + "-grant", grant, + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) +} + +// AddPrincipalToRoleCli adds a user/group to a role using the cli +func AddPrincipalToRoleCli(t testing.TB, ctx context.Context, roleId string, principal string) { + output := e2e.RunCommand(ctx, "boundary", + e2e.WithArgs( + "roles", "add-principals", + "-id", roleId, + "-principal", principal, + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) +} diff --git a/testing/internal/e2e/tests/static/session_cancel_user_test.go b/testing/internal/e2e/tests/static/session_cancel_user_test.go index 40148db72d..e82ce441e8 100644 --- a/testing/internal/e2e/tests/static/session_cancel_user_test.go +++ b/testing/internal/e2e/tests/static/session_cancel_user_test.go @@ -65,42 +65,11 @@ func TestCliSessionCancelUser(t *testing.T) { require.Equal(t, 403, response.Status) t.Log("Successfully received an error when connecting to target as a user without permissions") - // Create a role + // Create a role for user boundary.AuthenticateAdminCli(t, ctx) - output = e2e.RunCommand(ctx, "boundary", - e2e.WithArgs( - "roles", "create", - "-scope-id", newProjectId, - "-name", "e2e Role", - "-format", "json", - ), - ) - require.NoError(t, output.Err, string(output.Stderr)) - var newRoleResult roles.RoleCreateResult - err = json.Unmarshal(output.Stdout, &newRoleResult) - require.NoError(t, err) - newRoleId := newRoleResult.Item.Id - t.Logf("Created Role: %s", newRoleId) - - // Add grant to role - output = e2e.RunCommand(ctx, "boundary", - e2e.WithArgs( - "roles", "add-grants", - "-id", newRoleId, - "-grant", "id=*;type=target;actions=authorize-session", - ), - ) - require.NoError(t, output.Err, string(output.Stderr)) - - // Add user to role - output = e2e.RunCommand(ctx, "boundary", - e2e.WithArgs( - "roles", "add-principals", - "-id", newRoleId, - "-principal", newUserId, - ), - ) - require.NoError(t, output.Err, string(output.Stderr)) + newRoleId := boundary.CreateNewRoleCli(t, ctx, newProjectId) + boundary.AddGrantToRoleCli(t, ctx, newRoleId, "id=*;type=target;actions=authorize-session") + boundary.AddPrincipalToRoleCli(t, ctx, newRoleId, newUserId) // Connect to target to create a session ctxCancel, cancel := context.WithCancel(context.Background())