All checks were successful
CI / checks (pull_request) Successful in 36s
Problem
-------
1. Re-clicking an already-confirmed confirmation link (e.g. /confirm?token=…)
returned from confirmSubscription because the SQL WHERE
clause required . The caller then threw an
ActionError('invalid or expired') which rendered the 'Email not confirmed'
error page — misleading for someone who had already confirmed.
2. A second PUT /subscription/email with the same email+frequency could
silently bypass the upsert path when getSubscriptionByPubkey found the
active row but updateSubscription returned it unchanged (email and
frequency matched). While the unique index prevented a true duplicate
INSERT, the code path was fragile and the regression test was missing.
Changes
-------
database.ts:
- confirmSubscription now returns { sub, alreadyConfirmed } | undefined.
First it tries the existing UPDATE (unconfirmed tokens only). If that
returns no rows, it looks up the key directly: if the row exists and is
already confirmed, returns { sub, alreadyConfirmed: true }. If the row
doesn't exist or is unsubscribed, returns undefined (invalid/expired).
- Exported new ConfirmResult type for callers.
actions.ts:
- confirmSubscriptionAction destructures the new return type.
- Only registers the cron job on fresh confirmation (not re-confirms).
- Returns the ConfirmResult so the route can distinguish the two cases.
server.ts:
- /confirm route checks result.alreadyConfirmed and renders
confirm-already.html instead of confirm-success.html.
pages/confirm-already.html:
- New page with title 'Email already confirmed' and an info message
explaining the address was already confirmed.
Tests:
- test/confirm-already-confirmed.test.ts — NEW (3 tests): first confirm
succeeds with alreadyConfirmed=false; second confirm returns
alreadyConfirmed=true; nonexistent token returns undefined.
- test/duplicate-subscription.test.ts — NEW (4 tests): full cycle of
register → confirm → re-register → assert one active row with
unchanged key, verifying the upsert is idempotent.
- Adapted 3 existing test files to destructure the new ConfirmResult.
42 lines
No EOL
1.4 KiB
TypeScript
42 lines
No EOL
1.4 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
import * as db from '../src/database.js'
|
|
import { registerSubscription } from '../src/actions.js'
|
|
import { getJobCronSource, removeJob } from '../src/worker/email.js'
|
|
import { registerSubscription as regSub } from '../src/worker/index.js'
|
|
|
|
const pubkey = 'freq-test-' + Date.now()
|
|
const email = 'freq-test-' + Date.now() + '@example.com'
|
|
let sub: any = null
|
|
|
|
describe('Frequency change reschedules cron job', () => {
|
|
beforeAll(async () => {
|
|
await db.migrate()
|
|
})
|
|
|
|
it('creates confirmed subscription with daily frequency', async () => {
|
|
const s = await db.insertSubscription(pubkey, email, 'daily')
|
|
expect(s).toBeTruthy()
|
|
sub = s
|
|
|
|
const confirmed = await db.confirmSubscription(sub.key)
|
|
expect(confirmed).toBeTruthy()
|
|
sub = confirmed!.sub
|
|
})
|
|
|
|
it('registers cron job with daily frequency', () => {
|
|
regSub(sub)
|
|
const dailySource = getJobCronSource(sub.id)
|
|
expect(dailySource).toBe('0 0 17 * * *')
|
|
})
|
|
|
|
it('changes frequency to weekly via registerSubscription', async () => {
|
|
await registerSubscription({ pubkey, email, frequency: 'weekly' })
|
|
const weeklySource = getJobCronSource(sub.id)
|
|
expect(weeklySource).toBe('0 0 17 * * 1')
|
|
})
|
|
})
|
|
|
|
afterAll(async () => {
|
|
const updated = await db.getSubscriptionByPubkey(pubkey)
|
|
if (updated) removeJob(updated)
|
|
}) |