Files
KomediruzeckiandGitHub ceb43bb3a8 feat(forms): profiles, Settings and import say what is wrong at the field (slice 4b of 6) (#611)
Profiles, Settings and the Import page move onto the form kit from #602: a refused entry is said
under the field that is wrong, in the same words in cloud and local-first. Each runtime now runs one
set of rules from shared/ for a profile, a setting, a connected source, a learned category mapping,
an uploaded file and an export.

- A profile name that is blank, over 100 characters, or another profile's in other capitals is
  refused at name. Any profile but the last can be deleted, and the Danger Zone lists the page's
  profiles under their current names.
- PUT /api/settings refuses a key another route owns. The base currency select is labelled and
  says why at the select.
- Both runtimes read an uploaded file with shared/importUpload.ts: an upload is checked to be a
  complete file before it is read, every cell is text, and a file with no header row is refused at
  file. Local-first reads the file off the page's thread, with a time limit, and imports an
  uploaded file again. The email importer, /api/v1/import and MCP's upload use the same reader.
- A backup carries each profile's connected sources and only its own settings. A restore files
  each retirement plan and badge record under its profile, from either runtime's file, and
  restores profile names that differ only in case, the later one numbered.
- Clearing a profile deletes its connected sources on the Worker, and deleting an account runs the
  same deletes.
- Applying category mappings updates the linked goals. The Worker's full backup is indented when
  Settings asks. The Worker's reseed-demo answers 410.
- The contract suite settles 12 pins (43 to 31).

Slice 4b of 6 in docs/plans/2026-10-07-form-errors.md.
2026-10-09 21:18:26 +02:00

149 lines
5.5 KiB
TypeScript

import { env, SELF } from 'cloudflare:test';
import { beforeEach, describe, expect, it } from 'vitest';
import { sessionCookie } from './helpers/session';
import { mintApiToken } from '../src/apitoken';
/*
* The gates added with the tiered plans (docs/plans/billing-tiers.md). Before this, api-tokens,
* the MCP server and scheduled imports carried no plan check at all, so a Free account could
* drive the whole product from a script.
*/
const COOKIES: Record<string, string> = {};
const PROFILE: Record<string, number> = {};
beforeEach(async () => {
// Children first: profiles hold a FK to users, so deleting users alone trips the constraint.
await env.DB.prepare('DELETE FROM import_sources').run();
await env.DB.prepare('DELETE FROM api_tokens').run();
await env.DB.prepare('DELETE FROM profiles').run();
await env.DB.prepare('DELETE FROM users').run();
const users: Array<[number, string]> = [
[91, 'free'],
[92, 'basic'],
[93, 'advanced'],
[94, 'ultimate'],
];
for (const [id, plan] of users) {
await env.DB.prepare(
'INSERT INTO users (id, email, auth_provider, token_version, plan) VALUES (?, ?, ?, 1, ?)'
)
.bind(id, `${plan}@example.com`, 'password', plan)
.run();
await env.DB.prepare('INSERT INTO profiles (id, name, user_id) VALUES (?, ?, ?)')
.bind(id, `${plan} household`, id)
.run();
PROFILE[plan] = id;
COOKIES[plan] = (await sessionCookie(id, 'password', env)).split(';')[0];
}
});
const mint = (plan: string, name = 'test') =>
SELF.fetch('https://example.com/api/account/api-tokens', {
method: 'POST',
headers: { Cookie: COOKIES[plan], 'Content-Type': 'application/json' },
body: JSON.stringify({ name, scopes: ['read'] }),
});
describe('API access is a paid feature', () => {
it('Free cannot mint a token', async () => {
const res = await mint('free');
expect(res.status).toBe(402);
});
it('Basic can', async () => {
expect((await mint('basic')).status).toBe(201);
});
it('Basic is capped at two live tokens', async () => {
expect((await mint('basic', 'one')).status).toBe(201);
expect((await mint('basic', 'two')).status).toBe(201);
const third = await mint('basic', 'three');
expect(third.status).toBe(402);
expect(await third.text()).toContain('2 API tokens');
});
it('a revoked token frees its slot, so rotating never needs an upgrade', async () => {
const first = await mint('basic', 'one');
const { id } = (await first.json()) as { id: string };
expect((await mint('basic', 'two')).status).toBe(201);
await SELF.fetch(`https://example.com/api/account/api-tokens/${id}`, {
method: 'DELETE',
headers: { Cookie: COOKIES.basic },
});
expect((await mint('basic', 'three')).status).toBe(201);
});
it('an expired token frees its slot too', async () => {
expect((await mint('basic', 'one')).status).toBe(201);
expect((await mint('basic', 'two')).status).toBe(201);
// An expiry that has passed, in the ISO 8601 shape a client sends. D1 keeps its own clock,
// which vi.setSystemTime does not reach, so the expiry moves instead of the clock.
await env.DB.prepare(
"UPDATE api_tokens SET expires_at = strftime('%Y-%m-%dT00:00:00.000Z', 'now') WHERE user_id = 92 AND name = 'two'"
).run();
const third = await mint('basic', 'three');
expect(third.status, await third.clone().text()).toBe(201);
});
it('Ultimate is uncapped', async () => {
for (let i = 0; i < 12; i++) expect((await mint('ultimate', `t${i}`)).status).toBe(201);
});
});
describe('an existing token stops working when the plan lapses', () => {
it('a token minted on Advanced is refused after a downgrade to Free', async () => {
const minted = await mintApiToken(env.DB, 93, {
name: 'cli',
scopes: ['read'],
defaultProfileId: null,
expiresAt: null,
});
// /mcp is the one route behind requireToken, so it is where a bearer credential is proved.
const call = () =>
SELF.fetch('https://example.com/mcp', {
method: 'POST',
headers: {
Authorization: `Bearer ${minted.secret}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }),
});
expect((await call()).status).not.toBe(401);
await env.DB.prepare("UPDATE users SET plan = 'free' WHERE id = 93").run();
// Checked on the token's own verify, not only at mint time — otherwise whatever was minted
// while paying keeps working forever.
expect((await call()).status).toBe(401);
});
});
describe('scheduled imports are an Advanced feature', () => {
const create = (plan: string, schedule: string) =>
SELF.fetch(`https://example.com/api/import-sources?profileId=${PROFILE[plan]}`, {
method: 'POST',
headers: { Cookie: COOKIES[plan], 'Content-Type': 'application/json' },
body: JSON.stringify({
kind: 'google_sheet',
label: 'Bank',
config: { url: 'https://docs.google.com/spreadsheets/d/plan-gate/edit' },
schedule,
}),
});
it('Basic may keep a manual source', async () => {
expect((await create('basic', 'manual')).status).toBe(201);
});
it('Basic may not put one on a daily schedule', async () => {
const res = await create('basic', 'daily');
expect(res.status).toBe(402);
expect(await res.text()).toContain('Advanced');
});
it('Advanced may', async () => {
expect((await create('advanced', 'daily')).status).toBe(201);
});
});