mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
Merge dev into feat/native-apps
Brings the native apps branch up to dev (NestJS-only server, MikroORM
repositories, tracked sessions, per-domain locales, the lint ratchets) and
ports its new code onto that architecture:
- server: /api/auth/native moves onto injected services. The session is
minted by the auth domain's new LoginSessionService (login bookkeeping and
SessionsService.issue in one transaction), the one-time codes live behind a
NativeHandoffStore port with a capped in-memory implementation that drops
expired codes on write instead of running a timer, both routes declare
response contracts, and the exchange answers { success: true }. Unit and
e2e suites run on createTestModule and the ORM factories.
- shared: native strings for az, et, sk and th, native wired into every
locale, the address placeholder as a full URL.
- client: the account and export changes go into dev's shared hooks
(useAccountSettings, useTripExport), the native server card is a
desktop/phone pair on useNativeServer, the hand-off call moves out of the
page hook into native/, best-effort native failures go through
reportError instead of the console, and the address screen is known to
knip and lint:i18n-keys.
- package-lock.json regenerated on Linux.
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { Photos } from '../../../src/db/entities/Photos.entity';
|
||||
import { ShareTokens } from '../../../src/db/entities/ShareTokens.entity';
|
||||
import {
|
||||
applyPlatformUploads,
|
||||
applyPlatformSpa,
|
||||
applyPlatformStatic,
|
||||
storageStaticHandler,
|
||||
isBuildFilePath,
|
||||
PUBLIC_DIR,
|
||||
} from '../../../src/nest/platform/platform.routes';
|
||||
import { SpaFallbackFilter } from '../../../src/nest/platform/spa-fallback.filter';
|
||||
import type { StorageService } from '../../../src/nest/storage/storage.service';
|
||||
import { StorageNotFoundError, StorageInvalidKeyError } from '../../../src/nest/storage/storage.types';
|
||||
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
import { createUser, createTrip } from '../../helpers/factories';
|
||||
import { deleteRows, insertRow } from '../../helpers/factories/rows';
|
||||
import { makeShareToken } from '../../helpers/factories/trips';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
|
||||
import { describe, it, expect, vi, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
// --- hoisted mock fns so the vi.mock factories can reference them -----------------
|
||||
const h = vi.hoisted(() => ({
|
||||
verifyJwtAndLoadUser: vi.fn(),
|
||||
@@ -9,19 +28,9 @@ const h = vi.hoisted(() => ({
|
||||
sendToResponse: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/nest/auth/jwt-verify', () => ({ verifyJwtAndLoadUser: h.verifyJwtAndLoadUser }));
|
||||
vi.mock('../../../src/nest/auth-core/jwt-verify', () => ({ verifyJwtAndLoadUser: h.verifyJwtAndLoadUser }));
|
||||
vi.mock('../../../src/db/database', () => ({ db: { prepare: h.dbPrepare } }));
|
||||
|
||||
import {
|
||||
applyPlatformUploads,
|
||||
applyPlatformSpa,
|
||||
applyPlatformStatic,
|
||||
storageStaticHandler,
|
||||
} from '../../../src/nest/platform/platform.routes';
|
||||
import { SpaFallbackFilter } from '../../../src/nest/platform/spa-fallback.filter';
|
||||
import { StorageNotFoundError, StorageInvalidKeyError } from '../../../src/nest/storage/storage.types';
|
||||
import type { StorageService } from '../../../src/nest/storage/storage.service';
|
||||
|
||||
// The serving swap addresses files as (category, name) on the injected facade;
|
||||
// these unit tests only assert routing/auth/error mapping, so a two-method stub
|
||||
// is the whole storage surface.
|
||||
@@ -44,13 +53,15 @@ type Handler = (...args: unknown[]) => unknown;
|
||||
*/
|
||||
function fakeApp() {
|
||||
const calls: Array<{ method: string; path?: string; handlers: Handler[] }> = [];
|
||||
const record = (method: string) => (...args: unknown[]) => {
|
||||
if (typeof args[0] === 'string' || args[0] instanceof RegExp) {
|
||||
calls.push({ method, path: String(args[0]), handlers: args.slice(1) as Handler[] });
|
||||
} else {
|
||||
calls.push({ method, handlers: args as Handler[] });
|
||||
}
|
||||
};
|
||||
const record =
|
||||
(method: string) =>
|
||||
(...args: unknown[]) => {
|
||||
if (typeof args[0] === 'string' || args[0] instanceof RegExp) {
|
||||
calls.push({ method, path: String(args[0]), handlers: args.slice(1) as Handler[] });
|
||||
} else {
|
||||
calls.push({ method, handlers: args as Handler[] });
|
||||
}
|
||||
};
|
||||
const app = {
|
||||
use: record('use'),
|
||||
get: record('get'),
|
||||
@@ -65,16 +76,50 @@ function makeRes() {
|
||||
statusCode: 200,
|
||||
body: undefined as unknown,
|
||||
headers: {} as Record<string, string>,
|
||||
status: vi.fn(function (this: typeof res, c: number) { this.statusCode = c; return this; }),
|
||||
json: vi.fn(function (this: typeof res, b: unknown) { this.body = b; return this; }),
|
||||
send: vi.fn(function (this: typeof res, b: unknown) { this.body = b; return this; }),
|
||||
end: vi.fn(function (this: typeof res) { return this; }),
|
||||
sendFile: vi.fn(function (this: typeof res, p: string) { this.body = `FILE:${p}`; return this; }),
|
||||
setHeader: vi.fn(function (this: typeof res, k: string, v: string) { this.headers[k] = v; return this; }),
|
||||
status: vi.fn(function (this: typeof res, c: number) {
|
||||
this.statusCode = c;
|
||||
return this;
|
||||
}),
|
||||
json: vi.fn(function (this: typeof res, b: unknown) {
|
||||
this.body = b;
|
||||
return this;
|
||||
}),
|
||||
send: vi.fn(function (this: typeof res, b: unknown) {
|
||||
this.body = b;
|
||||
return this;
|
||||
}),
|
||||
end: vi.fn(function (this: typeof res) {
|
||||
return this;
|
||||
}),
|
||||
sendFile: vi.fn(function (this: typeof res, p: string) {
|
||||
this.body = `FILE:${p}`;
|
||||
return this;
|
||||
}),
|
||||
setHeader: vi.fn(function (this: typeof res, k: string, v: string) {
|
||||
this.headers[k] = v;
|
||||
return this;
|
||||
}),
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
// Task 0 (D6): applyPlatformUploads now wraps servePhoto in withRequestContext,
|
||||
// so every call site below needs a real `{ em }` to hand it — none of these
|
||||
// cases touch the ORM (jwt-verify and db/database are both mocked above), but
|
||||
// RequestContext.create needs a genuine EntityManager to open the ALS scope
|
||||
// around, not a hand-built stub.
|
||||
const uploadsTestDb = createSnapshotTestDb();
|
||||
let uploadsOrm: TestOrm;
|
||||
|
||||
beforeAll(async () => {
|
||||
uploadsOrm = await createTestOrm(uploadsTestDb);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await uploadsOrm.close();
|
||||
uploadsTestDb.close();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
@@ -82,7 +127,7 @@ beforeEach(() => {
|
||||
describe('applyPlatformUploads', () => {
|
||||
it('registers the four static mounts + the files block', () => {
|
||||
const { app, calls } = fakeApp();
|
||||
applyPlatformUploads(app, storage);
|
||||
applyPlatformUploads(app, storage, uploadsOrm.orm);
|
||||
const paths = calls.filter((c) => c.method === 'use').map((c) => c.path);
|
||||
expect(paths).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -97,7 +142,7 @@ describe('applyPlatformUploads', () => {
|
||||
|
||||
it('the /uploads/files block always answers 401', () => {
|
||||
const { app, calls } = fakeApp();
|
||||
applyPlatformUploads(app, storage);
|
||||
applyPlatformUploads(app, storage, uploadsOrm.orm);
|
||||
const filesBlock = calls.find((c) => c.path === '/uploads/files')!.handlers[0];
|
||||
const res = makeRes();
|
||||
filesBlock({}, res);
|
||||
@@ -108,7 +153,7 @@ describe('applyPlatformUploads', () => {
|
||||
describe('GET /uploads/photos/:filename', () => {
|
||||
function photoHandler() {
|
||||
const { app, calls } = fakeApp();
|
||||
applyPlatformUploads(app, storage);
|
||||
applyPlatformUploads(app, storage, uploadsOrm.orm);
|
||||
return calls.find((c) => c.method === 'get' && c.path === '/uploads/photos/:filename')!.handlers[0];
|
||||
}
|
||||
const next = vi.fn();
|
||||
@@ -159,7 +204,12 @@ describe('applyPlatformUploads', () => {
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(h.verifyJwtAndLoadUser).toHaveBeenCalledWith('jwt123');
|
||||
expect(h.verifyJwtAndLoadUser).toHaveBeenCalledWith(
|
||||
'jwt123',
|
||||
expect.objectContaining({ findByIdWithPasswordVersion: expect.any(Function) }),
|
||||
// the session gate's lookup (UserSessionsRepository)
|
||||
expect.objectContaining({ findActive: expect.any(Function), touchLastSeen: expect.any(Function) }),
|
||||
);
|
||||
expect(h.sendToResponse).toHaveBeenCalledWith('photos', 'a.jpg', res);
|
||||
});
|
||||
|
||||
@@ -169,7 +219,12 @@ describe('applyPlatformUploads', () => {
|
||||
h.verifyJwtAndLoadUser.mockReturnValue({ id: 1 });
|
||||
const res = makeRes();
|
||||
await photoHandler()({ params: { filename: 'a.jpg' }, headers: {}, query: { token: 'qtok' } }, res, next);
|
||||
expect(h.verifyJwtAndLoadUser).toHaveBeenCalledWith('qtok');
|
||||
expect(h.verifyJwtAndLoadUser).toHaveBeenCalledWith(
|
||||
'qtok',
|
||||
expect.objectContaining({ findByIdWithPasswordVersion: expect.any(Function) }),
|
||||
// the session gate's lookup (UserSessionsRepository)
|
||||
expect.objectContaining({ findActive: expect.any(Function), touchLastSeen: expect.any(Function) }),
|
||||
);
|
||||
expect(h.sendToResponse).toHaveBeenCalledWith('photos', 'a.jpg', res);
|
||||
});
|
||||
|
||||
@@ -182,42 +237,134 @@ describe('applyPlatformUploads', () => {
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
// R1/R5 (Plan 3h Task 6, R1 (Plan 4 Task 1)): both the share-token lookup
|
||||
// AND the sibling `photos` read now go through the SAME real ORM
|
||||
// (`ShareTokensRepository.findTripIdByToken` / `PhotosRepository
|
||||
// .findTripIdByFilename`, `orm.em.getRepository(...)` inside the SAME
|
||||
// `withRequestContext` wrap `applyPlatformUploads` already uses) — so
|
||||
// every case here seeds REAL rows in `uploadsTestDb` (bound to
|
||||
// `uploadsOrm.orm`) rather than mocking `db.prepare`'s return value; the
|
||||
// `h.dbPrepare` stub is dead for this whole describe block now.
|
||||
async function insertShareToken(tripId: number, userId: number, token: string, expiresAt: string | null = null) {
|
||||
await makeShareToken(uploadsOrm, tripId, userId, { token, expires_at: expiresAt });
|
||||
}
|
||||
|
||||
async function insertPhoto(tripId: number, filename: string) {
|
||||
await insertRow(uploadsOrm, Photos, { trip: tripId, filename, original_name: filename });
|
||||
}
|
||||
|
||||
it('401 when a share token does not cover the photo trip', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
const otherTrip = createTrip(uploadsTestDb, user.id);
|
||||
await insertShareToken(otherTrip.id, user.id, 'share-mismatch');
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
const photoStmt = { get: vi.fn().mockReturnValue({ trip_id: 7 }) };
|
||||
const shareStmt = { get: vi.fn().mockReturnValue({ trip_id: 8 }) };
|
||||
h.dbPrepare.mockImplementationOnce(() => photoStmt).mockImplementationOnce(() => shareStmt);
|
||||
const res = makeRes();
|
||||
await photoHandler()({ params: { filename: 'a.jpg' }, headers: {}, query: { token: 'share1' } }, res, next);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('401 when there is no matching share token at all', async () => {
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
const photoStmt = { get: vi.fn().mockReturnValue({ trip_id: 7 }) };
|
||||
const shareStmt = { get: vi.fn().mockReturnValue(undefined) };
|
||||
h.dbPrepare.mockImplementationOnce(() => photoStmt).mockImplementationOnce(() => shareStmt);
|
||||
const res = makeRes();
|
||||
await photoHandler()({ params: { filename: 'a.jpg' }, headers: {}, query: { token: 'share1' } }, res, next);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('serves the file when the share token covers the photo trip', async () => {
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.sendToResponse.mockResolvedValue(undefined);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
const photoStmt = { get: vi.fn().mockReturnValue({ trip_id: 7 }) };
|
||||
const shareStmt = { get: vi.fn().mockReturnValue({ trip_id: 7 }) };
|
||||
h.dbPrepare.mockImplementationOnce(() => photoStmt).mockImplementationOnce(() => shareStmt);
|
||||
await insertPhoto(photoTrip.id, 'photo-mismatch.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'a.jpg' }, headers: { authorization: 'Bearer share1' }, query: {} },
|
||||
{ params: { filename: 'photo-mismatch.jpg' }, headers: {}, query: { token: 'share-mismatch' } },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(h.sendToResponse).toHaveBeenCalledWith('photos', 'a.jpg', res);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('R5: 401 when there is no matching share token at all (unknown)', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
await insertPhoto(photoTrip.id, 'photo-unknown-token.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'photo-unknown-token.jpg' }, headers: {}, query: { token: 'never-issued' } },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('R5: 401 when the token is revoked (deleted, not merely unknown)', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
await insertShareToken(photoTrip.id, user.id, 'share-revoked');
|
||||
await deleteRows(uploadsOrm, ShareTokens, { token: 'share-revoked' });
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
await insertPhoto(photoTrip.id, 'photo-revoked.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'photo-revoked.jpg' }, headers: {}, query: { token: 'share-revoked' } },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('R5: 401 when the token is expired', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
await insertShareToken(photoTrip.id, user.id, 'share-expired', '2020-01-01 00:00:00');
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
await insertPhoto(photoTrip.id, 'photo-expired.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'photo-expired.jpg' }, headers: {}, query: { token: 'share-expired' } },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('R5: 401 for a wrong-case token — no case-folding is introduced', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
await insertShareToken(photoTrip.id, user.id, 'share-CaseSensitive');
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
await insertPhoto(photoTrip.id, 'photo-case.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'photo-case.jpg' }, headers: {}, query: { token: 'share-casesensitive' } },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('R5: 401 for a token with an embedded NUL byte', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
await insertShareToken(photoTrip.id, user.id, 'share-nul');
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
await insertPhoto(photoTrip.id, 'photo-nul.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'photo-nul.jpg' }, headers: {}, query: { token: 'share-nul\0extra' } },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(res.statusCode).toBe(401);
|
||||
});
|
||||
|
||||
it('serves the file when the share token covers the photo trip (R1 valid-token case)', async () => {
|
||||
const { user } = createUser(uploadsTestDb);
|
||||
const photoTrip = createTrip(uploadsTestDb, user.id);
|
||||
await insertShareToken(photoTrip.id, user.id, 'share-valid');
|
||||
h.exists.mockResolvedValue(true);
|
||||
h.sendToResponse.mockResolvedValue(undefined);
|
||||
h.verifyJwtAndLoadUser.mockReturnValue(null);
|
||||
await insertPhoto(photoTrip.id, 'photo-valid.jpg');
|
||||
const res = makeRes();
|
||||
await photoHandler()(
|
||||
{ params: { filename: 'photo-valid.jpg' }, headers: { authorization: 'Bearer share-valid' }, query: {} },
|
||||
res,
|
||||
next,
|
||||
);
|
||||
expect(h.sendToResponse).toHaveBeenCalledWith('photos', 'photo-valid.jpg', res);
|
||||
});
|
||||
|
||||
it('404 when the object vanishes between the exists check and the send', async () => {
|
||||
@@ -355,7 +502,9 @@ describe('storageStaticHandler', () => {
|
||||
|
||||
describe('applyPlatformStatic', () => {
|
||||
const original = process.env.NODE_ENV;
|
||||
afterEach(() => { process.env.NODE_ENV = original; });
|
||||
afterEach(() => {
|
||||
process.env.NODE_ENV = original;
|
||||
});
|
||||
|
||||
it('is a no-op outside production', () => {
|
||||
process.env.NODE_ENV = 'development';
|
||||
@@ -408,13 +557,15 @@ describe('applyPlatformStatic', () => {
|
||||
|
||||
describe('applyPlatformSpa', () => {
|
||||
const original = process.env.NODE_ENV;
|
||||
afterEach(() => { process.env.NODE_ENV = original; });
|
||||
afterEach(() => {
|
||||
process.env.NODE_ENV = original;
|
||||
});
|
||||
|
||||
it('only serves statics (no catch-all) outside production', () => {
|
||||
process.env.NODE_ENV = 'development';
|
||||
const { app, calls } = fakeApp();
|
||||
applyPlatformSpa(app);
|
||||
expect(calls.some((c) => c.method === 'get' && c.path === '/.*/' )).toBe(false);
|
||||
expect(calls.some((c) => c.method === 'get' && c.path === '/.*/')).toBe(false);
|
||||
});
|
||||
|
||||
it('registers the index.html catch-all in production', () => {
|
||||
@@ -424,33 +575,114 @@ describe('applyPlatformSpa', () => {
|
||||
const catchAll = calls.find((c) => c.method === 'get');
|
||||
expect(catchAll).toBeDefined();
|
||||
const res = makeRes();
|
||||
catchAll!.handlers[0]({}, res);
|
||||
catchAll!.handlers[0]({ path: '/trips/7' }, res);
|
||||
expect(res.headers['Cache-Control']).toBe('no-cache, no-store, must-revalidate');
|
||||
expect(String(res.body)).toContain('FILE:');
|
||||
expect(String(res.body)).toContain('index.html');
|
||||
expect(res.sendFile).toHaveBeenCalledWith('index.html', { root: PUBLIC_DIR });
|
||||
});
|
||||
|
||||
it('answers a missing build file with a 404, not index.html (#2524)', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const { app, calls } = fakeApp();
|
||||
applyPlatformSpa(app);
|
||||
const catchAll = calls.find((c) => c.method === 'get');
|
||||
const res = makeRes();
|
||||
catchAll!.handlers[0]({ path: '/assets/DashboardPage-v4ODOTOr.js' }, res);
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Not Found' });
|
||||
expect(res.headers['Cache-Control']).toBe('no-store');
|
||||
expect(res.sendFile).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isBuildFilePath', () => {
|
||||
it.each([
|
||||
'/assets/DashboardPage-v4ODOTOr.js',
|
||||
'/assets/index-cFGeBWen.css',
|
||||
'/assets/poppins-latin-400-normal-abc.woff2',
|
||||
'/assets/anything-without-a-known-extension',
|
||||
'/sw.js',
|
||||
'/workbox-2da51cb1.js',
|
||||
'/registerSW.js',
|
||||
'/manifest.webmanifest',
|
||||
'/icons/icon-192x192.png',
|
||||
'/favicon.ico',
|
||||
'/fonts/Inter.TTF',
|
||||
])('treats %s as a build file', (p) => {
|
||||
expect(isBuildFilePath(p)).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
'/',
|
||||
'/dashboard',
|
||||
'/trips/12/files',
|
||||
'/journey/3/studio',
|
||||
'/plugins/trip-todos',
|
||||
'/help/Atlas',
|
||||
'/shared/0f3a9c',
|
||||
'/public/journey/AbC_-12',
|
||||
'/oauth/consent',
|
||||
])('treats the page %s as a page', (p) => {
|
||||
expect(isBuildFilePath(p)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SpaFallbackFilter', () => {
|
||||
const original = process.env.NODE_ENV;
|
||||
afterEach(() => { process.env.NODE_ENV = original; });
|
||||
afterEach(() => {
|
||||
process.env.NODE_ENV = original;
|
||||
});
|
||||
|
||||
function host(req: { method: string }, res: ReturnType<typeof makeRes>) {
|
||||
function host(req: { method: string; path?: string }, res: ReturnType<typeof makeRes>) {
|
||||
return { switchToHttp: () => ({ getRequest: () => req, getResponse: () => res }) } as never;
|
||||
}
|
||||
|
||||
it('serves index.html for an unmatched GET in production', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const res = makeRes();
|
||||
new SpaFallbackFilter().catch(new NotFoundException('nope'), host({ method: 'GET' }, res));
|
||||
new SpaFallbackFilter().catch(new NotFoundException('nope'), host({ method: 'GET', path: '/dashboard' }, res));
|
||||
expect(res.headers['Cache-Control']).toBe('no-cache, no-store, must-revalidate');
|
||||
expect(String(res.body)).toContain('index.html');
|
||||
});
|
||||
|
||||
it('answers a GET for a chunk that is not on disk with a 404 in production (#2524)', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const res = makeRes();
|
||||
new SpaFallbackFilter().catch(
|
||||
new NotFoundException('Cannot GET /assets/DashboardPage-v4ODOTOr.js'),
|
||||
host({ method: 'GET', path: '/assets/DashboardPage-v4ODOTOr.js' }, res),
|
||||
);
|
||||
// A 200 with index.html here is what a service worker precached as the chunk.
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Cannot GET /assets/DashboardPage-v4ODOTOr.js' });
|
||||
expect(res.headers['Cache-Control']).toBe('no-store');
|
||||
expect(res.sendFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('answers a missing top-level build file with a 404 too', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const res = makeRes();
|
||||
new SpaFallbackFilter().catch(new NotFoundException(), host({ method: 'GET', path: '/workbox-0000aaaa.js' }, res));
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Not Found' });
|
||||
});
|
||||
|
||||
it('answers a missing build file whose exception has no message with Not Found (#2524)', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const res = makeRes();
|
||||
const exc = new NotFoundException();
|
||||
Object.defineProperty(exc, 'message', { value: '' });
|
||||
new SpaFallbackFilter().catch(exc, host({ method: 'GET', path: '/assets/gone-0000aaaa.js' }, res));
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Not Found' });
|
||||
expect(res.sendFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps the JSON 404 envelope for a non-GET miss in production', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const res = makeRes();
|
||||
new SpaFallbackFilter().catch(new NotFoundException('gone'), host({ method: 'POST' }, res));
|
||||
new SpaFallbackFilter().catch(new NotFoundException('gone'), host({ method: 'POST', path: '/dashboard' }, res));
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'gone' });
|
||||
});
|
||||
@@ -458,7 +690,7 @@ describe('SpaFallbackFilter', () => {
|
||||
it('keeps the JSON 404 envelope outside production even for GET', () => {
|
||||
process.env.NODE_ENV = 'development';
|
||||
const res = makeRes();
|
||||
new SpaFallbackFilter().catch(new NotFoundException('missing'), host({ method: 'GET' }, res));
|
||||
new SpaFallbackFilter().catch(new NotFoundException('missing'), host({ method: 'GET', path: '/dashboard' }, res));
|
||||
expect(res.statusCode).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'missing' });
|
||||
});
|
||||
@@ -469,7 +701,7 @@ describe('SpaFallbackFilter', () => {
|
||||
const exc = new NotFoundException();
|
||||
// force an empty message so the || branch is taken
|
||||
Object.defineProperty(exc, 'message', { value: '' });
|
||||
new SpaFallbackFilter().catch(exc, host({ method: 'GET' }, res));
|
||||
new SpaFallbackFilter().catch(exc, host({ method: 'GET', path: '/dashboard' }, res));
|
||||
expect(res.body).toEqual({ error: 'Not Found' });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user