Railyard side of PY1.3 (RYP-D2, RYP-E1, SC-E3). Acceptance:
1. apps/insights: umami-software/umami v3.4.0 (MIT, commit ec0ff503),
vendored --depth 1, LICENSE kept, NOTICE added. Fork additions only:
src/railyard/{ingest,summary,tenancy,api-only}.ts, /v1/ingest/batch (the
edge collector contract v1, bearer INSIGHTS_INGEST_TOKEN, no IP ever,
retried batches de-duplicated by migration 90_railyard_ingest_receipts),
/api/v1/tenants/{org}/summary (bearer INSIGHTS_API_TOKEN), src/proxy.ts
(INSIGHTS_API_ONLY=1 serves only the Railyard API and the heartbeat).
Tenant isolation is tested at the API level (an app id owned by another org
is refused) and at the SQL level (upstream migrations 01-26 plus 90 on a
throwaway cluster: org B never reads org A's rows, even naming its app).
Upstream vitest suite: not run on the Mac (pnpm 12.3.4 pinned upstream,
disk floor); it runs in the image build on box 2 or CI.
2. No upstream product name in anything a customer loads: the page tag, the
console pages and the fork's additions (tests); the upstream dashboard is
never served in API-only mode.
3. The console writes the collector's site entry on "Add app", on
auto-created apps from deploys, and rewrites every entry on add-on, plan or
pause changes (lib/sites.ts, SITES KV binding). The bridge gains
`sites` (the project's app, site key, page tag and collector URLs, entry
written) and the analytics proof is the first event from Insights. The
console's Insights page shows visitors, events, first event and the
visit -> signup -> first action funnel.
5. insights-edge/src/caps.ts: at 100% of the cap events are still counted and
not stored until the next UTC month; the console shows the same message
from a byte-identical copy (lib/caps.ts, tested).
4 (384 MB at 50 events a second for 10 min) is measured on staging before deploy.
Env: INSIGHTS_API_URL, INSIGHTS_API_TOKEN, SITES KV binding (console);
INSIGHTS_INGEST_TOKEN, INSIGHTS_API_TOKEN, INSIGHTS_API_ONLY (Insights).
Tests: console 54/54 (tsc clean), insights fork 7/7 (incl. SQL isolation),
insights-edge caps + collector + sim 14/14, tests/contract 3/3.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
5.5 KiB
TypeScript
162 lines
5.5 KiB
TypeScript
import { expect, test } from './fixtures';
|
|
import { login } from './helpers/auth';
|
|
import { UNKNOWN_UUID } from './helpers/constants';
|
|
import { uniqueName } from './helpers/entities';
|
|
|
|
test.describe('Users', () => {
|
|
test.describe.configure({ mode: 'serial' });
|
|
|
|
const username = uniqueName('user');
|
|
const password = 'spec-password-1';
|
|
let userId = '';
|
|
|
|
test.afterAll(async ({ admin }) => {
|
|
if (userId) {
|
|
await admin.del(`/api/users/${userId}`);
|
|
}
|
|
});
|
|
|
|
test('POST /api/users creates a user', async ({ admin }) => {
|
|
const response = await admin.post('/api/users', {
|
|
username: username.toUpperCase(),
|
|
password,
|
|
role: 'user',
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
// Usernames are stored lowercased.
|
|
expect(response.body).toMatchObject({ username, role: 'user' });
|
|
|
|
userId = response.body.id;
|
|
});
|
|
|
|
test('POST /api/users rejects duplicates and invalid bodies', async ({ admin }) => {
|
|
const duplicate = await admin.post('/api/users', { username, password, role: 'user' });
|
|
const shortPassword = await admin.post('/api/users', {
|
|
username: uniqueName('user'),
|
|
password: 'short',
|
|
role: 'user',
|
|
});
|
|
const badRole = await admin.post('/api/users', {
|
|
username: uniqueName('user'),
|
|
password,
|
|
role: 'superuser',
|
|
});
|
|
|
|
expect(duplicate.status).toBe(400);
|
|
expect(duplicate.body.error.message).toBe('User already exists');
|
|
expect(shortPassword.status).toBe(400);
|
|
expect(badRole.status).toBe(400);
|
|
});
|
|
|
|
test('POST /api/users requires an admin session', async ({ api, user, apiKey }) => {
|
|
const body = { username: uniqueName('user'), password, role: 'user' };
|
|
|
|
expect((await api.post('/api/users', body)).status).toBe(401);
|
|
expect((await user.post('/api/users', body)).status).toBe(401);
|
|
// API keys are blocked on /api/users even for admins.
|
|
expect((await apiKey.post('/api/users', body)).status).toBe(401);
|
|
});
|
|
|
|
test('GET /api/users/{userId} returns a user', async ({ admin, api, viewer }) => {
|
|
const response = await admin.get(`/api/users/${userId}`);
|
|
const self = await api
|
|
.bearer(await login(api, { username, password }))
|
|
.get(`/api/users/${userId}`);
|
|
const denied = await viewer.get(`/api/users/${userId}`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toMatchObject({ id: userId, username, role: 'user' });
|
|
expect(response.body).not.toHaveProperty('password');
|
|
expect(self.status).toBe(200);
|
|
expect(denied.status).toBe(401);
|
|
});
|
|
|
|
test('POST /api/users/{userId} lets admins update role, username and password', async ({
|
|
admin,
|
|
api,
|
|
}) => {
|
|
const newPassword = 'spec-password-2';
|
|
const response = await admin.post(`/api/users/${userId}`, {
|
|
role: 'view-only',
|
|
password: newPassword,
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toMatchObject({ id: userId, username, role: 'view-only' });
|
|
|
|
// The new password works and the old one does not.
|
|
expect(await login(api, { username, password: newPassword })).toEqual(expect.any(String));
|
|
expect((await api.post('/api/auth/login', { username, password })).status).toBe(401);
|
|
|
|
await admin.post(`/api/users/${userId}`, { password });
|
|
});
|
|
|
|
test('POST /api/users/{userId} ignores role changes from non-admins', async ({ user, seed }) => {
|
|
const response = await user.post(`/api/users/${seed.user.id}`, { role: 'admin' });
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.role).toBe('user');
|
|
});
|
|
|
|
test('POST /api/users/{userId} rejects unknown users and other users', async ({
|
|
admin,
|
|
user,
|
|
}) => {
|
|
const unknown = await admin.post(`/api/users/${UNKNOWN_UUID}`, { role: 'user' });
|
|
const denied = await user.post(`/api/users/${userId}`, { password: 'spec-password-3' });
|
|
|
|
expect(unknown.status).toBe(404);
|
|
expect(denied.status).toBe(401);
|
|
});
|
|
|
|
test('GET /api/users/{userId}/websites lists a user websites', async ({
|
|
admin,
|
|
user,
|
|
viewer,
|
|
seed,
|
|
}) => {
|
|
const response = await admin.get(`/api/users/${seed.user.id}/websites`);
|
|
const self = await user.get(`/api/users/${seed.user.id}/websites`, {
|
|
params: { includeTeams: 'true' },
|
|
});
|
|
const denied = await viewer.get(`/api/users/${seed.user.id}/websites`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.data.map((w: any) => w.id)).toContain(seed.website2.id);
|
|
expect(self.status).toBe(200);
|
|
expect(self.body.data.map((w: any) => w.id)).toContain(seed.website2.id);
|
|
expect(denied.status).toBe(401);
|
|
});
|
|
|
|
test('GET /api/users/{userId}/teams lists a user teams', async ({
|
|
admin,
|
|
user,
|
|
viewer,
|
|
seed,
|
|
}) => {
|
|
const response = await admin.get(`/api/users/${seed.user.id}/teams`);
|
|
const self = await user.get(`/api/users/${seed.user.id}/teams`);
|
|
const denied = await viewer.get(`/api/users/${seed.user.id}/teams`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.data.map((t: any) => t.id)).toContain(seed.team.id);
|
|
expect(self.status).toBe(200);
|
|
expect(denied.status).toBe(401);
|
|
});
|
|
|
|
test('DELETE /api/users/{userId} deletes a user', async ({ admin, user, seed }) => {
|
|
const denied = await user.del(`/api/users/${userId}`);
|
|
const self = await admin.del(`/api/users/${seed.admin.id}`);
|
|
const response = await admin.del(`/api/users/${userId}`);
|
|
|
|
expect(denied.status).toBe(401);
|
|
expect(self.status).toBe(400);
|
|
expect(self.body.error.message).toBe('You cannot delete yourself.');
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual({ ok: true });
|
|
|
|
userId = '';
|
|
});
|
|
});
|