Merge pull request #20009 from strapi/fix/creating-api-tokens

fix: making api tokens creation work
This commit is contained in:
Bassel Kanso 2024-04-08 11:55:09 +03:00 committed by GitHub
commit 6fe2e78edf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 2 deletions

View File

@ -190,7 +190,8 @@ export const EditView = () => {
const res = await createToken({
...body,
// lifespan must be "null" for unlimited (0 would mean instantly expired and isn't accepted)
lifespan: body?.lifespan || null,
lifespan:
body?.lifespan && body.lifespan !== '0' ? parseInt(body.lifespan.toString(), 10) : null,
permissions: body.type === 'custom' ? state.selectedActions : null,
});
@ -220,7 +221,7 @@ export const EditView = () => {
tokenType: API_TOKEN_TYPE,
});
navigate(res.data.id.toString(), {
navigate(`../api-tokens/${res.data.id.toString()}`, {
state: { apiToken: res.data },
replace: true,
});

View File

@ -0,0 +1,52 @@
import { test, expect } from '@playwright/test';
import { login } from '../../utils/login';
import { resetDatabaseAndImportDataFromPath } from '../../scripts/dts-import';
import { navToHeader } from '../../utils/shared';
const createAPIToken = async (page, tokenName, duration, type) => {
await navToHeader(page, ['Settings', 'API Tokens', 'Create new API Token'], 'Create API Token');
await page.getByLabel('Name*').click();
await page.getByLabel('Name*').fill(tokenName);
await page.getByLabel('Token duration').click();
await page.getByRole('option', { name: duration }).click();
await page.getByLabel('Token type').click();
await page.getByRole('option', { name: type }).click();
await page.getByRole('button', { name: 'Save' }).click();
await expect(page.getByText('Make sure to copy this token')).toBeVisible();
await expect(page.getByText('Expiration date:')).toBeVisible();
};
test.describe('API Tokens', () => {
test.beforeEach(async ({ page }) => {
await resetDatabaseAndImportDataFromPath('with-admin.tar');
await page.goto('/admin');
await login({ page });
});
// Test token creation
const testCases = [
['30-day Read-only token', '30 days', 'Read-only'],
['30-day full-access token', '30 days', 'Full access'],
['7-day token', '7 days', 'Full access'],
['90-day token', '90 days', 'Full access'],
['unlimited token', 'Unlimited', 'Full access'],
];
for (const [name, duration, type] of testCases) {
test(`A user should be able to create a ${name}`, async ({ page }) => {
await createAPIToken(page, name, duration, type);
});
}
test('Created tokens list page should be correct', async ({ page }) => {
await createAPIToken(page, 'my test token', 'unlimited', 'Full access');
await navToHeader(page, ['Settings', 'API Tokens'], 'API Tokens');
const row = page.getByRole('gridcell', { name: 'my test token', exact: true });
await expect(row).toBeVisible();
});
});