Files
token-circles/worker/test/billing-status-interval.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

65 lines
2.6 KiB
TypeScript

/**
* `/api/billing/status` reports the billing interval.
*
* The column has been written by the webhook since migration 0025 and read back by nothing, so
* the app could not say whether an account was on monthly or annual — and, worse, could not tell
* a monthly -> annual switch from a no-op. The tier either side of that switch is the same one
* and is already entitled, so the client's "has it landed yet?" check matched on the first read
* and announced a change Stripe had not made. The interval is the only field that separates
* before from after, which is why it has to reach the client.
*/
import { env, SELF } from 'cloudflare:test';
import { beforeEach, describe, expect, it } from 'vitest';
import { sessionCookie } from './helpers/session';
const UID = 8600;
async function seed(interval: string | null, plan = 'basic', status: string | null = 'active') {
await env.DB.prepare(
'INSERT INTO users (id, email, password_hash, auth_provider, email_verified, token_version, plan, subscription_status, subscription_interval) VALUES (?, ?, ?, ?, 1, 1, ?, ?, ?)'
)
.bind(UID, 'interval@example.com', 'pbkdf2$100000$x$y', 'password', plan, status, interval)
.run();
}
const status = async () =>
(await (
await SELF.fetch('https://api.example.com/api/billing/status', {
headers: { Cookie: (await sessionCookie(UID, 'password', env)).split(';')[0] },
})
).json()) as { plan: string; status: string | null; interval: string | null };
beforeEach(async () => {
await env.DB.prepare('DELETE FROM profiles').run();
await env.DB.prepare('DELETE FROM users').run();
});
describe('GET /api/billing/status — interval', () => {
it('reports an annual subscription as annual', async () => {
await seed('annual');
expect(await status()).toMatchObject({ plan: 'basic', interval: 'annual' });
});
it('reports a monthly subscription as monthly', async () => {
await seed('monthly');
expect(await status()).toMatchObject({ plan: 'basic', interval: 'monthly' });
});
it('reports null for a subscription that predates the column', async () => {
// Not 'monthly'. A row written before 0025 has no interval, and guessing the commoner one
// would put a number on the billing card that nobody verified — and would make the client
// wait out its whole backoff for an interval that is never going to arrive.
await seed(null);
expect((await status()).interval).toBeNull();
});
it('reports null on a free account, which has no subscription to have an interval', async () => {
await seed(null, 'free', null);
expect(await status()).toMatchObject({ plan: 'free', interval: null });
});
});