mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
- Google sign-in, an emailed sign-in code and a reset link confirm an account's address. On an account whose address was never confirmed, each first clears what was set up on it (sessions, password, passkeys, two-factor, API tokens, unused links) in the same D1 batch, and the app says once what went. - A new session or credential is written only while the account's token_version is still the one its check read. - Confirm and change-of-address links take effect only with their own account's session. Opened elsewhere, they finish after signing in there. A browser that starts local-first opens the sign-in for them, and the reset page works there too. - Google sign-in keeps and matches addresses in lower case. A reset link is refused once it has expired. An expired API token stops counting against the plan's limit. Deleting an account deletes its sessions.
71 lines
2.7 KiB
TypeScript
71 lines
2.7 KiB
TypeScript
import { env, SELF } from 'cloudflare:test';
|
|
import { beforeEach, describe, expect, it } from 'vitest';
|
|
import { sessionCookie } from './helpers/session';
|
|
|
|
// Import session log (migration 0014): the Import page records one row per successful
|
|
// import and lists past sessions. Rows are profile-scoped.
|
|
|
|
let cookie = '';
|
|
|
|
beforeEach(async () => {
|
|
for (const t of ['import_logs', 'profiles', 'users']) {
|
|
await env.DB.prepare(`DELETE FROM ${t}`).run();
|
|
}
|
|
await env.DB.batch([
|
|
env.DB.prepare(
|
|
"INSERT INTO users (id, email, auth_provider, token_version) VALUES (80, 'importlog@example.com', 'password', 1)"
|
|
),
|
|
env.DB.prepare("INSERT INTO profiles (id, user_id, name) VALUES (800, 80, 'Main')"),
|
|
]);
|
|
cookie = (await sessionCookie(80, 'password', env)).split(';')[0];
|
|
});
|
|
|
|
describe('import logs', () => {
|
|
it('records a session and lists it back', async () => {
|
|
const post = await SELF.fetch('https://example.com/api/import-logs', {
|
|
method: 'POST',
|
|
headers: { Cookie: cookie, 'Content-Type': 'application/json', 'X-Profile-Id': '800' },
|
|
body: JSON.stringify({
|
|
import_id: 'abc-123',
|
|
source: 'statement.csv',
|
|
imported: 42,
|
|
duplicates_skipped: 3,
|
|
accounts_created: 1,
|
|
categories_created: 2,
|
|
details: JSON.stringify({ created_accounts: ['Revolut'] }),
|
|
}),
|
|
});
|
|
expect(post.status).toBe(201);
|
|
|
|
const list = await SELF.fetch('https://example.com/api/import-logs', {
|
|
headers: { Cookie: cookie, 'X-Profile-Id': '800' },
|
|
});
|
|
expect(list.status).toBe(200);
|
|
const rows = (await list.json()) as Record<string, unknown>[];
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0].source).toBe('statement.csv');
|
|
expect(rows[0].imported).toBe(42);
|
|
expect(rows[0].duplicates_skipped).toBe(3);
|
|
expect(JSON.parse(rows[0].details as string).created_accounts).toEqual(['Revolut']);
|
|
});
|
|
|
|
it('does not leak sessions across users', async () => {
|
|
await SELF.fetch('https://example.com/api/import-logs', {
|
|
method: 'POST',
|
|
headers: { Cookie: cookie, 'Content-Type': 'application/json', 'X-Profile-Id': '800' },
|
|
body: JSON.stringify({ source: 'private.csv', imported: 1 }),
|
|
});
|
|
await env.DB.batch([
|
|
env.DB.prepare(
|
|
"INSERT INTO users (id, email, auth_provider, token_version) VALUES (81, 'other-import@example.com', 'password', 1)"
|
|
),
|
|
env.DB.prepare("INSERT INTO profiles (id, user_id, name) VALUES (801, 81, 'Main')"),
|
|
]);
|
|
const otherCookie = (await sessionCookie(81, 'password', env)).split(';')[0];
|
|
const list = await SELF.fetch('https://example.com/api/import-logs', {
|
|
headers: { Cookie: otherCookie, 'X-Profile-Id': '801' },
|
|
});
|
|
expect(await list.json()).toEqual([]);
|
|
});
|
|
});
|