Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/clients/BookingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class BookingClient extends BaseClient {
return auth && this.token ? { Cookie: `token=${this.token}` } : {};
}

list(): Promise<APIResponse> {
return this.request.get('/booking');
list(filter?: { firstname?: string }): Promise<APIResponse> {
return this.request.get('/booking', { params: filter });
}

get(id: number): Promise<APIResponse> {
Expand Down Expand Up @@ -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<void> {
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(', ')}`);
}
}
}
2 changes: 2 additions & 0 deletions tests/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
4 changes: 3 additions & 1 deletion tests/booking.contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
19 changes: 18 additions & 1 deletion tests/booking.crud.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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 }) => {
Expand All @@ -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();
});
});
26 changes: 19 additions & 7 deletions tests/booking.negative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]);
});
});
Loading