Files
TREK/server/tests/e2e/airports.e2e.test.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

110 lines
4.3 KiB
TypeScript

/**
* Airports module e2e — exercises the migrated /api/airports endpoints through
* the real JwtAuthGuard against a temp SQLite db (seeded via the shared harness).
* The airport service is mocked so the test doesn't depend on the bundled dataset.
*/
import { AirportsModule } from '../../src/nest/airports/airports.module';
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
import { createTestMikroOrmModule } from '../helpers/test-orm';
import { TestUnitOfWorkModule } from '../helpers/test-uow';
import { seedUser, sessionCookie } from './harness';
import { Test } from '@nestjs/testing';
import cookieParser from 'cookie-parser';
import type { Server } from 'http';
import request from 'supertest';
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
const { db } = vi.hoisted(() => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const Database = require('better-sqlite3');
const tmp = new Database(':memory:');
tmp.exec('PRAGMA journal_mode = WAL');
tmp.exec(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL,
email TEXT NOT NULL UNIQUE, role TEXT NOT NULL DEFAULT 'user', password_version INTEGER NOT NULL DEFAULT 0);`);
return { db: tmp };
});
vi.mock('../../src/db/database', () => ({ db, closeDb: () => {}, reinitialize: () => {} }));
const { mockSearch, mockFindByIata } = vi.hoisted(() => ({ mockSearch: vi.fn(), mockFindByIata: vi.fn() }));
vi.mock('../../src/nest/airports/airports.data', async (importActual) => {
const actual = await importActual<typeof import('../../src/nest/airports/airports.data')>();
return { ...actual, searchAirports: mockSearch, findByIata: mockFindByIata };
});
const BER = {
iata: 'BER',
icao: 'EDDB',
name: 'Berlin Brandenburg',
city: 'Berlin',
country: 'DE',
lat: 52.36,
lng: 13.5,
tz: 'Europe/Berlin',
};
describe('Airports e2e (real auth guard + temp SQLite)', () => {
let server: Server;
let app: Awaited<ReturnType<typeof build>>;
async function build() {
const moduleRef = await Test.createTestingModule({
// task-6-rereview2.md M4: without a MikroORM in the graph,
// CronRegistrarService.runOnBoot skips the backfill and logs a genuine
// [ERROR] line on every run of this file. createTestMikroOrmModule(db)
// wires one so the boot backfill actually runs here, same as production.
imports: [await TestUnitOfWorkModule.forRoot(db), createTestMikroOrmModule(db), AirportsModule],
}).compile();
const nest = moduleRef.createNestApplication();
nest.use(cookieParser());
nest.useGlobalFilters(new TrekExceptionFilter());
await nest.init();
return nest;
}
beforeAll(async () => {
seedUser(db as never, { id: 1 });
app = await build();
server = app.getHttpServer();
mockSearch.mockReturnValue([BER]);
mockFindByIata.mockImplementation((code: string) => (code === 'BER' ? BER : null));
});
afterAll(async () => {
await app.close();
});
it('401 { error, code } without a session cookie', async () => {
const res = await request(server).get('/api/airports/search').query({ q: 'ber' });
expect(res.status).toBe(401);
expect(res.body).toEqual({ error: 'Access token required', code: 'AUTH_REQUIRED' });
});
it('200 with results for a query', async () => {
const res = await request(server).get('/api/airports/search').set('Cookie', sessionCookie(1)).query({ q: 'ber' });
expect(res.status).toBe(200);
expect(res.body).toEqual([BER]);
});
it('200 [] for a missing query without hitting the service', async () => {
mockSearch.mockClear();
const res = await request(server).get('/api/airports/search').set('Cookie', sessionCookie(1));
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
expect(mockSearch).not.toHaveBeenCalled();
});
it('200 for a known IATA code', async () => {
const res = await request(server).get('/api/airports/BER').set('Cookie', sessionCookie(1));
expect(res.status).toBe(200);
expect(res.body).toEqual(BER);
});
it('404 { error } for an unknown IATA code', async () => {
const res = await request(server).get('/api/airports/ZZZ').set('Cookie', sessionCookie(1));
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Airport not found' });
});
});