MCPcopy Index your code
hub / github.com/coder/coder / TestTokens

Function TestTokens

cli/tokens_test.go:24–249  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22)
23
24func TestTokens(t *testing.T) {
25 t.Parallel()
26 client := coderdtest.New(t, nil)
27 adminUser := coderdtest.CreateFirstUser(t, client)
28
29 secondUserClient, secondUser := coderdtest.CreateAnotherUser(t, client, adminUser.OrganizationID)
30 thirdUserClient, thirdUser := coderdtest.CreateAnotherUser(t, client, adminUser.OrganizationID)
31
32 ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
33 defer cancelFunc()
34
35 // helpful empty response
36 inv, root := clitest.New(t, "tokens", "ls")
37 //nolint:gocritic // This should be run as the owner user.
38 clitest.SetupConfig(t, client, root)
39 buf := new(bytes.Buffer)
40 inv.Stdout = buf
41 inv.Stderr = buf
42 err := inv.WithContext(ctx).Run()
43 require.NoError(t, err)
44 res := buf.String()
45 require.Contains(t, res, "tokens found")
46
47 inv, root = clitest.New(t, "tokens", "create", "--name", "token-one")
48 clitest.SetupConfig(t, client, root)
49 buf = new(bytes.Buffer)
50 inv.Stdout = buf
51 err = inv.WithContext(ctx).Run()
52 require.NoError(t, err)
53 res = buf.String()
54 require.NotEmpty(t, res)
55 id := res[:10]
56
57 allowWorkspaceID := uuid.New()
58 allowSpec := fmt.Sprintf("workspace:%s", allowWorkspaceID.String())
59 inv, root = clitest.New(t, "tokens", "create", "--name", "scoped-token", "--scope", string(codersdk.APIKeyScopeWorkspaceRead), "--allow", allowSpec)
60 clitest.SetupConfig(t, client, root)
61 buf = new(bytes.Buffer)
62 inv.Stdout = buf
63 err = inv.WithContext(ctx).Run()
64 require.NoError(t, err)
65 res = buf.String()
66 require.NotEmpty(t, res)
67 scopedTokenID := res[:10]
68
69 // Test creating a token for second user from first user's (admin) session
70 inv, root = clitest.New(t, "tokens", "create", "--name", "token-two", "--user", secondUser.ID.String())
71 clitest.SetupConfig(t, client, root)
72 buf = new(bytes.Buffer)
73 inv.Stdout = buf
74 err = inv.WithContext(ctx).Run()
75 // Test should succeed in creating token for second user
76 require.NoError(t, err)
77 res = buf.String()
78 require.NotEmpty(t, res)
79 secondTokenID := res[:10]
80
81 // Test listing tokens from the first user's (admin) session

Callers

nothing calls this directly

Calls 15

NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
NewFunction · 0.92
SetupConfigFunction · 0.92
NowFunction · 0.92
WithContextMethod · 0.80
NotEmptyMethod · 0.80
APIKeyByNameMethod · 0.80
RunMethod · 0.65
NewMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected