/** * MAP_EXTRA_CONNECT_HOSTS end to end, on the whole app: the operator's own map * hosts reach the Content-Security-Policy the server sends and the app config * the client reads, as one list, so a GL map is only asked to draw what the * browser lets it fetch. Without the variable both stay as they were. */ import { buildApp } from '../../src/bootstrap'; import type { INestApplication } from '@nestjs/common'; import { mapExtraConnectHostsSchema } from '@trek/shared'; import request from 'supertest'; import { afterEach, describe, expect, it, vi } from 'vitest'; let app: INestApplication | undefined; afterEach(async () => { await app?.close(); app = undefined; vi.unstubAllEnvs(); }); /** The connect-src sources and the app config of an app built with `hosts` as MAP_EXTRA_CONNECT_HOSTS. */ async function bootWith(hosts: string) { vi.stubEnv('MAP_EXTRA_CONNECT_HOSTS', hosts); app = await buildApp(); const res = await request(app.getHttpServer()).get('/api/auth/app-config'); const connectSrc = String(res.headers['content-security-policy'] ?? '') .split(';') .map((directive) => directive.trim()) .find((directive) => directive.startsWith('connect-src')) ?.split(/\s+/) .slice(1) ?? []; return { status: res.status, config: res.body as Record, connectSrc }; } describe('the operator map hosts', () => { it('MAPHOST-E2E-001: unset, the app config lists none and the policy names only the shipped hosts', async () => { const { status, config, connectSrc } = await bootWith(''); expect(status).toBe(200); expect(config.map_extra_connect_hosts).toEqual([]); expect(connectSrc).toContain('https://tiles.openfreemap.org'); expect(connectSrc.some((source) => source.includes('example'))).toBe(false); }); it('MAPHOST-E2E-002: set, both carry the same hosts, the config in the shared contract', async () => { const { config, connectSrc } = await bootWith('Tiles.Example.com, *.maps.example.org:8443'); const hosts = mapExtraConnectHostsSchema.parse(config.map_extra_connect_hosts); expect(hosts).toEqual(['tiles.example.com', '*.maps.example.org:8443']); for (const host of hosts) expect(connectSrc).toContain(`https://${host}`); expect(connectSrc).toContain('https://tiles.openfreemap.org'); }); it('MAPHOST-E2E-003: an entry the boot would refuse reaches neither the policy nor the client', async () => { const { config, connectSrc } = await bootWith('tiles.example.com,https://evil.example.com,*.com'); expect(config.map_extra_connect_hosts).toEqual(['tiles.example.com']); expect(connectSrc.filter((source) => source.includes('evil') || source === 'https://*.com')).toEqual([]); }); });