From 9e734fdabdfe875b9ccbe4c001c408461f6a2ddc Mon Sep 17 00:00:00 2001 From: Vincent Jerico Date: Mon, 28 Sep 2026 16:04:59 +0800 Subject: [PATCH 1/2] test(booking): assert persisted state after writes and fail on leaked cleanup PUT, PATCH and the invalid-token write only checked the response, so a server that echoed the body without saving it passed. They now read the booking back. The malformed-create test pins the documented 500 and checks nothing was stored, with a valid booking under the same name proving the filter works. The list contract test now looks for a booking it just created. Missing credentials now get the same failure-schema check as bad credentials. cleanup() swallowed failed deletes and leaked bookings onto the shared demo. It now fails teardown and names the bookings it could not remove. --- src/clients/BookingClient.ts | 14 ++++++++++---- tests/auth.spec.ts | 2 ++ tests/booking.contract.spec.ts | 4 +++- tests/booking.crud.spec.ts | 6 +++++- tests/booking.negative.spec.ts | 26 +++++++++++++++++++------- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/clients/BookingClient.ts b/src/clients/BookingClient.ts index 55f89d4..d74c0bf 100644 --- a/src/clients/BookingClient.ts +++ b/src/clients/BookingClient.ts @@ -25,8 +25,8 @@ export class BookingClient extends BaseClient { return auth && this.token ? { Cookie: `token=${this.token}` } : {}; } - list(): Promise { - return this.request.get('/booking'); + list(filter?: { firstname?: string }): Promise { + return this.request.get('/booking', { params: filter }); } get(id: number): Promise { @@ -60,13 +60,19 @@ export class BookingClient extends BaseClient { return res; } - /** Best-effort removal of everything this client created — keeps the shared API tidy. */ + /** Removes everything this client created; throws if any booking is left on the shared API. */ async cleanup(): Promise { + const leaked: number[] = []; for (const id of this.created) { - await this.request + const res = await this.request .delete(`/booking/${id}`, { headers: this.authHeaders(true) }) .catch(() => undefined); + // 404 means the booking is already gone, which is the state cleanup wants. + if (!res?.ok() && res?.status() !== 404) leaked.push(id); } this.created.clear(); + if (leaked.length > 0) { + throw new Error(`cleanup could not delete bookings: ${leaked.join(', ')}`); + } } } diff --git a/tests/auth.spec.ts b/tests/auth.spec.ts index c1ba48b..b8bab49 100644 --- a/tests/auth.spec.ts +++ b/tests/auth.spec.ts @@ -22,6 +22,8 @@ test.describe('Auth', () => { test('missing credentials do not return a token @negative', async ({ authClient }) => { const res = await authClient.createToken({}); + expect(res.status()).toBe(200); + await expect(res).toMatchSchema(AuthFailureSchema); expect(await res.json()).not.toHaveProperty('token'); }); }); diff --git a/tests/booking.contract.spec.ts b/tests/booking.contract.spec.ts index fb62441..7012de8 100644 --- a/tests/booking.contract.spec.ts +++ b/tests/booking.contract.spec.ts @@ -7,10 +7,12 @@ import { import { buildBooking } from '../src/data/bookingFactory.js'; test.describe('Booking contract @contract', () => { - test('GET /booking returns a list of booking ids', async ({ bookingClient }) => { + test('GET /booking lists the id of a booking just created', async ({ bookingClient }) => { + const { bookingid } = await (await bookingClient.create(buildBooking())).json(); const res = await bookingClient.list(); expect(res.status()).toBe(200); await expect(res).toMatchSchema(BookingIdListSchema); + expect(await res.json()).toContainEqual({ bookingid }); }); test('POST /booking returns the created-booking envelope', async ({ bookingClient }) => { diff --git a/tests/booking.crud.spec.ts b/tests/booking.crud.spec.ts index d0e6bc2..df10973 100644 --- a/tests/booking.crud.spec.ts +++ b/tests/booking.crud.spec.ts @@ -19,15 +19,19 @@ test.describe('Booking CRUD', () => { const res = await bookingClient.update(bookingid, replacement); expect(res.status()).toBe(200); expect(await res.json()).toEqual(replacement); + expect(await (await bookingClient.get(bookingid)).json()).toEqual(replacement); }); test('PATCH changes only the given fields', async ({ bookingClient }) => { const original = buildBooking(); const { bookingid } = await (await bookingClient.create(original)).json(); + const patched = { ...original, firstname: 'Patched' }; + const res = await bookingClient.patch(bookingid, { firstname: 'Patched' }); expect(res.status()).toBe(200); - expect(await res.json()).toEqual({ ...original, firstname: 'Patched' }); + expect(await res.json()).toEqual(patched); + expect(await (await bookingClient.get(bookingid)).json()).toEqual(patched); }); test('DELETE removes the booking', async ({ bookingClient }) => { diff --git a/tests/booking.negative.spec.ts b/tests/booking.negative.spec.ts index ae5c89f..89f58af 100644 --- a/tests/booking.negative.spec.ts +++ b/tests/booking.negative.spec.ts @@ -26,16 +26,28 @@ test.describe('Booking negative & access control @negative', () => { expect((await bookingClient.get(bookingid)).status()).toBe(200); }); - test('an invalid token is rejected', async ({ request, bookingClient }) => { - const { bookingid } = await (await bookingClient.create(buildBooking())).json(); + test('an invalid token is rejected and changes nothing', async ({ request, bookingClient }) => { + const original = buildBooking(); + const { bookingid } = await (await bookingClient.create(original)).json(); const intruder = new BookingClient(request, 'not-a-real-token'); - expect((await intruder.update(bookingid, buildBooking())).status()).toBe(403); + const res = await intruder.update(bookingid, buildBooking({ firstname: 'Hijack' })); + expect(res.status()).toBe(403); + expect(await (await bookingClient.get(bookingid)).json()).toEqual(original); }); - test('a malformed create is rejected', async ({ bookingClient }) => { - const res = await bookingClient.create({ firstname: 'OnlyName' }); - // Documented quirk: the API returns 500 here; a well-behaved API would return 400. - expect(res.status()).toBeGreaterThanOrEqual(400); + test('a malformed create is rejected and stores nothing', async ({ bookingClient }) => { + test.info().annotations.push({ + type: 'issue', + description: 'Malformed POST /booking returns 500; a well-behaved API would return 400.', + }); + const firstname = `Malformed-${crypto.randomUUID()}`; + + expect((await bookingClient.create({ firstname })).status()).toBe(500); + + // A valid booking under the same name proves the firstname filter finds matches, so the + // exact list below shows the malformed body was not stored. + const { bookingid } = await (await bookingClient.create(buildBooking({ firstname }))).json(); + expect(await (await bookingClient.list({ firstname })).json()).toEqual([{ bookingid }]); }); }); From 0caf9b0a369a99c10ffeda2c463318757e16a324 Mon Sep 17 00:00:00 2001 From: Vincent Jerico Date: Tue, 29 Sep 2026 13:35:33 +0800 Subject: [PATCH 2/2] fix(client): don't report an already-deleted booking as leaked cleanup() treated a 404 on DELETE as "already gone", but Restful-Booker answers DELETE on a missing booking with 405 (GET returns 404). A booking wiped by the demo API's periodic reset therefore made cleanup throw and fail the test in teardown with no real leak. When DELETE doesn't succeed, cleanup now asks GET: 404 means gone, anything else still counts as leaked. Adds a regression test that deletes a booking behind the client's back and requires cleanup() to succeed. It failed before this change with "cleanup could not delete bookings: ". --- src/clients/BookingClient.ts | 6 ++++-- tests/booking.crud.spec.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/clients/BookingClient.ts b/src/clients/BookingClient.ts index d74c0bf..e40e077 100644 --- a/src/clients/BookingClient.ts +++ b/src/clients/BookingClient.ts @@ -67,8 +67,10 @@ export class BookingClient extends BaseClient { const res = await this.request .delete(`/booking/${id}`, { headers: this.authHeaders(true) }) .catch(() => undefined); - // 404 means the booking is already gone, which is the state cleanup wants. - if (!res?.ok() && res?.status() !== 404) leaked.push(id); + if (res?.ok()) continue; + // DELETE on a booking that is already gone returns 405 here, not 404, so ask GET instead. + const check = await this.request.get(`/booking/${id}`).catch(() => undefined); + if (check?.status() !== 404) leaked.push(id); } this.created.clear(); if (leaked.length > 0) { diff --git a/tests/booking.crud.spec.ts b/tests/booking.crud.spec.ts index df10973..5174fca 100644 --- a/tests/booking.crud.spec.ts +++ b/tests/booking.crud.spec.ts @@ -1,4 +1,5 @@ import { test, expect } from '../src/fixtures/api.js'; +import { BookingClient } from '../src/clients/BookingClient.js'; import { buildBooking } from '../src/data/bookingFactory.js'; test.describe('Booking CRUD', () => { @@ -41,4 +42,16 @@ test.describe('Booking CRUD', () => { expect((await bookingClient.delete(bookingid)).status()).toBe(201); expect((await bookingClient.get(bookingid)).status()).toBe(404); }); + + test('cleanup tolerates a booking that is already gone', async ({ + request, + token, + bookingClient, + }) => { + const { bookingid } = await (await bookingClient.create(buildBooking())).json(); + // Delete it behind this client's back, the way the demo API's periodic reset does. + expect((await new BookingClient(request, token).delete(bookingid)).ok()).toBe(true); + + await expect(bookingClient.cleanup()).resolves.toBeUndefined(); + }); });