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

Function TestTokenLegacySingularScopeCompat

coderd/apikey_test.go:137–181  ·  view source on GitHub ↗

Ensure backward-compat: when a token is created using the legacy singular scope names ("all" or "application_connect"), the API returns the same legacy value in the deprecated singular Scope field while also supporting the new multi-scope field.

(t *testing.T)

Source from the content-addressed store, hash-verified

135// legacy value in the deprecated singular Scope field while also supporting
136// the new multi-scope field.
137func TestTokenLegacySingularScopeCompat(t *testing.T) {
138 t.Parallel()
139
140 cases := []struct {
141 name string
142 scope codersdk.APIKeyScope
143 scopes []codersdk.APIKeyScope
144 }{
145 {
146 name: "all",
147 scope: codersdk.APIKeyScopeAll,
148 scopes: []codersdk.APIKeyScope{codersdk.APIKeyScopeCoderAll},
149 },
150 {
151 name: "application_connect",
152 scope: codersdk.APIKeyScopeApplicationConnect,
153 scopes: []codersdk.APIKeyScope{codersdk.APIKeyScopeCoderApplicationConnect},
154 },
155 }
156
157 for _, tc := range cases {
158 t.Run(tc.name, func(t *testing.T) {
159 t.Parallel()
160 ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitLong)
161 defer cancel()
162 client := coderdtest.New(t, nil)
163 _ = coderdtest.CreateFirstUser(t, client)
164
165 // Create with legacy singular scope.
166 _, err := client.CreateToken(ctx, codersdk.Me, codersdk.CreateTokenRequest{
167 Scope: tc.scope,
168 })
169 require.NoError(t, err)
170
171 // Read back and ensure the deprecated singular field matches exactly.
172 keys, err := client.Tokens(ctx, codersdk.Me, codersdk.TokensFilter{})
173 require.NoError(t, err)
174 require.Len(t, keys, 1)
175 require.Equal(t, tc.scope, keys[0].Scope)
176 require.ElementsMatch(t, keys[0].Scopes, tc.scopes)
177 require.Len(t, keys[0].AllowList, 1)
178 require.Equal(t, "*:*", keys[0].AllowList[0].String())
179 })
180 }
181}
182
183func TestUserSetTokenDuration(t *testing.T) {
184 t.Parallel()

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateTokenMethod · 0.80
TokensMethod · 0.80
RunMethod · 0.65
ContextMethod · 0.65
LenMethod · 0.45
EqualMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected