mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 21:46:49 +00:00
2c2d008dc2
* fix(security): mint idempotency keys properly, and resolve the extractor by path Three findings out of a sweep of every open Sonar security issue. The other 49 turned out to be false positives and are documented in sonar-project.properties rather than changed — each entry scoped to a file, never a whole rule, so a future Math.random() on an auth path still fails. The idempotency key is the one that can lose data. Both producers fell back to Math.random when crypto.randomUUID was missing, and randomUUID needs a secure context — TREK's own quickstart is http://localhost:3000 and LAN installs run on http://192.168.x.x, so on the deployments the README documents the fallback is what actually runs. The axios one was `Math.random().toString(36).slice(2)`, which is not length-stable: 0.5 yields a single character and 0 yields the empty string, which the interceptor reads as "no key" and skips deduplication for entirely. Two writes sharing a key collapse into one — the second gets the first one's cached response and the edit is gone with no error anywhere. Both now come from utils/randomId, which has the getRandomValues rung that works in insecure contexts too. The extractor probe stored the bare binary name, so every extraction re-resolved it through whatever PATH held at that moment. The official image sets KITINERARY_EXTRACTOR_PATH and never reaches that branch, but a bare-metal install without it ran an unqualified name from a PATH it does not control. It now walks the search path itself and keeps the absolute path of the file it verified; execFileSync also drops the /bin/sh hop the old probe needed. PATH comes through app-config because process.env is walled off in src/. * fix: four tests and sorts that were not doing what they claimed Sonar reported 43 high-impact reliability issues. Reading all of them, four are real; the rest are cosmetic or would be made worse by the suggested change. budget.test.ts hand-built `paid: true`, but both emitters send `paid: body.paid ? 1 : 0` and the handler stores the value verbatim — the test asserted a payload the server never sends. The line below it already used a number. admin.controller.test.ts had a `.resolves` assertion with no await. The promise was dropped, so that line could not fail the test. The two to-do category lists sorted with a bare .sort(), which is byte order: every accented name files behind the whole ASCII range, so in a German trip "Übernachtung" lands after "Zelt". TREK ships 23 locales, so this is real data. One side effect worth knowing: katColor picks a category's dot colour by its index, so re-sorting can move which category shows which colour — it already moves whenever a category is added, and the colour carries no meaning. Deliberately not changed: the two `| 0` truncations (Math.trunc is not equivalent — `| 0` wraps at 32 bits and these carry millisecond timestamps), the two .map() callbacks whose second parameter is declared and used, and the sha1 in thumbnail.service (its own comment says a swap would orphan every existing thumbnail). * refactor: give every button a type, and prefer the Number statics Two mechanical sweeps, no behaviour change intended in either. A <button> with no type attribute is type="submit" per the HTML spec. Outside a form that does nothing today, but it turns into a surprise the moment the markup is wrapped in one, which is what Sonar's S9011 is warning about. 870 buttons get an explicit type="button". Two things were left alone on purpose: buttons that take a {...spread}, because the spread may carry a type and the merge order decides which wins, and anything inside a <form> — there the answer is a judgement call, and checking all eleven form files found every real submit button already carrying type="submit". The Number sweep covers only the three globals that are exactly equivalent to their Number twin: parseInt, parseFloat and NaN. isNaN is NOT in it — the global coerces its argument, so isNaN('abc') is true where Number.isNaN('abc') is false, and renaming it would change what the code does. It reaches server and plugin-sdk as well as the client; both copies of egress-policy.ts got the identical edit, so the mirror the SDK's parity test guards stays intact.
206 lines
8.2 KiB
TypeScript
206 lines
8.2 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { useTripStore } from '../../../src/store/tripStore';
|
|
import { resetAllStores } from '../../helpers/store';
|
|
import { buildBudgetItem } from '../../helpers/factories';
|
|
import type { BudgetItemMember } from '../../../src/types';
|
|
|
|
beforeEach(() => {
|
|
resetAllStores();
|
|
});
|
|
|
|
describe('remoteEventHandler > budget', () => {
|
|
const member1: BudgetItemMember = { user_id: 5, paid: 0, username: 'eve' };
|
|
const member2: BudgetItemMember = { user_id: 6, paid: 1, username: 'frank' };
|
|
|
|
const seedData = () => {
|
|
useTripStore.setState({
|
|
budgetItems: [
|
|
buildBudgetItem({ id: 1, persons: 1, members: [{ ...member1 }] }),
|
|
buildBudgetItem({ id: 2, persons: 2, members: [{ ...member2 }] }),
|
|
],
|
|
});
|
|
};
|
|
|
|
it('FE-WSEVT-BUDGET-001: budget:created adds item to budgetItems', () => {
|
|
seedData();
|
|
const newItem = buildBudgetItem({ id: 99, name: 'Hotel' });
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:created', item: newItem });
|
|
const { budgetItems } = useTripStore.getState();
|
|
expect(budgetItems).toHaveLength(3);
|
|
expect(budgetItems.find(i => i.id === 99)).toBeDefined();
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-002: budget:created is idempotent — no duplicate if same ID', () => {
|
|
seedData();
|
|
const duplicate = buildBudgetItem({ id: 1, name: 'Duplicate' });
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:created', item: duplicate });
|
|
const { budgetItems } = useTripStore.getState();
|
|
expect(budgetItems).toHaveLength(2);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-003: budget:updated replaces item in array', () => {
|
|
seedData();
|
|
const updated = buildBudgetItem({ id: 1, name: 'Updated Hotel', total_price: 500 });
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:updated', item: updated });
|
|
const { budgetItems } = useTripStore.getState();
|
|
const item = budgetItems.find(i => i.id === 1);
|
|
expect(item?.name).toBe('Updated Hotel');
|
|
expect(item?.total_price).toBe(500);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-004: budget:deleted removes item by ID', () => {
|
|
seedData();
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:deleted', itemId: 1 });
|
|
const { budgetItems } = useTripStore.getState();
|
|
expect(budgetItems).toHaveLength(1);
|
|
expect(budgetItems.find(i => i.id === 1)).toBeUndefined();
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-005: budget:members-updated replaces entire members array and persons count', () => {
|
|
seedData();
|
|
const newMembers: BudgetItemMember[] = [{ user_id: 7, paid: 1, username: 'grace' }, { user_id: 8, paid: 0, username: 'heidi' }];
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:members-updated',
|
|
itemId: 1,
|
|
members: newMembers,
|
|
persons: 3,
|
|
});
|
|
const { budgetItems } = useTripStore.getState();
|
|
const item = budgetItems.find(i => i.id === 1);
|
|
expect(item?.members).toEqual(newMembers);
|
|
expect(item?.persons).toBe(3);
|
|
// Other item should be unchanged
|
|
const item2 = budgetItems.find(i => i.id === 2);
|
|
expect(item2?.members).toEqual([{ ...member2 }]);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-006: budget:member-paid-updated toggles specific member paid status', () => {
|
|
seedData();
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:member-paid-updated',
|
|
itemId: 1,
|
|
userId: 5,
|
|
// 1, not true: both emitters send `paid: body.paid ? 1 : 0`, and the handler
|
|
// stores the value verbatim. Asserting on `true` tested a payload the server
|
|
// never sends — the seeded value on the line below is a number for the same reason.
|
|
paid: 1,
|
|
});
|
|
const { budgetItems } = useTripStore.getState();
|
|
const item = budgetItems.find(i => i.id === 1);
|
|
const m = item?.members?.find(m => m.user_id === 5);
|
|
expect(m?.paid).toBe(1);
|
|
// Other item members unchanged (member2 keeps its seeded paid value)
|
|
const item2 = budgetItems.find(i => i.id === 2);
|
|
expect(item2?.members?.[0].paid).toBe(1);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-007: budget:members-updated for an unknown item leaves every item untouched', () => {
|
|
seedData();
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:members-updated',
|
|
itemId: 4242,
|
|
members: [{ user_id: 9, paid: 0, username: 'ivan' }],
|
|
persons: 9,
|
|
});
|
|
const { budgetItems } = useTripStore.getState();
|
|
expect(budgetItems.map(i => i.persons)).toEqual([1, 2]);
|
|
expect(budgetItems[0].members).toEqual([{ ...member1 }]);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-008: budget:member-paid-updated only touches the addressed member', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [
|
|
buildBudgetItem({ id: 1, members: [{ ...member1 }, { ...member2 }] }),
|
|
],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:member-paid-updated',
|
|
itemId: 1,
|
|
userId: 5,
|
|
paid: 1,
|
|
});
|
|
const members = useTripStore.getState().budgetItems[0].members ?? [];
|
|
expect(members.find(m => m.user_id === 5)?.paid).toBe(1);
|
|
expect(members.find(m => m.user_id === 6)?.paid).toBe(1); // seeded value, untouched
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-009: budget:member-paid-updated on an item without members does not throw', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [buildBudgetItem({ id: 1, members: undefined })],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:member-paid-updated',
|
|
itemId: 1,
|
|
userId: 5,
|
|
paid: 1,
|
|
});
|
|
expect(useTripStore.getState().budgetItems[0].members).toEqual([]);
|
|
});
|
|
|
|
describe('budget:reordered', () => {
|
|
it('FE-WSEVT-BUDGET-010: orderedIds rewrites sort_order and keeps unlisted items at the end', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [
|
|
buildBudgetItem({ id: 1, sort_order: 0 }),
|
|
buildBudgetItem({ id: 2, sort_order: 1 }),
|
|
buildBudgetItem({ id: 3, sort_order: 2 }),
|
|
],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:reordered', orderedIds: [3, 1] });
|
|
const { budgetItems } = useTripStore.getState();
|
|
expect(budgetItems.map(i => i.id)).toEqual([3, 1, 2]);
|
|
// The whole array is reindexed, so re-sorting by sort_order reproduces the
|
|
// order that was just applied.
|
|
expect(budgetItems.map(i => i.sort_order)).toEqual([0, 1, 2]);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-011: an orderedIds entry for an unknown item is dropped', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [buildBudgetItem({ id: 1 }), buildBudgetItem({ id: 2 })],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:reordered', orderedIds: [2, 999, 1] });
|
|
expect(useTripStore.getState().budgetItems.map(i => i.id)).toEqual([2, 1]);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-012: orderedCategories groups items and appends unlisted categories', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [
|
|
buildBudgetItem({ id: 1, category: 'Food' }),
|
|
buildBudgetItem({ id: 2, category: 'Transport' }),
|
|
buildBudgetItem({ id: 3, category: 'Food' }),
|
|
buildBudgetItem({ id: 4, category: 'Lodging' }),
|
|
],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:reordered',
|
|
orderedCategories: ['Transport', 'Food', 'Souvenirs'],
|
|
});
|
|
// Transport, then both Food rows in their original order, then the
|
|
// category the server did not mention.
|
|
expect(useTripStore.getState().budgetItems.map(i => i.id)).toEqual([2, 1, 3, 4]);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-013: an item without a category is grouped under "Other"', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [
|
|
buildBudgetItem({ id: 1, category: null }),
|
|
buildBudgetItem({ id: 2, category: 'Food' }),
|
|
],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({
|
|
type: 'budget:reordered',
|
|
orderedCategories: ['Other', 'Food'],
|
|
});
|
|
expect(useTripStore.getState().budgetItems.map(i => i.id)).toEqual([1, 2]);
|
|
});
|
|
|
|
it('FE-WSEVT-BUDGET-014: a payload with neither key is a no-op', () => {
|
|
useTripStore.setState({
|
|
budgetItems: [buildBudgetItem({ id: 1 }), buildBudgetItem({ id: 2 })],
|
|
});
|
|
useTripStore.getState().handleRemoteEvent({ type: 'budget:reordered' });
|
|
expect(useTripStore.getState().budgetItems.map(i => i.id)).toEqual([1, 2]);
|
|
});
|
|
});
|
|
});
|