mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(settings): a managed install keeps a map provider somebody chose
With the operator's Mapbox token a managed install replaced every empty or Leaflet provider with Mapbox, including a Leaflet the user picked and a Leaflet default the admin set. A plain "only when empty" would switch everyone whose Leaflet row only exists because the settings form writes every field (#1634), so the choice is recorded instead, by the server alone: - map_provider_chosen on the user's settings, set when a write changes the provider the user is shown (so saving the form unchanged is no choice, and leaving the Mapbox a managed install shows is one). A client patch naming the key is dropped on both routes, false included, so no client can set or clear it. - map_provider_default_chosen in app_settings, set when the admin saves a map_provider default and removed when it is reset. Mapbox now replaces an empty provider, or a Leaflet without the mark of where it came from. No stored row is rewritten: rows without a mark read exactly as before. The client Settings type gains the read-only field.
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* 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<number> {
|
||||
return (await makeUser(orm)).user.id;
|
||||
}
|
||||
|
||||
async function shownProvider(userId: number): Promise<unknown> {
|
||||
const res = await request(app.getHttpServer()).get('/api/settings').set('Cookie', sessionCookie(userId));
|
||||
expect(res.status).toBe(200);
|
||||
return (res.body as { settings: Record<string, unknown> }).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<string, unknown>) {
|
||||
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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user