1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-12-09 20:04:11 +01:00
unleash.unleash/frontend/src/component/common/ApiTokenTable/CreateApiTokenButton/CreateApiTokenButton.test.tsx
Thomas Heartman c5fdaeabd9
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.
2024-07-03 12:36:48 +00:00

68 lines
1.9 KiB
TypeScript

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();
});
});