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)
| 135 | // legacy value in the deprecated singular Scope field while also supporting |
| 136 | // the new multi-scope field. |
| 137 | func 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 | |
| 183 | func TestUserSetTokenDuration(t *testing.T) { |
| 184 | t.Parallel() |
nothing calls this directly
no test coverage detected