Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: making api tokens creation work #20009

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Bassel17 marked this conversation as resolved.
Show resolved Hide resolved
body?.lifespan && body.lifespan !== '0' ? parseInt(body.lifespan.toString(), 10) : null,
joshuaellis marked this conversation as resolved.
Show resolved Hide resolved
permissions: body.type === 'custom' ? state.selectedActions : null,
});

Expand Down Expand Up @@ -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,
});
Expand Down
52 changes: 52 additions & 0 deletions tests/e2e/tests/admin/api-tokens.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});