mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(share): answer place-photo cache miss with 204 like the maps proxy
This commit is contained in:
@@ -570,24 +570,30 @@ describe('Shared trip — place photos in shared links (issue #1100)', () => {
|
||||
expect(Buffer.from(res.body)).toEqual(photoBytes);
|
||||
});
|
||||
|
||||
it('SHARE-019 — public proxy 404s for a placeId not in the shared trip', async () => {
|
||||
// Every miss shape answers the same empty 204 (fix: mirrors the maps proxy,
|
||||
// #1727 extended to shared pages). The service still collapses "not in
|
||||
// trip" / "bad token" / "map disabled" into one indistinguishable null, so
|
||||
// no authorization detail leaks through the status change.
|
||||
it('SHARE-019 — public proxy answers an empty 204 for a placeId not in the shared trip', async () => {
|
||||
const { token } = await setupSharedPlaceWithPhoto();
|
||||
const res = await request(app).get(`/api/shared/${token}/place-photo/ChIJnotInTrip/bytes`);
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Photo not cached' });
|
||||
expect(res.status).toBe(204);
|
||||
expect(res.headers['cache-control']).toBe('no-store');
|
||||
expect(res.text ?? '').toBe('');
|
||||
});
|
||||
|
||||
it('SHARE-020 — public proxy 404s for an invalid token', async () => {
|
||||
it('SHARE-020 — public proxy answers an empty 204 for an invalid token', async () => {
|
||||
await setupSharedPlaceWithPhoto();
|
||||
const res = await request(app).get(`/api/shared/bad-token/place-photo/${encodeURIComponent(PLACE_ID)}/bytes`);
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Photo not cached' });
|
||||
expect(res.status).toBe(204);
|
||||
expect(res.headers['cache-control']).toBe('no-store');
|
||||
expect(res.text ?? '').toBe('');
|
||||
});
|
||||
|
||||
// Regression — GHSA-9hc8 sibling: place photos are part of the map/itinerary,
|
||||
// so the proxy must 404 when the owner disabled the map, even with a valid
|
||||
// token + cached bytes.
|
||||
it('SHARE-025 — public place-photo proxy 404s when share_map=false', async () => {
|
||||
// so the proxy must stream nothing when the owner disabled the map, even
|
||||
// with a valid token + cached bytes.
|
||||
it('SHARE-025 — public place-photo proxy streams nothing when share_map=false', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hidden Photo Place' });
|
||||
@@ -600,7 +606,7 @@ describe('Shared trip — place photos in shared links (issue #1100)', () => {
|
||||
cachedFilePath = cached.filePath;
|
||||
|
||||
const res = await request(app).get(`/api/shared/${token}/place-photo/${encodeURIComponent(PLACE_ID)}/bytes`);
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Photo not cached' });
|
||||
expect(res.status).toBe(204);
|
||||
expect(res.text ?? '').toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -89,8 +89,9 @@ describe('SharedController', () => {
|
||||
json: vi.fn(),
|
||||
set: vi.fn(),
|
||||
type: vi.fn(),
|
||||
end: vi.fn(),
|
||||
};
|
||||
return r as unknown as Response & { status: ReturnType<typeof vi.fn>; json: ReturnType<typeof vi.fn>; set: ReturnType<typeof vi.fn>; type: ReturnType<typeof vi.fn> };
|
||||
return r as unknown as Response & { status: ReturnType<typeof vi.fn>; json: ReturnType<typeof vi.fn>; set: ReturnType<typeof vi.fn>; type: ReturnType<typeof vi.fn>; end: ReturnType<typeof vi.fn> };
|
||||
}
|
||||
|
||||
// Braced body on purpose: mockReset() returns the mock, and a function
|
||||
@@ -105,11 +106,16 @@ describe('SharedController', () => {
|
||||
return new SharedController(svc({ getSharedPlacePhotoKey: vi.fn().mockReturnValue(key) } as Partial<ShareService>), storageStub);
|
||||
}
|
||||
|
||||
it('404 without streaming when the photo is not cached for the token', async () => {
|
||||
// #1727's rationale extended to public share pages: shared payloads keep
|
||||
// this URL in place image_urls, so an evicted cache entry means one request
|
||||
// per place per shared-page render — an empty 204 instead of 404 noise.
|
||||
it('204 without a body when the photo is not cached for the token', async () => {
|
||||
const res = photoRes();
|
||||
await controller(null).placePhotoBytes('tok', 'p1', res);
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Photo not cached' });
|
||||
expect(res.status).toHaveBeenCalledWith(204);
|
||||
expect(res.end).toHaveBeenCalled();
|
||||
expect(res.json).not.toHaveBeenCalled();
|
||||
expect(res.set).toHaveBeenLastCalledWith('Cache-Control', 'no-store');
|
||||
expect(getStream).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -124,26 +130,30 @@ describe('SharedController', () => {
|
||||
expect(stream.pipe).toHaveBeenCalledWith(res);
|
||||
});
|
||||
|
||||
it('falls back to 404 when the stream cannot be opened (cache-delete race)', async () => {
|
||||
it('falls back to an empty 204 when the stream cannot be opened (cache-delete race)', async () => {
|
||||
getStream.mockRejectedValue(new Error('storage object not found'));
|
||||
const res = photoRes();
|
||||
await controller('abc.jpg').placePhotoBytes('tok', 'p1', res);
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Photo not cached' });
|
||||
expect(res.status).toHaveBeenCalledWith(204);
|
||||
expect(res.end).toHaveBeenCalled();
|
||||
// The hit path already asked for a month of immutable caching — that
|
||||
// header must not survive onto the empty answer.
|
||||
expect(res.set).toHaveBeenLastCalledWith('Cache-Control', 'no-store');
|
||||
});
|
||||
|
||||
it('falls back to 404 when the stream errors before headers were sent', async () => {
|
||||
it('falls back to an empty 204 when the stream errors before headers were sent', async () => {
|
||||
let onError: () => void = () => {};
|
||||
const stream = { on: vi.fn((ev: string, cb: () => void) => { if (ev === 'error') onError = cb; return stream; }), pipe: vi.fn() };
|
||||
getStream.mockResolvedValue({ stream, stat: { key: 'photos/google/abc.jpg', size: 5, mtimeMs: 0 } });
|
||||
const res = photoRes();
|
||||
await controller('abc.jpg').placePhotoBytes('tok', 'p1', res);
|
||||
onError();
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'Photo not cached' });
|
||||
expect(res.status).toHaveBeenCalledWith(204);
|
||||
expect(res.end).toHaveBeenCalled();
|
||||
expect(res.set).toHaveBeenLastCalledWith('Cache-Control', 'no-store');
|
||||
});
|
||||
|
||||
it('does not re-send a 404 when the stream errors after headers were flushed', async () => {
|
||||
it('does not re-send a 204 when the stream errors after headers were flushed', async () => {
|
||||
let onError: () => void = () => {};
|
||||
const stream = { on: vi.fn((ev: string, cb: () => void) => { if (ev === 'error') onError = cb; return stream; }), pipe: vi.fn() };
|
||||
getStream.mockResolvedValue({ stream, stat: { key: 'photos/google/abc.jpg', size: 5, mtimeMs: 0 } });
|
||||
@@ -153,6 +163,7 @@ describe('SharedController', () => {
|
||||
onError();
|
||||
expect(res.status).not.toHaveBeenCalled();
|
||||
expect(res.json).not.toHaveBeenCalled();
|
||||
expect(res.end).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user