60 lines
4.0 KiB
TypeScript
60 lines
4.0 KiB
TypeScript
import { cleanup, render, screen, within } from '@testing-library/react';
|
|||
|
|
import userEvent from '@testing-library/user-event';
|
||
|
|
import { afterEach, expect, it, vi } from 'vitest';
|
||
|
|
import Admin from './Admin';
|
||
|
|
|
||
|
|
afterEach(() => { cleanup(); vi.unstubAllGlobals(); });
|
||
|
|
|
||
|
|
it('creates a project from the admin form and makes it available for account and agent grants', async () => {
|
||
|
|
let created = false;
|
||
|
|
let conflict = true;
|
||
|
|
const project = {
|
||
|
|
schema_version: 1, project_id: 'my-research', title: 'My research', status: 'active',
|
||
|
|
north_star: 'Investigate useful techniques.', key_questions: ['What works?', 'Under what conditions?'],
|
||
|
|
owners: ['user:admin'], autonomy_policy_id: 'my-research-policy',
|
||
|
|
};
|
||
|
|
const fetchMock = vi.fn(async (path: string, init?: RequestInit) => {
|
||
|
|
if (path === '/v1/session' && init?.method === 'GET') return { ok: false, status: 401, json: async () => ({}) };
|
||
|
|
let result: unknown = [];
|
||
|
|
if (path === '/v1/users/session') result = { csrf_token: 'csrf-fixture' };
|
||
|
|
if (path === '/v1/me') result = { principal_id: 'user:admin' };
|
||
|
|
if (path === '/v1/admin/projects') {
|
||
|
|
if (init?.method === 'POST') {
|
||
|
|
if (conflict) return { ok: false, status: 409, json: async () => ({ detail: 'Project ID is already registered' }) };
|
||
|
|
created = true; result = project;
|
||
|
|
} else result = created ? [project] : [];
|
||
|
|
}
|
||
|
|
return { ok: true, json: async () => result };
|
||
|
|
});
|
||
|
|
vi.stubGlobal('fetch', fetchMock);
|
||
|
|
const user = userEvent.setup();
|
||
|
|
render(<Admin />);
|
||
|
|
await user.type(await screen.findByLabelText('Username'), 'admin');
|
||
|
|
await user.type(screen.getByLabelText('Password'), 'password-fixture');
|
||
|
|
await user.click(screen.getByRole('button', { name: 'Sign in' }));
|
||
|
|
const projects = await screen.findByRole('region', { name: 'Projects' });
|
||
|
|
expect(within(projects).getByText('No projects yet. Create your first project below.')).toBeVisible();
|
||
|
|
expect(within(projects).getByLabelText(/Owner ID/)).toHaveValue('user:admin');
|
||
|
|
await user.type(within(projects).getByLabelText('Project name'), 'My research');
|
||
|
|
expect(within(projects).getByLabelText(/Project ID/)).toHaveValue('my-research');
|
||
|
|
await user.type(within(projects).getByLabelText('Project purpose'), 'Investigate useful techniques.');
|
||
|
|
await user.type(within(projects).getByLabelText('Key research questions'), 'What works?\nUnder what conditions?');
|
||
|
|
await user.click(within(projects).getByRole('button', { name: 'Create project' }));
|
||
|
|
expect(await screen.findByRole('alert')).toHaveTextContent('Project ID is already registered');
|
||
|
|
expect(within(projects).getByLabelText('Project name')).toHaveValue('My research');
|
||
|
|
conflict = false;
|
||
|
|
await user.click(within(projects).getByRole('button', { name: 'Create project' }));
|
||
|
|
await screen.findByText('Created My research. It is now available for user access and agent keys.');
|
||
|
|
expect(fetchMock).toHaveBeenCalledWith('/v1/admin/projects', expect.objectContaining({
|
||
|
|
method: 'POST', headers: expect.objectContaining({ 'X-GEWU-CSRF': 'csrf-fixture' }),
|
||
|
|
body: JSON.stringify({
|
||
|
|
project: { project_id: 'my-research', title: 'My research', status: 'active', owners: ['user:admin'], north_star: 'Investigate useful techniques.', key_questions: ['What works?', 'Under what conditions?'], autonomy_policy_id: 'my-research-policy', default_includes: [] },
|
||
|
|
policy: { policy_id: 'my-research-policy', policy_version: 1, project_id: 'my-research', automatic_source_registration: true, automatic_node_types: ['finding', 'problem', 'technique', 'case', 'entity', 'event', 'concept', 'datapoint', 'synthesis'], require_supported_assessment: true },
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
expect(within(projects).getByLabelText('Project name')).toHaveValue('');
|
||
|
|
await user.click(screen.getByRole('tab', { name: 'Users' }));
|
||
|
|
expect(within(screen.getByRole('region', { name: 'User accounts' })).getByLabelText('My research')).toBeVisible();
|
||
|
|
await user.click(screen.getByRole('tab', { name: 'Credentials' }));
|
||
|
|
expect(within(screen.getByRole('region', { name: 'Create a credential' })).getByLabelText('My research')).toBeVisible();
|
||
|
|
});
|