8000 Web UI for organisations: create one from Settings, manage members on its page by ynishi · Pull Request #17 · ynishi/evalhub · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion e2e/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ cd "$work"

check "evalhub migrate applies the schema" "$bin" migrate

# Create alice (admin) and bob for the browser suite. The token is captured
# into a variable and handed to Playwright on its command line only (an exported
# EVALHUB_* variable would reach the server, whose config rejects unknown keys); it is never
# printed to the terminal or a log.
created=$("$bin" user create alice --scope admin)
e2e_token=$(printf '%s\n' "$created" | awk '$1 == "token:" { print $2 }')
check "evalhub user create alice (admin) prints a token" [ -n "$e2e_token" ]
check "evalhub user create bob" bash -c '"$0" user create bob >/dev/null' "$bin"

"$bin" serve --bind "127.0.0.1:$http_port" >server.log 2>&1 &
server_pid=$!
base="http://127.0.0.1:$http_port"
Expand Down Expand Up @@ -104,7 +113,7 @@ check "server log shows it listening" grep -q 'evalhub listening' server.
# The browser suite (web/tests/browser) runs against the same server. It
# needs the Chromium headless shell that `just e2e-install` fetches.
echo "e2e: browser tests against $base"
if (cd "$repo/web" && EVALHUB_E2E_BASE_URL="$base" corepack pnpm exec playwright test); then
if (cd "$repo/web" && EVALHUB_E2E_BASE_URL="$base" EVALHUB_E2E_TOKEN="$e2e_token" EVALHUB_E2E_MEMBER=bob corepack pnpm exec playwright test); then
ok "browser suite (web/tests/browser)"
else
bad "browser suite (web/tests/browser); if the browser is missing, run \`just e2e-install\`"
Expand Down
19 changes: 19 additions & 0 deletions web/src/lib/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ export async function listMembers(org: string): Promise<Member[]> {
).items;
}

/** Create an organisation. The caller becomes its first admin. */
export async function createOrg(ns: string): Promise<Namespace> {
const params = { body: { ns } } as never;
return unwrap(await api.POST('/api/v1/orgs', params), 'creating the organisation');
}

/** Add a member to an organisation, or change their role. Requires admin on the org. */
export async function addMember(org: string, user: string, role: Scope): Promise<Member> {
const params = { params: { path: { org } }, body: { user, role } } as never;
return unwrap(await api.POST('/api/v1/orgs/{org}/members', params), 'adding the member');
}

/** Remove a member from an organisation. Requires admin on the org. */
export async function removeMember(org: string, user: string): Promise<void> {
const params = { params: { path: { org, user } } } as never;
const result = await api.DELETE('/api/v1/orgs/{org}/members/{user}', params);
if (result.response.status >= 400) unwrap(result, 'removing the member');
}

/** Browse one kind of registry entry. */
export async function listRegistry(
kind: string,
Expand Down
72 changes: 71 additions & 1 deletion web/src/routes/ns/[ns]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
getNamespace,
listMembers,
listRecords,
addMember,
removeMember,
type ListItem,
type Member,
type Namespace
type Namespace,
type Scope
} from '$lib/api/client';
import Errors from '$lib/components/Errors.svelte';
import { session } from '$lib/session.svelte';

const ns = $derived(page.params.ns ?? '');

Expand All @@ -22,6 +26,15 @@
let members = $state<Member[] | null>(null);
let error = $state<unknown>(null);

let newUser = $state('');
let newRole = $state<Scope>('read');
let notice = $state<string | null>(null);
const canManage = $derived(info?.kind === 'org' && members !== null && session.mayWrite(ns));

async function reloadMembers() {
members = await listMembers(ns).catch(() => null);
}

$effect(() => {
const current = ns;
error = null;
Expand All @@ -41,6 +54,33 @@
}
})();
});

async function add(event: SubmitEvent) {
event.preventDefault();
error = null;
notice = null;
try {
const user = newUser.trim();
const role = newRole;
await addMember(ns, user, role);
notice = `${user} is now ${role} in ${ns}.`;
newUser = '';
await reloadMembers();
} catch (e) {
error = e;
}
}

async function remove(member: Member) {
if (!confirm(`Remove ${member.user} from ${ns}?`)) return;
try {
await removeMember(ns, member.user);
notice = `${member.user} removed from ${ns}.`;
await reloadMembers();
} catch (e) {
error = e;
}
}
</script>

<svelte:head><title>{ns} · evalhub</title></svelte:head>
Expand All @@ -55,6 +95,8 @@

<Errors {error} />

