Match a submission's client token only within its event
Test and deploy / test-and-deploy (pull_request) Successful in 27s
Test and deploy / test-and-deploy (pull_request) Successful in 27s
The token is per form visit, and one visit can move on to another concert's form in the same tab. With a global match, the second event's submission was answered with the first event's id and its feedback was silently dropped. The lookup now joins on the event slug, and the unique index in 003 is (event_id, client_token) instead of client_token alone. 003 is edited in place: it has not been applied anywhere yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
4 files changed
+38
-13
No files matched your search
@@ -23,12 +23,16 @@ const mockGetEventConfigBySlug = getEventConfigBySlug as Mock;
|
||||
const TOKEN = '3f2b8c1e-4d5a-4b6c-9e7f-0a1b2c3d4e5f';
|
||||
const BODY = {answers: [{eventQuestionId: 26, text: 'Danke!'}], clientToken: TOKEN};
|
||||
|
||||
const SLUG = 'sommerkonzert-2026';
|
||||
const LOOKUP_SQL = 'SELECT s.submission_id FROM submissions s';
|
||||
|
||||
// One fake connection serves every query; `existingId` is what a lookup by
|
||||
// client_token finds, and `insertError` makes the submissions INSERT throw.
|
||||
const makeConn = ({existingId = null, insertError = null}: {existingId?: number | null; insertError?: any} = {}) => ({
|
||||
query: vi.fn(async (sql: string) => {
|
||||
if (sql.startsWith('SELECT submission_id FROM submissions')) {
|
||||
return existingId === null ? [] : [{submission_id: existingId}];
|
||||
// client_token finds when it was stored for `existingSlug`, and
|
||||
// `insertError` makes the submissions INSERT throw.
|
||||
const makeConn = ({existingId = null, existingSlug = SLUG, insertError = null}: {existingId?: number | null; existingSlug?: string; insertError?: any} = {}) => ({
|
||||
query: vi.fn(async (sql: string, params: any[] = []) => {
|
||||
if (sql.startsWith(LOOKUP_SQL)) {
|
||||
return existingId === null || params[1] !== existingSlug ? [] : [{submission_id: existingId}];
|
||||
}
|
||||
if (sql.startsWith('INSERT INTO submissions')) {
|
||||
if (insertError) throw insertError;
|
||||
@@ -88,13 +92,25 @@ describe('submitFeedback - client token idempotency', () => {
|
||||
expect(insert[1]).toContain(TOKEN);
|
||||
});
|
||||
|
||||
it('saves a new submission when the token was only ever used for a different event', async () => {
|
||||
// Same browser tab, two concerts: the token must not make the second
|
||||
// event's feedback come back as the first event's submission.
|
||||
const conn = makeConn({existingId: 82, existingSlug: 'winterkonzert-2026'});
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
const result = await submitFeedback(SLUG, BODY, 'hash');
|
||||
|
||||
expect(result).toEqual({status: 'OK', submissionId: 90, newsletterDropped: false});
|
||||
expect(allQueries(conn).find(([sql]) => sql.startsWith(LOOKUP_SQL))![1]).toEqual([TOKEN, SLUG]);
|
||||
});
|
||||
|
||||
it('ignores a malformed token: no lookup, and the row is stored without one', async () => {
|
||||
const conn = makeConn();
|
||||
mockGetConnection.mockResolvedValue(conn);
|
||||
|
||||
await submitFeedback('sommerkonzert-2026', {...BODY, clientToken: 'not-a-uuid'}, 'hash');
|
||||
|
||||
expect(allQueries(conn).some(([sql]) => sql.startsWith('SELECT submission_id FROM submissions'))).toBe(false);
|
||||
expect(allQueries(conn).some(([sql]) => sql.startsWith(LOOKUP_SQL))).toBe(false);
|
||||
const insert = allQueries(conn).find(([sql]) => sql.startsWith('INSERT INTO submissions'))!;
|
||||
expect(insert[1]).not.toContain('not-a-uuid');
|
||||
expect(insert[1][insert[1].length - 1]).toBeNull();
|
||||
|
||||
Reference in new issue
Block a user