|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/coder/coder/v2/cli/clitest" |
| 9 | + "github.com/coder/coder/v2/coderd/coderdtest" |
| 10 | + "github.com/coder/coder/v2/coderd/rbac" |
| 11 | + "github.com/coder/coder/v2/codersdk" |
| 12 | + "github.com/coder/coder/v2/enterprise/coderd/coderdenttest" |
| 13 | + "github.com/coder/coder/v2/enterprise/coderd/license" |
| 14 | + "github.com/coder/coder/v2/pty/ptytest" |
| 15 | + "github.com/coder/coder/v2/testutil" |
| 16 | +) |
| 17 | + |
| 18 | +func TestShowRoles(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + t.Run("OK", func(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + dv := coderdtest.DeploymentValues(t) |
| 25 | + dv.Experiments = []string{string(codersdk.ExperimentCustomRoles)} |
| 26 | + owner, admin := coderdenttest.New(t, &coderdenttest.Options{ |
| 27 | + Options: &coderdtest.Options{ |
| 28 | + DeploymentValues: dv, |
| 29 | + }, |
| 30 | + LicenseOptions: &coderdenttest.LicenseOptions{ |
| 31 | + Features: license.Features{ |
| 32 | + codersdk.FeatureCustomRoles: 1, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + // Requires an owner |
| 38 | + client, _ := coderdtest.CreateAnotherUser(t, owner, admin.OrganizationID, rbac.RoleOwner()) |
| 39 | + |
| 40 | + const expectedRole = "test-role" |
| 41 | + ctx := testutil.Context(t, testutil.WaitMedium) |
| 42 | + _, err := client.PatchRole(ctx, codersdk.Role{ |
| 43 | + Name: expectedRole, |
| 44 | + DisplayName: "Test Role", |
| 45 | + SitePermissions: codersdk.CreatePermissions(map[codersdk.RBACResource][]codersdk.RBACAction{ |
| 46 | + codersdk.ResourceWorkspace: {codersdk.ActionRead, codersdk.ActionUpdate}, |
| 47 | + }), |
| 48 | + }) |
| 49 | + require.NoError(t, err, "create role") |
| 50 | + |
| 51 | + inv, conf := newCLI(t, "roles", "show", "test-role") |
| 52 | + |
| 53 | + pty := ptytest.New(t) |
| 54 | + inv.Stdout = pty.Output() |
| 55 | + clitest.SetupConfig(t, client, conf) |
| 56 | + |
| 57 | + err = inv.Run() |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + matches := []string{ |
| 61 | + "test-role", "2 permissions", |
| 62 | + } |
| 63 | + |
| 64 | + for _, match := range matches { |
| 65 | + pty.ExpectMatch(match) |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
0 commit comments