mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
A password account confirms its email before it signs in or uses the app. - Create account ends on Check your inbox, which names the address, sends the link again and says where to open it. The screen is the same whatever the address. - A password sign-in works once the address is confirmed, or in the browser that opened the account's own confirm link. Until then the right password is refused as a wrong one is, and the form offers Send the link again. - A signed-in account whose address is not confirmed sees Confirm your email instead of the app. The Worker answers its sessions and API tokens 403 EMAIL_UNCONFIRMED everywhere but me, logout, the signed-in resend, the link's finish route, and the billing portal for an account that pays. - POST /api/auth/verify-email/resend sends the link again while signed out, with limits and the captcha before any look-up. One budget of three confirm links an hour covers every way of asking for one. - Sign-up answers first, then makes the account, its profile and its first link in one batch, tried twice. Signing up again for an address waiting for its link mails a fresh link. - A sign-in code, a reset link and Google sign-in confirm the address. A passkey or two-factor does not. - /api/v1 capabilities, reminders and sheet syncs leave out an account waiting for its link. - The welcome and confirm mails say the link starts the account and where to sign in after opening it. No mail on the sign-up path has an em dash before the shared footer. No migration.
144 lines
5.7 KiB
TypeScript
144 lines
5.7 KiB
TypeScript
/**
|
|
* The JSON-RPC layer. Stateless streamable HTTP: every POST is self-contained, so there is no
|
|
* session to resume and no Durable Object to hold one. GET is 405 because nothing here is
|
|
* server-initiated.
|
|
*/
|
|
import { env, SELF } from 'cloudflare:test';
|
|
import { beforeAll, describe, expect, it } from 'vitest';
|
|
import { mintApiToken } from '../src/apitoken';
|
|
|
|
const USER_ID = 9300;
|
|
const PROFILE_ID = 9301;
|
|
let secret = '';
|
|
|
|
async function rpc(
|
|
method: string,
|
|
params?: unknown,
|
|
token = secret
|
|
): Promise<{ status: number; body: any }> {
|
|
const res = await SELF.fetch('https://api.example.com/mcp', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
|
|
});
|
|
return { status: res.status, body: await res.json() };
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO users (id, email, password_hash, auth_provider, email_verified, plan, token_version) VALUES (?, 'mcp@example.com', 'pbkdf2$100000$x$y', 'password', 1, 'advanced', 1)"
|
|
)
|
|
.bind(USER_ID)
|
|
.run();
|
|
await env.DB.prepare(
|
|
"INSERT OR IGNORE INTO profiles (id, name, user_id) VALUES (?, 'MCP Profile', ?)"
|
|
)
|
|
.bind(PROFILE_ID, USER_ID)
|
|
.run();
|
|
secret = (
|
|
await mintApiToken(env.DB, USER_ID, {
|
|
name: 'mcp',
|
|
scopes: ['read', 'write', 'import'],
|
|
defaultProfileId: PROFILE_ID,
|
|
})
|
|
).secret;
|
|
});
|
|
|
|
describe('JSON-RPC dispatch', () => {
|
|
it('initializes with a protocol version and tool capability', async () => {
|
|
const { status, body } = await rpc('initialize', {
|
|
protocolVersion: '2025-06-18',
|
|
clientInfo: { name: 'test', version: '1' },
|
|
capabilities: {},
|
|
});
|
|
expect(status).toBe(200);
|
|
expect(body.jsonrpc).toBe('2.0');
|
|
expect(body.result.protocolVersion).toBe('2025-06-18');
|
|
expect(body.result.capabilities.tools).toBeDefined();
|
|
expect(body.result.serverInfo.name).toBe('token-circles');
|
|
});
|
|
|
|
it('lists tools with JSON Schema generated from the zod definitions', async () => {
|
|
const { body } = await rpc('tools/list');
|
|
const names = body.result.tools.map((t: { name: string }) => t.name);
|
|
expect(names).toContain('whoami');
|
|
const whoami = body.result.tools.find((t: { name: string }) => t.name === 'whoami');
|
|
expect(whoami.inputSchema.type).toBe('object');
|
|
expect(whoami.description.length).toBeGreaterThan(20);
|
|
});
|
|
|
|
it('calls a tool and returns structuredContent', async () => {
|
|
const { body } = await rpc('tools/call', { name: 'whoami', arguments: {} });
|
|
expect(body.result.isError).toBeFalsy();
|
|
expect(body.result.structuredContent.userId).toBe(USER_ID);
|
|
expect(body.result.structuredContent.profiles[0].id).toBe(PROFILE_ID);
|
|
expect(body.result.structuredContent.scopes).toEqual(['read', 'write', 'import']);
|
|
// Text content mirrors the structured payload for clients that only read text.
|
|
expect(JSON.parse(body.result.content[0].text).userId).toBe(USER_ID);
|
|
});
|
|
|
|
it('returns a JSON-RPC error, not a 500, for bad arguments', async () => {
|
|
const { status, body } = await rpc('tools/call', {
|
|
name: 'whoami',
|
|
arguments: { nope: [1, 2] },
|
|
});
|
|
expect(status).toBe(200);
|
|
expect(body.result.isError).toBe(true);
|
|
expect(body.result.content[0].text).toMatch(/argument/i);
|
|
});
|
|
|
|
it('returns -32601 for an unknown method and -32602 for an unknown tool', async () => {
|
|
expect((await rpc('does/not/exist')).body.error.code).toBe(-32601);
|
|
const unknownTool = await rpc('tools/call', { name: 'no_such_tool', arguments: {} });
|
|
expect(unknownTool.body.error.code).toBe(-32602);
|
|
});
|
|
|
|
// The route reads its body with .catch(() => null), which is what turns the 400 the
|
|
// malformed-JSON middleware raises (error-response.ts) into the JSON-RPC parse error an MCP
|
|
// client expects. Drop that catch and these answer 400 with the API's own error body instead.
|
|
for (const [label, body] of [
|
|
['a malformed body', '{"jsonrpc": "2.0", "id": 1, "method": '],
|
|
['no body', undefined],
|
|
] as const) {
|
|
it(`answers -32700 Parse error to ${label}`, async () => {
|
|
const res = await SELF.fetch('https://api.example.com/mcp', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${secret}`, 'Content-Type': 'application/json' },
|
|
body,
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({
|
|
jsonrpc: '2.0',
|
|
id: null,
|
|
error: { code: -32700, message: 'Parse error' },
|
|
});
|
|
});
|
|
}
|
|
|
|
it('accepts a notification with no id and answers ping', async () => {
|
|
const res = await SELF.fetch('https://api.example.com/mcp', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${secret}`, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ jsonrpc: '2.0', method: 'notifications/initialized' }),
|
|
});
|
|
expect(res.status).toBe(202);
|
|
expect((await rpc('ping')).body.result).toEqual({});
|
|
});
|
|
|
|
it('401s without a token and 405s a GET', async () => {
|
|
const res = await SELF.fetch('https://api.example.com/mcp', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }),
|
|
});
|
|
expect(res.status).toBe(401);
|
|
expect((await SELF.fetch('https://api.example.com/mcp')).status).toBe(405);
|
|
});
|
|
|
|
it('403s a tool whose scope the token lacks', async () => {
|
|
const readOnly = (await mintApiToken(env.DB, USER_ID, { name: 'ro', scopes: ['read'] })).secret;
|
|
const { body } = await rpc('tools/call', { name: 'whoami', arguments: {} }, readOnly);
|
|
expect(body.result.isError).toBeFalsy(); // whoami is a read tool, so a read token may call it
|
|
});
|
|
});
|