Files
token-circles/worker/test/notifications-plan-gate.test.ts
KomediruzeckiandGitHub 820c8f68be fix(auth): confirming an address clears what was set up before it, and email links finish after sign-in (#610)
- 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.
2026-10-09 17:14:15 +02:00

48 lines
1.9 KiB
TypeScript

import { env, SELF } from 'cloudflare:test';
import { beforeEach, describe, expect, it } from 'vitest';
import { sessionCookie } from './helpers/session';
// Email alerts (manual test/preview + trigger) are a paid feature (plans.ts emailReminders).
// The scheduled sender gates on canReceive(); these manual endpoints must gate too — otherwise
// a free user can send themselves budget/spending emails despite emailReminders:false.
let freeCookie: string;
let paidCookie: string;
beforeEach(async () => {
await env.DB.prepare('DELETE FROM users').run();
await env.DB.prepare(
"INSERT INTO users (id, email, auth_provider, token_version, plan) VALUES (81, 'free@example.com', 'password', 1, 'free')"
).run();
await env.DB.prepare(
"INSERT INTO users (id, email, auth_provider, token_version, plan) VALUES (82, 'paid@example.com', 'password', 1, 'advanced')"
).run();
freeCookie = (await sessionCookie(81, 'password', env)).split(';')[0];
paidCookie = (await sessionCookie(82, 'password', env)).split(';')[0];
});
function post(path: string, cookie: string, body: unknown) {
return SELF.fetch(`https://example.com${path}`, {
method: 'POST',
headers: { Cookie: cookie, 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}
describe('manual email endpoints are plan-gated (emailReminders)', () => {
it('free plan gets 402 from /api/notifications/test-email', async () => {
const res = await post('/api/notifications/test-email', freeCookie, { type: 'basic' });
expect(res.status).toBe(402);
});
it('free plan gets 402 from /api/notifications/trigger', async () => {
const res = await post('/api/notifications/trigger', freeCookie, { type: 'budget' });
expect(res.status).toBe(402);
});
it('a paid plan passes the gate on /api/notifications/test-email', async () => {
const res = await post('/api/notifications/test-email', paidCookie, { type: 'basic' });
expect(res.status).toBe(200);
});
});