/** * The map provider a managed install shows, on the whole app (both engines: the * Postgres lane runs this suite too). A managed install with the operator's * Mapbox token shows Mapbox in place of an empty provider and in place of a * Leaflet nobody chose, as it always has; a Leaflet the user or the admin picked * stays Leaflet. Whether it was picked is a mark only the server sets: on a * write that changes the provider shown, or when the admin sets the default. No * stored row is rewritten, and no client can set or clear a mark. */ import { buildApp } from '../../src/bootstrap'; import { AppSettings } from '../../src/db/entities/AppSettings.entity'; import { MAP_PROVIDER_CHOSEN_KEY } from '../../src/nest/settings/settings.service'; import { deleteRows } from '../helpers/factories/rows'; import { readAppSetting, readUserSetting, setAppSetting, setUserSetting } from '../helpers/factories/settings'; import { makeAdmin, makeUser } from '../helpers/factories/users'; import { sessionCookie } from './harness'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import request from 'supertest'; import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; let app: INestApplication; let orm: MikroORM; let adminId: number; const ADMIN_DEFAULT = 'default_user_setting_map_provider'; const ADMIN_MARK = 'map_provider_default_chosen'; beforeAll(async () => { app = await buildApp(); orm = app.get(MikroORM); adminId = (await makeAdmin(orm)).user.id; }, 60_000); beforeEach(async () => { vi.stubEnv('TREK_MANAGED', 'true'); vi.stubEnv('MAPBOX_ACCESS_TOKEN', 'pk.operator'); await deleteRows(orm, AppSettings, { key: { $in: [ADMIN_DEFAULT, ADMIN_MARK] } }); }); afterAll(async () => { vi.unstubAllEnvs(); await app?.close(); }); async function newUser(): Promise { return (await makeUser(orm)).user.id; } async function shownProvider(userId: number): Promise { const res = await request(app.getHttpServer()).get('/api/settings').set('Cookie', sessionCookie(userId)); expect(res.status).toBe(200); return (res.body as { settings: Record }).settings.map_provider; } function saveOne(userId: number, key: string, value: unknown) { return request(app.getHttpServer()).put('/api/settings').set('Cookie', sessionCookie(userId)).send({ key, value }); } function saveAll(userId: number, settings: Record) { return request(app.getHttpServer()) .post('/api/settings/bulk') .set('Cookie', sessionCookie(userId)) .send({ settings }); } function setAdminDefault(value: string | null) { return request(app.getHttpServer()) .put('/api/admin/default-user-settings') .set('Cookie', sessionCookie(adminId)) .send({ map_provider: value }); } describe('the provider a managed install shows', () => { it('MAPPROV-E2E-001: no provider anywhere reads as Mapbox', async () => { expect(await shownProvider(await newUser())).toBe('mapbox-gl'); }); it('MAPPROV-E2E-002: a Leaflet row without the mark reads as Mapbox, as it did before the mark existed', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); expect(await shownProvider(userId)).toBe('mapbox-gl'); expect(await readUserSetting(orm, userId, 'map_provider')).toBe('leaflet'); }); it('MAPPROV-E2E-003: a Leaflet row with the mark stays Leaflet', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); await setUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY, 'true'); expect(await shownProvider(userId)).toBe('leaflet'); }); it('MAPPROV-E2E-004: a MapLibre row stays MapLibre without any mark', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'maplibre-gl'); expect(await shownProvider(userId)).toBe('maplibre-gl'); }); it('MAPPROV-E2E-005: a Leaflet admin default stored before the mark existed reads as Mapbox', async () => { await setAppSetting(orm, ADMIN_DEFAULT, JSON.stringify('leaflet')); expect(await shownProvider(await newUser())).toBe('mapbox-gl'); }); it('MAPPROV-E2E-006: a Leaflet default the admin sets is kept, and resetting it takes the mark away', async () => { const userId = await newUser(); expect((await setAdminDefault('leaflet')).status).toBe(200); expect(await readAppSetting(orm, ADMIN_MARK)).toBe('true'); expect(await shownProvider(userId)).toBe('leaflet'); // An empty personal row still defers to that default. await setUserSetting(orm, userId, 'map_provider', ''); expect(await shownProvider(userId)).toBe('leaflet'); expect((await setAdminDefault(null)).status).toBe(200); expect(await readAppSetting(orm, ADMIN_MARK)).toBeNull(); expect(await readAppSetting(orm, ADMIN_DEFAULT)).toBeNull(); expect(await shownProvider(userId)).toBe('mapbox-gl'); }); it("MAPPROV-E2E-007: the admin's mark does not reach a user's own unmarked Leaflet row", async () => { expect((await setAdminDefault('leaflet')).status).toBe(200); const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); expect(await shownProvider(userId)).toBe('mapbox-gl'); }); }); describe('the mark a write sets', () => { it('MAPPROV-E2E-008: switching from the Mapbox shown to Leaflet is a choice and is kept', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); expect((await saveOne(userId, 'map_provider', 'leaflet')).status).toBe(200); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBe('true'); expect(await shownProvider(userId)).toBe('leaflet'); // Saving the whole form again later keeps it. expect((await saveAll(userId, { map_provider: 'leaflet', map_tile_url: '' })).status).toBe(200); expect(await shownProvider(userId)).toBe('leaflet'); }); it('MAPPROV-E2E-009: saving every field with the provider shown sets no mark', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); const res = await saveAll(userId, { map_provider: 'mapbox-gl', map_tile_url: '', mapbox_style: '' }); expect(res.status).toBe(200); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBeNull(); expect(await shownProvider(userId)).toBe('mapbox-gl'); }); it('MAPPROV-E2E-010: a switch through the bulk save is a choice too', async () => { const userId = await newUser(); expect((await saveAll(userId, { map_provider: 'leaflet' })).status).toBe(200); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBe('true'); expect(await shownProvider(userId)).toBe('leaflet'); }); it('MAPPROV-E2E-011: a value that names no provider sets no mark', async () => { const userId = await newUser(); expect((await saveOne(userId, 'map_provider', '')).status).toBe(200); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBeNull(); }); it('MAPPROV-E2E-012: outside a managed install the mark changes nothing about what is shown', async () => { vi.stubEnv('TREK_MANAGED', 'false'); const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); expect(await shownProvider(userId)).toBe('leaflet'); expect((await saveOne(userId, 'map_provider', 'maplibre-gl')).status).toBe(200); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBe('true'); expect(await shownProvider(userId)).toBe('maplibre-gl'); }); }); describe("the mark is the server's alone", () => { it('MAPPROV-E2E-013: a client cannot clear it, through either route', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); await setUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY, 'true'); const single = await saveOne(userId, MAP_PROVIDER_CHOSEN_KEY, false); expect(single.status).toBe(200); expect(single.body).toEqual({ success: true, key: MAP_PROVIDER_CHOSEN_KEY, unchanged: true }); const bulk = await saveAll(userId, { [MAP_PROVIDER_CHOSEN_KEY]: false, map_provider: 'leaflet' }); expect(bulk.status).toBe(200); expect(bulk.body).toMatchObject({ success: true, updated: 1 }); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBe('true'); expect(await shownProvider(userId)).toBe('leaflet'); }); it('MAPPROV-E2E-014: a client cannot set it either', async () => { const userId = await newUser(); await setUserSetting(orm, userId, 'map_provider', 'leaflet'); expect((await saveOne(userId, MAP_PROVIDER_CHOSEN_KEY, true)).status).toBe(200); expect((await saveAll(userId, { [MAP_PROVIDER_CHOSEN_KEY]: true })).body).toMatchObject({ updated: 0 }); expect(await readUserSetting(orm, userId, MAP_PROVIDER_CHOSEN_KEY)).toBeNull(); expect(await shownProvider(userId)).toBe('mapbox-gl'); }); });