-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: UI limit for API tokens (#7532)
This PR activates the limit for API token creation in both the global API token window and in the project-level API token tab. Because the same button is used in two places, I encapsulated the fetching of flags and resource limits within the button. I can be convinced to pass the current API token count and the limit as arguments, but I think this is the right solution for this case.
- Loading branch information
1 parent
08533d7
commit c5fdaea
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...end/src/component/common/ApiTokenTable/CreateApiTokenButton/CreateApiTokenButton.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import { render } from 'utils/testRenderer'; | ||
import { testServerRoute, testServerSetup } from 'utils/testServer'; | ||
import { CreateApiTokenButton } from './CreateApiTokenButton'; | ||
import { CREATE_PROJECT_API_TOKEN } from 'component/providers/AccessProvider/permissions'; | ||
|
||
const server = testServerSetup(); | ||
|
||
const setupApi = ({ | ||
apiTokenCount, | ||
apiTokenLimit, | ||
}: { apiTokenCount: number; apiTokenLimit: number }) => { | ||
testServerRoute(server, '/api/admin/ui-config', { | ||
flags: { | ||
resourceLimits: true, | ||
}, | ||
resourceLimits: { | ||
apiTokens: apiTokenLimit, | ||
}, | ||
}); | ||
|
||
testServerRoute(server, '/api/admin/api-tokens', { | ||
tokens: Array.from({ length: apiTokenCount }).map((_, i) => ({ | ||
secret: 'super-secret', | ||
tokenName: `token—name-${i}`, | ||
type: 'client', | ||
})), | ||
}); | ||
}; | ||
|
||
test('should allow you to create API tokens when there are fewer apiTokens than the limit', async () => { | ||
setupApi({ apiTokenLimit: 3, apiTokenCount: 2 }); | ||
|
||
render( | ||
<CreateApiTokenButton | ||
permission={CREATE_PROJECT_API_TOKEN} | ||
path='create' | ||
/>, | ||
{ | ||
permissions: [{ permission: CREATE_PROJECT_API_TOKEN }], | ||
}, | ||
); | ||
|
||
await waitFor(async () => { | ||
const button = await screen.findByRole('button'); | ||
expect(button).not.toBeDisabled(); | ||
}); | ||
}); | ||
|
||
test('should not allow you to create API tokens when you have reached the limit', async () => { | ||
setupApi({ apiTokenLimit: 3, apiTokenCount: 3 }); | ||
|
||
render( | ||
<CreateApiTokenButton | ||
permission={CREATE_PROJECT_API_TOKEN} | ||
path='create' | ||
/>, | ||
{ | ||
permissions: [{ permission: CREATE_PROJECT_API_TOKEN }], | ||
}, | ||
); | ||
|
||
await waitFor(async () => { | ||
const button = await screen.findByRole('button'); | ||
expect(button).toBeDisabled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters