Compare commits
11 Commits
feat/app-m
...
test/front
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec3c768262 | ||
|
|
3e72ddde78 | ||
|
|
cd20ffb580 | ||
|
|
cddd479fd2 | ||
|
|
8bbcdd86aa | ||
|
|
2d56e42699 | ||
|
|
f9d9ed8cb4 | ||
|
|
c17f8a5bd9 | ||
|
|
7198fb4d0e | ||
|
|
029a4a199f | ||
|
|
74f7b3b631 |
11
.gitignore
vendored
11
.gitignore
vendored
@@ -30,6 +30,17 @@ config.local.toml
|
||||
/dashboard/build
|
||||
/dashboard/.env
|
||||
|
||||
# Dashboard — Playwright E2E
|
||||
/dashboard/tests/e2e/.auth
|
||||
/dashboard/tests/e2e/.results
|
||||
/dashboard/playwright-report
|
||||
/dashboard/test-results
|
||||
/dashboard/.playwright
|
||||
# When playwright is invoked from the repo root by accident, these
|
||||
# also land here.
|
||||
/playwright-report
|
||||
/test-results
|
||||
|
||||
# Caddy
|
||||
/caddy/data
|
||||
/caddy/config
|
||||
|
||||
@@ -2,3 +2,9 @@
|
||||
build
|
||||
node_modules
|
||||
package-lock.json
|
||||
|
||||
# Playwright generated artifacts
|
||||
playwright-report
|
||||
test-results
|
||||
tests/e2e/.auth
|
||||
tests/e2e/.results
|
||||
|
||||
64
dashboard/package-lock.json
generated
64
dashboard/package-lock.json
generated
@@ -20,6 +20,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.18.0",
|
||||
"@playwright/test": "^1.60.0",
|
||||
"@sveltejs/adapter-static": "^3.0.8",
|
||||
"@sveltejs/kit": "^2.17.0",
|
||||
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
||||
@@ -885,6 +886,22 @@
|
||||
"integrity": "sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@playwright/test": {
|
||||
"version": "1.60.0",
|
||||
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.60.0.tgz",
|
||||
"integrity": "sha512-O71yZIbAh/PxDMNGns37GHBIfrVkEVyn+AXyIa5dOTfb4/xNvRWV+Vv/NMbNCtODB/pO7vLlF2OTmMVLhmr7Ag==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"playwright": "1.60.0"
|
||||
},
|
||||
"bin": {
|
||||
"playwright": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@polka/url": {
|
||||
"version": "1.0.0-next.29",
|
||||
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.29.tgz",
|
||||
@@ -3010,6 +3027,53 @@
|
||||
"url": "https://github.com/sponsors/jonschlinkert"
|
||||
}
|
||||
},
|
||||
"node_modules/playwright": {
|
||||
"version": "1.60.0",
|
||||
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.60.0.tgz",
|
||||
"integrity": "sha512-hheHdokM8cdqCb0lcE3s+zT4t4W+vvjpGxsZlDnikarzx8tSzMebh3UiFtgqwFwnTnjYQcsyMF8ei2mCO/tpeA==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"playwright-core": "1.60.0"
|
||||
},
|
||||
"bin": {
|
||||
"playwright": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"fsevents": "2.3.2"
|
||||
}
|
||||
},
|
||||
"node_modules/playwright-core": {
|
||||
"version": "1.60.0",
|
||||
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.60.0.tgz",
|
||||
"integrity": "sha512-9bW6zvX/m0lEbgTKJ6YppOKx8H3VOPBMOCFh2irXFOT4BbHgrx5hPjwJYLT40Lu+4qtD36qKc/Hn56StUW57IA==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"bin": {
|
||||
"playwright-core": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/playwright/node_modules/fsevents": {
|
||||
"version": "2.3.2",
|
||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
|
||||
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
],
|
||||
"engines": {
|
||||
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/postcss": {
|
||||
"version": "8.5.15",
|
||||
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.15.tgz",
|
||||
|
||||
@@ -11,10 +11,14 @@
|
||||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && eslint .",
|
||||
"test": "vitest run"
|
||||
"test": "vitest run",
|
||||
"test:e2e": "playwright test",
|
||||
"test:e2e:ui": "playwright test --ui",
|
||||
"test:e2e:install": "playwright install --with-deps chromium"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.18.0",
|
||||
"@playwright/test": "^1.60.0",
|
||||
"@sveltejs/adapter-static": "^3.0.8",
|
||||
"@sveltejs/kit": "^2.17.0",
|
||||
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
||||
|
||||
51
dashboard/playwright.config.ts
Normal file
51
dashboard/playwright.config.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { defineConfig, devices } from '@playwright/test';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const DASHBOARD_PORT = Number(process.env.PICLOUD_DASHBOARD_PORT ?? 5173);
|
||||
// baseURL is the origin only — the SvelteKit dashboard is mounted at
|
||||
// `/admin` (svelte.config.js paths.base), so tests use full paths like
|
||||
// `/admin/login` rather than relying on baseURL path resolution.
|
||||
const DASHBOARD_BASE = process.env.E2E_BASE_URL ?? `http://localhost:${DASHBOARD_PORT}`;
|
||||
|
||||
export default defineConfig({
|
||||
testDir: './tests/e2e',
|
||||
outputDir: './tests/e2e/.results',
|
||||
fullyParallel: true,
|
||||
forbidOnly: !!process.env.CI,
|
||||
// Local: 1 retry to absorb dev-server warmup flakiness. CI: 2.
|
||||
retries: process.env.CI ? 2 : 1,
|
||||
// Cap at 4 workers locally to keep the shared Vite dev server
|
||||
// from getting stampeded during cold-start compiles.
|
||||
workers: process.env.CI ? 2 : 4,
|
||||
reporter: process.env.CI ? [['html'], ['github']] : 'html',
|
||||
globalSetup: './tests/e2e/global-setup.ts',
|
||||
expect: { timeout: 5_000 },
|
||||
use: {
|
||||
baseURL: DASHBOARD_BASE,
|
||||
actionTimeout: 10_000,
|
||||
navigationTimeout: 30_000,
|
||||
trace: 'on-first-retry',
|
||||
screenshot: 'only-on-failure',
|
||||
video: 'retain-on-failure'
|
||||
},
|
||||
projects: [
|
||||
{
|
||||
name: 'chromium',
|
||||
use: {
|
||||
...devices['Desktop Chrome'],
|
||||
storageState: path.join(__dirname, 'tests/e2e/.auth/admin.json')
|
||||
}
|
||||
}
|
||||
],
|
||||
webServer: {
|
||||
command: 'npm run dev',
|
||||
url: `http://localhost:${DASHBOARD_PORT}/admin/`,
|
||||
reuseExistingServer: !process.env.CI,
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
timeout: 60_000
|
||||
}
|
||||
});
|
||||
75
dashboard/tests/e2e/README.md
Normal file
75
dashboard/tests/e2e/README.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# Dashboard E2E tests
|
||||
|
||||
Browser-driven tests for the PiCloud dashboard, powered by [Playwright].
|
||||
|
||||
## Prerequisites
|
||||
|
||||
The tests drive a real dashboard against a real backend. Bring up both
|
||||
before running:
|
||||
|
||||
```sh
|
||||
# 1. Postgres
|
||||
docker compose up -d postgres
|
||||
|
||||
# 2. Backend (port 18080 matches dashboard/vite.config.ts dev proxy)
|
||||
PICLOUD_BIND=127.0.0.1:18080 \
|
||||
PICLOUD_ADMIN_USERNAME=admin \
|
||||
PICLOUD_ADMIN_PASSWORD=admin \
|
||||
DATABASE_URL=postgres://picloud:picloud@127.0.0.1:15432/picloud \
|
||||
cargo run -p picloud
|
||||
|
||||
# 3. Browser binaries (one-time, ~200 MB)
|
||||
cd dashboard && npm run test:e2e:install
|
||||
```
|
||||
|
||||
The Vite dev server is started automatically by Playwright's `webServer`
|
||||
config — you do not need to run `npm run dev` yourself.
|
||||
|
||||
## Running
|
||||
|
||||
```sh
|
||||
cd dashboard
|
||||
npm run test:e2e # headless, full suite
|
||||
npm run test:e2e:ui # interactive UI runner
|
||||
npx playwright test smoke # run a single spec
|
||||
npx playwright show-report
|
||||
```
|
||||
|
||||
## Env vars
|
||||
|
||||
| Var | Default | Notes |
|
||||
| ------------------------ | ------------------------ | ----------------------------------------------------------------- |
|
||||
| `E2E_BASE_URL` | `http://localhost:5173` | Origin tests navigate against (dashboard is mounted at `/admin`). |
|
||||
| `E2E_API_BASE` | `http://127.0.0.1:18080` | Backend used by globalSetup health probe + admin login. |
|
||||
| `E2E_DASHBOARD_ORIGIN` | `http://localhost:5173` | Used to seed `localStorage` during globalSetup. |
|
||||
| `E2E_ADMIN_USERNAME` | `admin` | Bootstrap admin to log in as. |
|
||||
| `E2E_ADMIN_PASSWORD` | `admin` | Match `PICLOUD_ADMIN_PASSWORD` above. |
|
||||
| `PICLOUD_DASHBOARD_PORT` | `5173` | Dev server port — picked up by both Vite and Playwright. |
|
||||
|
||||
## How isolation works
|
||||
|
||||
Tests share one backend + one Postgres. To avoid cross-test interference:
|
||||
|
||||
- A shared bootstrap admin session is captured once in
|
||||
`tests/e2e/.auth/admin.json` (gitignored) and reused by every test via
|
||||
`storageState`.
|
||||
- Each test creates resources with a unique slug / username produced by
|
||||
`fixtures/ids.ts` (`e2e-<prefix>-w<worker>-<random>`).
|
||||
- Each test registers cleanup via `fixtures/cleanup.ts` and tears down
|
||||
in `afterEach`. Cleanup is best-effort: a missing resource doesn't
|
||||
fail the suite, so a test can pre-delete and still register the entry.
|
||||
|
||||
## Layout
|
||||
|
||||
```
|
||||
tests/e2e/
|
||||
global-setup.ts # health probe + admin login + storageState seed
|
||||
smoke.spec.ts # A.5 smoke
|
||||
fixtures/
|
||||
auth.ts # UI login/logout helpers (for login-flow specs)
|
||||
api.ts # bearer-token-backed APIRequestContext
|
||||
ids.ts # unique slug/username generators (test-fixture)
|
||||
cleanup.ts # afterEach resource teardown
|
||||
```
|
||||
|
||||
[Playwright]: https://playwright.dev
|
||||
226
dashboard/tests/e2e/apps/apps.spec.ts
Normal file
226
dashboard/tests/e2e/apps/apps.spec.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Phase B2 — Apps Lifecycle. Create, view, edit, delete, plus the
|
||||
// historical-slug takeover flow and adversarial inputs.
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
function failOnDialog(page: Page): void {
|
||||
page.on('dialog', async (dialog) => {
|
||||
await dialog.dismiss();
|
||||
throw new Error(`Unexpected browser dialog fired: ${dialog.type()} — "${dialog.message()}"`);
|
||||
});
|
||||
}
|
||||
|
||||
async function openCreateForm(page: Page): Promise<void> {
|
||||
await page.goto('/admin/apps');
|
||||
await page.getByRole('button', { name: 'New app' }).click();
|
||||
}
|
||||
|
||||
async function createApp(
|
||||
page: Page,
|
||||
opts: { name: string; slug: string; description?: string }
|
||||
): Promise<void> {
|
||||
await openCreateForm(page);
|
||||
await page.getByLabel('Name').fill(opts.name);
|
||||
// Clear the auto-derived slug and type the test-controlled one so
|
||||
// we know exactly which slug we'll register for cleanup.
|
||||
const slugInput = page.getByLabel('Slug');
|
||||
await slugInput.fill('');
|
||||
await slugInput.fill(opts.slug);
|
||||
if (opts.description !== undefined) {
|
||||
await page.getByLabel('Description').fill(opts.description);
|
||||
}
|
||||
await page.getByRole('button', { name: 'Create app' }).click();
|
||||
}
|
||||
|
||||
test.describe('B2 apps lifecycle', () => {
|
||||
test('create app: slug auto-derives from name, app appears in list', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('lifecycle');
|
||||
const displayName = slug.replace(/-/g, ' ');
|
||||
|
||||
await openCreateForm(page);
|
||||
await page.getByLabel('Name').fill(displayName);
|
||||
// Slug auto-derives — the input value is set, no extra typing.
|
||||
const slugInput = page.getByLabel('Slug');
|
||||
await expect(slugInput).toHaveValue(slug);
|
||||
await page.getByRole('button', { name: 'Create app' }).click();
|
||||
cleanup.app(slug);
|
||||
|
||||
await expect(page.getByRole('link', { name: new RegExp(displayName) })).toBeVisible();
|
||||
});
|
||||
|
||||
test('edit name + description in settings persists across reload', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('edit');
|
||||
await createApp(page, { name: slug, slug });
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.getByRole('link', { name: new RegExp(slug) }).click();
|
||||
await expect(page).toHaveURL(new RegExp(`/admin/apps/${slug}$`));
|
||||
await page.getByRole('button', { name: 'Settings' }).click();
|
||||
|
||||
const newName = `${slug} renamed`;
|
||||
const newDesc = 'updated description';
|
||||
await page.getByLabel('Name').fill(newName);
|
||||
await page.getByLabel('Description').fill(newDesc);
|
||||
await page.getByRole('button', { name: 'Save changes' }).click();
|
||||
// Wait for the network round-trip to settle — the busy label
|
||||
// flips back to "Save changes" when done.
|
||||
await expect(page.getByRole('button', { name: 'Save changes' })).toBeEnabled();
|
||||
|
||||
await page.reload();
|
||||
await page.getByRole('button', { name: 'Settings' }).click();
|
||||
await expect(page.getByLabel('Name')).toHaveValue(newName);
|
||||
await expect(page.getByLabel('Description')).toHaveValue(newDesc);
|
||||
});
|
||||
|
||||
test('delete: wrong phrase keeps button disabled, right phrase removes app', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('delete');
|
||||
await createApp(page, { name: slug, slug });
|
||||
cleanup.app(slug); // belt-and-braces; cleanup is best-effort
|
||||
|
||||
await page.getByRole('link', { name: new RegExp(slug) }).click();
|
||||
await page.getByRole('button', { name: 'Settings' }).click();
|
||||
await page.getByRole('button', { name: 'Delete app' }).click();
|
||||
|
||||
const dialog = page.getByRole('dialog');
|
||||
await expect(dialog).toBeVisible();
|
||||
const phraseInput = dialog.getByRole('textbox');
|
||||
const confirmBtn = dialog.getByRole('button', { name: 'Delete app' });
|
||||
await expect(confirmBtn).toBeDisabled();
|
||||
|
||||
await phraseInput.fill('wrong-phrase');
|
||||
await expect(confirmBtn).toBeDisabled();
|
||||
|
||||
await phraseInput.fill(slug);
|
||||
await expect(confirmBtn).toBeEnabled();
|
||||
await confirmBtn.click();
|
||||
|
||||
await expect(page).toHaveURL(/\/admin\/apps$/);
|
||||
await expect(page.getByRole('link', { name: new RegExp(slug) })).toHaveCount(0);
|
||||
});
|
||||
|
||||
test('historical slug warning surfaces; force-takeover succeeds', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const origSlug = uniqueSlug('hist');
|
||||
const renamedSlug = `${origSlug}-r`;
|
||||
|
||||
// Historical-redirect rows are created on RENAME, not on
|
||||
// delete. So: create app, rename it, original slug now lives
|
||||
// in app_slug_history.
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const created = await api.post('/api/v1/admin/apps', {
|
||||
data: { slug: origSlug, name: origSlug }
|
||||
});
|
||||
expect(created.ok()).toBe(true);
|
||||
const renamed = await api.patch(
|
||||
`/api/v1/admin/apps/${encodeURIComponent(origSlug)}`,
|
||||
{ data: { slug: renamedSlug } }
|
||||
);
|
||||
expect(renamed.ok()).toBe(true);
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
cleanup.app(renamedSlug); // the renamed app still exists
|
||||
|
||||
await openCreateForm(page);
|
||||
await page.getByLabel('Name').fill(origSlug);
|
||||
await page.getByLabel('Slug').fill('');
|
||||
await page.getByLabel('Slug').fill(origSlug);
|
||||
await page.getByRole('button', { name: 'Create app' }).click();
|
||||
|
||||
await expect(page.locator('.warning')).toBeVisible();
|
||||
await expect(page.locator('.warning')).toContainText(/previously redirected/i);
|
||||
await page.getByRole('button', { name: /claim slug anyway/i }).click();
|
||||
cleanup.app(origSlug); // the takeover created a new app
|
||||
|
||||
await expect(page.getByRole('link', { name: new RegExp(origSlug) })).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B2 apps adversarial', () => {
|
||||
test('slug with uppercase + spaces is normalized in-place', async ({ page, uniqueSlug }) => {
|
||||
const base = uniqueSlug('norm');
|
||||
await openCreateForm(page);
|
||||
await page.getByLabel('Name').fill(base);
|
||||
const slugInput = page.getByLabel('Slug');
|
||||
await slugInput.fill('');
|
||||
// Simulate the user typing/pasting an invalid slug. The
|
||||
// oninput handler runs slugify() and rewrites the input value.
|
||||
await slugInput.fill(` Hello WORLD ${base}!`);
|
||||
await expect(slugInput).toHaveValue(`hello-world-${base}`);
|
||||
});
|
||||
|
||||
test('xss in name and description renders as text everywhere', async ({ page, uniqueSlug }) => {
|
||||
failOnDialog(page);
|
||||
const slug = uniqueSlug('xss');
|
||||
const payload = '<img src=x onerror=alert(1)><script>window.__xss=true;</script>';
|
||||
|
||||
await createApp(page, { name: payload, slug, description: payload });
|
||||
cleanup.app(slug);
|
||||
|
||||
// List page — the link's accessible name contains the literal
|
||||
// payload text, not the parsed HTML.
|
||||
await expect(page.getByRole('link', { name: new RegExp('img src=x') })).toBeVisible();
|
||||
|
||||
// Detail page — open it; payload renders in the breadcrumb /
|
||||
// header as text only.
|
||||
await page.goto(`/admin/apps/${slug}`);
|
||||
const xssRan = await page.evaluate(
|
||||
() => (window as unknown as { __xss?: boolean }).__xss === true
|
||||
);
|
||||
expect(xssRan).toBe(false);
|
||||
expect(await page.locator('script:has-text("__xss")').count()).toBe(0);
|
||||
});
|
||||
|
||||
test('very long name does not crash the dashboard', async ({ page, uniqueSlug }) => {
|
||||
// The backend currently has no name length cap; the dashboard
|
||||
// just needs to keep rendering when handed an unusually long
|
||||
// value. Guards against layout / locator regressions when a
|
||||
// future test or user creates an oversized app.
|
||||
const slug = uniqueSlug('long');
|
||||
const longName = 'A'.repeat(10_000);
|
||||
|
||||
await openCreateForm(page);
|
||||
await page.getByLabel('Name').fill(longName);
|
||||
await page.getByLabel('Slug').fill('');
|
||||
await page.getByLabel('Slug').fill(slug);
|
||||
await page.getByRole('button', { name: 'Create app' }).click();
|
||||
|
||||
const errorVisible = await page
|
||||
.locator('.create-form .error')
|
||||
.isVisible()
|
||||
.catch(() => false);
|
||||
|
||||
if (errorVisible) {
|
||||
// Server rejected — fine, no cleanup needed.
|
||||
await expect(page.getByRole('link', { name: new RegExp(slug) })).toHaveCount(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Server accepted — confirm the dashboard still renders and is
|
||||
// navigable. Detail page must load too.
|
||||
cleanup.app(slug);
|
||||
await expect(page.getByRole('link', { name: new RegExp(slug) })).toBeVisible();
|
||||
await page.goto(`/admin/apps/${slug}`);
|
||||
await expect(page.getByRole('button', { name: 'Settings' })).toBeVisible();
|
||||
});
|
||||
});
|
||||
118
dashboard/tests/e2e/auth/auth.spec.ts
Normal file
118
dashboard/tests/e2e/auth/auth.spec.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { expect, test, type Page } from '@playwright/test';
|
||||
import { loginAsAdmin, logout } from '../fixtures/auth';
|
||||
|
||||
// Phase B1 — Auth & Navigation. Every interaction with the login form
|
||||
// and the layout-level redirects, plus the obvious adversarial inputs.
|
||||
|
||||
const VALID_USERNAME = process.env.E2E_ADMIN_USERNAME ?? 'admin';
|
||||
const VALID_PASSWORD = process.env.E2E_ADMIN_PASSWORD ?? 'admin';
|
||||
|
||||
function failOnDialog(page: Page): void {
|
||||
page.on('dialog', async (dialog) => {
|
||||
await dialog.dismiss();
|
||||
throw new Error(`Unexpected browser dialog fired: ${dialog.type()} — "${dialog.message()}"`);
|
||||
});
|
||||
}
|
||||
|
||||
test.describe('B1 auth — unauthenticated', () => {
|
||||
test.use({ storageState: { cookies: [], origins: [] } });
|
||||
|
||||
test('valid credentials land on the apps list', async ({ page }) => {
|
||||
await loginAsAdmin(page);
|
||||
await expect(page.getByRole('heading', { name: 'Apps', level: 1 })).toBeVisible();
|
||||
});
|
||||
|
||||
test('wrong password shows an inline error and stays on /login', async ({ page }) => {
|
||||
await page.goto('/admin/login');
|
||||
await page.getByLabel('Username').fill(VALID_USERNAME);
|
||||
await page.getByLabel('Password').fill('definitely-not-the-password');
|
||||
await page.getByRole('button', { name: /sign in/i }).click();
|
||||
|
||||
const error = page.locator('.error');
|
||||
await expect(error).toBeVisible();
|
||||
await expect(error).not.toHaveText('');
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
// localStorage must remain empty — a failed login should not
|
||||
// leak a session token.
|
||||
const token = await page.evaluate(() => localStorage.getItem('picloud.admin.token'));
|
||||
expect(token).toBeNull();
|
||||
});
|
||||
|
||||
test('empty submit is blocked by the browser and does not navigate', async ({ page }) => {
|
||||
await page.goto('/admin/login');
|
||||
await page.getByRole('button', { name: /sign in/i }).click();
|
||||
// HTML5 validation prevents submission; URL is unchanged and the
|
||||
// username input is reported invalid.
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
const usernameInvalid = await page
|
||||
.getByLabel('Username')
|
||||
.evaluate((el: HTMLInputElement) => !el.validity.valid);
|
||||
expect(usernameInvalid).toBe(true);
|
||||
await expect(page.locator('.error')).toBeHidden();
|
||||
});
|
||||
|
||||
test('visiting an authed route redirects to /login', async ({ page }) => {
|
||||
await page.goto('/admin/apps');
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
await expect(page.getByLabel('Username')).toBeVisible();
|
||||
});
|
||||
|
||||
test('password field is type=password (no plaintext echo)', async ({ page }) => {
|
||||
await page.goto('/admin/login');
|
||||
await expect(page.getByLabel('Password')).toHaveAttribute('type', 'password');
|
||||
});
|
||||
|
||||
test('xss payload in username is escaped and does not execute', async ({ page }) => {
|
||||
failOnDialog(page);
|
||||
const payload = '<script>window.__xss = true;</script><img src=x onerror=alert(1)>';
|
||||
|
||||
await page.goto('/admin/login');
|
||||
await page.getByLabel('Username').fill(payload);
|
||||
await page.getByLabel('Password').fill('whatever');
|
||||
await page.getByRole('button', { name: /sign in/i }).click();
|
||||
|
||||
// Whatever the API does with that input, the page must remain
|
||||
// safe: no script tag injected into the DOM, no global side
|
||||
// effect, and a visible error (since the credentials don't
|
||||
// match any user).
|
||||
await expect(page.locator('.error')).toBeVisible();
|
||||
const xssRan = await page.evaluate(
|
||||
() => (window as unknown as { __xss?: boolean }).__xss === true
|
||||
);
|
||||
expect(xssRan).toBe(false);
|
||||
const injectedScript = await page.locator('script:has-text("__xss")').count();
|
||||
expect(injectedScript).toBe(0);
|
||||
// The form must still be functional after the rejected attempt.
|
||||
await page.getByLabel('Username').fill('');
|
||||
await page.getByLabel('Username').fill(VALID_USERNAME);
|
||||
await page.getByLabel('Password').fill('');
|
||||
await page.getByLabel('Password').fill(VALID_PASSWORD);
|
||||
await page.getByRole('button', { name: /sign in/i }).click();
|
||||
await expect(page).toHaveURL(/\/admin\/apps$/);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B1 auth — authenticated', () => {
|
||||
test('visiting /login while signed in bounces to /apps', async ({ page }) => {
|
||||
await page.goto('/admin/login');
|
||||
await expect(page).toHaveURL(/\/admin\/apps$/);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B1 auth — logout', () => {
|
||||
// Logout must NOT use the shared storageState token, or it would
|
||||
// invalidate the session every other test relies on. Each run
|
||||
// here logs in fresh so its session is disposable.
|
||||
test.use({ storageState: { cookies: [], origins: [] } });
|
||||
|
||||
test('logout clears the session and lands on /login', async ({ page }) => {
|
||||
await loginAsAdmin(page);
|
||||
await expect(page.getByRole('heading', { name: 'Apps', level: 1 })).toBeVisible();
|
||||
await logout(page);
|
||||
const token = await page.evaluate(() => localStorage.getItem('picloud.admin.token'));
|
||||
expect(token).toBeNull();
|
||||
// And the authed area is now gated again.
|
||||
await page.goto('/admin/apps');
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
});
|
||||
});
|
||||
47
dashboard/tests/e2e/fixtures/api.ts
Normal file
47
dashboard/tests/e2e/fixtures/api.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { request, type APIRequestContext } from '@playwright/test';
|
||||
import { promises as fs } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const API_BASE = process.env.E2E_API_BASE ?? 'http://127.0.0.1:18080';
|
||||
const STATE_PATH = path.join(__dirname, '..', '.auth', 'admin.json');
|
||||
|
||||
interface StoredState {
|
||||
origins: Array<{
|
||||
origin: string;
|
||||
localStorage: Array<{ name: string; value: string }>;
|
||||
}>;
|
||||
}
|
||||
|
||||
let cachedToken: string | null = null;
|
||||
|
||||
async function readAdminToken(): Promise<string> {
|
||||
if (cachedToken) return cachedToken;
|
||||
const raw = await fs.readFile(STATE_PATH, 'utf8');
|
||||
const state = JSON.parse(raw) as StoredState;
|
||||
for (const origin of state.origins) {
|
||||
const entry = origin.localStorage.find((e) => e.name === 'picloud.admin.token');
|
||||
if (entry) {
|
||||
cachedToken = entry.value;
|
||||
return entry.value;
|
||||
}
|
||||
}
|
||||
throw new Error(`No picloud.admin.token in ${STATE_PATH} — did globalSetup run?`);
|
||||
}
|
||||
|
||||
// Thin wrapper around Playwright's request context that injects the
|
||||
// admin bearer token from the shared storageState. Use this for
|
||||
// setup/teardown shortcuts when the *test itself* is about something
|
||||
// else (e.g., a script-editor test that just needs an app to exist).
|
||||
export async function adminApi(): Promise<APIRequestContext> {
|
||||
const token = await readAdminToken();
|
||||
return request.newContext({
|
||||
baseURL: API_BASE,
|
||||
extraHTTPHeaders: {
|
||||
authorization: `Bearer ${token}`,
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
});
|
||||
}
|
||||
21
dashboard/tests/e2e/fixtures/auth.ts
Normal file
21
dashboard/tests/e2e/fixtures/auth.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { Page } from '@playwright/test';
|
||||
import { expect } from '@playwright/test';
|
||||
|
||||
const ADMIN_USERNAME = process.env.E2E_ADMIN_USERNAME ?? 'admin';
|
||||
const ADMIN_PASSWORD = process.env.E2E_ADMIN_PASSWORD ?? 'admin';
|
||||
|
||||
// Drive the login form like a real user. globalSetup already saves a
|
||||
// storageState for the shared admin, so most tests don't need this —
|
||||
// it's reserved for specs that explicitly cover the login UI.
|
||||
export async function loginAsAdmin(page: Page): Promise<void> {
|
||||
await page.goto('/admin/login');
|
||||
await page.getByLabel('Username').fill(ADMIN_USERNAME);
|
||||
await page.getByLabel('Password').fill(ADMIN_PASSWORD);
|
||||
await page.getByRole('button', { name: /sign in/i }).click();
|
||||
await expect(page).toHaveURL(/\/admin\/apps$/);
|
||||
}
|
||||
|
||||
export async function logout(page: Page): Promise<void> {
|
||||
await page.getByRole('button', { name: /logout/i }).click();
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
}
|
||||
48
dashboard/tests/e2e/fixtures/cleanup.ts
Normal file
48
dashboard/tests/e2e/fixtures/cleanup.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { APIRequestContext } from '@playwright/test';
|
||||
import { adminApi } from './api';
|
||||
|
||||
// Resources to delete after a test, in LIFO order. Tests register
|
||||
// their creations and the registry tears everything down in
|
||||
// `cleanupRegistered` — typically called from `test.afterEach`.
|
||||
|
||||
type Cleanup = (api: APIRequestContext) => Promise<void>;
|
||||
|
||||
export class CleanupRegistry {
|
||||
private items: Cleanup[] = [];
|
||||
|
||||
app(slugOrId: string): void {
|
||||
this.items.push(async (api) => {
|
||||
await api.delete(`/api/v1/admin/apps/${encodeURIComponent(slugOrId)}?force=true`);
|
||||
});
|
||||
}
|
||||
|
||||
adminUser(userId: string): void {
|
||||
this.items.push(async (api) => {
|
||||
await api.delete(`/api/v1/admin/admins/${userId}`);
|
||||
});
|
||||
}
|
||||
|
||||
apiKey(keyId: string): void {
|
||||
this.items.push(async (api) => {
|
||||
await api.delete(`/api/v1/admin/api-keys/${keyId}`);
|
||||
});
|
||||
}
|
||||
|
||||
async run(): Promise<void> {
|
||||
if (this.items.length === 0) return;
|
||||
const api = await adminApi();
|
||||
try {
|
||||
for (const item of this.items.reverse()) {
|
||||
try {
|
||||
await item(api);
|
||||
} catch {
|
||||
// Best-effort cleanup — a missing resource (already
|
||||
// deleted by the test) shouldn't fail the suite.
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await api.dispose();
|
||||
this.items = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
42
dashboard/tests/e2e/fixtures/ids.ts
Normal file
42
dashboard/tests/e2e/fixtures/ids.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/* eslint-disable no-empty-pattern -- Playwright fixtures require an
|
||||
object-pattern first arg; these fixtures don't depend on any other
|
||||
fixture so the pattern is intentionally empty. */
|
||||
import { test as base } from '@playwright/test';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
|
||||
// Tests share a single backend/Postgres. To avoid collisions we tag
|
||||
// every resource the test creates with a short random suffix plus the
|
||||
// Playwright worker index. This way two workers running the same spec
|
||||
// in parallel never fight over the same slug or username.
|
||||
|
||||
export function shortId(): string {
|
||||
return randomBytes(3).toString('hex');
|
||||
}
|
||||
|
||||
export function uniqueSlug(prefix: string, workerIndex: number): string {
|
||||
const cleaned = prefix
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-|-$/g, '');
|
||||
return `e2e-${cleaned}-w${workerIndex}-${shortId()}`;
|
||||
}
|
||||
|
||||
export function uniqueUsername(prefix: string, workerIndex: number): string {
|
||||
// Username regex is [a-z0-9._-]{2,32}. Mirror the slug format.
|
||||
const cleaned = prefix.toLowerCase().replace(/[^a-z0-9]+/g, '');
|
||||
return `e2e${cleaned}w${workerIndex}${shortId()}`.slice(0, 32);
|
||||
}
|
||||
|
||||
export const test = base.extend<{
|
||||
uniqueSlug: (prefix: string) => string;
|
||||
uniqueUsername: (prefix: string) => string;
|
||||
}>({
|
||||
uniqueSlug: async ({}, use, testInfo) => {
|
||||
await use((prefix) => uniqueSlug(prefix, testInfo.workerIndex));
|
||||
},
|
||||
uniqueUsername: async ({}, use, testInfo) => {
|
||||
await use((prefix) => uniqueUsername(prefix, testInfo.workerIndex));
|
||||
}
|
||||
});
|
||||
|
||||
export { expect } from '@playwright/test';
|
||||
94
dashboard/tests/e2e/global-setup.ts
Normal file
94
dashboard/tests/e2e/global-setup.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { chromium, request } from '@playwright/test';
|
||||
import { promises as fs } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const API_BASE = process.env.E2E_API_BASE ?? 'http://127.0.0.1:18080';
|
||||
const DASHBOARD_PORT = Number(process.env.PICLOUD_DASHBOARD_PORT ?? 5173);
|
||||
const DASHBOARD_ORIGIN = process.env.E2E_DASHBOARD_ORIGIN ?? `http://localhost:${DASHBOARD_PORT}`;
|
||||
const ADMIN_USERNAME = process.env.E2E_ADMIN_USERNAME ?? 'admin';
|
||||
const ADMIN_PASSWORD = process.env.E2E_ADMIN_PASSWORD ?? 'admin';
|
||||
|
||||
const AUTH_DIR = path.join(__dirname, '.auth');
|
||||
const ADMIN_STATE_PATH = path.join(AUTH_DIR, 'admin.json');
|
||||
|
||||
export default async function globalSetup(): Promise<void> {
|
||||
await assertBackendUp();
|
||||
await fs.mkdir(AUTH_DIR, { recursive: true });
|
||||
const token = await loginAsAdmin();
|
||||
await persistAdminStorageState(token);
|
||||
}
|
||||
|
||||
async function assertBackendUp(): Promise<void> {
|
||||
const probe = await request.newContext();
|
||||
try {
|
||||
const res = await probe.get(`${API_BASE}/healthz`, { timeout: 5_000 });
|
||||
if (!res.ok()) {
|
||||
throw new Error(
|
||||
`backend /healthz returned ${res.status()} — is \`cargo run -p picloud\` listening on ${API_BASE}?`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw new Error(
|
||||
`Could not reach backend at ${API_BASE}/healthz. ` +
|
||||
`Bring it up before running E2E tests:\n\n` +
|
||||
` docker compose up -d postgres\n` +
|
||||
` PICLOUD_BIND=127.0.0.1:18080 \\\n` +
|
||||
` PICLOUD_ADMIN_USERNAME=${ADMIN_USERNAME} \\\n` +
|
||||
` PICLOUD_ADMIN_PASSWORD=${ADMIN_PASSWORD} \\\n` +
|
||||
` DATABASE_URL=postgres://picloud:picloud@127.0.0.1:15432/picloud \\\n` +
|
||||
` cargo run -p picloud\n\n` +
|
||||
`Underlying error: ${(err as Error).message}`
|
||||
);
|
||||
} finally {
|
||||
await probe.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function loginAsAdmin(): Promise<string> {
|
||||
const ctx = await request.newContext();
|
||||
try {
|
||||
const res = await ctx.post(`${API_BASE}/api/v1/admin/auth/login`, {
|
||||
data: { username: ADMIN_USERNAME, password: ADMIN_PASSWORD },
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
if (!res.ok()) {
|
||||
const body = await res.text();
|
||||
throw new Error(
|
||||
`Admin login failed (${res.status()}): ${body}. ` +
|
||||
`Verify PICLOUD_ADMIN_USERNAME / PICLOUD_ADMIN_PASSWORD match the seeded bootstrap admin.`
|
||||
);
|
||||
}
|
||||
const payload = (await res.json()) as { token?: string };
|
||||
if (!payload.token) {
|
||||
throw new Error('Admin login response missing token field');
|
||||
}
|
||||
return payload.token;
|
||||
} finally {
|
||||
await ctx.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// The dashboard reads its session from localStorage under the key
|
||||
// `picloud.admin.token` (see src/lib/auth.ts). We can't write to
|
||||
// localStorage without a browser context, so launch a throwaway one,
|
||||
// seed the value, then save storageState for every test to reuse.
|
||||
async function persistAdminStorageState(token: string): Promise<void> {
|
||||
const browser = await chromium.launch();
|
||||
try {
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
await page.goto(`${DASHBOARD_ORIGIN}/admin/login`);
|
||||
await page.evaluate(
|
||||
([key, value]) => {
|
||||
localStorage.setItem(key, value);
|
||||
},
|
||||
['picloud.admin.token', token]
|
||||
);
|
||||
await context.storageState({ path: ADMIN_STATE_PATH });
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
155
dashboard/tests/e2e/integration/integration.spec.ts
Normal file
155
dashboard/tests/e2e/integration/integration.spec.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { expect, request, type Page } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Full-stack integration scenarios. Unlike the per-page B1–B8 specs,
|
||||
// these drive a complete user journey across multiple pages and then
|
||||
// verify the data plane / API surface behaves the way the dashboard
|
||||
// promised it would.
|
||||
|
||||
const API_BASE = process.env.E2E_API_BASE ?? 'http://127.0.0.1:18080';
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
async function fillCodeMirror(page: Page, locator: string, text: string): Promise<void> {
|
||||
const cm = page.locator(locator).first();
|
||||
await cm.click();
|
||||
await page.keyboard.press('ControlOrMeta+A');
|
||||
await page.keyboard.press('Delete');
|
||||
await page.keyboard.type(text);
|
||||
}
|
||||
|
||||
test('end-to-end: app + domain + script + route via dashboard → invoke via public URL', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('public');
|
||||
const domain = `${slug}.local`;
|
||||
const routePath = `/${slug}/hello`;
|
||||
const scriptName = `${slug}-hello`;
|
||||
const scriptSource = `return #{ statusCode: 200, body: #{ source: "public", slug: "${slug}" } };`;
|
||||
|
||||
// 1. Create the app from the apps list.
|
||||
await page.goto('/admin/apps');
|
||||
await page.getByRole('button', { name: 'New app' }).click();
|
||||
await page.getByLabel('Name').fill(slug);
|
||||
const slugInput = page.getByLabel('Slug');
|
||||
await slugInput.fill('');
|
||||
await slugInput.fill(slug);
|
||||
await page.getByRole('button', { name: 'Create app' }).click();
|
||||
cleanup.app(slug);
|
||||
await expect(page.getByRole('link', { name: new RegExp(slug) })).toBeVisible();
|
||||
|
||||
// 2. Open the app and claim the domain on the Domains tab.
|
||||
await page.getByRole('link', { name: new RegExp(slug) }).click();
|
||||
await expect(page).toHaveURL(new RegExp(`/admin/apps/${slug}$`));
|
||||
await page.getByRole('button', { name: /^Domains \(\d+\)$/ }).click();
|
||||
const domainForm = page.locator('form.create-form.inline');
|
||||
await domainForm.getByPlaceholder(/app\.example\.com/).fill(domain);
|
||||
await domainForm.getByRole('button', { name: /^Add domain$/ }).click();
|
||||
await expect(page.locator('.domain-row')).toContainText(domain);
|
||||
|
||||
// 3. Create the script on the Scripts tab.
|
||||
await page.getByRole('button', { name: /^Scripts \(\d+\)$/ }).click();
|
||||
await page.getByRole('button', { name: /^New script$/ }).click();
|
||||
await page.getByLabel('Name').fill(scriptName);
|
||||
await fillCodeMirror(page, '.cm-content', scriptSource);
|
||||
await page.getByRole('button', { name: /^Create script$/ }).click();
|
||||
|
||||
// 4. Open the script and bind a route on the Routing tab.
|
||||
await page.getByRole('link', { name: new RegExp(scriptName) }).click();
|
||||
await page.getByRole('button', { name: 'Routing' }).click();
|
||||
await page.getByRole('button', { name: '+ Add route' }).click();
|
||||
const routeForm = page.locator('form.route-form');
|
||||
await routeForm.getByLabel('Path', { exact: true }).fill(routePath);
|
||||
await routeForm.getByLabel('Method').selectOption('GET');
|
||||
await routeForm.getByLabel(/^Host/).fill(domain);
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
await expect(page.locator('.route-list')).toContainText(routePath);
|
||||
|
||||
// 5. Invoke via the public URL, with the Host header pointing at
|
||||
// the claimed domain. The dev backend listens on 127.0.0.1; the
|
||||
// orchestrator resolves the app from Host, then the route.
|
||||
const publicCtx = await request.newContext({ baseURL: API_BASE });
|
||||
try {
|
||||
const res = await publicCtx.get(routePath, { headers: { host: domain } });
|
||||
expect(res.status()).toBe(200);
|
||||
const body = (await res.json()) as { source: string; slug: string };
|
||||
expect(body.source).toBe('public');
|
||||
expect(body.slug).toBe(slug);
|
||||
} finally {
|
||||
await publicCtx.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
test('api key minted via dashboard works as a CLI bearer, then revoke disables it', async ({
|
||||
page
|
||||
}) => {
|
||||
const name = `e2e-cli-${Date.now()}`;
|
||||
|
||||
// 1. Mint the key from /profile and capture the revealed token.
|
||||
await page.goto('/admin/profile');
|
||||
await page.getByRole('button', { name: /\+ Mint API key/ }).click();
|
||||
const mintForm = page.locator('form.mint');
|
||||
await mintForm.getByPlaceholder('e.g. ci-deploy').fill(name);
|
||||
// script:read is enough to read the scripts list — that's our
|
||||
// "CLI verb" below.
|
||||
await page.locator('label.scope-chip', { hasText: 'script:read' }).click();
|
||||
await page.getByRole('button', { name: /^Mint key$/ }).click();
|
||||
|
||||
const reveal = page.locator('.reveal');
|
||||
await expect(reveal).toBeVisible();
|
||||
const rawToken = (await reveal.locator('code.token').textContent())?.trim();
|
||||
expect(rawToken).toBeTruthy();
|
||||
await reveal.getByRole('checkbox', { name: /saved this token/i }).check();
|
||||
await reveal.getByRole('button', { name: /^Done$/ }).click();
|
||||
|
||||
// 2. Act like a CLI: call the API directly with Bearer <token>.
|
||||
const cli = await request.newContext({
|
||||
baseURL: API_BASE,
|
||||
extraHTTPHeaders: { authorization: `Bearer ${rawToken}` }
|
||||
});
|
||||
try {
|
||||
const ok = await cli.get('/api/v1/admin/scripts');
|
||||
expect(ok.status()).toBe(200);
|
||||
const body = (await ok.json()) as unknown;
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
|
||||
// Sanity: a route the scope doesn't cover must reject.
|
||||
// `script:read` cannot list instance admins (that's
|
||||
// instance:admin territory).
|
||||
const denied = await cli.get('/api/v1/admin/admins');
|
||||
expect(denied.status()).toBe(403);
|
||||
|
||||
// 3. Revoke via the dashboard.
|
||||
await page.reload();
|
||||
const revokeBtn = page.getByRole('button', { name: `Revoke ${name}` });
|
||||
await expect(revokeBtn).toBeVisible();
|
||||
await revokeBtn.click();
|
||||
await page.getByRole('dialog').getByRole('button', { name: /^Revoke$/ }).click();
|
||||
await expect(revokeBtn).toHaveCount(0);
|
||||
|
||||
// 4. Same CLI call must now fail auth.
|
||||
const afterRevoke = await cli.get('/api/v1/admin/scripts');
|
||||
expect(afterRevoke.status()).toBe(401);
|
||||
} finally {
|
||||
await cli.dispose();
|
||||
}
|
||||
|
||||
// Belt-and-braces cleanup: if the UI revoke missed, drop via API.
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const list = await api.get('/api/v1/admin/api-keys');
|
||||
if (list.ok()) {
|
||||
const all = (await list.json()) as Array<{ id: string; name: string }>;
|
||||
const k = all.find((x) => x.name === name);
|
||||
if (k) cleanup.apiKey(k.id);
|
||||
}
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
});
|
||||
199
dashboard/tests/e2e/members/members.spec.ts
Normal file
199
dashboard/tests/e2e/members/members.spec.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
import { expect, type Browser, type Page } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Phase B5 — App Members. Setup creates one or two extra admin
|
||||
// users via the API; tests drive the Members tab through the
|
||||
// dashboard like a real app admin would.
|
||||
|
||||
const API_BASE = process.env.E2E_API_BASE ?? 'http://127.0.0.1:18080';
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
async function createApp(slug: string): Promise<string> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/apps', { data: { slug, name: slug } });
|
||||
expect(res.ok()).toBe(true);
|
||||
return ((await res.json()) as { id: string }).id;
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function createMemberUser(username: string): Promise<string> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/admins', {
|
||||
data: { username, password: 'e2e-member-pw', instance_role: 'member' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
return ((await res.json()) as { id: string }).id;
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function loginAsUserToken(username: string, password: string): Promise<string> {
|
||||
const probe = await (await import('@playwright/test')).request.newContext({
|
||||
baseURL: API_BASE
|
||||
});
|
||||
try {
|
||||
const res = await probe.post('/api/v1/admin/auth/login', {
|
||||
data: { username, password },
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
return ((await res.json()) as { token: string }).token;
|
||||
} finally {
|
||||
await probe.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function pageWithUserToken(browser: Browser, token: string): Promise<Page> {
|
||||
const ctx = await browser.newContext({ storageState: undefined });
|
||||
const page = await ctx.newPage();
|
||||
// Seed localStorage on the right origin, then navigate normally.
|
||||
await page.goto('/admin/login');
|
||||
await page.evaluate(
|
||||
([key, value]) => {
|
||||
localStorage.setItem(key, value);
|
||||
},
|
||||
['picloud.admin.token', token]
|
||||
);
|
||||
return page;
|
||||
}
|
||||
|
||||
test.describe('B5 app members', () => {
|
||||
test('invite a member-role user, then remove them', async ({ page, uniqueSlug, uniqueUsername }) => {
|
||||
const slug = uniqueSlug('mem');
|
||||
const username = uniqueUsername('inv');
|
||||
await createApp(slug);
|
||||
const userId = await createMemberUser(username);
|
||||
cleanup.app(slug);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
await page.goto(`/admin/apps/${slug}`);
|
||||
await page.getByRole('button', { name: /^Members \(\d+\)$/ }).click();
|
||||
|
||||
// Invite. Both selects sit in `form.create-form`; locate them
|
||||
// by position to avoid getByLabel ambiguity (the Svelte
|
||||
// markup nests both labels in a flex row, which makes their
|
||||
// accessible names overlap).
|
||||
const form = page.locator('form.create-form');
|
||||
await form.locator('select').nth(0).selectOption({ label: username });
|
||||
await form.locator('select').nth(1).selectOption('editor');
|
||||
await page.getByRole('button', { name: /^Add member$/ }).click();
|
||||
await expect(page.locator('.member-row')).toContainText(username);
|
||||
|
||||
// Remove via action menu + confirm modal.
|
||||
await page.getByRole('button', { name: new RegExp(`Member actions for ${username}`) }).click();
|
||||
await page.getByRole('menuitem', { name: /^Remove from app$/ }).click();
|
||||
const dialog = page.getByRole('dialog');
|
||||
await expect(dialog).toBeVisible();
|
||||
await dialog.getByRole('button', { name: /^Remove member$/ }).click();
|
||||
await expect(page.locator('.member-row')).toHaveCount(0);
|
||||
});
|
||||
|
||||
test('role change via action menu updates the role chip', async ({
|
||||
page,
|
||||
uniqueSlug,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const slug = uniqueSlug('mem');
|
||||
const username = uniqueUsername('role');
|
||||
await createApp(slug);
|
||||
const userId = await createMemberUser(username);
|
||||
cleanup.app(slug);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
// Seed the membership via API to skip the invite UI.
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post(`/api/v1/admin/apps/${slug}/members`, {
|
||||
data: { user_id: userId, role: 'viewer' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
|
||||
await page.goto(`/admin/apps/${slug}`);
|
||||
await page.getByRole('button', { name: /^Members \(\d+\)$/ }).click();
|
||||
await page.getByRole('button', { name: new RegExp(`Member actions for ${username}`) }).click();
|
||||
await page.getByRole('menuitem', { name: /^Make editor$/ }).click();
|
||||
|
||||
const row = page.locator('.member-row', { hasText: username });
|
||||
await expect(row).toContainText(/editor/i);
|
||||
});
|
||||
|
||||
test('non-app-admin viewers do not see the Members tab', async ({
|
||||
browser,
|
||||
uniqueSlug,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const slug = uniqueSlug('mem');
|
||||
const username = uniqueUsername('viewer');
|
||||
const password = 'e2e-member-pw';
|
||||
await createApp(slug);
|
||||
const userId = await createMemberUser(username);
|
||||
cleanup.app(slug);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
// Grant viewer membership (not app_admin) so the user can see
|
||||
// the app at all.
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post(`/api/v1/admin/apps/${slug}/members`, {
|
||||
data: { user_id: userId, role: 'viewer' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
|
||||
const token = await loginAsUserToken(username, password);
|
||||
const viewerPage = await pageWithUserToken(browser, token);
|
||||
try {
|
||||
await viewerPage.goto(`/admin/apps/${slug}`);
|
||||
// Scripts tab loads — that's what a viewer sees.
|
||||
await expect(
|
||||
viewerPage.getByRole('button', { name: /^Scripts \(\d+\)$/ })
|
||||
).toBeVisible();
|
||||
// Members tab button is absent for non-app-admins.
|
||||
await expect(
|
||||
viewerPage.getByRole('button', { name: /^Members \(\d+\)$/ })
|
||||
).toHaveCount(0);
|
||||
} finally {
|
||||
await viewerPage.context().close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B5 app members adversarial', () => {
|
||||
test('role dropdown exposes only the documented values', async ({
|
||||
page,
|
||||
uniqueSlug,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const slug = uniqueSlug('mem');
|
||||
const username = uniqueUsername('rolelist');
|
||||
await createApp(slug);
|
||||
const userId = await createMemberUser(username);
|
||||
cleanup.app(slug);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
await page.goto(`/admin/apps/${slug}`);
|
||||
await page.getByRole('button', { name: /^Members \(\d+\)$/ }).click();
|
||||
const form = page.locator('form.create-form');
|
||||
const roleSelect = form.locator('select').nth(1);
|
||||
const optionValues = await roleSelect.evaluate((el: HTMLSelectElement) =>
|
||||
Array.from(el.options).map((o) => o.value)
|
||||
);
|
||||
expect(optionValues.sort()).toEqual(['app_admin', 'editor', 'viewer']);
|
||||
});
|
||||
});
|
||||
150
dashboard/tests/e2e/profile/profile.spec.ts
Normal file
150
dashboard/tests/e2e/profile/profile.spec.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Phase B7 — Profile + API Keys (/admin/profile). Covers the
|
||||
// mint/reveal/revoke flow, the app-binding mutual-exclusion guard,
|
||||
// and adversarial inputs.
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
async function createApp(slug: string): Promise<string> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/apps', { data: { slug, name: slug } });
|
||||
expect(res.ok()).toBe(true);
|
||||
return ((await res.json()) as { id: string }).id;
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function openMintForm(page: Page): Promise<void> {
|
||||
await page.goto('/admin/profile');
|
||||
await page.getByRole('button', { name: /\+ Mint API key/ }).click();
|
||||
}
|
||||
|
||||
async function registerKeyCleanupByName(name: string): Promise<void> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.get('/api/v1/admin/api-keys');
|
||||
const all = (await res.json()) as Array<{ id: string; name: string }>;
|
||||
const k = all.find((x) => x.name === name);
|
||||
if (k) cleanup.apiKey(k.id);
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
test.describe('B7 profile + API keys', () => {
|
||||
test('mint instance-wide key: reveal → ack → key appears in list', async ({ page }) => {
|
||||
const name = `e2e-mint-${Date.now()}`;
|
||||
await openMintForm(page);
|
||||
await page.locator('form.mint').getByPlaceholder('e.g. ci-deploy').fill(name);
|
||||
// Pick a non-instance scope so we don't need to worry about
|
||||
// mutual exclusion here. The scope-chip is a <label> wrapping
|
||||
// the checkbox — clicking the label toggles it.
|
||||
await page.locator('label.scope-chip', { hasText: 'script:read' }).click();
|
||||
await page.getByRole('button', { name: /^Mint key$/ }).click();
|
||||
|
||||
const reveal = page.locator('.reveal');
|
||||
await expect(reveal).toBeVisible();
|
||||
await expect(reveal.locator('code.token')).toContainText(/\S{16,}/);
|
||||
await expect(reveal.getByRole('button', { name: /^Done$/ })).toBeDisabled();
|
||||
await reveal.getByRole('checkbox', { name: /saved this token/i }).check();
|
||||
await reveal.getByRole('button', { name: /^Done$/ }).click();
|
||||
|
||||
await registerKeyCleanupByName(name);
|
||||
await expect(page.getByText(name)).toBeVisible();
|
||||
});
|
||||
|
||||
test('binding to an app disables instance scopes', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('keyapp');
|
||||
const appId = await createApp(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await openMintForm(page);
|
||||
|
||||
// Default binding is Instance-wide — instance scopes are
|
||||
// enabled.
|
||||
const instChip = page.locator('label.scope-chip', { hasText: 'instance:admin' });
|
||||
await expect(instChip).not.toHaveClass(/disabled/);
|
||||
|
||||
// Switch binding to the app. The chip becomes disabled.
|
||||
await page.getByLabel(/Binding/i).selectOption(appId);
|
||||
await expect(instChip).toHaveClass(/disabled/);
|
||||
});
|
||||
|
||||
test('revoke key removes it from the list', async ({ page }) => {
|
||||
const name = `e2e-revoke-${Date.now()}`;
|
||||
// Seed a key via API so the test focuses on the revoke UI.
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/api-keys', {
|
||||
data: { name, scopes: ['script:read'] }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
const body = (await res.json()) as { id: string };
|
||||
cleanup.apiKey(body.id);
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
|
||||
await page.goto('/admin/profile');
|
||||
const revokeBtn = page.getByRole('button', { name: `Revoke ${name}` });
|
||||
await expect(revokeBtn).toBeVisible();
|
||||
await revokeBtn.click();
|
||||
|
||||
const dialog = page.getByRole('dialog');
|
||||
await dialog.getByRole('button', { name: /^Revoke$/ }).click();
|
||||
// Assert the row's revoke button is gone (the flash banner
|
||||
// also mentions the name, so a plain getByText would still
|
||||
// match — anchor on the row-scoped button instead).
|
||||
await expect(revokeBtn).toHaveCount(0);
|
||||
});
|
||||
|
||||
test('denied=users banner shows when arriving from the users redirect', async ({ page }) => {
|
||||
await page.goto('/admin/profile?denied=users');
|
||||
await expect(page.getByText(/don.?t have access to the Users page/i)).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B7 profile adversarial', () => {
|
||||
test('empty name keeps the mint button disabled', async ({ page }) => {
|
||||
await openMintForm(page);
|
||||
// Trying to click would HTML5-validate; instead verify the
|
||||
// button is disabled while name is empty.
|
||||
await page.locator('label.scope-chip', { hasText: 'script:read' }).click();
|
||||
await expect(page.getByRole('button', { name: /^Mint key$/ })).toBeDisabled();
|
||||
});
|
||||
|
||||
test('copy-token button copies the full token, not a truncated form', async ({
|
||||
page,
|
||||
context
|
||||
}) => {
|
||||
// Permission must be granted explicitly; chromium will throw
|
||||
// otherwise when calling navigator.clipboard.readText().
|
||||
await context.grantPermissions(['clipboard-read', 'clipboard-write']);
|
||||
|
||||
const name = `e2e-copy-${Date.now()}`;
|
||||
await openMintForm(page);
|
||||
await page.locator('form.mint').getByPlaceholder('e.g. ci-deploy').fill(name);
|
||||
await page.locator('label.scope-chip', { hasText: 'script:read' }).click();
|
||||
await page.getByRole('button', { name: /^Mint key$/ }).click();
|
||||
|
||||
const reveal = page.locator('.reveal');
|
||||
const tokenInDom = await reveal.locator('code.token').textContent();
|
||||
expect(tokenInDom).toBeTruthy();
|
||||
await reveal.getByRole('button', { name: /^Copy$/ }).click();
|
||||
const copied = await page.evaluate(() => navigator.clipboard.readText());
|
||||
expect(copied).toBe(tokenInDom);
|
||||
|
||||
await reveal.getByRole('checkbox', { name: /saved this token/i }).check();
|
||||
await reveal.getByRole('button', { name: /^Done$/ }).click();
|
||||
await registerKeyCleanupByName(name);
|
||||
});
|
||||
});
|
||||
189
dashboard/tests/e2e/routing/routing.spec.ts
Normal file
189
dashboard/tests/e2e/routing/routing.spec.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Phase B4 — Routing tab in the script editor. Add / remove / match
|
||||
// preview + validation paths (host check, path-kind mismatch, reserved
|
||||
// prefix, duplicate conflict, adversarial paths).
|
||||
|
||||
const HELLO_RHAI = `return #{ statusCode: 200, body: #{ ok: true } };`;
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
async function makeAppWithScript(slug: string): Promise<{ appId: string; scriptId: string }> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const appRes = await api.post('/api/v1/admin/apps', {
|
||||
data: { slug, name: slug }
|
||||
});
|
||||
expect(appRes.ok()).toBe(true);
|
||||
const appBody = (await appRes.json()) as { id: string };
|
||||
|
||||
const scriptRes = await api.post('/api/v1/admin/scripts', {
|
||||
data: { app_id: appBody.id, name: 'route-target', source: HELLO_RHAI }
|
||||
});
|
||||
expect(scriptRes.ok()).toBe(true);
|
||||
const scriptBody = (await scriptRes.json()) as { id: string };
|
||||
|
||||
return { appId: appBody.id, scriptId: scriptBody.id };
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function gotoRoutingTab(page: Page, scriptId: string): Promise<void> {
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
await page.getByRole('button', { name: 'Routing' }).click();
|
||||
}
|
||||
|
||||
async function addRoute(
|
||||
page: Page,
|
||||
opts: { path: string; pathKind?: 'exact' | 'param' | 'prefix'; method?: string; host?: string }
|
||||
): Promise<void> {
|
||||
await page.getByRole('button', { name: '+ Add route' }).click();
|
||||
const form = page.locator('form.route-form');
|
||||
await form.getByLabel('Path', { exact: true }).fill(opts.path);
|
||||
if (opts.pathKind) {
|
||||
await form.getByLabel('Path kind').selectOption(opts.pathKind);
|
||||
}
|
||||
if (opts.method !== undefined) {
|
||||
await form.getByLabel('Method').selectOption(opts.method);
|
||||
}
|
||||
if (opts.host !== undefined) {
|
||||
await form.getByLabel(/^Host/).fill(opts.host);
|
||||
}
|
||||
}
|
||||
|
||||
test.describe('B4 routing', () => {
|
||||
test('add route appears in list and matches in the preview', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('addr');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await addRoute(page, { path: '/greet', method: 'GET' });
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
|
||||
await expect(page.locator('.route-list')).toContainText('/greet');
|
||||
|
||||
// Match preview confirms the route resolves.
|
||||
await page.getByLabel('URL').fill('http://localhost/greet');
|
||||
await page.locator('.actions').getByRole('button', { name: 'Match' }).click();
|
||||
await expect(page.locator('pre.preview')).toContainText('script_id');
|
||||
});
|
||||
|
||||
test('remove route updates the list', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('remr');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await addRoute(page, { path: '/transient', method: 'GET' });
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
await expect(page.locator('.route-list')).toContainText('/transient');
|
||||
|
||||
// removeRoute() uses window.confirm — accept it.
|
||||
page.once('dialog', (d) => void d.accept());
|
||||
await page.locator('.route-list').getByRole('button', { name: 'remove' }).click();
|
||||
await expect(page.locator('.route-list')).toHaveCount(0);
|
||||
await expect(page.getByText(/no routes yet/i)).toBeVisible();
|
||||
});
|
||||
|
||||
test('duplicate route surfaces a 409 conflict error inline', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('dupr');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await addRoute(page, { path: '/twice', method: 'GET' });
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
await expect(page.locator('.route-list')).toContainText('/twice');
|
||||
|
||||
// Same path + method again — must conflict.
|
||||
await addRoute(page, { path: '/twice', method: 'GET' });
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
await expect(page.locator('.route-form .error.inline')).toBeVisible();
|
||||
});
|
||||
|
||||
test('path-kind mismatch warns inline when /:name is set to exact', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('mism');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await page.getByRole('button', { name: '+ Add route' }).click();
|
||||
await page.getByLabel('Path', { exact: true }).fill('/users/:id');
|
||||
// Override to a wrong kind — auto-detect would have picked
|
||||
// `param`; selecting `exact` should fire the warning.
|
||||
await page.getByLabel('Path kind').selectOption('exact');
|
||||
await expect(page.locator('.route-form .warning.inline')).toBeVisible();
|
||||
});
|
||||
|
||||
test('host validation warns when the host is not a claimed domain', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('unclaim');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await page.getByRole('button', { name: '+ Add route' }).click();
|
||||
await page.getByLabel('Path', { exact: true }).fill('/x');
|
||||
await page.getByLabel(/^Host/).fill('example.test-not-claimed.local');
|
||||
// One of the inline warnings is the unclaimed-host explainer.
|
||||
await expect(page.locator('.route-form .warning.inline').first()).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B4 routing adversarial', () => {
|
||||
test('reserved prefix /api/ is rejected with a visible error', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('reserv');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await addRoute(page, { path: '/api/v9/oops', method: 'GET' });
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
await expect(page.locator('.route-form .error.inline')).toBeVisible();
|
||||
await expect(page.locator('.route-form .error.inline')).toContainText(
|
||||
/reserved|api|prefix/i
|
||||
);
|
||||
// Empty-state copy renders when no routes exist; the path
|
||||
// itself must not appear anywhere on the routing tab.
|
||||
await expect(page.getByText(/no routes yet/i)).toBeVisible();
|
||||
});
|
||||
|
||||
test('xss payload in path stored or rejected — never executes on render', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
page.on('dialog', async (d) => {
|
||||
await d.dismiss();
|
||||
throw new Error(`Unexpected dialog: ${d.message()}`);
|
||||
});
|
||||
const slug = uniqueSlug('pxss');
|
||||
const { scriptId } = await makeAppWithScript(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await gotoRoutingTab(page, scriptId);
|
||||
await addRoute(page, {
|
||||
path: '/<script>alert(1)</script>',
|
||||
method: 'GET'
|
||||
});
|
||||
await page.getByRole('button', { name: /^Create route$/ }).click();
|
||||
|
||||
// Either accepted (rendered as text in the list) or rejected
|
||||
// (error inline). Both fine — what's NOT fine is an alert
|
||||
// dialog or an injected <script> tag in the list.
|
||||
const xssScripts = await page.locator('.route-list script:has-text("alert")').count();
|
||||
expect(xssScripts).toBe(0);
|
||||
});
|
||||
});
|
||||
203
dashboard/tests/e2e/scripts/scripts.spec.ts
Normal file
203
dashboard/tests/e2e/scripts/scripts.spec.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
import { expect, type Page } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Phase B3 — Scripts CRUD + Editor. The script editor lives at
|
||||
// /admin/scripts/{id}. Setup uses the API to create the app (and
|
||||
// sometimes a baseline script) so each test can focus on the editor
|
||||
// flow it actually covers.
|
||||
|
||||
const HELLO_RHAI = `return #{ statusCode: 200, body: #{ ok: true } };`;
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
async function createAppViaApi(slug: string): Promise<string> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/apps', {
|
||||
data: { slug, name: slug }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
const body = (await res.json()) as { id: string };
|
||||
return body.id;
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function createScriptViaApi(
|
||||
appId: string,
|
||||
name: string,
|
||||
source = HELLO_RHAI
|
||||
): Promise<string> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/scripts', {
|
||||
data: { app_id: appId, name, source }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
const body = (await res.json()) as { id: string };
|
||||
return body.id;
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function fillCodeMirror(page: Page, locator: string, text: string): Promise<void> {
|
||||
const cm = page.locator(locator).first();
|
||||
await cm.click();
|
||||
await page.keyboard.press('ControlOrMeta+A');
|
||||
await page.keyboard.press('Delete');
|
||||
await page.keyboard.type(text);
|
||||
}
|
||||
|
||||
test.describe('B3 scripts CRUD', () => {
|
||||
test('create script via UI navigates to scripts list with the new entry', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('cscr');
|
||||
await createAppViaApi(slug);
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/apps/${slug}`);
|
||||
await page.getByRole('button', { name: /^New script$/ }).click();
|
||||
await page.getByLabel('Name').fill('echo');
|
||||
// The CodeMirror editor starts empty in create mode; type a
|
||||
// minimal valid script.
|
||||
await fillCodeMirror(page, '.cm-content', HELLO_RHAI);
|
||||
await page.getByRole('button', { name: 'Create script' }).click();
|
||||
|
||||
await expect(page.getByRole('link', { name: /echo/i })).toBeVisible();
|
||||
});
|
||||
|
||||
test('edit + save Rhai source persists across reload', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('edit');
|
||||
const appId = await createAppViaApi(slug);
|
||||
const scriptId = await createScriptViaApi(appId, 'edit-target');
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
await expect(page.locator('.cm-content').first()).toContainText('statusCode');
|
||||
|
||||
const updated = `// edited by e2e\nreturn #{ statusCode: 201, body: #{ edited: true } };`;
|
||||
await fillCodeMirror(page, '.cm-content', updated);
|
||||
await page.getByRole('button', { name: /^Save$/ }).click();
|
||||
// Save button becomes disabled once the buffer matches the
|
||||
// just-saved source — that's our settle signal.
|
||||
await expect(page.getByRole('button', { name: /^Save$/ })).toBeDisabled();
|
||||
|
||||
await page.reload();
|
||||
await expect(page.locator('.cm-content').first()).toContainText('edited by e2e');
|
||||
});
|
||||
|
||||
test('invalid Rhai source: Format shows a parse error', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('invrhai');
|
||||
const appId = await createAppViaApi(slug);
|
||||
const scriptId = await createScriptViaApi(appId, 'bad-syntax');
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
await fillCodeMirror(page, '.cm-content', 'this is not rhai @@@ {{{');
|
||||
await page
|
||||
.locator('.editor-header')
|
||||
.getByRole('button', { name: 'Format' })
|
||||
.click();
|
||||
|
||||
await expect(page.locator('.error.inline').first()).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B3 test-invoke', () => {
|
||||
test('valid JSON body returns status + body in the result panel', async ({
|
||||
page,
|
||||
uniqueSlug
|
||||
}) => {
|
||||
const slug = uniqueSlug('inv-ok');
|
||||
const appId = await createAppViaApi(slug);
|
||||
const scriptId = await createScriptViaApi(appId, 'invoke-ok');
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
// Body editor is the second .cm-content (source is first).
|
||||
const bodyEditor = page.locator('.cm-content').nth(1);
|
||||
await bodyEditor.click();
|
||||
await page.keyboard.press('ControlOrMeta+A');
|
||||
await page.keyboard.press('Delete');
|
||||
await page.keyboard.type('{"hello":"world"}');
|
||||
|
||||
await page.getByRole('button', { name: /^Send$/ }).click();
|
||||
await expect(page.locator('.status')).toContainText('HTTP 200');
|
||||
await expect(page.locator('.result pre')).toContainText('ok');
|
||||
});
|
||||
|
||||
test('malformed JSON body: Format surfaces the parse error', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('inv-bad');
|
||||
const appId = await createAppViaApi(slug);
|
||||
const scriptId = await createScriptViaApi(appId, 'invoke-bad');
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
const bodyEditor = page.locator('.cm-content').nth(1);
|
||||
await bodyEditor.click();
|
||||
await page.keyboard.press('ControlOrMeta+A');
|
||||
await page.keyboard.press('Delete');
|
||||
await page.keyboard.type('{not valid json,');
|
||||
|
||||
// The Format button for the request body sits inside the
|
||||
// Test-invoke card next to the body editor.
|
||||
await page
|
||||
.locator('.json-block')
|
||||
.first()
|
||||
.getByRole('button', { name: 'Format' })
|
||||
.click();
|
||||
await expect(page.locator('.error.inline').first()).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B3 settings', () => {
|
||||
test('timeout input rejects zero and non-positive values', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('settz');
|
||||
const appId = await createAppViaApi(slug);
|
||||
const scriptId = await createScriptViaApi(appId, 'settings-target');
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
await page.getByRole('button', { name: 'Settings' }).click();
|
||||
const timeout = page.getByLabel(/Timeout/);
|
||||
await timeout.fill('0');
|
||||
const invalid = await timeout.evaluate((el: HTMLInputElement) => !el.validity.valid);
|
||||
expect(invalid).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B3 adversarial', () => {
|
||||
test('infinite loop script hits the sandbox timeout', async ({ page, uniqueSlug }) => {
|
||||
const slug = uniqueSlug('loop');
|
||||
const appId = await createAppViaApi(slug);
|
||||
const scriptId = await createScriptViaApi(
|
||||
appId,
|
||||
'inf-loop',
|
||||
'loop { let x = 1; }'
|
||||
);
|
||||
cleanup.app(slug);
|
||||
|
||||
await page.goto(`/admin/scripts/${scriptId}`);
|
||||
await page.getByRole('button', { name: /^Send$/ }).click();
|
||||
|
||||
// Either the status renders with a 5xx code, or an error
|
||||
// banner shows up. Either way, the page recovers.
|
||||
await Promise.race([
|
||||
expect(page.locator('.status')).toBeVisible({ timeout: 30_000 }),
|
||||
expect(page.locator('.error.inline').last()).toBeVisible({ timeout: 30_000 })
|
||||
]);
|
||||
|
||||
// The dashboard must remain interactive after the timeout.
|
||||
await page.getByRole('button', { name: 'Settings' }).click();
|
||||
await expect(page.getByLabel(/Timeout/)).toBeVisible();
|
||||
});
|
||||
});
|
||||
81
dashboard/tests/e2e/security/security.spec.ts
Normal file
81
dashboard/tests/e2e/security/security.spec.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
// Phase B8 — Cross-cutting security. Things that aren't tied to a
|
||||
// single page: session handling, secret leakage, error states for
|
||||
// missing resources, and a sanity check that no XSS sink fires
|
||||
// anywhere in the dashboard's main authed routes.
|
||||
|
||||
const VALID_USERNAME = process.env.E2E_ADMIN_USERNAME ?? 'admin';
|
||||
const VALID_PASSWORD = process.env.E2E_ADMIN_PASSWORD ?? 'admin';
|
||||
|
||||
test.describe('B8 cross-cutting security', () => {
|
||||
test('expired/stale token: any authed call redirects to /login', async ({ page }) => {
|
||||
// Replace the storageState token with an obvious garbage
|
||||
// value; the fetch wrapper treats 401 as "go to /login".
|
||||
await page.goto('/admin/login');
|
||||
await page.evaluate(() => {
|
||||
localStorage.setItem('picloud.admin.token', 'expired-or-bogus-token');
|
||||
});
|
||||
await page.goto('/admin/apps');
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
});
|
||||
|
||||
test('login response cookie is HttpOnly', async ({ request }) => {
|
||||
const res = await request.post('/api/v1/admin/auth/login', {
|
||||
data: { username: VALID_USERNAME, password: VALID_PASSWORD },
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
const headers = res.headers();
|
||||
const setCookie = headers['set-cookie'];
|
||||
// Backend may or may not set a cookie (the dashboard primarily
|
||||
// uses bearer-in-localStorage). If it does, it must be
|
||||
// HttpOnly so XSS can't exfiltrate it.
|
||||
if (setCookie) {
|
||||
expect(setCookie.toLowerCase()).toContain('httponly');
|
||||
}
|
||||
});
|
||||
|
||||
test('bootstrap password is not present in the DOM after login', async ({ page }) => {
|
||||
await page.goto('/admin/apps');
|
||||
const body = await page.locator('body').innerText();
|
||||
expect(body).not.toContain(VALID_PASSWORD);
|
||||
});
|
||||
|
||||
test('non-existent app slug shows a recoverable error, not a crash', async ({ page }) => {
|
||||
await page.goto('/admin/apps/does-not-exist-e2e-9999');
|
||||
// Page must render *something* and the layout must remain
|
||||
// intact (header link to Apps still works).
|
||||
await expect(page.getByRole('link', { name: 'Apps' })).toBeVisible();
|
||||
// And surface the failure to the user — either a "couldn't
|
||||
// load" message or a "back to apps" link.
|
||||
const errorOrBack = page.locator('.error, a[href$="/admin/apps"]');
|
||||
await expect(errorOrBack.first()).toBeVisible();
|
||||
});
|
||||
|
||||
test('xss probe across major surfaces never fires a dialog', async ({ page }) => {
|
||||
page.on('dialog', async (dialog) => {
|
||||
await dialog.dismiss();
|
||||
throw new Error(
|
||||
`XSS sink fired — got a ${dialog.type()} dialog: "${dialog.message()}"`
|
||||
);
|
||||
});
|
||||
|
||||
// Cover each main authed route. None should evaluate any
|
||||
// payload that earlier tests may have stored, and none should
|
||||
// inject inline <script> tags from server responses.
|
||||
for (const path of ['/admin/apps', '/admin/profile', '/admin/users']) {
|
||||
await page.goto(path);
|
||||
await page.waitForLoadState('domcontentloaded');
|
||||
const inlineScripts = await page.locator('script[src=""], script:not([src])').count();
|
||||
// Svelte itself injects no inline <script> in the
|
||||
// production bundle; vite dev does, but never with
|
||||
// onerror/alert payload text in them.
|
||||
const evilInline = await page
|
||||
.locator('script:has-text("alert"), script:has-text("__xss")')
|
||||
.count();
|
||||
expect(evilInline, `evil inline script tag on ${path}`).toBe(0);
|
||||
expect(inlineScripts).toBeGreaterThanOrEqual(0); // sanity assertion, no crash
|
||||
}
|
||||
});
|
||||
});
|
||||
28
dashboard/tests/e2e/smoke.spec.ts
Normal file
28
dashboard/tests/e2e/smoke.spec.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
import { loginAsAdmin } from './fixtures/auth';
|
||||
|
||||
// A1 smoke: prove globalSetup + webServer + fixtures + proxy all work.
|
||||
|
||||
test.describe('smoke', () => {
|
||||
test.describe('unauthenticated', () => {
|
||||
test.use({ storageState: { cookies: [], origins: [] } });
|
||||
|
||||
test('root redirects to login and shows the form', async ({ page }) => {
|
||||
await page.goto('/admin/');
|
||||
await expect(page).toHaveURL(/\/admin\/login$/);
|
||||
await expect(page.getByLabel('Username')).toBeVisible();
|
||||
await expect(page.getByLabel('Password')).toBeVisible();
|
||||
await expect(page.getByRole('button', { name: /sign in/i })).toBeVisible();
|
||||
});
|
||||
|
||||
test('valid credentials land on the apps page', async ({ page }) => {
|
||||
await loginAsAdmin(page);
|
||||
await expect(page.getByRole('link', { name: 'Apps' })).toBeVisible();
|
||||
});
|
||||
});
|
||||
|
||||
test('admin storageState already lands on apps', async ({ page }) => {
|
||||
await page.goto('/admin/');
|
||||
await expect(page).toHaveURL(/\/admin\/apps$/);
|
||||
});
|
||||
});
|
||||
209
dashboard/tests/e2e/users/users.spec.ts
Normal file
209
dashboard/tests/e2e/users/users.spec.ts
Normal file
@@ -0,0 +1,209 @@
|
||||
import { expect, type Browser, type Page, request } from '@playwright/test';
|
||||
import { test } from '../fixtures/ids';
|
||||
import { CleanupRegistry } from '../fixtures/cleanup';
|
||||
import { adminApi } from '../fixtures/api';
|
||||
|
||||
// Phase B6 — Instance Users (/admin/users). Covers the bootstrap
|
||||
// admin's view of the user directory: invite, edit, deactivate,
|
||||
// search, delete, plus the member-role redirect and adversarial
|
||||
// inputs to the invite form.
|
||||
|
||||
const API_BASE = process.env.E2E_API_BASE ?? 'http://127.0.0.1:18080';
|
||||
|
||||
const cleanup = new CleanupRegistry();
|
||||
test.afterEach(async () => {
|
||||
await cleanup.run();
|
||||
});
|
||||
|
||||
async function createMember(username: string, password = 'e2e-member-pw'): Promise<string> {
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const res = await api.post('/api/v1/admin/admins', {
|
||||
data: { username, password, instance_role: 'member' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
return ((await res.json()) as { id: string }).id;
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function loginToken(username: string, password: string): Promise<string> {
|
||||
const ctx = await request.newContext({ baseURL: API_BASE });
|
||||
try {
|
||||
const res = await ctx.post('/api/v1/admin/auth/login', {
|
||||
data: { username, password },
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
return ((await res.json()) as { token: string }).token;
|
||||
} finally {
|
||||
await ctx.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
async function pageWithToken(browser: Browser, token: string): Promise<Page> {
|
||||
const ctx = await browser.newContext({ storageState: undefined });
|
||||
const page = await ctx.newPage();
|
||||
await page.goto('/admin/login');
|
||||
await page.evaluate(
|
||||
([key, value]) => {
|
||||
localStorage.setItem(key, value);
|
||||
},
|
||||
['picloud.admin.token', token]
|
||||
);
|
||||
return page;
|
||||
}
|
||||
|
||||
test.describe('B6 instance users', () => {
|
||||
test('invite happy path: form → reveal modal → user in list', async ({
|
||||
page,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const username = uniqueUsername('inv');
|
||||
|
||||
await page.goto('/admin/users');
|
||||
await page.getByRole('button', { name: '+ Invite user' }).click();
|
||||
const modal = page.locator('form.modal');
|
||||
await modal.getByLabel('Username').fill(username);
|
||||
await modal.getByRole('radio', { name: /^Member/ }).check();
|
||||
await modal.getByRole('button', { name: /^Create user$/ }).click();
|
||||
|
||||
// Reveal modal shows the one-time password.
|
||||
const reveal = page.locator('.reveal-modal');
|
||||
await expect(reveal).toBeVisible();
|
||||
await expect(reveal).toContainText(/User created — /);
|
||||
await expect(reveal.getByRole('button', { name: /^Done$/ })).toBeDisabled();
|
||||
await reveal.getByRole('checkbox', { name: /shared this/i }).check();
|
||||
await reveal.getByRole('button', { name: /^Done$/ }).click();
|
||||
|
||||
// Now in the table.
|
||||
await expect(page.locator('.row:not(.head-row):not(.empty-row)', { hasText: username })).toBeVisible();
|
||||
|
||||
// API cleanup — we don't have the user id from the UI alone.
|
||||
const api = await adminApi();
|
||||
try {
|
||||
const list = await api.get('/api/v1/admin/admins');
|
||||
const all = (await list.json()) as Array<{ id: string; username: string }>;
|
||||
const u = all.find((x) => x.username === username);
|
||||
if (u) cleanup.adminUser(u.id);
|
||||
} finally {
|
||||
await api.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
test('username live validation: bad chars → submit disabled', async ({ page }) => {
|
||||
await page.goto('/admin/users');
|
||||
await page.getByRole('button', { name: '+ Invite user' }).click();
|
||||
const modal = page.locator('form.modal');
|
||||
await modal.getByLabel('Username').fill('UPPER_CASE_invalid');
|
||||
await expect(modal.locator('small.invalid')).toContainText(/allowed pattern/i);
|
||||
await modal.getByRole('radio', { name: /^Member/ }).check();
|
||||
await expect(modal.getByRole('button', { name: /^Create user$/ })).toBeDisabled();
|
||||
});
|
||||
|
||||
test('search filters the table by username', async ({ page, uniqueUsername }) => {
|
||||
const target = uniqueUsername('hit');
|
||||
const decoy = uniqueUsername('miss');
|
||||
const ids = await Promise.all([createMember(target), createMember(decoy)]);
|
||||
ids.forEach((id) => cleanup.adminUser(id));
|
||||
|
||||
await page.goto('/admin/users');
|
||||
await page.getByPlaceholder(/Search by username/).fill(target);
|
||||
await expect(page.locator('.row', { hasText: target })).toBeVisible();
|
||||
await expect(page.locator('.row', { hasText: decoy })).toHaveCount(0);
|
||||
});
|
||||
|
||||
test('deactivate then reactivate toggles the inactive indicator', async ({
|
||||
page,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const username = uniqueUsername('toggle');
|
||||
const userId = await createMember(username);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
await page.goto('/admin/users');
|
||||
await page.getByPlaceholder(/Search by username/).fill(username);
|
||||
const row = page.locator('.row:not(.head-row):not(.empty-row)', { hasText: username });
|
||||
await expect(row).toBeVisible();
|
||||
|
||||
await row.getByRole('button', { name: new RegExp(`User actions for ${username}`) }).click();
|
||||
await page.getByRole('menuitem', { name: /^Deactivate$/ }).click();
|
||||
await expect(row).toContainText(/inactive/i);
|
||||
|
||||
await row.getByRole('button', { name: new RegExp(`User actions for ${username}`) }).click();
|
||||
await page.getByRole('menuitem', { name: /^Reactivate$/ }).click();
|
||||
await expect(row).not.toContainText(/inactive/i);
|
||||
});
|
||||
|
||||
test('delete: wrong phrase keeps disabled, right phrase removes the user', async ({
|
||||
page,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const username = uniqueUsername('del');
|
||||
const userId = await createMember(username);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
await page.goto('/admin/users');
|
||||
await page.getByPlaceholder(/Search by username/).fill(username);
|
||||
const row = page.locator('.row:not(.head-row):not(.empty-row)', { hasText: username });
|
||||
await row.getByRole('button', { name: new RegExp(`User actions for ${username}`) }).click();
|
||||
await page.getByRole('menuitem', { name: /^Delete$/ }).click();
|
||||
|
||||
const dialog = page.getByRole('dialog');
|
||||
const confirm = dialog.getByRole('button', { name: /^Delete user$/ });
|
||||
await expect(confirm).toBeDisabled();
|
||||
await dialog.getByRole('textbox').fill('not-the-username');
|
||||
await expect(confirm).toBeDisabled();
|
||||
await dialog.getByRole('textbox').fill(username);
|
||||
await expect(confirm).toBeEnabled();
|
||||
await confirm.click();
|
||||
|
||||
await expect(page.locator('.row:not(.head-row):not(.empty-row)', { hasText: username })).toHaveCount(0);
|
||||
});
|
||||
|
||||
test('member-role user visiting /admin/users is bounced to profile with denied banner', async ({
|
||||
browser,
|
||||
uniqueUsername
|
||||
}) => {
|
||||
const username = uniqueUsername('memvw');
|
||||
const password = 'e2e-member-pw';
|
||||
const userId = await createMember(username, password);
|
||||
cleanup.adminUser(userId);
|
||||
|
||||
const token = await loginToken(username, password);
|
||||
const memberPage = await pageWithToken(browser, token);
|
||||
try {
|
||||
await memberPage.goto('/admin/users');
|
||||
await expect(memberPage).toHaveURL(/\/admin\/profile\?denied=users$/);
|
||||
await expect(memberPage.getByText(/don.?t have access to the Users page/i)).toBeVisible();
|
||||
} finally {
|
||||
await memberPage.context().close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('B6 instance users adversarial', () => {
|
||||
test('username too short: live invalid + submit disabled', async ({ page }) => {
|
||||
await page.goto('/admin/users');
|
||||
await page.getByRole('button', { name: '+ Invite user' }).click();
|
||||
const modal = page.locator('form.modal');
|
||||
await modal.getByLabel('Username').fill('a'); // 1 char — minimum is 2
|
||||
await expect(modal.locator('small.invalid')).toBeVisible();
|
||||
await modal.getByRole('radio', { name: /^Member/ }).check();
|
||||
await expect(modal.getByRole('button', { name: /^Create user$/ })).toBeDisabled();
|
||||
});
|
||||
|
||||
test('email with script tag fails validation, never executes', async ({ page }) => {
|
||||
page.on('dialog', async (d) => {
|
||||
await d.dismiss();
|
||||
throw new Error(`Unexpected dialog: ${d.message()}`);
|
||||
});
|
||||
|
||||
await page.goto('/admin/users');
|
||||
await page.getByRole('button', { name: '+ Invite user' }).click();
|
||||
const modal = page.locator('form.modal');
|
||||
await modal.getByLabel(/Email/).fill('<script>alert(1)</script>@x');
|
||||
await expect(modal.locator('small.invalid')).toContainText(/email/i);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user