Skip to content

Commit c67ed19

Browse files
committed
add tests
1 parent 3d1b075 commit c67ed19

6 files changed

+233
-5
lines changed

docs/cli/provisionerd_keys.md

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/cli/provisionerd_keys_create.md

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/cli/provisionerd_keys_delete.md

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/cli/provisionerd_keys_list.md

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/provisionerkeys.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

78
"github.com/google/uuid"
@@ -58,7 +59,7 @@ func (r *RootCmd) provisionerKeysCreate() *serpent.Command {
5859
return xerrors.Errorf("create provisioner key: %w", err)
5960
}
6061

61-
_, _ = fmt.Fprintf(inv.Stdout, "Successfully created provisioner key %s!\n\n%s", pretty.Sprint(cliui.DefaultStyles.Keyword, inv.Args[0]), pretty.Sprint(cliui.DefaultStyles.Keyword, res.Key))
62+
_, _ = fmt.Fprintf(inv.Stdout, "Successfully created provisioner key %s!\n\n%s\n", pretty.Sprint(cliui.DefaultStyles.Keyword, strings.ToLower(inv.Args[0])), pretty.Sprint(cliui.DefaultStyles.Keyword, res.Key))
6263

6364
return nil
6465
},
@@ -104,8 +105,9 @@ func (r *RootCmd) provisionerKeysList() *serpent.Command {
104105

105106
client := new(codersdk.Client)
106107
cmd := &serpent.Command{
107-
Use: "list",
108-
Short: "List provisioner keys",
108+
Use: "list",
109+
Short: "List provisioner keys",
110+
Aliases: []string{"ls"},
109111
Middleware: serpent.Chain(
110112
serpent.RequireNArgs(0),
111113
r.InitClient(client),
@@ -165,18 +167,28 @@ func (r *RootCmd) provisionerKeysDelete() *serpent.Command {
165167
return xerrors.Errorf("current organization: %w", err)
166168
}
167169

170+
_, err = cliui.Prompt(inv, cliui.PromptOptions{
171+
Text: fmt.Sprintf("Are you sure you want to delete provisioner key %s?", pretty.Sprint(cliui.DefaultStyles.Keyword, inv.Args[0])),
172+
IsConfirm: true,
173+
})
174+
if err != nil {
175+
return err
176+
}
177+
168178
err = client.DeleteProvisionerKey(ctx, org.ID, inv.Args[0])
169179
if err != nil {
170180
return xerrors.Errorf("delete provisioner key: %w", err)
171181
}
172182

173-
_, _ = fmt.Fprintf(inv.Stdout, "Successfully deleted provisioner key %s!", pretty.Sprint(cliui.DefaultStyles.Keyword, inv.Args[0]))
183+
_, _ = fmt.Fprintf(inv.Stdout, "Successfully deleted provisioner key %s!\n", pretty.Sprint(cliui.DefaultStyles.Keyword, strings.ToLower(inv.Args[0])))
174184

175185
return nil
176186
},
177187
}
178188

179-
cmd.Options = serpent.OptionSet{}
189+
cmd.Options = serpent.OptionSet{
190+
cliui.SkipPromptOption(),
191+
}
180192
orgContext.AttachOptions(cmd)
181193

182194
return cmd
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package cli_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/google/uuid"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/v2/cli/clitest"
11+
"github.com/coder/coder/v2/coderd/coderdtest"
12+
"github.com/coder/coder/v2/coderd/rbac"
13+
"github.com/coder/coder/v2/codersdk"
14+
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
15+
"github.com/coder/coder/v2/enterprise/coderd/license"
16+
"github.com/coder/coder/v2/pty/ptytest"
17+
"github.com/coder/coder/v2/testutil"
18+
)
19+
20+
func TestProvisionerKeys(t *testing.T) {
21+
t.Parallel()
22+
23+
t.Run("CRUD", func(t *testing.T) {
24+
t.Parallel()
25+
26+
dv := coderdtest.DeploymentValues(t)
27+
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}
28+
client, owner := coderdenttest.New(t, &coderdenttest.Options{
29+
Options: &coderdtest.Options{
30+
DeploymentValues: dv,
31+
},
32+
LicenseOptions: &coderdenttest.LicenseOptions{
33+
Features: license.Features{
34+
codersdk.FeatureMultipleOrganizations: 1,
35+
},
36+
},
37+
})
38+
orgAdminClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.ScopedRoleOrgAdmin(owner.OrganizationID))
39+
40+
name := "dont-TEST-me"
41+
ctx := testutil.Context(t, testutil.WaitMedium)
42+
inv, conf := newCLI(
43+
t,
44+
"provisioner", "keys", "create", name,
45+
)
46+
47+
pty := ptytest.New(t)
48+
inv.Stdout = pty.Output()
49+
clitest.SetupConfig(t, orgAdminClient, conf)
50+
51+
err := inv.WithContext(ctx).Run()
52+
require.NoError(t, err)
53+
54+
line := pty.ReadLine(ctx)
55+
require.Contains(t, line, "Successfully created provisioner key")
56+
require.Contains(t, line, strings.ToLower(name))
57+
// empty line
58+
_ = pty.ReadLine(ctx)
59+
key := pty.ReadLine(ctx)
60+
require.NotEmpty(t, key)
61+
parts := strings.Split(key, ":")
62+
require.Len(t, parts, 2, "expected 2 parts")
63+
_, err = uuid.Parse(parts[0])
64+
require.NoError(t, err, "expected token to be a uuid")
65+
66+
inv, conf = newCLI(
67+
t,
68+
"provisioner", "keys", "ls",
69+
)
70+
pty = ptytest.New(t)
71+
inv.Stdout = pty.Output()
72+
clitest.SetupConfig(t, orgAdminClient, conf)
73+
74+
err = inv.WithContext(ctx).Run()
75+
require.NoError(t, err)
76+
line = pty.ReadLine(ctx)
77+
require.Contains(t, line, "NAME")
78+
require.Contains(t, line, "CREATED AT")
79+
require.Contains(t, line, "ORGANIZATION ID")
80+
line = pty.ReadLine(ctx)
81+
require.Contains(t, line, strings.ToLower(name))
82+
83+
inv, conf = newCLI(
84+
t,
85+
"provisioner", "keys", "delete", "-y", name,
86+
)
87+
88+
pty = ptytest.New(t)
89+
inv.Stdout = pty.Output()
90+
clitest.SetupConfig(t, orgAdminClient, conf)
91+
92+
err = inv.WithContext(ctx).Run()
93+
require.NoError(t, err)
94+
line = pty.ReadLine(ctx)
95+
require.Contains(t, line, "Successfully deleted provisioner key")
96+
require.Contains(t, line, strings.ToLower(name))
97+
98+
inv, conf = newCLI(
99+
t,
100+
"provisioner", "keys", "ls",
101+
)
102+
pty = ptytest.New(t)
103+
inv.Stdout = pty.Output()
104+
clitest.SetupConfig(t, orgAdminClient, conf)
105+
106+
err = inv.WithContext(ctx).Run()
107+
require.NoError(t, err)
108+
line = pty.ReadLine(ctx)
109+
require.Contains(t, line, "No provisioner keys found")
110+
})
111+
}

0 commit comments

Comments
 (0)