mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(api): expose shortlisted places, detached bookings and the bucket list
Reading the surface back against real data turned up three things it dropped without saying so. Places with no day were invisible, because the payload only reached places through day_assignments. On a live instance that is half of them, and they are the ones carrying a coordinate and no claim about when — exactly what an integration can act on. They come back under `unplanned_places`, minus the hotel, which is already reported as an accommodation. Bookings with no day were dropped by the day filter. reservations.day_id is nullable and deleting a day detaches its bookings rather than removing them, so this is not a hypothetical. The bucket list had no endpoint at all. It hangs off the user rather than a trip, so it gets its own, and it answers whether or not the Atlas addon is switched on: the addon decides what TREK shows, not what a key can read. Also fixes ?include=notes returning the bare trip: places, notes and reservations are reported on days, so asking for one now implies days instead of quietly answering with nothing. The module stays a leaf. Reaching for AtlasService pulled AuthModule and the storage registry in behind it and broke the surface's own boot, so the one SELECT lives here like every other query in the file.
This commit is contained in:
@@ -25,6 +25,15 @@ const TRIP = {
|
||||
updated_at: '2026-06-01 10:00:00',
|
||||
};
|
||||
|
||||
const ITEM = {
|
||||
name: 'Hokkaido',
|
||||
lat: 43.06,
|
||||
lng: 141.35,
|
||||
country_code: 'JP',
|
||||
notes: null,
|
||||
target_date: null,
|
||||
};
|
||||
|
||||
/** The limiter always allows unless a test says otherwise. */
|
||||
function makeController(svc: Partial<PublicApiService>, allow = true) {
|
||||
const rl = { check: vi.fn().mockReturnValue(allow) } as unknown as RateLimitService;
|
||||
@@ -47,6 +56,29 @@ function thrown(fn: () => unknown): { status: number; body: unknown } {
|
||||
}
|
||||
|
||||
describe('PublicApiController', () => {
|
||||
describe('GET /api/v1/bucket-list', () => {
|
||||
it('returns the caller’s wishlist and passes the id from the guard, never the query', () => {
|
||||
const listBucketList = vi.fn().mockReturnValue([ITEM]);
|
||||
expect(makeController({ listBucketList }).listBucketList(req(7))).toEqual({ items: [ITEM] });
|
||||
expect(listBucketList).toHaveBeenCalledWith(7);
|
||||
});
|
||||
|
||||
it('401s when the guard left no user behind', () => {
|
||||
const listBucketList = vi.fn();
|
||||
expect(thrown(() => makeController({ listBucketList }).listBucketList(reqWithoutUser()))).toEqual({
|
||||
status: 401,
|
||||
body: { error: 'API token required', code: 'API_TOKEN_REQUIRED' },
|
||||
});
|
||||
expect(listBucketList).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('counts against the same rate budget as everything else', () => {
|
||||
const listBucketList = vi.fn();
|
||||
expect(thrown(() => makeController({ listBucketList }, false).listBucketList(req(7))).status).toBe(429);
|
||||
expect(listBucketList).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/trips', () => {
|
||||
it('returns the accessible trips for the token owner', () => {
|
||||
const listTrips = vi.fn().mockReturnValue([TRIP]);
|
||||
|
||||
@@ -23,7 +23,7 @@ function contextWith(headers: Record<string, string | undefined>) {
|
||||
}
|
||||
|
||||
function makeGuard(verify: (raw: string) => User | null) {
|
||||
return new ApiTokenGuard({ verifyMcpToken: verify } as unknown as TokenService);
|
||||
return new ApiTokenGuard({ verifyApiToken: verify } as unknown as TokenService);
|
||||
}
|
||||
|
||||
/** Run the guard, expecting a refusal; return its { status, body }. */
|
||||
@@ -69,6 +69,15 @@ describe('ApiTokenGuard', () => {
|
||||
expect(verify).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('never reaches for the MCP verifier, so the two credentials cannot swap', () => {
|
||||
const verifyApiToken = vi.fn().mockReturnValue(USER);
|
||||
const verifyMcpToken = vi.fn();
|
||||
const guard = new ApiTokenGuard({ verifyApiToken, verifyMcpToken } as unknown as TokenService);
|
||||
guard.canActivate(contextWith({ authorization: 'Bearer trek_abc123' }));
|
||||
expect(verifyApiToken).toHaveBeenCalledTimes(1);
|
||||
expect(verifyMcpToken).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('401s on a token the store does not know, without saying which part was wrong', () => {
|
||||
const verify = vi.fn().mockReturnValue(null);
|
||||
expect(
|
||||
|
||||
Reference in New Issue
Block a user