diff --git a/src/clients/BookingClient.ts b/src/clients/BookingClient.ts index 55f89d4..e40e077 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,21 @@ 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); + 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) { + 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..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', () => { @@ -19,15 +20,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 }) => { @@ -37,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(); + }); }); 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 }]); }); });