{#if notice}<p class="notice">{notice}</p>{/if}

{#if members}
<h2>Members</h2>
<div class="scroll-x">
Expand All @@ -63,13 +105,19 @@
<tr>
<th scope="col">User</th>
<th scope="col">Role</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{#each members as member (member.user)}
<tr>
<td><a href="/ns/{member.user}">{member.user}</a></td>
<td><span class="tag">{member.role}</span></td>
<td>
{#if canManage && member.user !== session.who.user}
<button class="link danger" => remove(member)}>remove</button>
{/if}
</td>
</tr>
{/each}
</tbody>
Expand All @@ -78,6 +126,28 @@
<p class="faint small">
A member's token acts with the lesser of its own scope and their role here.
</p>

{#if canManage}
<form class="controls" >
<div>
<label for="member-user">User</label>
<input id="member-user" t 10BC8 ype="text" bind:value={newUser} placeholder="login" />
</div>
<div>
<label for="member-role">Role</label>
<select id="member-role" bind:value={newRole}>
<option value="read">read</option>
<option value="write">write</option>
<option value="admin">admin</option>
</select>
</div>
<button type="submit" disabled={!newUser.trim()}>Add or update</button>
</form>
<p class="faint small">
Adding a user who is already a member changes their role. Only an admin of the organisation
may do this; the hub refuses otherwise.
</p>
{/if}
{/if}

{#snippet recordTable(kind: 'cards' | 'evals', items: ListItem[])}
Expand Down
34 changes: 34 additions & 0 deletions web/src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// only its hash — so this screen makes that moment loud rather than
// letting it scroll past as one more row.
import {
createOrg,
createToken,
listTokens,
revokeToken,
Expand Down Expand Up @@ -33,6 +34,9 @@
let labelName = $state('');
let notice = $state<string | null>(null);

let orgNs = $state('');
let createdOrg = $state<string | null>(null);

async function reload() {
error = null;
try {
Expand Down Expand Up @@ -104,6 +108,20 @@
error = e;
}
}

async function createOrganisation(event: SubmitEvent) {
event.preventDefault();
error = null;
notice = null;
try {
const result = await createOrg(orgNs.trim());
notice = `Organisation ${result.ns} created; you are its admin. Manage members on its page.`;
createdOrg = result.ns;
orgNs = '';
} catch (e) {
error = e;
}
}
</script>

<svelte:head><title>Settings · evalhub</title></svelte:head>
Expand Down Expand Up @@ -198,6 +216,22 @@
</div>
{/if}

<h2>Organisations</h2>
<form class="controls" >
<div class="grow">
<label for="org-ns">Namespace</label>
<input id="org-ns" type="text" bind:value={orgNs} placeholder="acme" />
</div>
<button class="primary" type="submit" disabled={!orgNs.trim()}>Create</button>
</form>
{#if createdOrg}
<p class="small"><a href="/ns/{createdOrg}">Open {createdOrg}</a></p>
{/if}
<p class="faint small">
An organisation is a namespace with members; the creator is its first admin. A token must name
the organisation in its namespaces to act in it.
</p>

<h2>Record settings</h2>
<p class="muted small">
Both need <code>write</code> on the namespace. Creating records is the API's job, not this UI's.
Expand Down
87 changes: 87 additions & 0 deletions web/tests/browser/orgs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* End-to-end tests for the organisation screens through the real binary.
*
* Proves: an admin can create an org, issue a token that names it, sign in with
* that token, add a member, and remove a member.
*
* Skipped when the harness did not provide EVALHUB_E2E_TOKEN (e.g. during
* `pnpm exec playwright test` without the e2e/smoke.sh wrapper).
*/

import { expect, test } from '@playwright/test';

declare const process: {
env: Record<string, string | undefined>;
};

const token = process.env.EVALHUB_E2E_TOKEN;
const member = process.env.EVALHUB_E2E_MEMBER ?? 'bob';

test.describe('organisations', () => {
test.skip(!token, 'EVALHUB_E2E_TOKEN is not set; e2e/smoke.sh provides it');

test('an admin creates an organisation, issues a token for it, and manages its members', async ({
page
}) => {
// 1. Sign in as alice with the admin token from the harness.
await page.goto('/login');
await page.locator('#token').fill(token!);
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(
page.locator('header.masthead').getByRole('link', { name: 'alice' })
).toBeVisible();

// 2. Create an organisation from /settings.
await page.goto('/settings');
const org = 'acme-' + Date.now().toString(36);
await page.locator('#org-ns').fill(org);
await page.getByRole('button', { name: 'Create' }).click();
await expect(page.getByText(`Organisation ${org} created`)).toBeVisible();
await expect(page.getByRole('link', { name: `Open ${org}` })).toBeVisible();

// 3. Issue a token that names alice and the new org.
await page.locator('#scope').selectOption('admin');
await page.locator('#namespaces').fill(`alice, ${org}`);
await page.getByRole('button', { name: 'Issue' }).click();
await expect(page.getByRole('alert')).toContainText('Copy this token now');
const secret = (await page.locator('.secret-value').textContent())?.trim() ?? '';
expect(secret.length).toBeGreaterThan(20);
await page.getByRole('button', { name: 'I have copied it' }).click();

// 4. Sign out, then sign back in with the new token that names the org.
await page.locator('header.masthead').getByRole('button', { name: 'sign out' }).click();
await expect(page).toHaveURL('/login');
await page.locator('#token').fill(secret);
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(
page.locator('header.masthead').getByRole('link', { name: 'alice' })
).toBeVisible();

// 5. Navigate to the org page and verify admin row.
await page.goto(`/ns/${org}`);
await expect(page.getByRole('heading', { level: 1, name: org })).toBeVisible();
await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible();
const aliceRow = page.getByRole('row', { name: /alice/ });
await expect(aliceRow).toContainText('admin');
await expect(aliceRow.getByRole('button', { name: 'remove' })).toHaveCount(0);

// 6. Add member.
await page.locator('#member-user').fill(member);
await page.locator('#member-role').selectOption('write');
await page.getByRole('button', { name: 'Add or update' }).click();
await expect(page.getByText(`${member} is now write in ${org}.`)).toBeVisible();
const memberRow = page.getByRole('row', {
name: new RegExp(member)
});
await expect(memberRow).toContainText('write');

// 7. Remove member.
page.once('dialog', (d) => d.accept());
await memberRow.getByRole('button', { name: 'remove' }).click();
await expect(page.getByText(`${member} removed from ${org}.`)).toBeVisible();
await expect(page.getByRole('row', { name: new RegExp(member) })).toHaveCount(0);

// 8. No error notices.
await expect(page.getByRole('alert')).toHaveCount(0);
});
});
Loading
0