mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
feat(api): rate-limit /api/v1 and report when a trip last changed
Two gaps from the first pass, both about a client that polls. Rate limiting is keyed by user rather than by IP. A self-hosted integration and its owner's browser routinely leave the same address, and an IP bucket would let one starve the other; 120 requests a minute is generous for a sync surface and still bounded, so a runaway poll degrades its own integration instead of the instance. updated_at now ships with every trip, and its docblock says plainly what it is not: a content timestamp. It moves when the trip's own fields change — title, dates, currency, cover — and stays put when a place, a day or a note is edited, because the child tables carry no updated_at of their own. That also rules out the ?since= filter this was meant to enable: it would silently hide every trip whose itinerary changed, which is worse than no filter at all. Making it honest needs a schema change, not an API field, so the field is documented for what it is and the filter is left out.
This commit is contained in:
@@ -12,6 +12,7 @@ import type { Request } from 'express';
|
||||
import { PUBLIC_API_INCLUDES } from '@trek/shared';
|
||||
import { PublicApiController } from '../../../src/nest/public-api/public-api.controller';
|
||||
import type { PublicApiService } from '../../../src/nest/public-api/public-api.service';
|
||||
import type { RateLimitService } from '../../../src/nest/common/rate-limit.service';
|
||||
|
||||
const TRIP = {
|
||||
id: 12,
|
||||
@@ -21,10 +22,13 @@ const TRIP = {
|
||||
end_date: '2026-06-22',
|
||||
currency: 'EUR',
|
||||
archived: false,
|
||||
updated_at: '2026-06-01 10:00:00',
|
||||
};
|
||||
|
||||
function makeController(svc: Partial<PublicApiService>) {
|
||||
return new PublicApiController(svc as PublicApiService);
|
||||
/** 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;
|
||||
return new PublicApiController(svc as PublicApiService, rl);
|
||||
}
|
||||
|
||||
const req = (userId = 7) => ({ user: { id: userId } }) as Request;
|
||||
@@ -125,4 +129,19 @@ describe('PublicApiController', () => {
|
||||
expect(getTrip).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('rate limiting', () => {
|
||||
it('429s the list once the budget is spent, without reaching the service', () => {
|
||||
const listTrips = vi.fn();
|
||||
const res = thrown(() => makeController({ listTrips }, false).listTrips(req(7)));
|
||||
expect(res).toEqual({ status: 429, body: { error: 'Too many requests. Please slow down.' } });
|
||||
expect(listTrips).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('429s the detail route the same way', () => {
|
||||
const getTrip = vi.fn();
|
||||
expect(thrown(() => makeController({ getTrip }, false).getTrip(req(7), '12', undefined)).status).toBe(429);
|
||||
expect(getTrip).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user