8000 Organisation page: say why the roster is missing, and offer only the controls the hub accepts by ynishi · Pull Request #21 · 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
29 changes: 23 additions & 6 deletions web/src/routes/ns/[ns]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@
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));
// The roster answers only to a token that names the organisation. A
// signed-in caller whose token does not is told so, whether or not they
// are a member: the hint says what a token needs, not who is in it.
const unnamed = $derived(
info?.kind === 'org' && session.signedIn && !session.who.namespaces.includes(ns)
);
// Adding and removing members is `admin` on the organisation, which the
// hub reads as the lesser of the token's scope and the caller's role.
const myRole = $derived(members?.find((m) => m.user === session.who.user)?.role ?? null);
const canManage = $derived(
info?.kind === 'org' && session.who.scope === 'admin' && myRole === 'admin'
);

async function reloadMembers() {
members = await listMembers(ns).catch(() => null);
Expand All @@ -46,7 +57,8 @@
cards = (await listRecords('cards', { ns: current, limit: 25 })).items;
evals = (await listRecords('evals', { ns: current, limit: 25 })).items;
if (info.kind === 'org') {
// Only a member may read the roster; a 403 here is normal.
// Only a token that names the organisation may read the
// roster; a 403 here is normal.
members = await listMembers(current).catch(() => null);
}
} catch (e) {
Expand Down Expand Up @@ -97,6 +109,13 @@

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

{#if unnamed}
<p class="notice">
Your token does not name {ns}, so its members are not shown. A member sees them with a token
that names it: <a href="/settings?ns={ns}">issue one in Settings</a> and sign in with it.
</p>
{/if}

{#if members}
<h2>Members</h2>
<div class="scroll-x">
Expand Down Expand Up @@ -125,6 +144,7 @@
</div>
<p class="faint small">
A member's token acts with the lesser of its own scope and their role here.
{#if !canManage}Adding and removing members takes an admin token of an admin member.{/if}
</p>

{#if canManage}
Expand All @@ -143,10 +163,7 @@
</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>
<p class="faint small">Adding a user who is already a member changes their role.</p>
{/if}
{/if}

Expand Down
17 changes: 15 additions & 2 deletions web/src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
type Token,
type Visibility
} from '$lib/api/client';
import { page } from '$app/state';
import Errors from '$lib/components/Errors.svelte';
import { session } from '$lib/session.svelte';

Expand Down Expand Up @@ -50,8 +51,14 @@
if (session.signedIn) void reload();
});

// The form starts with the caller's own login, plus the organisation an
// organisation's page sent them here for (`?ns=`), so the token that
// names it takes no typing.
$effect(() => {
if (namespaces === '' && session.who.user) namespaces = session.who.user;
if (namespaces === '' && session.who.user) {
const org = page.url.searchParams.get('ns');
namespaces = [session.who.user, org].filter(Boolean).join(', ');
}
});

async function issue(event: SubmitEvent) {
Expand Down Expand Up @@ -115,7 +122,13 @@
notice = null;
try {
const result = await createOrg(orgNs.trim());
notice = `Organisation ${result.ns} created; you are its admin. Manage members on its page.`;
// The token in use predates the organisation and cannot see it;
// the next step is a token that names it, so fill that form in.
notice =
`Organisation ${result.ns} created; you are its admin. Your current token does not ` +
`name it: issue one that does (the form above is filled in) and sign in with it ` +
`to see its members. Managing them takes an admin token.`;
namespaces = [session.who.user, result.ns].filter(Boolean).join(', ');
createdOrg = result.ns;
orgNs = '';
} catch (e) {
Expand Down
112 changes: 81 additions & 31 deletions web/tests/browser/orgs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* 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.
* Proves: an admin can create an org, is told on its page that the token in use
* does not name it, finds the token form filled in for it, signs in with the
* token that names it, adds a member and removes one; and that a `write` token
* naming the org shows the roster without the controls the hub would refuse.
*
* 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';
import { expect, test, type Page } from '@playwright/test';

declare const process: {
env: Record<string, string | undefined>;
Expand All @@ -17,49 +19,78 @@ declare const process: {
const token = process.env.EVALHUB_E2E_TOKEN;
const member = process.env.EVALHUB_E2E_MEMBER ?? 'bob';

async function signIn(page: Page, secret: string) {
await page.goto('/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();
}

async function signOut(page: Page) {
await page.locator('header.masthead').getByRole('button', { name: 'sign out' }).click();
await expect(page).toHaveURL('/login');
}

/** Create an organisation from /settings as the signed-in user; returns its slug. */
async function createOrg(page: Page): Promise<string> {
await page.goto('/settings');
const org = 'acme-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
await page.locator('#org-ns').fill(org);
await page.getByRole('button', { name: 'Create' }).click();
await expect(page.getByText(`Organisation ${org} created`)).toBeVisible();
return org;
}

/** The token form names the caller and the organisation without typing. */
async function expectPrefilled(page: Page, org: string) {
await expect(page.locator('#namespaces')).toHaveValue(`alice, ${org}`);
}

/** Issue a token with the namespaces the form already holds; returns the secret. */
async function issue(page: Page, scope: 'read' | 'write' | 'admin'): Promise<string> {
await page.locator('#scope').selectOption(scope);
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();
return secret;
}

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();
await signIn(page, token!);

// 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();
const org = await createOrg(page);
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();
// 3. Its page, with the token that created it: no roster, and a line
// saying why instead of silence.
await page.getByRole('link', { name: `Open ${org}` }).click();
await expect(page.getByRole('heading', { level: 1, name: org })).toBeVisible();
await expect(page.getByText(`Your token does not name ${org}`)).toBeVisible();
await expect(page.getByRole('heading', { name: 'Members' })).toHaveCount(0);

// 4. The line links back to /settings with the token form already
// naming alice and the org.
await page.getByRole('link', { name: 'issue one in Settings' }).click();
await expectPrefilled(page, org);
const secret = await issue(page, 'admin');

// Sign out, then sign back in with the new token that names the org.
await signOut(page);
await signIn(page, secret);

// 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.getByText(`Your token does not name ${org}`)).toHaveCount(0);
await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible();
const aliceRow = page.getByRole('row', { name: /alice/ });
await expect(aliceRow).toContainText('admin');
Expand All @@ -84,4 +115,23 @@ test.describe('organisations', () => {
// 8. No error notices.
await expect(page.getByRole('alert')).toHaveCount(0);
});

test('a write token that names the organisation shows its roster without member controls', async ({
page
}) => {
await signIn(page, token!);
const org = await createOrg(page);
// Straight after creating it, the same screen's form names it.
await expectPrefilled(page, org);
const secret = await issue(page, 'write');
await signOut(page);
await signIn(page, secret);

await page.goto(`/ns/${org}`);
await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible();
await expect(page.getByRole('row', { name: /alice/ })).toContainText('admin');
await expect(page.locator('#member-user')).toHaveCount(0);
await expect(page.getByRole('button', { name: 'remove' })).toHaveCount(0);
await expect(page.getByText('Adding and removing members takes an admin token')).toBeVisible();
});
});
Loading
0