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

Function TestTokenCreation_ScopeValidation

coderd/apikey_scopes_validation_test.go:14–64  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestTokenCreation_ScopeValidation(t *testing.T) {
15 t.Parallel()
16
17 cases := []struct {
18 name string
19 scope codersdk.APIKeyScope
20 wantErr bool
21 }{
22 {name: "AllowsPublicLowLevelScope", scope: "workspace:read", wantErr: false},
23 {name: "RejectsInternalOnlyScope", scope: "debug_info:read", wantErr: true},
24 {name: "AllowsLegacyScopes", scope: "application_connect", wantErr: false},
25 {name: "AllowsLegacyScopes2", scope: "all", wantErr: false},
26 {name: "AllowsCanonicalSpecialScope", scope: "coder:all", wantErr: false},
27 }
28
29 for _, tc := range cases {
30 t.Run(tc.name, func(t *testing.T) {
31 t.Parallel()
32
33 client := coderdtest.New(t, nil)
34 _ = coderdtest.CreateFirstUser(t, client)
35
36 ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitShort)
37 defer cancel()
38
39 resp, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{Scope: tc.scope})
40 if tc.wantErr {
41 require.Error(t, err)
42 return
43 }
44 require.NoError(t, err)
45 require.NotEmpty(t, resp.Key)
46
47 // Fetch and verify the stored scopes match expectation.
48 keys, err := client.Tokens(ctx, codersdk.Me, codersdk.TokensFilter{})
49 require.NoError(t, err)
50 require.Len(t, keys, 1)
51
52 // Normalize legacy singular scopes to canonical coder:* values.
53 expected := tc.scope
54 switch tc.scope {
55 case codersdk.APIKeyScopeAll:
56 expected = codersdk.APIKeyScopeCoderAll
57 case codersdk.APIKeyScopeApplicationConnect:
58 expected = codersdk.APIKeyScopeCoderApplicationConnect
59 }
60
61 require.Contains(t, keys[0].Scopes, expected)
62 })
63 }
64}

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateTokenMethod · 0.80
NotEmptyMethod · 0.80
TokensMethod · 0.80
RunMethod · 0.65
ContextMethod · 0.65
ErrorMethod · 0.45
LenMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected